-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitbucket_org.ts
65 lines (59 loc) · 2.11 KB
/
bitbucket_org.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
import type { HostingService, Range } from "../mod.ts";
import type { ExecuteOptions } from "../../process.ts";
import { extname } from "jsr:@std/path@^0.221.0";
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 = `commits/${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 = `src/${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);
if (!range || extname(path) !== ".md") {
const suffix = formatSuffix(range);
const pathname = `src/${commitish}/${path}${suffix}`;
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
}
// Bitbucket does not provide `?plain=1` like GitHub so we need to use annotation URL
// instead to proerply select the line range of Markdown file
const suffix = formatSuffix(range);
const pathname = `annotate/${commitish}/${path}${suffix}`;
return Promise.resolve(new URL(`${urlBase}/${pathname}`));
},
};
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 {
if (Array.isArray(range)) {
return `#lines-${range[0]}:${range[1]}`;
} else if (range) {
return `#lines-${range}`;
}
return "";
}