@devries-cameron20 / t3code-1 / commits / 42a0e917cf
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

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

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Antony committed Nov 6, 2026 at 02:31 UTC 42a0e917cf89df34b48c7106b7753e12830036a4
7 files changed +164 -9
apps/mobile/src/features/pull-requests/pullRequestAutolinks.test.ts
+4 -4
@@ -29,10 +29,10 @@ describe("linkReferenceAutolinks", () => {
29 });
30
31 it("leaves markdown unchanged for a host that links nothing", () => {
32 - const gitlab = changeRequestReferenceAutolinker({
33 - provider: "gitlab",
34 - url: "https://gitlab.com/group/project/-/merge_requests/7",
32 + const forgejo = changeRequestReferenceAutolinker({
33 + provider: "forgejo",
34 + url: "https://codeberg.org/acme/web/pulls/7",
35 });
36 - expect(linkReferenceAutolinks("Closes #123", gitlab)).toBe("Closes #123");
36 + expect(linkReferenceAutolinks("Closes #123", forgejo)).toBe("Closes #123");
37 });
38 });
packages/client-runtime/src/pullRequestMarkdown.test.ts
-1
@@ -157,7 +157,6 @@ describe("change request reference autolinks", () => {
157
158 // regression: the mobile review guide linked every host's `#5` with GitHub's grammar
159 it("links nothing on a host whose grammar is not applied", () => {
160 - expect(urls("gitlab", "https://gitlab.com/group/project/-/merge_requests/7", "#5")).toBeNull();
160 expect(urls("forgejo", "https://codeberg.org/acme/web/pulls/7", "#5")).toBeNull();
161 });
162 });
packages/client-runtime/src/sourceControlClients.test.ts
+5
@@ -72,6 +72,11 @@ describe("sourceControlClients.parseChangeRequestUrl", () => {
72 repository: "team/project",
73 number: 9,
74 });
75 + expect(
76 + sourceControlClients.changeRequestUrlFromReferenceAutolink(
77 + "https://gitlab.com/team/apps/web/-/merge_requests/42",
78 + ),
79 + ).toBe("https://gitlab.com/team/apps/web/-/merge_requests/42");
80 });
81
82 it("reads Bitbucket and both Azure DevOps URL forms", () => {
packages/source-control-core/src/shared/definition.ts
+3 -3
@@ -259,9 +259,9 @@ export interface SourceControlDefinition {
259 /** Null when the URL is not one of this host's change requests. */
260 readonly parseChangeRequestUrl: (url: URL) => ParsedChangeRequestUrl | null;
261 /**
262 - * The change request URL a bare `#123` autolink might name, for hosts that write every
263 - * reference through their issue route. Only a candidate: the caller must read it as a change
264 - * request before treating it as one.
262 + * The change request URL a `reference` autolink might name, such as a GitHub `#123` written
263 + * through the issue route. Only a candidate: the caller must read it as a change request
264 + * before treating it as one.
265 */
266 readonly changeRequestUrlFromReferenceAutolink: (url: URL) => string | null;
267 /**
packages/source-control-gitlab/src/shared/definition.ts
+5 -1
@@ -11,6 +11,7 @@ import {
11 remoteWebOrigin,
12 } from "@t3tools/source-control-core/shared/definition";
13 import { icon } from "./icon.ts";
14 +import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts";
15
16 const CHECKOUT_COMMAND = /^glab\s+mr\s+checkout\s+(.+)$/i;
17 const CHANGE_REQUEST_REFERENCE =
@@ -46,7 +47,9 @@ export const definition = defineSourceControl({
47 parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH),
48 commitUrl: (input) => buildCommitUrl(input, "-/commit"),
49 parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
49 - changeRequestUrlFromReferenceAutolink: () => null,
50 + // `!12` already links the merge request itself; `#12` is always an issue.
51 + changeRequestUrlFromReferenceAutolink: (url) =>
52 + CHANGE_REQUEST_PATH.test(url.pathname) ? url.toString() : null,
53 changeRequestActions: new Set([
54 "merge",
55 "ready",
@@ -64,6 +67,7 @@ export const definition = defineSourceControl({
67 // Bot users, unlike GitHub's, have real profile pages.
68 authorProfileUrl: (login, repositoryUrl) =>
69 new URL(`/${encodeURIComponent(login)}`, repositoryUrl).toString(),
70 + referenceAutolinks: gitLabReferenceAutolinks,
71 reviewSummaryRequired: () => false,
72 checkoutCommandArgument: (input) => CHECKOUT_COMMAND.exec(input)?.[1]?.trim() ?? null,
73 isChangeRequestReference: (url) => CHANGE_REQUEST_REFERENCE.test(url),
packages/source-control-gitlab/src/shared/referenceAutolinks.test.ts new
+64
@@ -0,0 +1,64 @@
1 +import { describe, expect, it } from "vite-plus/test";
2 +
3 +import { gitLabReferenceAutolinks } from "./referenceAutolinks.ts";
4 +
5 +const repositoryUrl = "https://gitlab.com/team/apps/web";
6 +const linked = (text: string) =>
7 + gitLabReferenceAutolinks(text, repositoryUrl).map(({ label, url, kind }) => [label, url, kind]);
8 +
9 +describe("gitLabReferenceAutolinks", () => {
10 + it("links issues, merge requests, cross-project references, commits and mentions", () => {
11 + expect(
12 + linked(
13 + "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]",
14 + ),
15 + ).toEqual([
16 + ["#123", "https://gitlab.com/team/apps/web/-/issues/123", "reference"],
17 + ["!45", "https://gitlab.com/team/apps/web/-/merge_requests/45", "reference"],
18 + ["api!9", "https://gitlab.com/team/apps/api/-/merge_requests/9", "reference"],
19 + ["other/group/lib#2", "https://gitlab.com/other/group/lib/-/issues/2", "reference"],
20 + ["@julius.dev", "https://gitlab.com/julius.dev", "mention"],
21 + ["@team/mobile", "https://gitlab.com/team/mobile", "mention"],
22 + ["abc1234", "https://gitlab.com/team/apps/web/-/commit/abc1234", "commit"],
23 + ["api@abcdef1", "https://gitlab.com/team/apps/api/-/commit/abcdef1", "commit"],
24 + ["GL-7", "https://gitlab.com/team/apps/web/-/issues/7", "reference"],
25 + ["[issue:8]", "https://gitlab.com/team/apps/web/-/issues/8", "reference"],
26 + ["[issue:api/9]", "https://gitlab.com/team/apps/api/-/issues/9", "reference"],
27 + ["[issue:other/lib/3]", "https://gitlab.com/other/lib/-/issues/3", "reference"],
28 + ]);
29 + });
30 +
31 + it("shows a full SHA by its first seven characters", () => {
32 + const sha = "0123456789abcdef0123456789abcdef01234567";
33 + expect(linked(`Reverts ${sha}.`)).toEqual([
34 + ["0123456", `https://gitlab.com/team/apps/web/-/commit/${sha}`, "commit"],
35 + ]);
36 + });
37 +
38 + it("leaves URLs, escapes, emails, words, plain numbers and @all alone", () => {
39 + expect(
40 + linked(
41 + "https://gitlab.com/a/b/-/issues/1#note_2 user@example.com \\!12 1234567 issue#x a!1b @all gl-4 XGL-5",
42 + ),
43 + ).toEqual([]);
44 + });
45 +
46 + it("scans long runs of periods, dashes and slashes in linear time", () => {
47 + expect(linked(`${".".repeat(50_000)} ${".-/".repeat(25_000)} !1`)).toEqual([
48 + ["!1", "https://gitlab.com/team/apps/web/-/merge_requests/1", "reference"],
49 + ]);
50 + });
51 +
52 + it("links references that follow punctuation and a mention's trailing period", () => {
53 + expect(linked("(!5), thanks @julius.")).toEqual([
54 + ["!5", "https://gitlab.com/team/apps/web/-/merge_requests/5", "reference"],
55 + ["@julius", "https://gitlab.com/julius", "mention"],
56 + ]);
57 + });
58 +
59 + it("links against the repository's own host", () => {
60 + expect(
61 + gitLabReferenceAutolinks("See !5", "https://git.example.com/acme/web").map(({ url }) => url),
62 + ).toEqual(["https://git.example.com/acme/web/-/merge_requests/5"]);
63 + });
64 +});
packages/source-control-gitlab/src/shared/referenceAutolinks.ts new
+83
@@ -0,0 +1,83 @@
1 +import type { ReferenceAutolink } from "@t3tools/source-control-core/shared/definition";
2 +
3 +// GitLab's autolinked references: `#12` issues and `!12` merge requests, qualified by
4 +// `project` (same namespace) or a full `group/sub/project` path; `GL-12` and `[issue:12]`
5 +// (or `[issue:project/12]`) issues; a SHA of 7 to 40 characters,
6 +// optionally qualified the same way with `@`; `@user` and `@group/subgroup`.
7 +const PROJECT = String.raw`[\w.-]+(?:\/[\w.-]+)*`;
8 +const REFERENCE = new RegExp(
9 + 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])`,
10 + "giu",
11 +);
12 +const CANDIDATE = /[#!@]|\bGL-\d|\[issue:|\b[a-f\d]{7,40}\b/iu;
13 +const SHORT_SHA_LENGTH = 7;
14 +const ROUTE = { "#": "issues", "!": "merge_requests" } as const;
15 +
16 +export function gitLabReferenceAutolinks(
17 + text: string,
18 + repositoryUrl: string,
19 +): ReadonlyArray<ReferenceAutolink> {
20 + if (!CANDIDATE.test(text)) return [];
21 + let url: URL;
22 + try {
23 + url = new URL(repositoryUrl);
24 + } catch {
25 + return [];
26 + }
27 + const path = url.pathname.split("/").filter(Boolean);
28 + if (path.length < 2 || !/^https?:$/u.test(url.protocol)) return [];
29 + const namespace = path.slice(0, -1).join("/");
30 + const project = (qualifier: string | undefined) =>
31 + qualifier?.includes("/") ? qualifier : qualifier ? `${namespace}/${qualifier}` : path.join("/");
32 + const links: ReferenceAutolink[] = [];
33 + for (const match of text.matchAll(REFERENCE)) {
34 + const [
35 + matched,
36 + referenceProject,
37 + sigil,
38 + number,
39 + commitProject,
40 + sha,
41 + mention,
42 + prefixedIssue,
43 + bracketProject,
44 + bracketIssue,
45 + ] = match;
46 + if (number && (sigil === "#" || sigil === "!")) {
47 + links.push({
48 + index: match.index,
49 + text: matched,
50 + label: matched,
51 + url: `${url.origin}/${project(referenceProject)}/-/${ROUTE[sigil]}/${number}`,
52 + kind: "reference",
53 + });
54 + } else if (bracketIssue || (prefixedIssue && matched.startsWith("GL-"))) {
55 + links.push({
56 + index: match.index,
57 + text: matched,
58 + label: matched,
59 + url: `${url.origin}/${project(bracketProject)}/-/issues/${bracketIssue ?? prefixedIssue}`,
60 + kind: "reference",
61 + });
62 + } else if (sha) {
63 + // A run of digits is a number far more often than a SHA.
64 + if (!/[a-f]/iu.test(sha)) continue;
65 + links.push({
66 + index: match.index,
67 + text: matched,
68 + label: `${commitProject ? `${commitProject}@` : ""}${sha.slice(0, SHORT_SHA_LENGTH)}`,
69 + url: `${url.origin}/${project(commitProject)}/-/commit/${sha}`,
70 + kind: "commit",
71 + });
72 + } else if (mention && mention.toLowerCase() !== "all") {
73 + links.push({
74 + index: match.index,
75 + text: matched,
76 + label: matched,
77 + url: `${url.origin}/${mention}`,
78 + kind: "mention",
79 + });
80 + }
81 + }
82 + return links;
83 +}