Skip to content
TypeScript

feat(gitlab): link issue, merge request, commit and user references (#30524)

Antony42a0e917cf89
diff --git a/apps/mobile/src/features/pull-requests/pullRequestAutolinks.test.ts b/apps/mobile/src/features/pull-requests/pullRequestAutolinks.test.ts
index 4aeeb1f388a..e953dad37f0 100644
--- a/apps/mobile/src/features/pull-requests/pullRequestAutolinks.test.ts
+++ b/apps/mobile/src/features/pull-requests/pullRequestAutolinks.test.ts
@@ -29,10 +29,10 @@ describe("linkReferenceAutolinks", () => {
});
it("leaves markdown unchanged for a host that links nothing", () => {
- const gitlab = changeRequestReferenceAutolinker({
- provider: "gitlab",
- url: "https://gitlab.com/group/project/-/merge_requests/7",
+ const forgejo = changeRequestReferenceAutolinker({
+ provider: "forgejo",
+ url: "https://codeberg.org/acme/web/pulls/7",
});
- expect(linkReferenceAutolinks("Closes #123", gitlab)).toBe("Closes #123");
+ expect(linkReferenceAutolinks("Closes #123", forgejo)).toBe("Closes #123");
});
});
diff --git a/packages/client-runtime/src/pullRequestMarkdown.test.ts b/packages/client-runtime/src/pullRequestMarkdown.test.ts
index 232f7d11843..8fab1fdd3b6 100644
--- a/packages/client-runtime/src/pullRequestMarkdown.test.ts
+++ b/packages/client-runtime/src/pullRequestMarkdown.test.ts
@@ -157,7 +157,6 @@ describe("change request reference autolinks", () => {
// regression: the mobile review guide linked every host's `#5` with GitHub's grammar
it("links nothing on a host whose grammar is not applied", () => {
- expect(urls("gitlab", "https://gitlab.com/group/project/-/merge_requests/7", "#5")).toBeNull();
expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull();
});
});
diff --git a/packages/client-runtime/src/sourceControlClients.test.ts b/packages/client-runtime/src/sourceControlClients.test.ts
index 359da39b33b..49a2f3ad653 100644
--- a/packages/client-runtime/src/sourceControlClients.test.ts
+++ b/packages/client-runtime/src/sourceControlClients.test.ts
@@ -72,6 +72,11 @@ describe("sourceControlClients.parseChangeRequestUrl", () => {
repository: "team/project",
number: 9,
});
+ expect(
+ sourceControlClients.changeRequestUrlFromReferenceAutolink(
+ "https://gitlab.com/team/apps/web/-/merge_requests/42",
+ ),
+ ).toBe("https://gitlab.com/team/apps/web/-/merge_requests/42");
});
it("reads Bitbucket and both Azure DevOps URL forms", () => {
diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts
index c515aaa47db..8f71e1cc734 100644
--- a/packages/source-control-core/src/shared/definition.ts
+++ b/packages/source-control-core/src/shared/definition.ts
@@ -259,9 +259,9 @@ export interface SourceControlDefinition {
/** Null when the URL is not one of this host's change requests. */
readonly parseChangeRequestUrl: (url: URL) => ParsedChangeRequestUrl | null;
/**
- * The change request URL a bare `#123` autolink might name, for hosts that write every
- * reference through their issue route. Only a candidate: the caller must read it as a change
- * request before treating it as one.
+ * The change request URL a `reference` autolink might name, such as a GitHub `#123` written
+ * through the issue route. Only a candidate: the caller must read it as a change request
+ * before treating it as one.
*/
readonly changeRequestUrlFromReferenceAutolink: (url: URL) => string | null;
/**
diff --git a/packages/source-control-gitlab/src/shared/definition.ts b/packages/source-control-gitlab/src/shared/definition.ts
index d31c7d9dfd1..2ec891cae34 100644
--- a/packages/source-control-gitlab/src/shared/definition.ts
+++ b/packages/source-control-gitlab/src/shared/definition.ts
@@ -11,6 +11,7 @@ import {
remoteWebOrigin,
} from "@t3tools/source-control-core/shared/definition";
import { icon } from "./icon.ts";
+import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts";
const CHECKOUT_COMMAND = /^glab\s+mr\s+checkout\s+(.+)$/i;
const CHANGE_REQUEST_REFERENCE =
@@ -46,7 +47,9 @@ export const definition = defineSourceControl({
parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH),
commitUrl: (input) => buildCommitUrl(input, "-/commit"),
parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
- changeRequestUrlFromReferenceAutolink: () => null,
+ // `!12` already links the merge request itself; `#12` is always an issue.
+ changeRequestUrlFromReferenceAutolink: (url) =>
+ CHANGE_REQUEST_PATH.test(url.pathname) ? url.toString() : null,
changeRequestActions: new Set([
"merge",
"ready",
@@ -64,6 +67,7 @@ export const definition = defineSourceControl({
// Bot users, unlike GitHub's, have real profile pages.
authorProfileUrl: (login, repositoryUrl) =>
new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(),
+ referenceAutolinks: gitLabReferenceAutolinks,
reviewSummaryRequired: () => false,
checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
diff --git a/packages/source-control-gitlab/src/shared/referenceAutolinks.test.ts b/packages/source-control-gitlab/src/shared/referenceAutolinks.test.ts
new file mode 100644
index 00000000000..fe8aa76f60e
--- /dev/null
+++ b/packages/source-control-gitlab/src/shared/referenceAutolinks.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, it } from "vite-plus/test";
+
+import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts";
+
+const repositoryUrl = "https://gitlab.com/team/apps/web";
+const linked = (text: string) =>
+ gitLabReferenceAutolinks(text, repositoryUrl).map(({ label, url, kind }) => [label, url, kind]);
+
+describe("gitLabReferenceAutolinks", () => {
+ it("links issues, merge requests, cross-project references, commits and mentions", () => {
+ expect(
+ linked(
+ "Closes #123 and !45, see api!9, other/group/lib#2; @julius.dev @team/mobile abc1234 api@abcdef1 GL-7 [issue:8] [issue:api/9] [issue:other/lib/3]",
+ ),
+ ).toEqual([
+ ["#123", "https://gitlab.com/team/apps/web/-/issues/123", "reference"],
+ ["!45", "https://gitlab.com/team/apps/web/-/merge_requests/45", "reference"],
+ ["api!9", "https://gitlab.com/team/apps/api/-/merge_requests/9", "reference"],
+ ["other/group/lib#2", "https://gitlab.com/other/group/lib/-/issues/2", "reference"],
+ ["@julius.dev", "https://gitlab.com/julius.dev", "mention"],
+ ["@team/mobile", "https://gitlab.com/team/mobile", "mention"],
+ ["abc1234", "https://gitlab.com/team/apps/web/-/commit/abc1234", "commit"],
+ ["api@abcdef1", "https://gitlab.com/team/apps/api/-/commit/abcdef1", "commit"],
+ ["GL-7", "https://gitlab.com/team/apps/web/-/issues/7", "reference"],
+ ["[issue:8]", "https://gitlab.com/team/apps/web/-/issues/8", "reference"],
+ ["[issue:api/9]", "https://gitlab.com/team/apps/api/-/issues/9", "reference"],
+ ["[issue:other/lib/3]", "https://gitlab.com/other/lib/-/issues/3", "reference"],
+ ]);
+ });
+
+ it("shows a full SHA by its first seven characters", () => {
+ const sha = "0123456789abcdef0123456789abcdef01234567";
+ expect(linked(`Reverts ${sha}.`)).toEqual([
+ ["0123456", `https://gitlab.com/team/apps/web/-/commit/${sha}`, "commit"],
+ ]);
+ });
+
+ it("leaves URLs, escapes, emails, words, plain numbers and @all alone", () => {
+ expect(
+ linked(
+ "https://gitlab.com/a/b/-/issues/1#note_2 [email protected] \\!12 1234567 issue#x a!1b @all gl-4 XGL-5",
+ ),
+ ).toEqual([]);
+ });
+
+ it("scans long runs of periods, dashes and slashes in linear time", () => {
+ expect(linked(`${".".repeat(50_000)} ${".-/".repeat(25_000)} !1`)).toEqual([
+ ["!1", "https://gitlab.com/team/apps/web/-/merge_requests/1", "reference"],
+ ]);
+ });
+
+ it("links references that follow punctuation and a mention's trailing period", () => {
+ expect(linked("(!5), thanks @julius.")).toEqual([
+ ["!5", "https://gitlab.com/team/apps/web/-/merge_requests/5", "reference"],
+ ["@julius", "https://gitlab.com/julius", "mention"],
+ ]);
+ });
+
+ it("links against the repository's own host", () => {
+ expect(
+ gitLabReferenceAutolinks("See !5", "https://git.example.com/acme/web").map(({ url }) => url),
+ ).toEqual(["https://git.example.com/acme/web/-/merge_requests/5"]);
+ });
+});
diff --git a/packages/source-control-gitlab/src/shared/referenceAutolinks.ts b/packages/source-control-gitlab/src/shared/referenceAutolinks.ts
new file mode 100644
index 00000000000..189e9f7085c
--- /dev/null
+++ b/packages/source-control-gitlab/src/shared/referenceAutolinks.ts
@@ -0,0 +1,83 @@
+import type { ReferenceAutolink } from "@t3tools/source-control-core/shared/definition";
+
+// GitLab's autolinked references: `#12` issues and `!12` merge requests, qualified by
+// `project` (same namespace) or a full `group/sub/project` path; `GL-12` and `[issue:12]`
+// (or `[issue:project/12]`) issues; a SHA of 7 to 40 characters,
+// optionally qualified the same way with `@`; `@user` and `@group/subgroup`.
+const PROJECT = String.raw`[\w.-]+(?:\/[\w.-]+)*`;
+const REFERENCE = new RegExp(
+ String.raw`(?<![\w./\\-])(?:(${PROJECT})?([#!])([1-9]\d*)|(?:(${PROJECT})@)?([a-f\d]{7,40})(?![\w])|@([\w](?:[\w.-]*[\w])?(?:\/[\w](?:[\w.-]*[\w])?)*)|GL-([1-9]\d*)|\[issue:(?:(${PROJECT})\/)?([1-9]\d*)\])(?![\w])`,
+ "giu",
+);
+const CANDIDATE = /[#!@]|\bGL-\d|\[issue:|\b[a-f\d]{7,40}\b/iu;
+const SHORT_SHA_LENGTH = 7;
+const ROUTE = { "#": "issues", "!": "merge_requests" } as const;
+
+export function gitLabReferenceAutolinks(
+ text: string,
+ repositoryUrl: string,
+): ReadonlyArray<ReferenceAutolink> {
+ if (!CANDIDATE.test(text)) return [];
+ let url: URL;
+ try {
+ url = new URL(repositoryUrl);
+ } catch {
+ return [];
+ }
+ const path = url.pathname.split("/").filter(Boolean);
+ if (path.length < 2 || !/^https?:$/u.test(url.protocol)) return [];
+ const namespace = path.slice(0, -1).join("/");
+ const project = (qualifier: string | undefined) =>
+ qualifier?.includes("/") ? qualifier : qualifier ? `${namespace}/${qualifier}` : path.join("/");
+ const links: ReferenceAutolink[] = [];
+ for (const match of text.matchAll(REFERENCE)) {
+ const [
+ matched,
+ referenceProject,
+ sigil,
+ number,
+ commitProject,
+ sha,
+ mention,
+ prefixedIssue,
+ bracketProject,
+ bracketIssue,
+ ] = match;
+ if (number && (sigil === "#" || sigil === "!")) {
+ links.push({
+ index: match.index,
+ text: matched,
+ label: matched,
+ url: `${url.origin}/${project(referenceProject)}/-/${ROUTE[sigil]}/${number}`,
+ kind: "reference",
+ });
+ } else if (bracketIssue || (prefixedIssue && matched.startsWith("GL-"))) {
+ links.push({
+ index: match.index,
+ text: matched,
+ label: matched,
+ url: `${url.origin}/${project(bracketProject)}/-/issues/${bracketIssue ?? prefixedIssue}`,
+ kind: "reference",
+ });
+ } else if (sha) {
+ // A run of digits is a number far more often than a SHA.
+ if (!/[a-f]/iu.test(sha)) continue;
+ links.push({
+ index: match.index,
+ text: matched,
+ label: `${commitProject ? `${commitProject}@` : ""}${sha.slice(0, SHORT_SHA_LENGTH)}`,
+ url: `${url.origin}/${project(commitProject)}/-/commit/${sha}`,
+ kind: "commit",
+ });
+ } else if (mention && mention.toLowerCase() !== "all") {
+ links.push({
+ index: match.index,
+ text: matched,
+ label: matched,
+ url: `${url.origin}/${mention}`,
+ kind: "mention",
+ });
+ }
+ }
+ return links;
+}