TypeScript
fix(shared): recognise Azure DevOps Server pull request links (#14626)
Leonel Rivasfa68107b8b31
diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.test.tsindex e0c36d7b173..23d48eba21e 100644--- a/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts+++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts@@ -426,6 +426,44 @@ describe("pull request toolkit handlers", () => {}),);+ it.effect("links Azure DevOps Server URLs and numeric references without a known provider", () =>+ Effect.gen(function* () {+ const harness = yield* makeHarness({+ project: makeProject({+ canonicalKey: "scm.example.test/tfs/collection/project/_git/repo",+ locator: {+ source: "git-remote",+ remoteName: "origin",+ remoteUrl: "http://dummy:[email protected]:8080/tfs/Collection/Project/_git/Repo",+ },+ provider: "unknown",+ displayName: "tfs/collection/project/_git/repo",+ }),+ });+ const generated =+ "http://scm.example.test:8080/tfs/collection/project/_git/repo/pullrequest/124";+ const fromNumber = yield* harness.call("link_pull_request", {+ repository: "tfs/Collection/Project/_git/Repo",+ number: 124,+ });+ expect.soft(fromNumber.url).toBe(generated);++ const supplied = "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/123";+ const fromUrl = yield* harness.call("link_pull_request", { url: supplied });+ expect(fromUrl).toEqual({+ host: "scm.example.test",+ repository: "tfs/collection/project/_git/repo",+ number: 123,+ url: supplied,+ alreadyLinked: false,+ });+ expect(yield* Ref.get(harness.commands)).toMatchObject([+ { type: "thread.pull-request.link", number: 124, url: generated },+ { type: "thread.pull-request.link", number: 123, url: supplied },+ ]);+ }),+ );+it.effect("rejects a target that names neither a URL nor repository and number", () =>Effect.gen(function* () {const harness = yield* makeHarness();diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.tsindex 570d9020715..c777214aad5 100644--- a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts+++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts@@ -97,7 +97,17 @@ const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (repository,input.number,project?.repositoryIdentity?.locator.remoteUrl,- ) ?? `https://${host}/${repository}/pull/${input.number}`;+ ) ??+ (/(?:^|\/)_git\/[^/]+$/u.test(repository)+ ? changeRequestUrlFor(+ "azure-devops",+ host,+ repository,+ input.number,+ project?.repositoryIdentity?.locator.remoteUrl,+ )+ : null) ??+ `https://${host}/${repository}/pull/${input.number}`;return {...normalizeThreadPullRequestKey({ host, repository, number: input.number, url }),url,diff --git a/apps/web/src/lib/openPullRequestLink.test.ts b/apps/web/src/lib/openPullRequestLink.test.tsindex f2eadb52f0b..ef222b8b007 100644--- a/apps/web/src/lib/openPullRequestLink.test.ts+++ b/apps/web/src/lib/openPullRequestLink.test.ts@@ -249,17 +249,24 @@ describe("findProjectOnChangeRequestHost", () => {});it.each([- "ssh.dev.azure.com/v3/org-a/project/web",- "vs-ssh.visualstudio.com/v3/org-a/project/web",- "org-a.visualstudio.com/defaultcollection/project/_git/web",- "dev.azure.com/org-a/project/_git/web",- ])("matches Azure browser references against %s", (canonicalKey) => {+ ["ssh.dev.azure.com/v3/org-a/project/web", "[email protected]:v3/org-a/project/web"],+ [+ "vs-ssh.visualstudio.com/v3/org-a/project/web",+ "[email protected]:v3/org-a/project/web",+ ],+ [+ "org-a.visualstudio.com/defaultcollection/project/_git/web",+ "https://org-a.visualstudio.com/DefaultCollection/Project/_git/Web",+ ],+ ["dev.azure.com/org-a/project/_git/web", "https://[email protected]/org-a/Project/_git/Web"],+ ])("matches Azure browser references against %s", (canonicalKey, remoteUrl) => {const checkout = project("azure", {- canonicalKey,- provider: "azure-devops",+ ...repositoryIdentity("azure-devops", canonicalKey, remoteUrl),displayName: canonicalKey.split("/").slice(1).join("/"),});- const reference = { host: "dev.azure.com", repository: "org-a/project/_git/web", number: 42 };+ const reference = parseChangeRequestUrl(+ "https://dev.azure.com/org-a/project/_git/web/pullrequest/42",+ )!;expect(findProjectForChangeRequest([checkout], reference)).toBe(checkout);expect(findProjectOnChangeRequestHost([checkout], reference)).toBe(checkout);expect(diff --git a/packages/shared/src/changeRequestUrl.test.ts b/packages/shared/src/changeRequestUrl.test.tsindex 68211c0faf5..183634d07b5 100644--- a/packages/shared/src/changeRequestUrl.test.ts+++ b/packages/shared/src/changeRequestUrl.test.ts@@ -1,11 +1,14 @@+import { ProjectId } from "@t3tools/contracts";import { describe, expect, it } from "vite-plus/test";import {changeRequestUrlFor,+ matchesLinkedPullRequestUrl,parseChangeRequestUrl,pullRequestCandidateUrlFromReferenceAutolink,siblingPullRequestUrl,} from "./changeRequestUrl.ts";+import { normalizeThreadPullRequestKey } from "./threadPullRequests.ts";describe("parseChangeRequestUrl", () => {it("reads a GitHub pull request, lower-casing the repository", () => {@@ -69,6 +72,32 @@ describe("parseChangeRequestUrl", () => {).toEqual({ host: "acme.visualstudio.com", repository: "platform/_git/t3code", number: 17 });});+ it("reads Azure DevOps Server links and keeps servers on different ports distinct", () => {+ for (const host of ["scm.example.test", "git.cafe", "code.bitbucket.example.test"]) {+ const repository = "tfs/collection/project/_git/repo";+ const linked = {+ projectId: ProjectId.make("project-1"),+ host,+ repository,+ number: 123,+ url: `https://${host.toUpperCase()}:8443/tfs/Collection/Project/_git/Repo/pullrequest/123`,+ };+ const other = { ...linked, url: linked.url.replace(":8443/", ":9443/") };+ expect.soft(parseChangeRequestUrl(linked.url), host).toEqual({+ host,+ authority: `${host}:8443`,+ repository,+ number: 123,+ });+ expect.soft(matchesLinkedPullRequestUrl(linked, `${linked.url}?view=files`), host).toBe(true);+ expect.soft(matchesLinkedPullRequestUrl(linked, other.url), host).toBe(false);+ expect.soft([linked, other].map(normalizeThreadPullRequestKey), host).toEqual([+ { host: `${host}:8443`, repository, number: 123 },+ { host: `${host}:9443`, repository, number: 123 },+ ]);+ }+ });+it("survives trailing segments, a trailing slash and a query string", () => {expect(parseChangeRequestUrl("https://github.com/t3tools/t3code/pull/123/files?w=1")).toEqual({host: "github.com",@@ -90,6 +119,12 @@ describe("parseChangeRequestUrl", () => {"https://github.com/t3tools/t3code/pull/abc","https://gitlab.com/t3tools/t3code/-/issues/12","https://blog.example.test/2026/updates/pull/3",+ "https://scm.example.test/tfs/Collection/Project/_git/Repo",+ "https://scm.example.test/tfs/Collection/Project/pullrequest/123",+ "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/abc",+ "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/0",+ "https://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/9007199254740992",+ "ftp://scm.example.test/tfs/Collection/Project/_git/Repo/pullrequest/123","javascript:alert(1)//github.com/t3tools/t3code/pull/1","not a url",]) {diff --git a/packages/shared/src/changeRequestUrl.ts b/packages/shared/src/changeRequestUrl.tsindex 3c24ed75019..a39593a9c09 100644--- a/packages/shared/src/changeRequestUrl.ts+++ b/packages/shared/src/changeRequestUrl.ts@@ -30,28 +30,28 @@ export function parseChangeRequestUrl(targetUrl: string): ChangeRequestLink | nuif (host === "git.cafe" || host === "staging.git.cafe") {const match = /^\/([^/]+\/[^/]+)\/pulls\/(\d+)(?:\/|$)/u.exec(url.pathname);- return claim(host, match);- }- if (isHostOf(host, "github.com", "github")) {- const match = /^\/([^/]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u.exec(url.pathname);if (match) return claim(host, match);+ } else {+ if (isHostOf(host, "github.com", "github")) {+ const match = /^\/([^/]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u.exec(url.pathname);+ if (match) return claim(host, match);+ }+ const forgejo = /^\/([^/]+(?:\/[^/]+)+)\/pulls\/(\d+)(?:\/|$)/u.exec(url.pathname);+ if (forgejo) {+ const link = claim(host, forgejo);+ return link === null ? null : { ...link, authority: url.host.toLowerCase() };+ }+ const gitlab = /^\/([^/]+(?:\/[^/]+)+)\/-\/merge_requests\/(\d+)(?:\/|$)/u.exec(url.pathname);+ if (gitlab) return claim(host, gitlab);+ if (isHostOf(host, "bitbucket.org", "bitbucket")) {+ const match = /^\/([^/]+\/[^/]+)\/pull-requests\/(\d+)(?:\/|$)/u.exec(url.pathname);+ if (match) return claim(host, match);+ }}- const forgejo = /^\/([^/]+(?:\/[^/]+)+)\/pulls\/(\d+)(?:\/|$)/u.exec(url.pathname);- if (forgejo) {- const link = claim(host, forgejo);- return link === null ? null : { ...link, authority: url.host.toLowerCase() };- }- const gitlab = /^\/([^/]+(?:\/[^/]+)+)\/-\/merge_requests\/(\d+)(?:\/|$)/u.exec(url.pathname);- if (gitlab) return claim(host, gitlab);- if (isHostOf(host, "bitbucket.org", "bitbucket")) {- const match = /^\/([^/]+\/[^/]+)\/pull-requests\/(\d+)(?:\/|$)/u.exec(url.pathname);- return claim(host, match);- }- if (isHostOf(host, "dev.azure.com") || host.endsWith(".visualstudio.com")) {- const match = /^\/((?:[^/]+\/)*_git\/[^/]+)\/pullrequest\/(\d+)(?:\/|$)/u.exec(url.pathname);- return claim(host, match);- }- return null;+ // Azure DevOps Server can use any hostname; its `_git` pull request route identifies it.+ const azure = /^\/((?:[^/]+\/)*_git\/[^/]+)\/pullrequest\/(\d+)(?:\/|$)/u.exec(url.pathname);+ const link = claim(host, azure);+ return link === null || url.port === "" ? link : { ...link, authority: url.host.toLowerCase() };}function claim(host: string, match: RegExpExecArray | null): ChangeRequestLink | null {@@ -62,6 +62,22 @@ function claim(host: string, match: RegExpExecArray | null): ChangeRequestLink |: null;}+function remoteWebOrigin(host: string, remoteUrl: string | undefined): string {+ try {+ const remote = new URL(remoteUrl ?? "");+ if (+ (remote.protocol === "http:" || remote.protocol === "https:") &&+ (remote.hostname.toLowerCase() === host.toLowerCase() ||+ remote.host.toLowerCase() === host.toLowerCase())+ ) {+ return remote.origin;+ }+ } catch {+ // SSH remotes do not specify the server's web origin.+ }+ return `https://${host}`;+}+export function changeRequestUrlFor(kind: string | null | undefined,host: string,@@ -74,27 +90,18 @@ export function changeRequestUrlFor(return `https://${host}/${repository}/pull/${number}`;case "gitcafe":return `https://${host}/${repository}/pulls/${number}`;- case "forgejo": {- try {- const remote = new URL(remoteUrl ?? "");- if (- (remote.protocol === "http:" || remote.protocol === "https:") &&- (remote.hostname.toLowerCase() === host.toLowerCase() ||- remote.host.toLowerCase() === host.toLowerCase())- ) {- return `${remote.origin}/${repository}/pulls/${number}`;- }- } catch {- // SSH remotes do not specify the server's web origin.- }- return `https://${host}/${repository}/pulls/${number}`;- }+ case "forgejo":+ return `${remoteWebOrigin(host, remoteUrl)}/${repository}/pulls/${number}`;case "gitlab":return `https://${host}/${repository}/-/merge_requests/${number}`;case "bitbucket":return `https://${host}/${repository}/pull-requests/${number}`;- case "azure-devops":- return `https://${canonicalRepositoryKey(`${host}/${repository}`.toLowerCase())}/pullrequest/${number}`;+ case "azure-devops": {+ const [canonicalHost = host, ...path] = canonicalRepositoryKey(+ `${host}/${repository}`.toLowerCase(),+ ).split("/");+ return `${remoteWebOrigin(canonicalHost, remoteUrl)}/${path.join("/")}/pullrequest/${number}`;+ }default:return null;}