fix(server): resolve prefixed GitLab merge requests to their project #21576
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
juliusmarminge changed the title
juliusmarminge added labelsmacroscope-review
- apps/server/src/mcp/toolkits/pullRequests/handlers.ts lines 268 to 274OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -248,3 +256,19 @@248256 ? Effect.failCause(cause as Cause.Cause<never>)249257 : Effect.fail(new Failure({ cause }));250258259+ const resolveLink = Effect.fn("PullRequestsToolkit.resolveLink")(function* (260+ target: ResolvedTarget,261+ input: PullRequestTargetInput,262+ project: OrchestrationProjectShell | undefined,263+ ) {264+ if (input.url !== undefined) {265+ return { url: target.url, repositoryWebUrl: target.url };266+ }267+ if (project?.repositoryIdentity?.provider !== "gitlab") return { url: target.url };268+ const detail = yield* pullRequests.detail({269+ projectId: project.id,270+ host: target.host,271+ repository: target.repository,272+ number: target.number,273+ webUrl: null,274+ });
- @@ -1066 +1105,21 @@10661105 return GitLabPullRequestCli.of({1106+ resolveProjectPath: ({ cwd, webUrl }) =>1107+ Effect.try({1108+ try: () => {1109+ const url = new URL(webUrl);1110+ url.username = "";1111+ url.password = "";1112+ // glab owns the installation prefix; only the known MR suffix is removed here.1113+ url.pathname = url.pathname.replace(/\/-\/merge_requests\/\d+(?:\/.*)?$/, "");1114+ url.search = "";1115+ url.hash = "";1116+ return url.toString();1117+ },1118+ catch: (cause) =>1119+ new GitLabMergeRequestReadError({1120+ command: "glab",1121+ cwd,1122+ operation: "resolveProjectPath",1123+ cause,1124+ }),1125+ }).pipe(Effect.flatMap((url) => Cache.get(projectPaths, { cwd, webUrl: url }))),
- @@ -276,3 +306,9 @@276306 if (watching && before?.snapshot?.state === "merged") {277307 return yield* new PullRequestNotOpenError({ state: "merged" });278308 }309+ const link =310+ watching && before === undefined311+ ? yield* resolveLink(target, input, project).pipe(312+ Effect.mapError((cause) => new PullRequestWatchFailedError({ cause })),313+ )314+ : undefined;
juliusmarminge added labelsbugfix
juliusmarminge added labelspr:needs-julius
- @@ -263,4 +264,17 @@263264 ),264265 );265266266- const provider: PullRequestProviderApi = {267+ // The path read from a clone or MR URL drops a GitLab installation's path prefix; glab resolves268+ // the authoritative URL to the project before any read or write.269+ const onProject =270+ <I extends ProviderRepositoryRef, A, E>(271+ operation: string,272+ call: (input: I) => Effect.Effect<A, E>,273+ ) =>274+ (input: I) =>275+ input.webUrl === undefined276+ ? call(input)277+ : cli.resolveProjectPath({ cwd: input.cwd, webUrl: input.webUrl }).pipe(278+ Effect.mapError(fail(operation)),279+ Effect.flatMap((repository) => call({ ...input, repository })),280+ );@@ -658,3 +674,7 @@658674 })659675 .pipe(Effect.mapError(fail("setThreadResolution"))),660676 ),677+ } satisfies PullRequestProviderApi;678+679+ const routed: PullRequestProviderApi = {680+ ...provider,
- @@ -262,0 +267,21 @@267+ input: PullRequestTargetInput,268+ project: OrchestrationProjectShell | undefined,269+ ) {270+ if (input.url !== undefined) {271+ return { url: target.url, repositoryWebUrl: target.url };272+ }273+ // Only a URL built from the project's own GitLab remote can miss an installation prefix.274+ const projectHost = projectHostAndProvider(project);275+ if (276+ project === undefined ||277+ projectHost.kind !== "gitlab" ||278+ projectHost.host?.toLowerCase() !== target.host279+ ) {280+ return { url: target.url };281+ }282+ const detail = yield* pullRequests.detail({283+ projectId: project.id,284+ host: target.host,285+ repository: target.repository,286+ number: target.number,287+ });
- apps/server/src/pullRequest/PullRequestService.ts line 1020OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -981,3 +1010,11 @@9811010 }),9821011 );98310121013+ // A linked change request's URL is the only record of where an SSH checkout's project lives1014+ // on a host served under a path prefix, and clients ask by repository alone.1015+ const linkedWebUrls = new Map<string, string>();1016+ const linkedWebUrl = (key: string, webUrl: string | null | undefined) => {1017+ if (typeof webUrl !== "string") return webUrl;1018+ const url = withoutUrlCredentials(webUrl);1019+ linkedWebUrls.delete(key);1020+ linkedWebUrls.set(key, url);apps/server/src/pullRequest/PullRequestService.ts line 1040OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -984,7 +1020,21 @@1020+ linkedWebUrls.set(key, url);1021+ if (linkedWebUrls.size > LINKED_WEB_URL_CAPACITY) {1022+ const oldest = linkedWebUrls.keys().next().value;1023+ if (oldest !== undefined) linkedWebUrls.delete(oldest);1024+ }1025+ return url;1026+ };1027+9841028 // Azure derives organization from the checkout; require the matching repository.9851029 const requireProject = (ref: PullRequestRef): Effect.Effect<SupportedProject, PullRequestError> =>9861030 listWorkspaceProjects({ projectId: ref.projectId }).pipe(9871031 Effect.flatMap(({ supported }): Effect.Effect<SupportedProject, PullRequestError> => {9881032 const own = supported[0];9891033 const repository = ref.repository.trim();9901034 const host = ref.host?.trim().toLowerCase();1035+ const webUrl =1036+ host === undefined1037+ ? ref.webUrl1038+ : ref.webUrl === undefined1039+ ? linkedWebUrls.get(`${host}/${repository.toLowerCase()}`)1040+ : linkedWebUrl(`${host}/${repository.toLowerCase()}`, ref.webUrl);
- @@ -984,7 +1019,21 @@1019+ linkedWebUrls.delete(key);1020+ linkedWebUrls.set(key, url);1021+ if (linkedWebUrls.size > LINKED_WEB_URL_CAPACITY) {1022+ const oldest = linkedWebUrls.keys().next().value;1023+ if (oldest !== undefined) linkedWebUrls.delete(oldest);1024+ }1025+ return url;1026+ };1027+9841028 // Azure derives organization from the checkout; require the matching repository.9851029 const requireProject = (ref: PullRequestRef): Effect.Effect<SupportedProject, PullRequestError> =>9861030 listWorkspaceProjects({ projectId: ref.projectId }).pipe(9871031 Effect.flatMap(({ supported }): Effect.Effect<SupportedProject, PullRequestError> => {9881032 const own = supported[0];9891033 const repository = ref.repository.trim();9901034 const host = ref.host?.trim().toLowerCase();1035+ const webUrl =1036+ host === undefined1037+ ? ref.webUrl1038+ : ref.webUrl === undefined1039+ ? linkedWebUrls.get(`${host}/${repository.toLowerCase()}`)
tanstack-source added labelsbug
juliusmarminge linked this to fix #20226
juliusmarminge added labelstakeover:in-progress
Sign in to join this conversation.
Approvability#
Verdict: Needs Human Approval
Macroscope's review found this PR requires human approval — The change resolves prefixed GitLab URLs before pull-request reads and writes. Unanswered review concerns about cross-project cached results and stale state after refresh require resolution because users could act on information that does not match the operation’s target.
Needs human approval because:
- 3 blocking correctness issues found at or above your repo's Minimum Blocking Severity
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in [Settings](https://app.macroscope.com/pingdotgg/settings#Repos). You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).