Skip to content

Commit aaa0581

Browse files
committed
refactor(server): simplify GitLab installation-prefix handling
Key the project-path cache on the repository ref itself instead of a NUL-joined string that was split back apart, drop the typed read error that the fallback discarded, and table-drive the three resolveProjectPath cases that differed only in their inputs.
1 parent 1427a36 commit aaa0581

2 files changed

Lines changed: 39 additions & 66 deletions

File tree

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

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,58 +1500,43 @@ layer("GitLabPullRequestCli.layer", (it) => {
15001500
const projectJson = (pathWithNamespace: string) =>
15011501
output(JSON.stringify({ id: 12, path_with_namespace: pathWithNamespace }));
15021502

1503-
it.effect("asks GitLab for the project path behind an installation prefix", () =>
1503+
it.effect.each([
1504+
{
1505+
name: "asks GitLab for the project path behind an installation prefix",
1506+
host: "code.example.com",
1507+
repository: "gitlab/team/subgroup/project",
1508+
reported: "team/subgroup/project",
1509+
},
1510+
{
1511+
name: "keeps a group named like the prefix when no prefix is installed",
1512+
host: "plain.example.com",
1513+
repository: "gitlab/project",
1514+
reported: "gitlab/project",
1515+
},
1516+
{
1517+
name: "drops the prefix once when a group is named like it",
1518+
host: "nested.example.com",
1519+
repository: "gitlab/gitlab/project",
1520+
reported: "gitlab/project",
1521+
},
1522+
] as const)("$name", ({ host, repository, reported }) =>
15041523
Effect.gen(function* () {
1505-
mockedExecute.mockReturnValueOnce(Effect.succeed(projectJson("team/subgroup/project")));
1524+
mockedExecute.mockReturnValueOnce(Effect.succeed(projectJson(reported)));
15061525
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
15071526

1508-
const path = yield* cli.resolveProjectPath({
1509-
cwd: "/w",
1510-
host: "code.example.com",
1511-
repository: "gitlab/team/subgroup/project",
1512-
});
1527+
const path = yield* cli.resolveProjectPath({ cwd: "/w", host, repository });
15131528

1514-
assert.strictEqual(path, "team/subgroup/project");
1529+
assert.strictEqual(path, reported);
15151530
expect(argsOfCall(0)).toEqual([
15161531
"repo",
15171532
"view",
1518-
"https://code.example.com/gitlab/team/subgroup/project",
1533+
`https://${host}/${repository}`,
15191534
"--output",
15201535
"json",
15211536
]);
15221537
}),
15231538
);
15241539

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-
15551540
it.effect("asks once per repository while the answer is fresh", () =>
15561541
Effect.gen(function* () {
15571542
mockedExecute.mockReturnValue(Effect.succeed(projectJson("team/project")));

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

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ import {
4848
type GitLabMergeRequestListItem,
4949
type GitLabProjectUsers,
5050
} from "./gitLabMergeRequestJson.ts";
51-
import type { ProviderListCursor } from "@t3tools/source-control-core/server/PullRequestProvider";
51+
import type {
52+
ProviderListCursor,
53+
ProviderRepositoryRef,
54+
} from "@t3tools/source-control-core/server/PullRequestProvider";
5255

5356
export class GitLabMergeRequestReadError extends Schema.TaggedError<GitLabMergeRequestReadError>()(
5457
"GitLabMergeRequestReadError",
@@ -211,11 +214,7 @@ export class GitLabPullRequestCli extends Context.Service<
211214
* say where the prefix ends and the project path begins. Never fails: a host that does not
212215
* answer is asked about `repository` as given.
213216
*/
214-
readonly resolveProjectPath: (input: {
215-
readonly cwd: string;
216-
readonly host: string;
217-
readonly repository: string;
218-
}) => Effect.Effect<string>;
217+
readonly resolveProjectPath: (input: ProviderRepositoryRef) => Effect.Effect<string>;
219218

220219
readonly getViewerUsername: (input: {
221220
readonly cwd: string;
@@ -515,29 +514,17 @@ export const make = Effect.gen(function* () {
515514
});
516515

517516
const projectPaths = yield* Cache.makeWith(
518-
(key: string) => {
519-
const [cwd = "", host = "", repository = ""] = key.split("\u0000");
520-
return gitlab
517+
({ cwd, host, repository }: ProviderRepositoryRef) =>
518+
gitlab
521519
.execute({
522520
cwd,
523521
args: ["repo", "view", `https://${host}/${repository}`, "--output", "json"],
524522
})
525523
.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-
},
524+
Effect.flatMap((result) =>
525+
Effect.fromResult(decodeProjectPathJson(result.stdout.trim())),
526+
),
527+
),
541528
{
542529
capacity: PROJECT_PATH_CACHE_CAPACITY,
543530
timeToLive: (exit) => (Exit.isSuccess(exit) ? PROJECT_PATH_CACHE_TTL : Duration.zero),
@@ -1093,9 +1080,10 @@ export const make = Effect.gen(function* () {
10931080
);
10941081

10951082
return GitLabPullRequestCli.of({
1096-
resolveProjectPath: (input) =>
1097-
Cache.get(projectPaths, [input.cwd, input.host, input.repository].join("\u0000")).pipe(
1098-
Effect.orElseSucceed(() => input.repository),
1083+
// Destructured so the key holds only what names a project: callers pass their whole input.
1084+
resolveProjectPath: ({ cwd, host, repository }) =>
1085+
Cache.get(projectPaths, { cwd, host, repository }).pipe(
1086+
Effect.orElseSucceed(() => repository),
10991087
),
11001088

11011089
getViewerUsername: viewerUsername,

0 commit comments

Comments
 (0)