Skip to content

Commit ac5f6a9

Browse files
committed
feat(server): display individual GitLab pipeline jobs as checks
- Fetch jobs and bridge triggers for the head pipeline when reading merge request details - Map pipeline jobs to individual check statuses in execution order - Fall back to pipeline-level check if jobs cannot be retrieved or exceed page limit
1 parent b2b43be commit ac5f6a9

5 files changed

Lines changed: 322 additions & 14 deletions

File tree

‎apps/server/src/pullRequest/GitLabPullRequestCli.test.ts‎

Lines changed: 100 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/unstable/process";
@@ -882,6 +882,105 @@ layer("GitLabPullRequestCli.layer", (it) => {
882882
}),
883883
);
884884

885+
describe("the head pipeline's jobs", () => {
886+
const detailWithPipeline = mergeRequestJson({
887+
head_pipeline: {
888+
id: 9,
889+
// A merge request from a fork runs its pipeline in the fork, not in acme/web.
890+
project_id: 42,
891+
status: "failed",
892+
web_url: "https://gitlab.com/fork/web/-/pipelines/9",
893+
source: "merge_request_event",
894+
},
895+
});
896+
const pipelineCheck = {
897+
name: "Pipeline",
898+
status: "failure",
899+
description: "merge_request_event",
900+
url: "https://gitlab.com/fork/web/-/pipelines/9",
901+
};
902+
const job = (id: number) => ({ id, name: `job ${id}`, stage: "test", status: "success" });
903+
904+
/** Answers each endpoint by its path, since the two job reads run side by side. */
905+
function answer(pages: { readonly jobs: string | null; readonly bridges: string }) {
906+
mockedExecute.mockImplementation((input) => {
907+
const path = input.args[1] ?? "";
908+
if (path.includes("/jobs?")) {
909+
return pages.jobs === null
910+
? Effect.succeed(output('{"message":"404 Not Found"}'))
911+
: Effect.succeed(output(pages.jobs));
912+
}
913+
if (path.includes("/bridges?")) return Effect.succeed(output(pages.bridges));
914+
return Effect.succeed(output(detailWithPipeline));
915+
});
916+
}
917+
918+
const read = (includeJobs: boolean) =>
919+
GitLabPullRequestCli.GitLabPullRequestCli.pipe(
920+
Effect.flatMap((cli) =>
921+
cli.getMergeRequestDetail({ cwd: "/w", repository: "acme/web", number: 7, includeJobs }),
922+
),
923+
);
924+
925+
it.effect("reads them from the project the pipeline ran in, in place of the pipeline", () =>
926+
Effect.gen(function* () {
927+
answer({
928+
// @effect-diagnostics-next-line preferSchemaOverJson:off
929+
jobs: JSON.stringify([
930+
{ id: 12, name: "lint", stage: "test", status: "failed" },
931+
{ id: 11, name: "build", stage: "build", status: "success" },
932+
]),
933+
bridges: "[]",
934+
});
935+
936+
const detail = yield* read(true);
937+
938+
expect(mockedExecute.mock.calls.map(([input]) => input.args[1]).toSorted()).toEqual([
939+
"projects/42/pipelines/9/bridges?per_page=100",
940+
"projects/42/pipelines/9/jobs?per_page=100",
941+
"projects/acme%2Fweb/merge_requests/7?include_diverged_commits_count=true",
942+
]);
943+
expect(detail.checks.map((check) => [check.name, check.status])).toEqual([
944+
["build", "success"],
945+
["lint", "failure"],
946+
]);
947+
}),
948+
);
949+
950+
it.effect("keeps the pipeline where its jobs cannot stand for all of it", () =>
951+
Effect.gen(function* () {
952+
// A full page may have left out the job that failed, and a pipeline with no jobs yet
953+
// still has a status worth showing.
954+
// @effect-diagnostics-next-line preferSchemaOverJson:off
955+
const full = JSON.stringify(Array.from({ length: 100 }, (_, index) => job(index + 1)));
956+
for (const pages of [
957+
{ jobs: null, bridges: "[]" },
958+
{ jobs: full, bridges: "[]" },
959+
{ jobs: "[]", bridges: full },
960+
{ jobs: "[]", bridges: "[]" },
961+
]) {
962+
mockedExecute.mockReset();
963+
answer(pages);
964+
965+
const detail = yield* read(true);
966+
967+
expect(detail.checks).toEqual([pipelineCheck]);
968+
}
969+
}),
970+
);
971+
972+
it.effect("asks for no jobs when the caller only needs the merge request", () =>
973+
Effect.gen(function* () {
974+
answer({ jobs: "[]", bridges: "[]" });
975+
976+
const detail = yield* read(false);
977+
978+
expect(mockedExecute).toHaveBeenCalledTimes(1);
979+
expect(detail.checks).toEqual([pipelineCheck]);
980+
}),
981+
);
982+
});
983+
885984
it.effect("fails when the authenticated account has no username", () =>
886985
Effect.gen(function* () {
887986
// @effect-diagnostics-next-line preferSchemaOverJson:off

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
decodeMergeRequestListJson,
3434
decodeNotesJson,
3535
decodeOwnAwardIdJson,
36+
decodePipelineJobsJson,
3637
decodeProjectMergeCapabilitiesJson,
3738
decodeProjectUsersJson,
3839
decodeRepositoryBlobsJson,
@@ -240,6 +241,8 @@ export class GitLabPullRequestCli extends Context.Service<
240241
readonly cwd: string;
241242
readonly repository: string;
242243
readonly number: number;
244+
/** Read the head pipeline's jobs as its checks, which only the detail page shows. */
245+
readonly includeJobs?: boolean;
243246
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
244247

245248
readonly listNotes: (input: {
@@ -914,6 +917,61 @@ export const make = Effect.gen(function* () {
914917
}),
915918
);
916919

920+
/**
921+
* One page each of a pipeline's jobs and of its trigger jobs, which GitLab lists apart. The
922+
* pipeline is addressed in its own project: on a merge request from a fork that is the fork,
923+
* and the target project answers 404 for it.
924+
*/
925+
const pipelineJobs = (input: {
926+
readonly cwd: string;
927+
readonly pipeline: { readonly id: number; readonly projectId: number };
928+
}) =>
929+
Effect.forEach(
930+
["jobs", "bridges"],
931+
(kind) =>
932+
api({
933+
cwd: input.cwd,
934+
path: `projects/${input.pipeline.projectId}/pipelines/${input.pipeline.id}/${kind}?${query(
935+
[["per_page", String(MAX_PAGE_SIZE)]],
936+
)}`,
937+
}),
938+
{ concurrency: 2 },
939+
).pipe(
940+
Effect.flatMap((results) => {
941+
const decoded = decodePipelineJobsJson(results.map((result) => result.stdout.trim()));
942+
return Result.isSuccess(decoded)
943+
? Effect.succeed(decoded.success)
944+
: Effect.fail(
945+
new GitLabMergeRequestReadError({
946+
command: "glab",
947+
cwd: input.cwd,
948+
operation: "listPipelineJobs",
949+
cause: decoded.failure,
950+
}),
951+
);
952+
}),
953+
);
954+
955+
/**
956+
* The head pipeline's jobs in place of the pipeline itself, so a failure names the job that
957+
* failed. The pipeline stays the one check wherever its jobs cannot stand for all of it: a
958+
* failed read, a full page that may have left the failed job out, or a pipeline that has not
959+
* created any jobs yet.
960+
*/
961+
const withPipelineJobs =
962+
(cwd: string) =>
963+
(detail: GitLabMergeRequestDetail): Effect.Effect<GitLabMergeRequestDetail> =>
964+
detail.headPipeline === undefined
965+
? Effect.succeed(detail)
966+
: pipelineJobs({ cwd, pipeline: detail.headPipeline }).pipe(
967+
Effect.map(({ checks, rawCounts }) =>
968+
checks.length === 0 || rawCounts.some((count) => count >= MAX_PAGE_SIZE)
969+
? detail
970+
: { ...detail, checks },
971+
),
972+
Effect.orElseSucceed(() => detail),
973+
);
974+
917975
/**
918976
* The merge request itself, which several calls need for different parts of it: the detail for
919977
* everything, and the reviewer paths for the ids GitLab writes a reviewer set with.
@@ -922,6 +980,7 @@ export const make = Effect.gen(function* () {
922980
readonly cwd: string;
923981
readonly repository: string;
924982
readonly number: number;
983+
readonly includeJobs?: boolean;
925984
}): Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError> =>
926985
api({
927986
cwd: input.cwd,
@@ -933,16 +992,19 @@ export const make = Effect.gen(function* () {
933992
}).pipe(
934993
Effect.flatMap((result) => {
935994
const decoded = decodeMergeRequestDetailJson(result.stdout.trim());
936-
return Result.isSuccess(decoded)
937-
? Effect.succeed(decoded.success)
938-
: Effect.fail(
939-
new GitLabMergeRequestReadError({
940-
command: "glab",
941-
cwd: input.cwd,
942-
operation: "getMergeRequestDetail",
943-
cause: decoded.failure,
944-
}),
945-
);
995+
if (!Result.isSuccess(decoded)) {
996+
return Effect.fail(
997+
new GitLabMergeRequestReadError({
998+
command: "glab",
999+
cwd: input.cwd,
1000+
operation: "getMergeRequestDetail",
1001+
cause: decoded.failure,
1002+
}),
1003+
);
1004+
}
1005+
return input.includeJobs === true
1006+
? withPipelineJobs(input.cwd)(decoded.success)
1007+
: Effect.succeed(decoded.success);
9461008
}),
9471009
);
9481010

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export const make = Effect.gen(function* () {
146146
getChangeRequest: (input) =>
147147
Effect.all(
148148
[
149-
cli.getMergeRequestDetail(input),
149+
cli.getMergeRequestDetail({ ...input, includeJobs: true }),
150150
cli.getProjectMergeCapabilities({ cwd: input.cwd, repository: input.repository }),
151151
],
152152
{ concurrency: 2 },

‎apps/server/src/pullRequest/gitLabMergeRequestJson.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
decodeMergeRequestListJson,
1111
decodeNotesJson,
1212
decodeOwnAwardIdJson,
13+
decodePipelineJobsJson,
1314
decodeViewerJson,
1415
gitLabAwardName,
1516
} from "./gitLabMergeRequestJson.ts";
@@ -237,6 +238,88 @@ describe("decodeMergeRequestDetailJson", () => {
237238
});
238239
});
239240

241+
describe("decodePipelineJobsJson", () => {
242+
it("reads each job as a check, in the order the pipeline runs them", () => {
243+
const { checks, rawCounts } = expectSuccess(
244+
decodePipelineJobsJson([
245+
JSON.stringify([
246+
{
247+
id: 13,
248+
name: "deploy",
249+
stage: "deploy",
250+
status: "created",
251+
web_url: "https://gitlab.com/acme/web/-/jobs/13",
252+
},
253+
{
254+
id: 12,
255+
name: "lint",
256+
stage: "test",
257+
status: "failed",
258+
allow_failure: true,
259+
web_url: "https://gitlab.com/acme/web/-/jobs/12",
260+
},
261+
{ id: 11, name: "build", stage: "build", status: "running" },
262+
{ id: 10, stage: "build", status: "success" },
263+
]),
264+
JSON.stringify([]),
265+
]),
266+
);
267+
268+
expect(rawCounts).toEqual([4, 0]);
269+
expect(checks).toEqual([
270+
{ name: "build", status: "pending", description: "build", url: null },
271+
// Allowed to fail, so the pipeline passes over it and so does the check.
272+
{
273+
name: "lint",
274+
status: "neutral",
275+
description: "test",
276+
url: "https://gitlab.com/acme/web/-/jobs/12",
277+
},
278+
{
279+
name: "deploy",
280+
status: "pending",
281+
description: "deploy",
282+
url: "https://gitlab.com/acme/web/-/jobs/13",
283+
},
284+
]);
285+
});
286+
287+
it("reads a trigger job as the pipeline it started", () => {
288+
const { checks } = expectSuccess(
289+
decodePipelineJobsJson([
290+
JSON.stringify([{ id: 20, name: "unit", stage: "test", status: "success" }]),
291+
JSON.stringify([
292+
{
293+
id: 21,
294+
name: "trigger:docs",
295+
stage: "test",
296+
// The trigger itself succeeds as soon as the child pipeline exists.
297+
status: "success",
298+
web_url: "https://gitlab.com/acme/web/-/jobs/21",
299+
downstream_pipeline: {
300+
id: 30,
301+
status: "failed",
302+
web_url: "https://gitlab.com/acme/web/-/pipelines/30",
303+
},
304+
},
305+
{ id: 22, name: "trigger:e2e", stage: "test", status: "created" },
306+
]),
307+
]),
308+
);
309+
310+
expect(checks).toEqual([
311+
{ name: "unit", status: "success", description: "test", url: null },
312+
{
313+
name: "trigger:docs",
314+
status: "failure",
315+
description: "test",
316+
url: "https://gitlab.com/acme/web/-/pipelines/30",
317+
},
318+
{ name: "trigger:e2e", status: "pending", description: "test", url: null },
319+
]);
320+
});
321+
});
322+
240323
describe("decodeViewerJson", () => {
241324
it("reads the signed-in username", () => {
242325
expect(expectSuccess(decodeViewerJson(JSON.stringify({ username: "bilal" })))).toBe("bilal");

0 commit comments

Comments
 (0)