summaryrefslogtreecommitdiff
path: root/source/features/clone-branch.tsx
blob: 2373ff86e2ba3caee15b70f6976f7713c1db0403 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React from 'dom-chef';
import select from 'select-dom';
import onetime from 'onetime';
import delegate from 'delegate-it';
import {observe} from 'selector-observer';
import GitBranchIcon from 'octicon/git-branch.svg';
import * as pageDetect from 'github-url-detection';
import * as textFieldEdit from 'text-field-edit';

import features from '.';
import * as api from '../github-helpers/api';
import LoadingIcon from '../github-helpers/icon-loading';

const getBranchBaseSha = async (branchName: string): Promise<string> => {
	const {repository} = await api.v4(`
		repository() {
			ref(qualifiedName: "${branchName}") {
				target {
					oid
				}
			}
		}
	`);

	return repository.ref.target.oid;
};

async function createBranch(newBranchName: string, baseSha: string): Promise<true | string> {
	const response = await api.v3('git/refs', {
		method: 'POST',
		body: {
			sha: baseSha,
			ref: 'refs/heads/' + newBranchName
		},
		ignoreHTTPStatus: true
	});

	return response.ok || response.message;
}

async function cloneBranch({delegateTarget: cloneButton}: delegate.Event<MouseEvent, HTMLButtonElement>): Promise<void> {
	const branchName = cloneButton.closest('[branch]')!.getAttribute('branch')!;

	const currentBranch = getBranchBaseSha(branchName);
	let newBranchName = prompt('Enter the new branch name')?.trim();

	const spinner = <LoadingIcon className="ml-2"/>;

	while (newBranchName) {
		cloneButton.replaceWith(spinner);
		// eslint-disable-next-line no-await-in-loop
		const result = await createBranch(newBranchName, await currentBranch);
		spinner.replaceWith(cloneButton);

		if (result === true) {
			break;
		}

		newBranchName = prompt(result.replace('refs/heads/' + newBranchName, 'This') + '\nEnter the new branch name', newBranchName)?.trim();
	}

	if (!newBranchName) {
		return;
	}

	textFieldEdit.set(
		select<HTMLInputElement>('[name="query"]')!,
		newBranchName
	);
}

async function init(): Promise<void | false> {
	await api.expectToken();
	const deleteIconClass = [
		'branch-filter-item-controller .octicon-trashcan', // Pre "Repository refresh" layout
		'branch-filter-item .octicon-trashcan'
	].join();

	observe(deleteIconClass, {
		add(deleteIcon) {
			// Branches with open PRs use `span`, the others use `form`
			deleteIcon.closest('form, span')!.before(
				<button
					type="button"
					aria-label="Clone this branch"
					className="link-gray btn-link tooltipped tooltipped-nw ml-3 rgh-clone-branch"
				>
					<GitBranchIcon/>
				</button>
			);
		}
	});

	delegate(document, '.rgh-clone-branch', 'click', cloneBranch);
}

void features.add(__filebasename, {
	include: [
		pageDetect.isBranches
	],
	init: onetime(init)
});