summaryrefslogtreecommitdiff
path: root/source/github-helpers/github-url.ts
blob: 395723d4482c93e95d947b078ffc8672c9025c9c (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
import {getCurrentBranch} from '.';

function disambiguateReference(ambiguousReference: string[]): {branch: string; filePath: string} {
	const branch = ambiguousReference[0];
	const filePath = ambiguousReference.slice(1).join('/');

	const currentBranch = getCurrentBranch();
	const currentBranchSections = currentBranch.split('/');
	if (
		ambiguousReference.length === 1 || // Ref has no slashes
		currentBranchSections.length === 1 || // Current branch has no slashes
		/\^|~|@{/.test(branch) // Ref is an extended revision #3137 https://git-scm.com/docs/git-rev-parse#_specifying_revisions
	) {
		// Then the reference is not ambiguous
		return {branch, filePath};
	}

	for (const [i, section] of currentBranchSections.entries()) {
		if (ambiguousReference[i] !== section) {
			console.warn(`The supplied path (${ambiguousReference.join('/')}) is ambiguous (current reference is \`${currentBranch}\`)`);
			return {branch, filePath};
		}
	}

	return {
		branch: currentBranch,
		filePath: ambiguousReference.slice(currentBranchSections.length).join('/')
	};
}

export default class GitHubURL extends URL {
	// @ts-ignore https://github.com/microsoft/TypeScript/issues/26792
	user: string;
	// @ts-ignore
	repository: string;
	// @ts-ignore
	route: string;
	// @ts-ignore
	branch: string;
	// @ts-ignore
	filePath: string;
	constructor(url: string) {
		super(url);
		this.pathname = super.pathname;
	}

	assign(replacements: Partial<GitHubURL>): this {
		Object.assign(this, replacements);
		return this;
	}

	get pathname() {
		return `/${this.user}/${this.repository}/${this.route}/${this.branch}/${this.filePath}`.replace(/\/+$/, '');
	}

	set pathname(pathname) {
		const [user, repository, route, ...ambiguousReference] = pathname.replace(/^\/|\/$/g, '').split('/');
		const {branch, filePath} = disambiguateReference(ambiguousReference);
		this.assign({user, repository, route, branch, filePath});
	}

	get href() {
		// Update the actual underlying URL
		super.pathname = this.pathname;
		return super.href;
	}

	toString() {
		return this.href;
	}
}