Open History to see the last commit
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}`;
40}
41
42function repositoryKeyFromRemote(key: string): string | null {
43 return canonicalRepositoryKey(key)?.replace(/\.git$/u, "") ?? null;
44}
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 };
52}
53
54export const definition = defineSourceControl({
55 kind: KIND,
56 label: "GitLab",
57 pickerLabel: "GitLab",
58 icon,
59 changeRequest: {
60 shortLabel: "MR",
61 shortLabelWithArticle: "an MR",
62 singular: "merge request",
63 plural: "merge requests",
64 },
65 repositoryPathHint: "group/project",
66 publicHost: "gitlab.com",
67 publishDescription: "gitlab.com",
68 publishHost: () => "gitlab.com",
69 newRepositoryOwner: (account) => ({ owner: account }),
70 defaultCloneTransport: "ssh",
71 changeRequestUrl: ({ host, repository, number, remoteUrl, webUrl }) =>
72 webUrl
73 ? changeRequestUrlBelow(webUrl, `-/merge_requests/${number}`)
74 : `${remoteWebOrigin(host, remoteUrl)}/${repository}/-/merge_requests/${number}`,
75 parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH),
76 parseIssueUrl: (url) => parseIssuePath(url, ISSUE_PATH),
77 commitUrl: (input) => buildCommitUrl(input, "-/commit"),
78 parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
79 repositoryUrlFromPage: (url) => repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH),
80 // `!12` already links the merge request itself; `#12` is always an issue.
81 changeRequestUrlFromReferenceAutolink: (url) =>
82 CHANGE_REQUEST_PATH.test(url.pathname) ? url.toString() : null,
83 changeRequestActions: new Set([
84 "merge",
85 "ready",
86 "draft",
87 "close",
88 "reopen",
89 "update-branch",
90 "enable-auto-merge",
91 "disable-auto-merge",
92 "revert",
93 "delete-branch",
94 ] as const),
95 checkoutCommand: ({ number }) => `glab mr checkout ${number}`,
96 // `accept` is glab's alias for `merge`.
97 changeRequestCloseCommand: /\bglab\s+mr\s+(?:merge|accept|close)\b/u,
98 // `new` is glab's alias for `create`.
99 changeRequestCreateCommand: /\bglab\s+mr\s+(?:create|new)\b/u,
100 // Bot users, unlike GitHub's, have real profile pages.
101 authorProfileUrl: (login, repositoryUrl) =>
102 new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(),
103 referenceAutolinks: gitLabReferenceAutolinks,
104 reviewSummaryRequired: () => false,
105 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
106 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
107 checkoutChangeRequestHost: () => null,
108 changeRequestHeadRef: (number) => `refs/merge-requests/${number}/head`,
109 recognizeRemote: ({ host, hostname }) =>
110 hostname === "altssh.gitlab.com"
111 ? { kind: KIND, name: "GitLab", baseUrl: "https://gitlab.com" }
112 : hostname === "gitlab.com" || hostname.split(".").includes("gitlab")
113 ? {
114 kind: KIND,
115 name: hostname === "gitlab.com" ? "GitLab" : "GitLab Self-Hosted",
116 baseUrl: `https://${host}`,
117 }
118 : null,
119 repositoryKeyFromRemote,
120 canonicalRepositoryKey,
121 isChangeRequestInRepository: (saved, link) => {
122 const identity = withCanonicalKey(saved);
123 return isChangeRequestInProjectRepository(
124 projectChangeRequestHost(identity, KIND),
125 identity,
126 link,
127 );
128 },
129 canReadChangeRequestOnHost: (saved, link) => {
130 const identity = withCanonicalKey(saved);
131 return isChangeRequestOnProjectHost(projectChangeRequestHost(identity, KIND), identity, link);
132 },
133});