Skip to content

Commit 7df82c5

Browse files
TonybynMp4claude
andcommitted
fix(gitlab): the checks panel shows which stage and job failed
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1d626d0 commit 7df82c5

4 files changed

Lines changed: 462 additions & 50 deletions

File tree

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

Lines changed: 172 additions & 43 deletions
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";
@@ -965,39 +965,42 @@ layer("GitLabPullRequestCli.layer", (it) => {
965965

966966
it.effect("shows a failed child pipeline that left the head pipeline green", () =>
967967
Effect.gen(function* () {
968-
mockedExecute
969-
.mockReturnValueOnce(
970-
Effect.succeed(
971-
output(
972-
mergeRequestJson({
973-
// A fork's pipeline runs in the source project, so its id is not the target's.
974-
head_pipeline: {
975-
id: 31,
976-
project_id: 12,
977-
status: "success",
978-
web_url: "https://gitlab.com/bilal/web/-/pipelines/31",
979-
},
980-
}),
981-
),
982-
),
983-
)
984-
.mockReturnValueOnce(
985-
Effect.succeed(
986-
output(
987-
JSON.stringify([
988-
{
989-
name: "deploy",
990-
status: "success",
991-
downstream_pipeline: {
992-
status: "failed",
993-
web_url: "https://gitlab.com/bilal/web/-/pipelines/32",
968+
mockedExecute.mockImplementation((request) => {
969+
switch (request.args[1]) {
970+
case "graphql":
971+
return Effect.succeed(output("{}"));
972+
case "projects/12/pipelines/31/bridges?per_page=100":
973+
return Effect.succeed(
974+
output(
975+
JSON.stringify([
976+
{
977+
name: "deploy",
978+
status: "success",
979+
downstream_pipeline: {
980+
status: "failed",
981+
web_url: "https://gitlab.com/bilal/web/-/pipelines/32",
982+
},
994983
},
995-
},
996-
{ name: "docs", status: "created", downstream_pipeline: null },
997-
]),
998-
),
999-
),
1000-
);
984+
{ name: "docs", status: "created", downstream_pipeline: null },
985+
]),
986+
),
987+
);
988+
default:
989+
return Effect.succeed(
990+
output(
991+
mergeRequestJson({
992+
// A fork's pipeline runs in the source project, so its id is not the target's.
993+
head_pipeline: {
994+
id: 31,
995+
project_id: 12,
996+
status: "success",
997+
web_url: "https://gitlab.com/bilal/web/-/pipelines/31",
998+
},
999+
}),
1000+
),
1001+
);
1002+
}
1003+
});
10011004
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
10021005

10031006
const detail = yield* cli.getMergeRequestDetail({
@@ -1010,7 +1013,6 @@ layer("GitLabPullRequestCli.layer", (it) => {
10101013
["Pipeline", "success", "https://gitlab.com/bilal/web/-/pipelines/31"],
10111014
["deploy", "failure", "https://gitlab.com/bilal/web/-/pipelines/32"],
10121015
]);
1013-
expect(argsOfCall(1)[1]).toBe("projects/12/pipelines/31/bridges?per_page=100");
10141016
}),
10151017
);
10161018

@@ -1020,9 +1022,11 @@ layer("GitLabPullRequestCli.layer", (it) => {
10201022
mergeRequestJson({ head_pipeline: { id: 31, project_id: 12, status: "success" } }),
10211023
);
10221024
const failedChild = { name: "deploy", downstream_pipeline: { status: "failed" } };
1023-
mockedExecute
1024-
.mockReturnValueOnce(Effect.succeed(detailOutput))
1025-
.mockReturnValueOnce(
1025+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1026+
1027+
for (const [scenario, bridges] of [
1028+
[
1029+
"read fails",
10261030
Effect.fail(
10271031
new GitLabCli.GitLabCliCommandError({
10281032
operation: "execute",
@@ -1031,15 +1035,20 @@ layer("GitLabPullRequestCli.layer", (it) => {
10311035
cause: new Error("403 Forbidden"),
10321036
}),
10331037
),
1034-
)
1035-
.mockReturnValueOnce(Effect.succeed(detailOutput))
1038+
],
10361039
// A full page may have left out the trigger job whose pipeline failed.
1037-
.mockReturnValueOnce(
1040+
[
1041+
"page is full",
10381042
Effect.succeed(output(JSON.stringify(Array.from({ length: 100 }, () => failedChild)))),
1043+
],
1044+
] as const) {
1045+
mockedExecute.mockImplementation((request) =>
1046+
request.args[1] === "graphql"
1047+
? Effect.succeed(output("{}"))
1048+
: request.args[1]?.includes("/bridges")
1049+
? bridges
1050+
: Effect.succeed(detailOutput),
10391051
);
1040-
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1041-
1042-
for (const scenario of ["read fails", "page is full"]) {
10431052
const detail = yield* cli.getMergeRequestDetail({
10441053
cwd: "/w",
10451054
repository: "acme/web",
@@ -1388,4 +1397,124 @@ layer("GitLabPullRequestCli.layer", (it) => {
13881397
expect(result).toMatchObject({ _tag: "Failure", failure: error });
13891398
}),
13901399
);
1400+
describe("the head pipeline's stages", () => {
1401+
const pipeline = {
1402+
id: 9,
1403+
project_id: 12,
1404+
status: "failed",
1405+
web_url: "https://gitlab.com/acme/web/-/pipelines/9",
1406+
source: "merge_request_event",
1407+
};
1408+
const stages = JSON.stringify({
1409+
data: {
1410+
project: {
1411+
mergeRequest: {
1412+
headPipeline: {
1413+
path: "/acme/web/-/pipelines/9",
1414+
stages: {
1415+
nodes: [
1416+
{ name: "build", status: "success", detailedStatus: { label: "passed" } },
1417+
{
1418+
name: "test",
1419+
status: "failed",
1420+
jobs: {
1421+
nodes: [{ name: "unit", status: "FAILED", webPath: "/acme/web/-/jobs/3" }],
1422+
},
1423+
},
1424+
],
1425+
},
1426+
},
1427+
},
1428+
},
1429+
},
1430+
});
1431+
1432+
function answer(graphql: Effect.Effect<ReturnType<typeof output>, GitLabCli.GitLabCliError>) {
1433+
mockedExecute.mockImplementation((request) =>
1434+
request.args[1] === "graphql"
1435+
? graphql
1436+
: request.args[1]?.includes("/bridges")
1437+
? Effect.succeed(output("[]"))
1438+
: Effect.succeed(output(mergeRequestJson({ head_pipeline: pipeline }))),
1439+
);
1440+
}
1441+
1442+
const read = (downstreamChecks: boolean) =>
1443+
GitLabPullRequestCli.GitLabPullRequestCli.pipe(
1444+
Effect.flatMap((cli) =>
1445+
cli.getMergeRequestDetail({
1446+
cwd: "/w",
1447+
repository: "group/acme/web",
1448+
number: 7,
1449+
downstreamChecks,
1450+
}),
1451+
),
1452+
);
1453+
1454+
it.effect("shows the stages, and the failed stage as its failed job", () =>
1455+
Effect.gen(function* () {
1456+
answer(Effect.succeed(output(stages)));
1457+
1458+
const detail = yield* read(true);
1459+
1460+
const graphql = mockedExecute.mock.calls.find(([call]) => call.args[1] === "graphql");
1461+
assert.isDefined(graphql);
1462+
expect(JSON.parse(graphql[0].stdin ?? "").variables).toEqual({
1463+
fullPath: "group/acme/web",
1464+
iid: "7",
1465+
});
1466+
expect(detail.checks).toEqual([
1467+
{
1468+
name: "build",
1469+
status: "success",
1470+
description: "passed",
1471+
url: "https://gitlab.com/acme/web/-/pipelines/9",
1472+
},
1473+
{
1474+
name: "test / unit",
1475+
status: "failure",
1476+
description: null,
1477+
url: "https://gitlab.com/acme/web/-/jobs/3",
1478+
},
1479+
]);
1480+
}),
1481+
);
1482+
1483+
it.effect("keeps the pipeline check where the stages cannot be read", () =>
1484+
Effect.gen(function* () {
1485+
answer(
1486+
Effect.fail(
1487+
new GitLabCli.GitLabCliCommandError({
1488+
operation: "execute",
1489+
command: "glab",
1490+
cwd: "/w",
1491+
cause: new Error("graphql unavailable"),
1492+
}),
1493+
),
1494+
);
1495+
1496+
const detail = yield* read(true);
1497+
1498+
expect(detail.checks).toEqual([
1499+
{
1500+
name: "Pipeline",
1501+
status: "failure",
1502+
description: "merge_request_event",
1503+
url: "https://gitlab.com/acme/web/-/pipelines/9",
1504+
},
1505+
]);
1506+
}),
1507+
);
1508+
1509+
it.effect("asks for no stages when the caller only needs the merge request", () =>
1510+
Effect.gen(function* () {
1511+
answer(Effect.succeed(output(stages)));
1512+
1513+
const detail = yield* read(false);
1514+
1515+
expect(mockedExecute).toHaveBeenCalledTimes(1);
1516+
expect(detail.checks.map((check) => check.name)).toEqual(["Pipeline"]);
1517+
}),
1518+
);
1519+
});
13911520
});

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type {
2929
import * as GitLabCli from "./GitLabCli.ts";
3030
import {
3131
AWARD_EMOJI_GRAPHQL_QUERY,
32+
PIPELINE_STAGES_GRAPHQL_QUERY,
3233
decodeAwardEmojiJson,
3334
decodeCommitDiffRefsJson,
3435
decodeCommitsJson,
@@ -42,6 +43,7 @@ import {
4243
decodeMergeRequestStatsJson,
4344
decodeNotesJson,
4445
decodeOwnAwardIdJson,
46+
decodePipelineStageChecksJson,
4547
decodeProjectMergeCapabilitiesJson,
4648
decodeProjectLabelsJson,
4749
decodeProjectUsersJson,
@@ -247,7 +249,7 @@ export class GitLabPullRequestCli extends Context.Service<
247249
readonly host?: string | undefined;
248250
readonly repository: string;
249251
readonly number: number;
250-
/** `false` skips the trigger job read; the permission check before every write does. */
252+
/** `false` skips the stage and trigger job reads, as write permission checks do. */
251253
readonly downstreamChecks?: boolean;
252254
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
253255

@@ -1023,6 +1025,25 @@ export const make = Effect.gen(function* () {
10231025
Effect.orElseSucceed((): ReadonlyArray<PullRequestCheck> => []),
10241026
);
10251027

1028+
// Best-effort: stages that can't be read keep the merge request's single pipeline check.
1029+
const pipelineStageChecks = (input: {
1030+
readonly cwd: string;
1031+
readonly host?: string | undefined;
1032+
readonly repository: string;
1033+
readonly number: number;
1034+
readonly url: string;
1035+
}): Effect.Effect<ReadonlyArray<PullRequestCheck> | null> =>
1036+
URL.canParse(input.url)
1037+
? graphqlRead({
1038+
cwd: input.cwd,
1039+
host: input.host,
1040+
operation: "getPipelineStageChecks",
1041+
query: PIPELINE_STAGES_GRAPHQL_QUERY,
1042+
variables: { fullPath: input.repository, iid: String(input.number) },
1043+
decode: (raw) => decodePipelineStageChecksJson(raw, new URL(input.url).origin),
1044+
}).pipe(Effect.orElseSucceed(() => null))
1045+
: Effect.succeed(null);
1046+
10261047
const projectUsers = (input: {
10271048
readonly cwd: string;
10281049
readonly host?: string | undefined;
@@ -1328,13 +1349,16 @@ export const make = Effect.gen(function* () {
13281349
function* (input) {
13291350
const detail = yield* mergeRequestDetail(input);
13301351
if (input.downstreamChecks === false || detail.headPipeline === undefined) return detail;
1331-
const downstream = yield* triggerJobChecks({
1332-
cwd: input.cwd,
1333-
pipeline: detail.headPipeline,
1334-
});
1335-
return downstream.length === 0
1352+
const [stages, downstream] = yield* Effect.all(
1353+
[
1354+
pipelineStageChecks({ ...input, url: detail.url }),
1355+
triggerJobChecks({ cwd: input.cwd, pipeline: detail.headPipeline }),
1356+
],
1357+
{ concurrency: 2 },
1358+
);
1359+
return stages === null && downstream.length === 0
13361360
? detail
1337-
: { ...detail, checks: [...detail.checks, ...downstream] };
1361+
: { ...detail, checks: [...(stages ?? detail.checks), ...downstream] };
13381362
},
13391363
),
13401364

0 commit comments

Comments
 (0)