Skip to content

Commit 0e543d5

Browse files
committed
fix(source-control): preserve Forgejo origins and legacy drafts
1 parent eb3fc61 commit 0e543d5

5 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎apps/server/src/mcp/toolkits/pullRequests/handlers.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,40 @@ describe("pull request toolkit handlers", () => {
265265
}),
266266
);
267267

268+
it.effect("links a numeric Forgejo reference with its remote's web origin and mount path", () =>
269+
Effect.gen(function* () {
270+
const harness = yield* makeHarness({
271+
project: makeProject({
272+
canonicalKey: "forge.example/git/owner/repo",
273+
locator: {
274+
source: "git-remote",
275+
remoteName: "origin",
276+
remoteUrl: "http://forge.example:3000/git/owner/repo.git",
277+
},
278+
provider: "forgejo",
279+
displayName: "git/owner/repo",
280+
}),
281+
});
282+
const result = yield* harness.call("link_pull_request", {
283+
repository: "git/owner/repo",
284+
number: 42,
285+
});
286+
expect(result).toEqual({
287+
host: "forge.example",
288+
repository: "git/owner/repo",
289+
number: 42,
290+
url: "http://forge.example:3000/git/owner/repo/pulls/42",
291+
alreadyLinked: false,
292+
});
293+
const other = yield* harness.call("link_pull_request", {
294+
host: "other.example",
295+
repository: "owner/repo",
296+
number: 42,
297+
});
298+
expect(other.url).toBe("https://other.example/owner/repo/pull/42");
299+
}),
300+
);
301+
268302
it.effect("rejects a target that names neither a URL nor repository and number", () =>
269303
Effect.gen(function* () {
270304
const harness = yield* makeHarness();

‎apps/server/src/mcp/toolkits/pullRequests/handlers.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const resolveTarget = Effect.fn("PullRequestsToolkit.resolveTarget")(function* (
8989
host,
9090
repository,
9191
input.number,
92+
project?.repositoryIdentity?.locator.remoteUrl,
9293
) ?? `https://${host}/${repository}/pull/${input.number}`;
9394
return { host, repository, number: input.number, url } satisfies ResolvedTarget;
9495
});

‎apps/server/src/pullRequest/forgejoPullRequestJson.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function forgejoChangeRequest(pr: typeof ForgejoPullRequest.Type) {
134134
baseBranch: pr.base.ref,
135135
headRepositoryNameWithOwner: pr.head.repo?.full_name ?? null,
136136
state: pr.merged ? "merged" : pr.state === "closed" ? "closed" : "open",
137-
isDraft: pr.draft ?? false,
137+
isDraft: pr.draft ?? /^(?:\[WIP\]|WIP:)/i.test(pr.title),
138138
mergeability:
139139
pr.mergeable === undefined ? "unknown" : pr.mergeable ? "mergeable" : "conflicting",
140140
additions: pr.additions ?? 0,

‎packages/shared/src/changeRequestUrl.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ describe("siblingPullRequestUrl", () => {
140140
});
141141

142142
describe("changeRequestUrlFor", () => {
143+
it.each([
144+
["http://forge.example:3000/git/owner/repo.git", "http://forge.example:3000"],
145+
["https://forge.example:8443/git/owner/repo.git", "https://forge.example:8443"],
146+
["[email protected]:git/owner/repo.git", "https://forge.example"],
147+
["http://other.example:3000/git/owner/repo.git", "https://forge.example"],
148+
])("preserves the matching Forgejo web origin from %s", (remoteUrl, origin) => {
149+
expect(changeRequestUrlFor("forgejo", "forge.example", "git/owner/repo", 42, remoteUrl)).toBe(
150+
`${origin}/git/owner/repo/pulls/42`,
151+
);
152+
});
153+
143154
it.each([
144155
["ssh.dev.azure.com", "v3/org/project/web"],
145156
["vs-ssh.visualstudio.com", "v3/org/project/web"],

‎packages/shared/src/changeRequestUrl.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,25 @@ export function changeRequestUrlFor(
8686
host: string,
8787
repository: string,
8888
number: number,
89+
remoteUrl?: string,
8990
): string | null {
9091
switch (kind) {
9192
case "github":
9293
return `https://${host}/${repository}/pull/${number}`;
93-
case "forgejo":
94+
case "forgejo": {
95+
try {
96+
const remote = new URL(remoteUrl ?? "");
97+
if (
98+
(remote.protocol === "http:" || remote.protocol === "https:") &&
99+
remote.hostname.toLowerCase() === host.toLowerCase()
100+
) {
101+
return `${remote.origin}/${repository}/pulls/${number}`;
102+
}
103+
} catch {
104+
// SSH remotes do not specify the server's web origin.
105+
}
94106
return `https://${host}/${repository}/pulls/${number}`;
107+
}
95108
case "gitlab":
96109
return `https://${host}/${repository}/-/merge_requests/${number}`;
97110
case "bitbucket":

0 commit comments

Comments
 (0)