Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

definition.ts

137 lines5.5 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import {
3 buildCommitUrl,
4 changeRequestUrlBelow,
5 defineSourceControl,
6 isChangeRequestInProjectRepository,
7 isChangeRequestOnProjectHost,
8 parseChangeRequestPath,
9 parseIssuePath,
10 parseCommitPath,
11 repositoryUrlFromPagePath,
12 projectChangeRequestHost,
13 remoteWebOrigin,
14} from "@t3tools/source-control-core/shared/definition";
15import { icon } from "./icon.ts";
16import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts";
17​
18const CHECKOUT_COMMAND = /^glab\s+mr\s+checkout\s+(.+)$/i;
19const CHANGE_REQUEST_REFERENCE =
20 /^https?:\/\/[^/?#\s]+\/[^/?#\s]+(?:\/[^/?#\s]+)+\/-\/merge_requests\/(\d+)(?:[/?#].*)?$/i;
21​
22// Self-managed GitLab runs on any hostname and nests projects in groups.
23const CHANGE_REQUEST_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/merge_requests\/(\d+)(?:\/|$)/u;
24const COMMIT_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/commit\/([0-9a-f]+)\/?$/iu;
25// An incident is an issue that GitLab also serves at `/-/issues/incident/<iid>`. No project is
26// below `/groups/`: a work item there is the group's own, such as an epic.
27const ISSUE_PATH =
28 /^\/(?!groups\/)([^/]+(?:\/[^/]+)+)\/-\/(?:issues(?:\/incident)?|work_items)\/(\d+)(?:\/|$)/u;
29// Every GitLab page inside a project sits below its first `/-/`.
30const REPOSITORY_PAGE_PATH = /^\/([^/]+(?:\/[^/]+)+?)\/-\//u;
31​
32const KIND = SourceControlProviderKind.make("gitlab");
33​
34// GitLab.com also serves SSH on port 443 from this host but names every project on `gitlab.com`.
35const ALT_SSH_HOST_KEY = /^altssh\.gitlab\.com\/(.+)$/u;
36​
37function canonicalRepositoryKey(key: string): string | null {
38 const repository = ALT_SSH_HOST_KEY.exec(key)?.[1];
39 return repository === undefined ? null : `gitlab.com/${repository}`;
41​
42function repositoryKeyFromRemote(key: string): string | null {
43 return canonicalRepositoryKey(key)?.replace(/\.git$/u, "") ?? null;
45​
46// Saved identities reach the predicates as written, so a key from before this mapping still matches.
47function withCanonicalKey<Identity extends { readonly canonicalKey: string }>(
48 identity: Identity,
49): Identity {
50 const canonicalKey = canonicalRepositoryKey(identity.canonicalKey);
51 return canonicalKey === null ? identity : { ...identity, canonicalKey };
53​
54function webHost(host: string): string {
55 return host === "altssh.gitlab.com" ? "gitlab.com" : host;
57​
58export const definition = defineSourceControl({
59 kind: KIND,
60 label: "GitLab",
61 pickerLabel: "GitLab",
62 icon,
63 changeRequest: {
64 shortLabel: "MR",
65 shortLabelWithArticle: "an MR",
66 singular: "merge request",
67 plural: "merge requests",
68 },
69 repositoryPathHint: "group/project",
70 publicHost: "gitlab.com",
71 publishDescription: "gitlab.com",
72 publishHost: () => "gitlab.com",
73 newRepositoryOwner: (account) => ({ owner: account }),
74 defaultCloneTransport: "ssh",
75 changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) =>
76 webUrl
77 ? changeRequestUrlBelow(webUrl, `-/merge_requests/${number}`)
78 : `${remoteWebOrigin(webHost(host), remoteUrl)}/${repository}/-/merge_requests/${number}`,
79 parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH),
80 parseIssueUrl: (url) => parseIssuePath(url, ISSUE_PATH),
81 commitUrl: (input) => buildCommitUrl({ ...input, host: webHost(input.host) }, "-/commit"),
82 parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
83 repositoryUrlFromPage: (url) => repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH),
84 // `!12` already links the merge request itself; `#12` is always an issue.
85 changeRequestUrlFromReferenceAutolink: (url) =>
86 CHANGE_REQUEST_PATH.test(url.pathname) ? url.toString() : null,
87 changeRequestActions: new Set([
88 "merge",
89 "ready",
90 "draft",
91 "close",
92 "reopen",
93 "update-branch",
94 "enable-auto-merge",
95 "disable-auto-merge",
96 "revert",
97 "delete-branch",
98 ] as const),
99 checkoutCommand: ({ number }) => `glab mr checkout ${number}`,
100 // `accept` is glab's alias for `merge`.
101 changeRequestCloseCommand: /\bglab\s+mr\s+(?:merge|accept|close)\b/u,
102 // `new` is glab's alias for `create`.
103 changeRequestCreateCommand: /\bglab\s+mr\s+(?:create|new)\b/u,
104 // Bot users, unlike GitHub's, have real profile pages.
105 authorProfileUrl: ({ login }, repositoryUrl) =>
106 new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(),
107 referenceAutolinks: gitLabReferenceAutolinks,
108 reviewSummaryRequired: () => false,
109 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
110 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
111 checkoutChangeRequestHost: () => null,
112 changeRequestHeadRef: (number) => `refs/merge-requests/${number}/head`,
113 recognizeRemote: ({ host, hostname }) =>
114 hostname === "altssh.gitlab.com"
115 ? { kind: KIND, name: "GitLab", baseUrl: "https://gitlab.com" }
116 : hostname === "gitlab.com" || hostname.split(".").includes("gitlab")
117 ? {
118 kind: KIND,
119 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
120 baseUrl: `https://${host}`,
121 }
122 : null,
123 repositoryKeyFromRemote,
124 canonicalRepositoryKey,
125 isChangeRequestInRepository: (saved, link) => {
126 const identity = withCanonicalKey(saved);
127 return isChangeRequestInProjectRepository(
128 projectChangeRequestHost(identity, KIND),
129 identity,
130 link,
131 );
132 },
133 canReadChangeRequestOnHost: (saved, link) => {
134 const identity = withCanonicalKey(saved);
135 return isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link);
136 },
137});