Skip to content

Commit 1427a36

Browse files
committed
fix(server): GitLab merge requests under an installation prefix resolve their project
A self-managed GitLab served under a URL prefix (glab's `subfolder`) has that prefix in its browser URLs, so a merge request linked from one was stored as `gitlab/team/project`. glab adds the prefix to every API call itself, so GitLab was asked for a project at `gitlab/team/project`, which does not exist, and every read and the watcher failed with a 404. The GitLab provider now asks GitLab for the project's `path_with_namespace` (`glab repo view <browser URL> --output json`, remembered for ten minutes) and uses it for every API call. The stored repository and the links built from it stay as they are, so merge requests that were already linked start working without a migration. glab, not string handling, decides where the prefix ends, so a group that shares the prefix's name is left alone. If GitLab does not answer, the repository is used as given. Fixes #20226
1 parent cfafb4b commit 1427a36

6 files changed

Lines changed: 281 additions & 20 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ describe("parseChangeRequestUrl", () => {
5555
);
5656
});
5757

58+
it("keeps a GitLab installation prefix in the repository so the link rebuilds to the same page", () => {
59+
const url = "https://code.example.com/gitlab/team/subgroup/project/-/merge_requests/9";
60+
const parsed = parseChangeRequestUrl(url);
61+
62+
expect(parsed).toEqual({
63+
host: "code.example.com",
64+
repository: "gitlab/team/subgroup/project",
65+
number: 9,
66+
});
67+
expect(changeRequestUrlFor("gitlab", parsed!.host, parsed!.repository, parsed!.number)).toBe(
68+
url,
69+
);
70+
});
71+
72+
it("does not take a GitLab group named gitlab for an installation prefix", () => {
73+
expect(
74+
parseChangeRequestUrl("https://gitlab.example.com/gitlab/project/-/merge_requests/9"),
75+
).toEqual({ host: "gitlab.example.com", repository: "gitlab/project", number: 9 });
76+
});
77+
5878
it("reads Bitbucket and both Azure DevOps URL forms", () => {
5979
expect(parseChangeRequestUrl("https://bitbucket.org/workspace/repo/pull-requests/5")).toEqual({
6080
host: "bitbucket.org",

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, assert, expect, it, vi } from "@effect/vitest";
1+
import { afterEach, assert, describe, expect, it, vi } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
44
import { ChildProcessSpawner } from "effect/process";
@@ -1495,4 +1495,98 @@ layer("GitLabPullRequestCli.layer", (it) => {
14951495
assert.strictEqual(mockedExecute.mock.calls.length, 3);
14961496
}),
14971497
);
1498+
1499+
describe("resolveProjectPath", () => {
1500+
const projectJson = (pathWithNamespace: string) =>
1501+
output(JSON.stringify({ id: 12, path_with_namespace: pathWithNamespace }));
1502+
1503+
it.effect("asks GitLab for the project path behind an installation prefix", () =>
1504+
Effect.gen(function* () {
1505+
mockedExecute.mockReturnValueOnce(Effect.succeed(projectJson("team/subgroup/project")));
1506+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1507+
1508+
const path = yield* cli.resolveProjectPath({
1509+
cwd: "/w",
1510+
host: "code.example.com",
1511+
repository: "gitlab/team/subgroup/project",
1512+
});
1513+
1514+
assert.strictEqual(path, "team/subgroup/project");
1515+
expect(argsOfCall(0)).toEqual([
1516+
"repo",
1517+
"view",
1518+
"https://code.example.com/gitlab/team/subgroup/project",
1519+
"--output",
1520+
"json",
1521+
]);
1522+
}),
1523+
);
1524+
1525+
it.effect("keeps a group named like the prefix when no prefix is installed", () =>
1526+
Effect.gen(function* () {
1527+
mockedExecute.mockReturnValueOnce(Effect.succeed(projectJson("gitlab/project")));
1528+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1529+
1530+
const path = yield* cli.resolveProjectPath({
1531+
cwd: "/w",
1532+
host: "plain.example.com",
1533+
repository: "gitlab/project",
1534+
});
1535+
1536+
assert.strictEqual(path, "gitlab/project");
1537+
}),
1538+
);
1539+
1540+
it.effect("drops the prefix once when a group is named like it", () =>
1541+
Effect.gen(function* () {
1542+
mockedExecute.mockReturnValueOnce(Effect.succeed(projectJson("gitlab/project")));
1543+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1544+
1545+
const path = yield* cli.resolveProjectPath({
1546+
cwd: "/w",
1547+
host: "nested.example.com",
1548+
repository: "gitlab/gitlab/project",
1549+
});
1550+
1551+
assert.strictEqual(path, "gitlab/project");
1552+
}),
1553+
);
1554+
1555+
it.effect("asks once per repository while the answer is fresh", () =>
1556+
Effect.gen(function* () {
1557+
mockedExecute.mockReturnValue(Effect.succeed(projectJson("team/project")));
1558+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1559+
const input = { cwd: "/w", host: "fresh.example.com", repository: "gitlab/team/project" };
1560+
1561+
yield* cli.resolveProjectPath(input);
1562+
const path = yield* cli.resolveProjectPath(input);
1563+
1564+
assert.strictEqual(path, "team/project");
1565+
assert.strictEqual(mockedExecute.mock.calls.length, 1);
1566+
}),
1567+
);
1568+
1569+
it.effect("uses the repository as given when GitLab cannot answer, and asks again", () =>
1570+
Effect.gen(function* () {
1571+
mockedExecute.mockReturnValueOnce(
1572+
Effect.fail(
1573+
new GitLabCli.GitLabCliCommandError({
1574+
operation: "execute",
1575+
command: "glab",
1576+
cwd: "/w",
1577+
cause: new Error("glab exited with status 1"),
1578+
}),
1579+
),
1580+
);
1581+
mockedExecute.mockReturnValueOnce(Effect.succeed(output("not json")));
1582+
mockedExecute.mockReturnValueOnce(Effect.succeed(projectJson("team/project")));
1583+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1584+
const input = { cwd: "/w", host: "flaky.example.com", repository: "gitlab/team/project" };
1585+
1586+
assert.strictEqual(yield* cli.resolveProjectPath(input), "gitlab/team/project");
1587+
assert.strictEqual(yield* cli.resolveProjectPath(input), "gitlab/team/project");
1588+
assert.strictEqual(yield* cli.resolveProjectPath(input), "team/project");
1589+
}),
1590+
);
1591+
});
14981592
});

‎packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import * as Cache from "effect/Cache";
12
import * as Context from "effect/Context";
3+
import * as Duration from "effect/Duration";
24
import * as Effect from "effect/Effect";
5+
import * as Exit from "effect/Exit";
36
import * as Layer from "effect/Layer";
47
import * as Result from "effect/Result";
58
import * as Schema from "effect/Schema";
@@ -34,6 +37,7 @@ import {
3437
decodeNotesJson,
3538
decodeOwnAwardIdJson,
3639
decodeProjectMergeCapabilitiesJson,
40+
decodeProjectPathJson,
3741
decodeProjectUsersJson,
3842
decodeRepositoryBlobsJson,
3943
decodeViewerJson,
@@ -183,6 +187,8 @@ const CONVERSATION_PAGES = 10;
183187
const DIFF_MAX_OUTPUT_BYTES = 8 * 1024 * 1024;
184188
const DIFF_TIMEOUT_MS = 60_000;
185189
const DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024;
190+
const PROJECT_PATH_CACHE_CAPACITY = 128;
191+
const PROJECT_PATH_CACHE_TTL = Duration.minutes(10);
186192

187193
export interface GitLabMergeRequestListBatch {
188194
readonly items: ReadonlyArray<GitLabMergeRequestListItem>;
@@ -199,6 +205,18 @@ export interface GitLabMergeRequestDiffSlice {
199205
export class GitLabPullRequestCli extends Context.Service<
200206
GitLabPullRequestCli,
201207
{
208+
/**
209+
* The path GitLab's API knows `repository` by. A repository read from a browser URL carries
210+
* the installation's URL prefix, which glab adds to every API call itself, so only GitLab can
211+
* say where the prefix ends and the project path begins. Never fails: a host that does not
212+
* answer is asked about `repository` as given.
213+
*/
214+
readonly resolveProjectPath: (input: {
215+
readonly cwd: string;
216+
readonly host: string;
217+
readonly repository: string;
218+
}) => Effect.Effect<string>;
219+
202220
readonly getViewerUsername: (input: {
203221
readonly cwd: string;
204222
}) => Effect.Effect<string, GitLabPullRequestCliError>;
@@ -496,6 +514,36 @@ export const make = Effect.gen(function* () {
496514
...(input.timeoutMs === undefined ? {} : { timeoutMs: input.timeoutMs }),
497515
});
498516

517+
const projectPaths = yield* Cache.makeWith(
518+
(key: string) => {
519+
const [cwd = "", host = "", repository = ""] = key.split("\u0000");
520+
return gitlab
521+
.execute({
522+
cwd,
523+
args: ["repo", "view", `https://${host}/${repository}`, "--output", "json"],
524+
})
525+
.pipe(
526+
Effect.flatMap((result): Effect.Effect<string, GitLabPullRequestCliError> => {
527+
const decoded = decodeProjectPathJson(result.stdout.trim());
528+
return Result.isSuccess(decoded)
529+
? Effect.succeed(decoded.success)
530+
: Effect.fail(
531+
new GitLabMergeRequestReadError({
532+
command: "glab",
533+
cwd,
534+
operation: "resolveProjectPath",
535+
cause: decoded.failure,
536+
}),
537+
);
538+
}),
539+
);
540+
},
541+
{
542+
capacity: PROJECT_PATH_CACHE_CAPACITY,
543+
timeToLive: (exit) => (Exit.isSuccess(exit) ? PROJECT_PATH_CACHE_TTL : Duration.zero),
544+
},
545+
);
546+
499547
const listPage = (input: {
500548
readonly cwd: string;
501549
readonly repository: string;
@@ -1045,6 +1093,11 @@ export const make = Effect.gen(function* () {
10451093
);
10461094

10471095
return GitLabPullRequestCli.of({
1096+
resolveProjectPath: (input) =>
1097+
Cache.get(projectPaths, [input.cwd, input.host, input.repository].join("\u0000")).pipe(
1098+
Effect.orElseSucceed(() => input.repository),
1099+
),
1100+
10481101
getViewerUsername: viewerUsername,
10491102

10501103
listMergeRequests: (input) => {

‎packages/source-control-gitlab/src/server/GitLabPullRequestProvider.test.ts‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import * as Layer from "effect/Layer";
55
import * as GitLabPullRequestCli from "./GitLabPullRequestCli.ts";
66
import { gitLabViewerPermissions, make } from "./GitLabPullRequestProvider.ts";
77

8+
const resolvesToItself = {
9+
resolveProjectPath: (input: { readonly repository: string }) => Effect.succeed(input.repository),
10+
};
11+
812
describe("gitLabViewerPermissions", () => {
913
it("offers everything to a viewer GitLab says can merge", () => {
1014
expect(gitLabViewerPermissions({ viewerCanMerge: true })).toEqual({
@@ -82,6 +86,7 @@ describe("getChangeRequest base freshness", () => {
8286
const provider = yield* make.pipe(
8387
Effect.provide(
8488
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
89+
...resolvesToItself,
8590
getMergeRequestDetail: () => Effect.succeed(detail),
8691
}),
8792
),
@@ -106,6 +111,7 @@ describe("getChangeRequest base freshness", () => {
106111
}).pipe(
107112
Effect.provide(
108113
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
114+
...resolvesToItself,
109115
getMergeRequestDetail: () => Effect.succeed({ ...detail, ...divergence }),
110116
getProjectMergeCapabilities: () =>
111117
Effect.succeed({ merge: true, squash: true, rebase: true }),
@@ -147,7 +153,11 @@ describe("rewriting what has already been said", () => {
147153

148154
const providerWith = make.pipe(
149155
Effect.provide(
150-
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({ updateMergeRequest, updateNote }),
156+
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
157+
...resolvesToItself,
158+
updateMergeRequest,
159+
updateNote,
160+
}),
151161
),
152162
);
153163

@@ -218,6 +228,7 @@ describe("expanding unchanged lines", () => {
218228
const provider = yield* make.pipe(
219229
Effect.provide(
220230
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
231+
...resolvesToItself,
221232
getMergeRequestDiffFileContents,
222233
}),
223234
),
@@ -240,6 +251,7 @@ describe("expanding unchanged lines", () => {
240251
const provider = yield* make.pipe(
241252
Effect.provide(
242253
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
254+
...resolvesToItself,
243255
getMergeRequestDiffFileContents: () =>
244256
Effect.fail(
245257
new GitLabPullRequestCli.GitLabDiffFileContentsUnavailableError({
@@ -264,3 +276,44 @@ describe("expanding unchanged lines", () => {
264276
}),
265277
);
266278
});
279+
280+
describe("a repository linked from a browser URL under an installation prefix", () => {
281+
const stored = {
282+
cwd: "/w",
283+
repository: "gitlab/team/subgroup/project",
284+
host: "code.example.com",
285+
number: 9,
286+
};
287+
const resolveProjectPath = vi.fn(() => Effect.succeed("team/subgroup/project"));
288+
const getMergeRequestDiff = vi.fn(() =>
289+
Effect.succeed({ patch: "", truncated: false, nextCursor: null }),
290+
);
291+
const commentOnMergeRequest = vi.fn(() => Effect.void);
292+
293+
it.effect("reads and writes through the project path GitLab reports for the stored one", () =>
294+
Effect.gen(function* () {
295+
const provider = yield* make.pipe(
296+
Effect.provide(
297+
Layer.mock(GitLabPullRequestCli.GitLabPullRequestCli)({
298+
resolveProjectPath,
299+
getMergeRequestDiff,
300+
commentOnMergeRequest,
301+
}),
302+
),
303+
);
304+
305+
yield* provider.getDiff(stored);
306+
yield* provider.comment({ ...stored, body: "Looks good." });
307+
308+
expect(resolveProjectPath).toHaveBeenCalledWith(
309+
expect.objectContaining({ host: "code.example.com", repository: stored.repository }),
310+
);
311+
expect(getMergeRequestDiff).toHaveBeenCalledWith(
312+
expect.objectContaining({ repository: "team/subgroup/project", number: 9 }),
313+
);
314+
expect(commentOnMergeRequest).toHaveBeenCalledWith(
315+
expect.objectContaining({ repository: "team/subgroup/project", number: 9 }),
316+
);
317+
}),
318+
);
319+
});

0 commit comments

Comments
 (0)