-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitlab_com.ts
78 lines (70 loc) · 2.3 KB
/
gitlab_com.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
68
69
70
71
72
73
74
75
76
77
78
import type { HostingService, Range } from "../mod.ts";
import type { ExecuteOptions } from "../../process.ts";
import { getCommitSHA1 } from "../../util.ts";
export const service: HostingService = {
getHomeURL(fetchURL: URL, _options?: ExecuteOptions): Promise<URL> {
return Promise.resolve(new URL(formatURLBase(fetchURL)));
},
async getCommitURL(
fetchURL: URL,
commitish: string,
options: ExecuteOptions = {},
): Promise<URL> {
const sha = await getCommitSHA1(commitish, options) ?? commitish;
const urlBase = formatURLBase(fetchURL);
const pathname = `-/commit/${sha}`;
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
},
getTreeURL(
fetchURL: URL,
commitish: string,
path: string,
_options?: ExecuteOptions,
): Promise<URL> {
const urlBase = formatURLBase(fetchURL);
const pathname = `-/tree/${commitish}/${path}`;
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
},
getBlobURL(
fetchURL: URL,
commitish: string,
path: string,
{ range }: { range?: Range } & ExecuteOptions = {},
): Promise<URL> {
const urlBase = formatURLBase(fetchURL);
const suffix = formatSuffix(range);
const pathname = `-/blob/${commitish}/${path}${suffix}`;
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
},
getPullRequestURL(
fetchURL: URL,
n: number,
_options?: ExecuteOptions,
): Promise<URL> {
const urlBase = formatURLBase(fetchURL);
const pathname = `-/merge_requests/${n}`;
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
},
extractPullRequestID(commit: string): number | undefined {
const m = commit.match(/See merge request (?:.*)!(\d+)/);
if (m) {
return Number(m[1]);
}
return undefined;
},
};
function formatURLBase(fetchURL: URL): string {
const [owner, repo] = fetchURL.pathname.split("/").slice(1);
return `https://${fetchURL.hostname}/${owner}/${repo.replace(/\.git$/, "")}`;
}
function formatSuffix(range: Range | undefined): string {
// Note:
// Without `?plain=1`, GitHub shows the rendering result of content (e.g. Markdown) so that we
// cannot specify the line range.
if (Array.isArray(range)) {
return `?plain=1#L${range[0]}-${range[1]}`;
} else if (range) {
return `?plain=1#L${range}`;
}
return "";
}