Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:secret@scm.example.test: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();
Expand Down
12 changes: 11 additions & 1 deletion apps/server/src/mcp/toolkits/pullRequests/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions packages/shared/src/changeRequestUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ describe("parseChangeRequestUrl", () => {
).toEqual({ host: "acme.visualstudio.com", repository: "platform/_git/t3code", number: 17 });
});

it("reads an Azure DevOps Server pull request with its collection path and normalized identity", () => {
expect(
parseChangeRequestUrl(
"https://SCM.Example.test:8443/tfs/Collection/Project/_git/Repo/pullrequest/123",
),
).toEqual({
host: "scm.example.test",
repository: "tfs/collection/project/_git/repo",
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",
Expand All @@ -90,6 +102,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",
]) {
Expand Down
54 changes: 30 additions & 24 deletions packages/shared/src/changeRequestUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ export function parseChangeRequestUrl(targetUrl: string): ChangeRequestLink | nu
if (url.protocol !== "https:" && url.protocol !== "http:") return null;
const host = url.hostname.toLowerCase();

// Azure DevOps Server can use any hostname; its `_git` pull request route identifies it.
const azure = /^\/((?:[^/]+\/)*_git\/[^/]+)\/pullrequest\/(\d+)(?:\/|$)/u.exec(url.pathname);

if (host === "git.cafe" || host === "staging.git.cafe") {
const match = /^\/([^/]+\/[^/]+)\/pulls\/(\d+)(?:\/|$)/u.exec(url.pathname);
return claim(host, match);
return claim(host, match ?? azure);
}
if (isHostOf(host, "github.com", "github")) {
const match = /^\/([^/]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u.exec(url.pathname);
Expand All @@ -45,13 +48,9 @@ export function parseChangeRequestUrl(targetUrl: string): ChangeRequestLink | nu
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 claim(host, match ?? azure);
}
return null;
return claim(host, azure);
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Outdated
}

function claim(host: string, match: RegExpExecArray | null): ChangeRequestLink | null {
Expand All @@ -62,6 +61,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,
Expand All @@ -74,27 +89,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;
}
Expand Down
Loading