-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter-github.ts
67 lines (57 loc) · 2.01 KB
/
adapter-github.ts
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
import { Termination } from "./util/error";
export const createGithubURLForStackedPR = ({
repoOwner, //
repo,
baseBranch,
newBranch,
}: {
repoOwner: string;
repo: string;
baseBranch: string;
newBranch: string;
}): string => `https://github.com/${repoOwner}/${repo}/compare/${baseBranch}...${newBranch}`;
/**
* TODO: support all formats properly, see:
* - https://stackoverflow.com/a/31801532/9285308
* - https://github.com/git/git/blob/master/urlmatch.c
* - https://github.com/git/git/blob/master/urlmatch.h
* - https://github.com/git/git/blob/master/t/t0110-urlmatch-normalization.sh
*/
export function parseGithubRemoteUrl(remoteUrl: string) {
if (remoteUrl.startsWith("git@")) {
// git@http://github.com:kiprasmel/git-stacked-rebase.git
const hasHttp = remoteUrl.includes("http://") || remoteUrl.includes("https://");
if (hasHttp) {
remoteUrl = remoteUrl.replace(/https?:\/\//, "");
}
// [email protected]:kiprasmel/git-stacked-rebase.git
// remove base url
remoteUrl = remoteUrl.split(":").slice(1).join(":");
// kiprasmel/git-stacked-rebase.git
if (remoteUrl.endsWith(".git")) {
remoteUrl = remoteUrl.slice(0, -4);
}
// kiprasmel/git-stacked-rebase
const [repoOwner, repo] = remoteUrl.split("/");
return { repoOwner, repo };
} else if (remoteUrl.startsWith("http")) {
// https://github.com/kiprasmel/git-stacked-rebase.git
const hasHttp = remoteUrl.includes("http://") || remoteUrl.includes("https://");
if (hasHttp) {
remoteUrl = remoteUrl.replace(/https?:\/\//, "");
}
// github.com/kiprasmel/git-stacked-rebase.git
// remove base url
remoteUrl = remoteUrl.split("/").slice(1).join("/");
// kiprasmel/git-stacked-rebase.git
if (remoteUrl.endsWith(".git")) {
remoteUrl = remoteUrl.slice(0, -4);
}
// kiprasmel/git-stacked-rebase
const [repoOwner, repo] = remoteUrl.split("/");
return { repoOwner, repo };
} else {
const msg = `\nUnrecognized URL format of remote: got "${remoteUrl}". Probably just un-implemented yet..\n\n`;
throw new Termination(msg);
}
}