Skip to content

Commit 4146e51

Browse files
only21milclaude
andauthored
fix(gitlab): a failed child pipeline no longer shows as a passing check (pingdotgg#24688)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent b010581 commit 4146e51

5 files changed

Lines changed: 228 additions & 2 deletions

File tree

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,122 @@ layer("GitLabPullRequestCli.layer", (it) => {
963963
}),
964964
);
965965

966+
it.effect("shows a failed child pipeline that left the head pipeline green", () =>
967+
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",
994+
},
995+
},
996+
{ name: "docs", status: "created", downstream_pipeline: null },
997+
]),
998+
),
999+
),
1000+
);
1001+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1002+
1003+
const detail = yield* cli.getMergeRequestDetail({
1004+
cwd: "/w",
1005+
repository: "acme/web",
1006+
number: 7,
1007+
});
1008+
1009+
expect(detail.checks.map((check) => [check.name, check.status, check.url])).toEqual([
1010+
["Pipeline", "success", "https://gitlab.com/bilal/web/-/pipelines/31"],
1011+
["deploy", "failure", "https://gitlab.com/bilal/web/-/pipelines/32"],
1012+
]);
1013+
expect(argsOfCall(1)[1]).toBe("projects/12/pipelines/31/bridges?per_page=100");
1014+
}),
1015+
);
1016+
1017+
it.effect("keeps the one pipeline check when the trigger jobs cannot be read whole", () =>
1018+
Effect.gen(function* () {
1019+
const detailOutput = output(
1020+
mergeRequestJson({ head_pipeline: { id: 31, project_id: 12, status: "success" } }),
1021+
);
1022+
const failedChild = { name: "deploy", downstream_pipeline: { status: "failed" } };
1023+
mockedExecute
1024+
.mockReturnValueOnce(Effect.succeed(detailOutput))
1025+
.mockReturnValueOnce(
1026+
Effect.fail(
1027+
new GitLabCli.GitLabCliCommandError({
1028+
operation: "execute",
1029+
command: "glab",
1030+
cwd: "/w",
1031+
cause: new Error("403 Forbidden"),
1032+
}),
1033+
),
1034+
)
1035+
.mockReturnValueOnce(Effect.succeed(detailOutput))
1036+
// A full page may have left out the trigger job whose pipeline failed.
1037+
.mockReturnValueOnce(
1038+
Effect.succeed(output(JSON.stringify(Array.from({ length: 100 }, () => failedChild)))),
1039+
);
1040+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1041+
1042+
for (const scenario of ["read fails", "page is full"]) {
1043+
const detail = yield* cli.getMergeRequestDetail({
1044+
cwd: "/w",
1045+
repository: "acme/web",
1046+
number: 7,
1047+
});
1048+
expect(
1049+
detail.checks.map((check) => [check.name, check.status]),
1050+
scenario,
1051+
).toEqual([["Pipeline", "success"]]);
1052+
}
1053+
}),
1054+
);
1055+
1056+
it.effect("reads merge permissions without reading the trigger jobs", () =>
1057+
Effect.gen(function* () {
1058+
mockedExecute.mockReturnValueOnce(
1059+
Effect.succeed(
1060+
output(
1061+
mergeRequestJson({
1062+
head_pipeline: { id: 31, project_id: 12, status: "success" },
1063+
user: { can_merge: false },
1064+
}),
1065+
),
1066+
),
1067+
);
1068+
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
1069+
1070+
const detail = yield* cli.getMergeRequestDetail({
1071+
cwd: "/w",
1072+
repository: "acme/web",
1073+
number: 7,
1074+
downstreamChecks: false,
1075+
});
1076+
1077+
expect(detail.viewerCanMerge).toBe(false);
1078+
expect(mockedExecute).toHaveBeenCalledTimes(1);
1079+
}),
1080+
);
1081+
9661082
it.effect("reads who has access to the project and who is already on the merge request", () =>
9671083
Effect.gen(function* () {
9681084
mockedExecute

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { resolvePullRequestAuthorFilter, VcsProcessExitError } from "@t3tools/co
88
import type {
99
PullRequestAction,
1010
PullRequestActor,
11+
PullRequestCheck,
1112
PullRequestComment,
1213
PullRequestCommit,
1314
PullRequestInvolvement,
@@ -45,6 +46,7 @@ import {
4546
decodeProjectLabelsJson,
4647
decodeProjectUsersJson,
4748
decodeRepositoryBlobsJson,
49+
decodeTriggerJobChecksJson,
4850
decodeViewerJson,
4951
gitLabAwardName,
5052
MERGE_REQUEST_FILE_STATS_GRAPHQL_QUERY,
@@ -245,6 +247,8 @@ export class GitLabPullRequestCli extends Context.Service<
245247
readonly host?: string | undefined;
246248
readonly repository: string;
247249
readonly number: number;
250+
/** `false` skips the trigger job read; the permission check before every write does. */
251+
readonly downstreamChecks?: boolean;
248252
}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;
249253

250254
/** Line counts for the given merge requests; one with no answer is left out. */
@@ -996,6 +1000,29 @@ export const make = Effect.gen(function* () {
9961000
}),
9971001
);
9981002

1003+
// Best-effort: a trigger job read that fails, or a full page that may have left out the one
1004+
// whose pipeline failed, adds no rows and leaves the detail as it was.
1005+
const triggerJobChecks = (input: {
1006+
readonly cwd: string;
1007+
readonly pipeline: { readonly id: number; readonly projectId: number };
1008+
}): Effect.Effect<ReadonlyArray<PullRequestCheck>> =>
1009+
api({
1010+
cwd: input.cwd,
1011+
// `bridges` rather than `trigger_jobs`: GitLab 19.2 deprecates it but still serves it, and
1012+
// older self-managed installs know no other name.
1013+
path: `projects/${input.pipeline.projectId}/pipelines/${input.pipeline.id}/bridges?${query([
1014+
["per_page", String(MAX_PAGE_SIZE)],
1015+
])}`,
1016+
}).pipe(
1017+
Effect.map((result) => {
1018+
const decoded = decodeTriggerJobChecksJson(result.stdout.trim());
1019+
return Result.isSuccess(decoded) && decoded.success.rawCount < MAX_PAGE_SIZE
1020+
? decoded.success.checks
1021+
: [];
1022+
}),
1023+
Effect.orElseSucceed((): ReadonlyArray<PullRequestCheck> => []),
1024+
);
1025+
9991026
const projectUsers = (input: {
10001027
readonly cwd: string;
10011028
readonly host?: string | undefined;
@@ -1297,7 +1324,19 @@ export const make = Effect.gen(function* () {
12971324
return listPage({ ...input, page, collected: [], cursorAdvance: 0 });
12981325
}),
12991326

1300-
getMergeRequestDetail: mergeRequestDetail,
1327+
getMergeRequestDetail: Effect.fn("GitLabPullRequestCli.getMergeRequestDetail")(
1328+
function* (input) {
1329+
const detail = yield* mergeRequestDetail(input);
1330+
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
1336+
? detail
1337+
: { ...detail, checks: [...detail.checks, ...downstream] };
1338+
},
1339+
),
13011340

13021341
listMergeRequestStats,
13031342

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export const make = Effect.gen(function* () {
317317
// through on, and access granted or withdrawn within the minute must count.
318318
getViewerPermissions: (input) =>
319319
cli
320-
.getMergeRequestDetail(input)
320+
.getMergeRequestDetail({ ...input, downstreamChecks: false })
321321
.pipe(Effect.mapError(fail("getViewerPermissions")), Effect.map(gitLabViewerPermissions)),
322322

323323
forgetChangeRequestReads: (scope) =>

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
decodeMergeRequestStatsJson,
1313
decodeNotesJson,
1414
decodeOwnAwardIdJson,
15+
decodeTriggerJobChecksJson,
1516
} from "./gitLabMergeRequestJson.ts";
1617

1718
function listJson(entries: ReadonlyArray<Record<string, unknown>>): string {
@@ -454,6 +455,26 @@ describe("decodeMergeRequestDiffsJson", () => {
454455
});
455456
});
456457

458+
describe("decodeTriggerJobChecksJson", () => {
459+
it("reads a downstream failure GitLab was told to allow as neutral, not failure", () => {
460+
const result = expectSuccess(
461+
decodeTriggerJobChecksJson(
462+
JSON.stringify([
463+
{ name: "e2e", allow_failure: true, downstream_pipeline: { status: "failed" } },
464+
{ name: "deploy", allow_failure: false, downstream_pipeline: { status: "running" } },
465+
{ name: 7 },
466+
]),
467+
),
468+
);
469+
470+
expect(result.checks.map((check) => [check.name, check.status])).toEqual([
471+
["e2e", "neutral"],
472+
["deploy", "pending"],
473+
]);
474+
expect(result.rawCount).toBe(3);
475+
});
476+
});
477+
457478
describe("merge request viewer fields", () => {
458479
it("leaves merging permitted where GitLab answered without the field", () => {
459480
expect(expectSuccess(decodeMergeRequestDetailJson(detailJson({}))).viewerCanMerge).toBe(true);

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ const RawUserSchema = Schema.Struct({
3232
});
3333

3434
const RawPipelineSchema = Schema.Struct({
35+
id: Schema.optional(Schema.NullOr(Schema.Int)),
36+
project_id: Schema.optional(Schema.NullOr(Schema.Int)),
3537
status: Schema.optional(Schema.NullOr(Schema.String)),
3638
web_url: Schema.optional(Schema.NullOr(Schema.String)),
3739
source: Schema.optional(Schema.NullOr(Schema.String)),
3840
});
3941

42+
const RawTriggerJobSchema = Schema.Struct({
43+
name: Schema.String,
44+
allow_failure: Schema.optional(Schema.NullOr(Schema.Boolean)),
45+
downstream_pipeline: Schema.optional(Schema.NullOr(RawPipelineSchema)),
46+
});
47+
4048
const RawMergeRequestSchema = Schema.Struct({
4149
iid: Schema.Int,
4250
title: Schema.String,
@@ -198,6 +206,8 @@ export interface GitLabMergeRequestDetail extends GitLabMergeRequestListItem {
198206
/** Absent means not counted, not up to date. */
199207
readonly divergedCommits?: number;
200208
readonly headSha?: string;
209+
/** On a fork the pipeline often belongs to the source project, not the target. */
210+
readonly headPipeline?: { readonly id: number; readonly projectId: number };
201211
}
202212

203213
function toActor(raw: Schema.Schema.Type<typeof RawUserSchema> | null | undefined) {
@@ -331,12 +341,16 @@ function toDetail(raw: Schema.Schema.Type<typeof RawMergeRequestSchema>): GitLab
331341
: {}),
332342
...(raw.diverged_commits_count == null ? {} : { divergedCommits: raw.diverged_commits_count }),
333343
...(headSha === null ? {} : { headSha }),
344+
...(raw.head_pipeline?.id == null || raw.head_pipeline.project_id == null
345+
? {}
346+
: { headPipeline: { id: raw.head_pipeline.id, projectId: raw.head_pipeline.project_id } }),
334347
};
335348
}
336349

337350
const decodeUnknownList = decodeJsonResult(Schema.Array(Schema.Unknown));
338351
const decodeMergeRequestEntry = Schema.decodeUnknownExit(RawMergeRequestSchema);
339352
const decodeMergeRequest = decodeJsonResult(RawMergeRequestSchema);
353+
const decodeTriggerJobEntry = Schema.decodeUnknownExit(RawTriggerJobSchema);
340354
const decodeNoteEntry = Schema.decodeUnknownExit(RawNoteSchema);
341355
const decodeUserEntry = Schema.decodeUnknownExit(RawUserSchema);
342356
const decodeCommitEntry = Schema.decodeUnknownExit(RawCommitSchema);
@@ -396,6 +410,42 @@ export function decodeMergeRequestDetailJson(
396410
: Result.fail(decoded.failure);
397411
}
398412

413+
export interface GitLabTriggerJobChecks {
414+
readonly checks: ReadonlyArray<PullRequestCheck>;
415+
readonly rawCount: number;
416+
}
417+
418+
/**
419+
* Without `strategy: depend` a trigger job succeeds as soon as it starts its downstream
420+
* pipeline, so the head pipeline stays green when that pipeline fails. Each row carries the
421+
* downstream pipeline's own status instead of the trigger job's.
422+
*/
423+
export function decodeTriggerJobChecksJson(
424+
raw: string,
425+
): Result.Result<GitLabTriggerJobChecks, DecodeFailure> {
426+
const decoded = decodeUnknownList(raw);
427+
if (!Result.isSuccess(decoded)) {
428+
return Result.fail(decoded.failure);
429+
}
430+
const checks: PullRequestCheck[] = [];
431+
for (const entry of decoded.success) {
432+
const job = decodeTriggerJobEntry(entry);
433+
if (Exit.isFailure(job)) continue;
434+
const name = trimmed(job.value.name);
435+
const downstream = job.value.downstream_pipeline;
436+
if (name === null || !downstream) continue;
437+
const status = toPipelineStatus(downstream.status);
438+
checks.push({
439+
name,
440+
// The same reading as `manual`: GitLab was told this failure does not fail the pipeline.
441+
status: status === "failure" && job.value.allow_failure === true ? "neutral" : status,
442+
description: "Downstream pipeline",
443+
url: trimmed(downstream.web_url),
444+
});
445+
}
446+
return Result.succeed({ checks, rawCount: decoded.success.length });
447+
}
448+
399449
export function decodeViewerJson(raw: string): Result.Result<string | null, DecodeFailure> {
400450
const decoded = decodeViewer(raw);
401451
return Result.isSuccess(decoded)

0 commit comments

Comments
 (0)