TypeScript
fix(gitlab): the checks panel lists each pipeline job (#28728)
Antonybda0e7f41124
diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.tsindex 39c57fba8b7..8c55e786c7e 100644--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts@@ -1,4 +1,4 @@-import { afterEach, assert, expect, it, vi } from "@effect/vitest";+import { afterEach, assert, describe, expect, it, vi } from "@effect/vitest";import { VcsProcessExitError } from "@t3tools/contracts";import * as Effect from "effect/Effect";import * as Layer from "effect/Layer";@@ -1494,40 +1494,42 @@ layer("GitLabPullRequestCli.layer", (it) => {it.effect("shows a failed child pipeline that left the head pipeline green", () =>Effect.gen(function* () {- mockedExecute- .mockReturnValueOnce(- Effect.succeed(- output(- mergeRequestJson({- // A fork's pipeline runs in the source project, so its id is not the target's.- head_pipeline: {- id: 31,- project_id: 12,- status: "success",- web_url: "https://gitlab.com/bilal/web/-/pipelines/31",- },- }),- ),- ),- )- .mockReturnValueOnce(Effect.succeed(labelPermission))- .mockReturnValueOnce(- Effect.succeed(- output(- JSON.stringify([- {- name: "deploy",- status: "success",- downstream_pipeline: {- status: "failed",- web_url: "https://gitlab.com/bilal/web/-/pipelines/32",+ mockedExecute.mockImplementation((request) => {+ switch (request.args[1]) {+ case "graphql":+ return Effect.succeed(output("{}"));+ case "projects/12/pipelines/31/bridges?per_page=100":+ return Effect.succeed(+ output(+ JSON.stringify([+ {+ name: "deploy",+ status: "success",+ downstream_pipeline: {+ status: "failed",+ web_url: "https://gitlab.com/bilal/web/-/pipelines/32",+ },},- },- { name: "docs", status: "created", downstream_pipeline: null },- ]),- ),- ),- );+ { name: "docs", status: "created", downstream_pipeline: null },+ ]),+ ),+ );+ default:+ return Effect.succeed(+ output(+ mergeRequestJson({+ // A fork's pipeline runs in the source project, so its id is not the target's.+ head_pipeline: {+ id: 31,+ project_id: 12,+ status: "success",+ web_url: "https://gitlab.com/bilal/web/-/pipelines/31",+ },+ }),+ ),+ );+ }+ });const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;const detail = yield* cli.getMergeRequestDetail({@@ -1540,7 +1542,6 @@ layer("GitLabPullRequestCli.layer", (it) => {["Pipeline", "success", "https://gitlab.com/bilal/web/-/pipelines/31"],["deploy", "failure", "https://gitlab.com/bilal/web/-/pipelines/32"],]);- expect(argsOfCall(2)[1]).toBe("projects/12/pipelines/31/bridges?per_page=100");}),);@@ -1550,10 +1551,11 @@ layer("GitLabPullRequestCli.layer", (it) => {mergeRequestJson({ head_pipeline: { id: 31, project_id: 12, status: "success" } }),);const failedChild = { name: "deploy", downstream_pipeline: { status: "failed" } };- mockedExecute- .mockReturnValueOnce(Effect.succeed(detailOutput))- .mockReturnValueOnce(Effect.succeed(labelPermission))- .mockReturnValueOnce(+ const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;++ for (const [scenario, bridges] of [+ [+ "read fails",Effect.fail(new GitLabCli.GitLabCliCommandError({operation: "execute",@@ -1562,16 +1564,20 @@ layer("GitLabPullRequestCli.layer", (it) => {cause: new Error("403 Forbidden"),}),),- )- .mockReturnValueOnce(Effect.succeed(detailOutput))- .mockReturnValueOnce(Effect.succeed(labelPermission))+ ],// A full page may have left out the trigger job whose pipeline failed.- .mockReturnValueOnce(+ [+ "page is full",Effect.succeed(output(JSON.stringify(Array.from({ length: 100 }, () => failedChild)))),+ ],+ ] as const) {+ mockedExecute.mockImplementation((request) =>+ request.args[1] === "graphql"+ ? Effect.succeed(output("{}"))+ : request.args[1]?.includes("/bridges")+ ? bridges+ : Effect.succeed(detailOutput),);- const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;-- for (const scenario of ["read fails", "page is full"]) {const detail = yield* cli.getMergeRequestDetail({cwd: "/w",repository: "acme/web",@@ -2147,4 +2153,171 @@ layer("GitLabPullRequestCli.layer", (it) => {expect(result).toMatchObject({ _tag: "Failure", failure: error });}),);+ describe("the head pipeline's stages", () => {+ const pipeline = {+ id: 9,+ project_id: 12,+ status: "failed",+ web_url: "https://gitlab.com/acme/web/-/pipelines/9",+ source: "merge_request_event",+ };+ const stages = JSON.stringify({+ data: {+ project: {+ mergeRequest: {+ headPipeline: {+ path: "/acme/web/-/pipelines/9",+ stages: {+ nodes: [+ { name: "build", status: "success", detailedStatus: { label: "passed" } },+ {+ name: "test",+ status: "failed",+ jobs: {+ nodes: [{ name: "unit", status: "FAILED", webPath: "/acme/web/-/jobs/3" }],+ },+ },+ {+ name: "deploy",+ status: "success",+ jobs: {+ nodes: [+ // A trigger job without `strategy: depend` succeeds whatever its child does.+ {+ name: "child-pipeline",+ status: "SUCCESS",+ webPath: null,+ downstreamPipeline: {+ status: "FAILED",+ path: "/acme/child/-/pipelines/10",+ },+ },+ ],+ },+ },+ ],+ },+ },+ },+ },+ },+ });++ function answer(graphql: Effect.Effect<ReturnType<typeof output>, GitLabCli.GitLabCliError>) {+ mockedExecute.mockImplementation((request) =>+ request.args[1] === "graphql"+ ? request.stdin?.includes("adminMergeRequest")+ ? Effect.succeed(labelPermission)+ : graphql+ : request.args[1]?.includes("/bridges")+ ? Effect.succeed(+ output(+ JSON.stringify([+ {+ name: "child-pipeline",+ status: "success",+ downstream_pipeline: {+ status: "failed",+ web_url: "https://gitlab.com/acme/child/-/pipelines/10",+ },+ },+ ]),+ ),+ )+ : Effect.succeed(output(mergeRequestJson({ head_pipeline: pipeline }))),+ );+ }++ const read = (downstreamChecks: boolean) =>+ GitLabPullRequestCli.GitLabPullRequestCli.pipe(+ Effect.flatMap((cli) =>+ cli.getMergeRequestDetail({+ cwd: "/w",+ repository: "group/acme/web",+ number: 7,+ downstreamChecks,+ }),+ ),+ );++ it.effect("shows the stage jobs, and a stage GitLab listed no jobs for", () =>+ Effect.gen(function* () {+ answer(Effect.succeed(output(stages)));++ const detail = yield* read(true);++ const graphql = mockedExecute.mock.calls.find(([call]) => call.stdin?.includes("stages"));+ assert.isDefined(graphql);+ expect(JSON.parse(graphql[0].stdin ?? "").variables).toEqual({+ fullPath: "group/acme/web",+ iid: "7",+ });+ expect(detail.checks).toEqual([+ {+ name: "build",+ status: "success",+ description: "passed",+ url: "https://gitlab.com/acme/web/-/pipelines/9",+ },+ {+ name: "test / unit",+ status: "failure",+ description: null,+ url: "https://gitlab.com/acme/web/-/jobs/3",+ },+ {+ name: "deploy / child-pipeline",+ status: "failure",+ description: "Downstream pipeline",+ url: "https://gitlab.com/acme/child/-/pipelines/10",+ },+ ]);+ }),+ );++ it.effect("keeps the pipeline check where the stages cannot be read", () =>+ Effect.gen(function* () {+ answer(+ Effect.fail(+ new GitLabCli.GitLabCliCommandError({+ operation: "execute",+ command: "glab",+ cwd: "/w",+ cause: new Error("graphql unavailable"),+ }),+ ),+ );++ const detail = yield* read(true);++ expect(detail.checks).toEqual([+ {+ name: "Pipeline",+ status: "failure",+ description: "merge_request_event",+ url: "https://gitlab.com/acme/web/-/pipelines/9",+ },+ {+ name: "child-pipeline",+ status: "failure",+ description: "Downstream pipeline",+ url: "https://gitlab.com/acme/child/-/pipelines/10",+ },+ ]);+ }),+ );++ it.effect("asks for no stages when the caller only needs the merge request", () =>+ Effect.gen(function* () {+ answer(Effect.succeed(output(stages)));++ const detail = yield* read(false);++ expect(+ mockedExecute.mock.calls.filter(([call]) => call.stdin?.includes("stages")),+ ).toHaveLength(0);+ expect(detail.checks.map((check) => check.name)).toEqual(["Pipeline"]);+ }),+ );+ });});diff --git a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.tsindex 44390544f8d..f3a0c2cd391 100644--- a/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts+++ b/packages/source-control-gitlab/src/server/GitLabPullRequestCli.ts@@ -33,6 +33,7 @@ import type {import * as GitLabCli from "./GitLabCli.ts";import {AWARD_EMOJI_GRAPHQL_QUERY,+ PIPELINE_STAGES_GRAPHQL_QUERY,decodeAwardEmojiJson,decodeBranchJson,decodeCommitDiffRefsJson,@@ -52,6 +53,7 @@ import {decodeMergeRequestStatsJson,decodeNotesJson,decodeOwnAwardIdJson,+ decodePipelineStageChecksJson,decodeProjectMergeCapabilitiesJson,decodeProjectLabelsJson,decodeProjectUsersJson,@@ -343,7 +345,7 @@ export class GitLabPullRequestCli extends Context.Service<readonly host?: string | undefined;readonly repository: string;readonly number: number;- /** `false` skips the trigger job read; the permission check before every write does. */+ /** `false` skips the stage and trigger job reads, as write permission checks do. */readonly downstreamChecks?: boolean;}) => Effect.Effect<GitLabMergeRequestDetail, GitLabPullRequestCliError>;@@ -1255,6 +1257,25 @@ export const make = Effect.gen(function* () {Effect.orElseSucceed((): ReadonlyArray<PullRequestCheck> => []),);+ // Best-effort: stages that can't be read keep the merge request's single pipeline check.+ const pipelineStageChecks = (input: {+ readonly cwd: string;+ readonly host?: string | undefined;+ readonly repository: string;+ readonly number: number;+ readonly url: string;+ }): Effect.Effect<ReadonlyArray<PullRequestCheck> | null> =>+ URL.canParse(input.url)+ ? graphqlRead({+ cwd: input.cwd,+ host: input.host,+ operation: "getPipelineStageChecks",+ query: PIPELINE_STAGES_GRAPHQL_QUERY,+ variables: { fullPath: input.repository, iid: String(input.number) },+ decode: (raw) => decodePipelineStageChecksJson(raw, new URL(input.url).origin),+ }).pipe(Effect.orElseSucceed(() => null))+ : Effect.succeed(null);+const projectUsers = (input: {readonly cwd: string;readonly host?: string | undefined;@@ -1848,6 +1869,9 @@ export const make = Effect.gen(function* () {),};if (input.downstreamChecks === false || detail.headPipeline === undefined) return detail;+ // The stages already carry each trigger job's downstream pipeline.+ const stages = yield* pipelineStageChecks({ ...input, url: detail.url });+ if (stages !== null) return { ...detail, checks: stages };const downstream = yield* triggerJobChecks({cwd: input.cwd,pipeline: detail.headPipeline,diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.tsindex b231a25b3d3..f755f185adc 100644--- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts+++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.test.ts@@ -17,6 +17,7 @@ import {decodeOwnAwardIdJson,decodeListStatesJson,decodeTriggerJobChecksJson,+ decodePipelineStageChecksJson,} from "./gitLabMergeRequestJson.ts";function listJson(entries: ReadonlyArray<Record<string, unknown>>): string {@@ -255,6 +256,191 @@ describe("decodeMergeRequestDetailJson", () => {});});+describe("decodePipelineStageChecksJson", () => {+ function stagesJson(+ stages: ReadonlyArray<Record<string, unknown> | null> | null,+ hasNextPage = false,+ ): string {+ return JSON.stringify({+ data: {+ project: {+ mergeRequest: {+ headPipeline:+ stages === null+ ? null+ : {+ path: "/fork/web/-/pipelines/9",+ stages: { nodes: stages, pageInfo: { hasNextPage } },+ },+ },+ },+ },+ });+ }++ it("shows every job under its stage", () => {+ const checks = expectSuccess(+ decodePipelineStageChecksJson(+ stagesJson([+ {+ name: "build",+ status: "success",+ jobs: {+ nodes: [+ { name: "compile", status: "FAILED", retried: true, webPath: "/acme/web/-/jobs/1" },+ {+ name: "compile",+ status: "SUCCESS",+ detailedStatus: { label: "passed" },+ webPath: "/acme/web/-/jobs/2",+ },+ ],+ },+ },+ {+ name: "test",+ status: "failed",+ jobs: {+ nodes: [+ {+ name: "unit",+ status: "FAILED",+ allowFailure: false,+ webPath: "/acme/web/-/jobs/3",+ },+ {+ name: "lint",+ status: "FAILED",+ allowFailure: true,+ webPath: "/acme/web/-/jobs/4",+ },+ { name: "slow", status: "CANCELED", webPath: "/acme/web/-/jobs/5" },+ { name: "e2e", status: "RUNNING", webPath: "/acme/web/-/jobs/6" },+ ],+ },+ },+ {+ name: "deploy",+ status: "manual",+ jobs: { nodes: [{ name: "release", status: "MANUAL", webPath: "/acme/web/-/jobs/7" }] },+ },+ ]),+ "https://gitlab.example.com",+ ),+ );++ expect(checks?.map((check) => [check.name, check.status, check.url])).toEqual([+ ["build / compile", "success", "https://gitlab.example.com/acme/web/-/jobs/2"],+ ["test / unit", "failure", "https://gitlab.example.com/acme/web/-/jobs/3"],+ ["test / lint", "neutral", "https://gitlab.example.com/acme/web/-/jobs/4"],+ ["test / slow", "cancelled", "https://gitlab.example.com/acme/web/-/jobs/5"],+ ["test / e2e", "pending", "https://gitlab.example.com/acme/web/-/jobs/6"],+ ["deploy / release", "neutral", "https://gitlab.example.com/acme/web/-/jobs/7"],+ ]);+ expect(checks?.[0]?.description).toBe("passed");+ });++ it.each([+ ["no jobs", undefined],+ ["an empty job list", { nodes: [] }],+ [+ "more jobs than one page",+ { nodes: [{ name: "unit", status: "FAILED" }], pageInfo: { hasNextPage: true } },+ ],+ ["a job GitLab could not resolve", { nodes: [{ name: "unit", status: "FAILED" }, null] }],+ [+ "a job without a name",+ {+ nodes: [+ { name: "unit", status: "FAILED" },+ { name: null, status: "FAILED" },+ ],+ },+ ],+ ])("keeps a stage whole where its jobs come back with %s", (_, jobs) => {+ expect(+ expectSuccess(+ decodePipelineStageChecksJson(+ stagesJson([+ {+ name: "test",+ status: "failed",+ detailedStatus: { label: "failed", detailsPath: "/fork/web/-/pipelines/9#test" },+ ...(jobs === undefined ? {} : { jobs }),+ },+ ]),+ "https://gitlab.com",+ ),+ ),+ ).toEqual([+ {+ name: "test",+ status: "failure",+ description: "failed",+ url: "https://gitlab.com/fork/web/-/pipelines/9#test",+ },+ ]);+ });++ it("answers nothing where there are no stages to show", () => {+ expect(+ expectSuccess(decodePipelineStageChecksJson(stagesJson(null), "https://gitlab.com")),+ ).toBe(null);+ expect(expectSuccess(decodePipelineStageChecksJson(stagesJson([]), "https://gitlab.com"))).toBe(+ null,+ );+ // A stage past the first page could be the failed one.+ expect(+ expectSuccess(+ decodePipelineStageChecksJson(+ stagesJson([{ name: "build", status: "success" }], true),+ "https://gitlab.com",+ ),+ ),+ ).toBe(null);+ // A stage GitLab failed to resolve comes back null beside an error, and could be the failed one.+ expect(+ expectSuccess(+ decodePipelineStageChecksJson(+ stagesJson([{ name: "build", status: "success" }, null]),+ "https://gitlab.com",+ ),+ ),+ ).toBe(null);+ expect(+ expectSuccess(+ decodePipelineStageChecksJson(+ stagesJson([+ { name: "build", status: "success" },+ { name: null, status: "failed" },+ ]),+ "https://gitlab.com",+ ),+ ),+ ).toBe(null);+ expect(+ expectSuccess(+ decodePipelineStageChecksJson(+ JSON.stringify({+ ...JSON.parse(stagesJson([{ name: "build", status: "success" }])),+ errors: [{ message: "boom" }],+ }),+ "https://gitlab.com",+ ),+ ),+ ).toBe(null);+ // GitLab answers an error it hit with no data at all.+ expect(+ expectSuccess(+ decodePipelineStageChecksJson(+ JSON.stringify({ errors: [{ message: "boom" }] }),+ "https://gitlab.com",+ ),+ ),+ ).toBe(null);+ });+});+describe("decodeNotesJson", () => {it("keeps comments and drops GitLab's own activity notes", () => {const notes = expectSuccess(diff --git a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.tsindex f456236b852..770712c2397 100644--- a/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts+++ b/packages/source-control-gitlab/src/server/gitLabMergeRequestJson.ts@@ -305,6 +305,162 @@ function toChecks(];}+export const PIPELINE_STAGES_GRAPHQL_QUERY = `query($fullPath: ID!, $iid: String!) {+ project(fullPath: $fullPath) {+ mergeRequest(iid: $iid) {+ headPipeline {+ path+ stages(first: 100) {+ nodes {+ name+ status+ detailedStatus { label detailsPath }+ jobs(first: 100) {+ nodes {+ name status allowFailure retried webPath detailedStatus { label }+ downstreamPipeline { status path }+ }+ pageInfo { hasNextPage }+ }+ }+ pageInfo { hasNextPage }+ }+ }+ }+ }+}`;++const OptionalString = Schema.optional(Schema.NullOr(Schema.String));++const RawStageJobSchema = Schema.Struct({+ name: OptionalString,+ status: OptionalString,+ allowFailure: Schema.optional(Schema.NullOr(Schema.Boolean)),+ retried: Schema.optional(Schema.NullOr(Schema.Boolean)),+ webPath: OptionalString,+ detailedStatus: Schema.optional(Schema.NullOr(Schema.Struct({ label: OptionalString }))),+ downstreamPipeline: Schema.optional(+ Schema.NullOr(Schema.Struct({ status: OptionalString, path: OptionalString })),+ ),+});++const RawStageSchema = Schema.Struct({+ name: OptionalString,+ status: OptionalString,+ detailedStatus: Schema.optional(+ Schema.NullOr(Schema.Struct({ label: OptionalString, detailsPath: OptionalString })),+ ),+ jobs: Schema.optional(+ Schema.NullOr(+ Schema.Struct({+ nodes: Schema.Array(Schema.NullOr(RawStageJobSchema)),+ pageInfo: Schema.optional(+ Schema.NullOr(+ Schema.Struct({ hasNextPage: Schema.optional(Schema.NullOr(Schema.Boolean)) }),+ ),+ ),+ }),+ ),+ ),+});++const RawPipelineStagesSchema = Schema.Struct({+ errors: Schema.optional(Schema.NullOr(Schema.Array(Schema.Unknown))),+ data: Schema.optional(+ Schema.NullOr(+ Schema.Struct({+ project: Schema.NullOr(+ Schema.Struct({+ mergeRequest: Schema.NullOr(+ Schema.Struct({+ headPipeline: Schema.NullOr(+ Schema.Struct({+ path: OptionalString,+ stages: Schema.optional(+ Schema.NullOr(+ Schema.Struct({+ nodes: Schema.Array(Schema.NullOr(RawStageSchema)),+ pageInfo: Schema.optional(+ Schema.NullOr(+ Schema.Struct({+ hasNextPage: Schema.optional(Schema.NullOr(Schema.Boolean)),+ }),+ ),+ ),+ }),+ ),+ ),+ }),+ ),+ }),+ ),+ }),+ ),+ }),+ ),+ ),+});++const decodePipelineStages = decodeJsonResult(RawPipelineStagesSchema);++/**+ * The head pipeline as one check per job, named after its stage. A stage whose jobs GitLab did+ * not list whole stays one check with GitLab's stage status. Null where there are no stages to+ * show, or GitLab returned only some of them, so the caller keeps the single pipeline check.+ */+export function decodePipelineStageChecksJson(+ raw: string,+ origin: string,+): Result.Result<ReadonlyArray<PullRequestCheck> | null, DecodeFailure> {+ const decoded = decodePipelineStages(raw);+ if (!Result.isSuccess(decoded)) {+ return Result.fail(decoded.failure);+ }+ const pipeline = decoded.success.data?.project?.mergeRequest?.headPipeline;+ // Any stage left out could be the failed one, so a partial list is no list.+ if (+ (decoded.success.errors?.length ?? 0) > 0 ||+ pipeline?.stages?.pageInfo?.hasNextPage === true ||+ pipeline?.stages?.nodes.some((stage) => stage === null || trimmed(stage.name) === null) === true+ ) {+ return Result.succeed(null);+ }+ const link = (path: string | null) => (path?.startsWith("/") ? `${origin}${path}` : null);+ const checks: Array<PullRequestCheck> = [];+ for (const stage of pipeline?.stages?.nodes ?? []) {+ const name = trimmed(stage?.name);+ if (stage === null || name === null) continue;+ const jobs = stage.jobs?.nodes ?? [];+ const listedWhole =+ jobs.length > 0 &&+ stage.jobs?.pageInfo?.hasNextPage !== true &&+ jobs.every((job) => job !== null && trimmed(job.name) !== null);+ if (!listedWhole) {+ checks.push({+ name,+ status: toPipelineStatus(stage.status),+ description: trimmed(stage.detailedStatus?.label),+ url: link(trimmed(stage.detailedStatus?.detailsPath) ?? trimmed(pipeline?.path)),+ });+ continue;+ }+ for (const job of jobs) {+ if (job === null || job.retried === true) continue;+ // A trigger job reads as its downstream pipeline, as `decodeTriggerJobChecksJson` explains.+ const downstream = job.downstreamPipeline;+ const status = toPipelineStatus(downstream ? downstream.status : job.status);+ checks.push({+ name: `${name} / ${trimmed(job.name)}`,+ // The same reading as `manual`: GitLab was told this failure does not fail the pipeline.+ status: status === "failure" && job.allowFailure === true ? "neutral" : status,+ description: downstream ? "Downstream pipeline" : trimmed(job.detailedStatus?.label),+ url: link(trimmed(downstream ? downstream.path : job.webPath)),+ });+ }+ }+ return Result.succeed(checks.length === 0 ? null : checks);+}+function toListItem(raw: Schema.Schema.Type<typeof RawMergeRequestSchema>,): GitLabMergeRequestListItem {