-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathRemoteOverride.ts
More file actions
77 lines (70 loc) · 2.18 KB
/
RemoteOverride.ts
File metadata and controls
77 lines (70 loc) · 2.18 KB
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
import type {
ProjectRemoteOverride,
SourceControlProviderInfo,
SourceControlProviderKind,
} from "@t3tools/contracts";
import * as SourceControlProvider from "./SourceControlProvider.ts";
export function parseRemoteHost(remoteUrl: string): string | null {
const trimmed = remoteUrl.trim();
if (trimmed.startsWith("git@")) {
const hostWithPath = trimmed.slice("git@".length);
const separatorIndex = hostWithPath.search(/[:/]/);
return separatorIndex > 0 ? hostWithPath.slice(0, separatorIndex).toLowerCase() : null;
}
try {
const hostname = new URL(trimmed).hostname.toLowerCase();
return hostname || null;
} catch {
return null;
}
}
export function parseBaseUrl(value: string): string | null {
try {
const url = new URL(value);
return `${url.protocol}//${url.host}`;
} catch {
const host = parseRemoteHost(value);
return host ? `https://${host}` : null;
}
}
export function providerName(kind: SourceControlProviderKind, baseUrl: string | null): string {
switch (kind) {
case "github":
return baseUrl === "https://github.com" ? "GitHub" : "GitHub Self-Hosted";
case "gitlab":
return baseUrl === "https://gitlab.com" ? "GitLab" : "GitLab Self-Hosted";
case "azure-devops":
return "Azure DevOps";
case "bitbucket":
return baseUrl === "https://bitbucket.org" ? "Bitbucket" : "Bitbucket Self-Hosted";
case "unknown":
return parseRemoteHost(baseUrl ?? "") ?? "Source control";
}
}
export function providerInfoFromOverride(
override: ProjectRemoteOverride,
): SourceControlProviderInfo | null {
const baseUrl = override.webUrl
? parseBaseUrl(override.webUrl)
: parseBaseUrl(override.remoteUrl);
if (!baseUrl) {
return null;
}
return {
kind: override.provider,
name: providerName(override.provider, baseUrl),
baseUrl,
};
}
export function providerContextFromOverride(
override: ProjectRemoteOverride,
): SourceControlProvider.SourceControlProviderContext | null {
const provider = providerInfoFromOverride(override);
return provider
? {
provider,
remoteName: override.remoteName ?? "origin",
remoteUrl: override.remoteUrl,
}
: null;
}