diff --git a/HOOKS.md b/HOOKS.md index 2d9d14b..a28cab7 100644 --- a/HOOKS.md +++ b/HOOKS.md @@ -32,6 +32,7 @@ hooks: | [`dco`](#dco) | Enforces the Developer Certificate of Origin (`Signed-off-by`) on every commit of a pull request | | [`label`](#label) | Automatically applies labels to pull requests and issues based on keywords or file paths | | [`assign`](#assign) | Automatically assigns reviewers and assignees to pull requests based on file path pattern matching | +| [`pasteCI`](#pasteci) | Pastes the failing CI job output as a comment on the pull request | ## `acknowledge` @@ -121,6 +122,22 @@ Enforces the [Developer Certificate of Origin](https://developercertificate.org/ | `enabled` | `boolean` | `false` | Enables the DCO validation | | `fail` | `boolean` | `true` | Creates a failing check run when a sign-off is missing | +## `pasteCI` + +When a GitHub Actions workflow run attached to a pull request fails, pastes the tail of each failing job's log into a comment on the PR, so you don't have to click through to the Actions tab. The comment is kept up to date on every run (single comment, never one per push), and when CI passes again the same comment is refreshed to say so. + +> [!NOTE] +> GitHub only attaches `pull_requests` to workflow runs from branches of the same repository, so runs triggered by forked PRs are skipped. Reading job logs requires the `actions: read` app permission. + +**Listens to:** `workflow_run.completed` + +### Config + +| Setting | Type | Default | Description | +| --------- | --------- | ------- | -------------------------------------------- | +| `enabled` | `boolean` | `false` | Enables pasting CI output on failure | +| `lines` | `number` | `50` | How many trailing log lines to paste per job | + ## `assign` Automatically assigns reviewers and assignees to pull requests based on file path pattern matching rules. Supports individual user handles (`@user`) and GitHub team slugs (`team-slug`). diff --git a/app.yml b/app.yml index f0381bb..1e822a3 100644 --- a/app.yml +++ b/app.yml @@ -46,12 +46,17 @@ default_events: # - team # - team_add # - watch + - workflow_run # The set of permissions needed by the GitHub App. The format of the object uses # the permission name for the key (for example, issues) and the access type for # the value (for example, write). # Valid values are `read`, `write`, and `none` default_permissions: + # Workflows, workflow runs and artifacts (needed by the pasteCI hook to read job logs). + # https://developer.github.com/v3/apps/permissions/#permission-on-actions + actions: read + # Repository creation, deletion, settings, teams, and collaborators. # https://developer.github.com/v3/apps/permissions/#permission-on-administration # administration: read diff --git a/src/app/hooks/pasteCI/index.ts b/src/app/hooks/pasteCI/index.ts new file mode 100644 index 0000000..0f18e81 --- /dev/null +++ b/src/app/hooks/pasteCI/index.ts @@ -0,0 +1,126 @@ +import { defineHook } from "../../../lib/eventHandler.js"; + +const commentMark = ""; +const maxLogChars = 6000; + +const tailOfLog = (log: string, lines: number) => + log + .split("\n") + .map((line) => line.replace(/^\S+Z\s/, "")) + .filter((line) => line.trim().length > 0) + .slice(-lines) + .join("\n") + .slice(-maxLogChars); + +export default defineHook({ + events: ["workflow_run.completed"], + callback: async ({ ctx, config }) => { + const { enabled, lines } = config.hooks.pasteCI; + + if (!enabled) return; + + const run = ctx.payload.workflow_run; + + if (run.pull_requests.length === 0) return; + + const { owner, repo } = ctx.repo(); + + const findBotComment = async (issueNumber: number) => + ( + await ctx.octokit.rest.issues.listComments({ + owner, + repo, + issue_number: issueNumber, + }) + ).data.find( + (comment) => + comment.user?.type === "Bot" && comment.body?.includes(commentMark), + ); + + const upsertComment = async (issueNumber: number, body: string) => { + const botComment = await findBotComment(issueNumber); + + if (botComment) { + await ctx.octokit.rest.issues.updateComment({ + owner, + repo, + comment_id: botComment.id, + body, + }); + } else { + await ctx.octokit.rest.issues.createComment({ + owner, + repo, + issue_number: issueNumber, + body, + }); + } + }; + + if (run.conclusion !== "failure") { + const passedMD = [ + commentMark, + "> [!NOTE]", + `> CI is passing again on [\`${run.name}\`](${run.html_url}). Good to go!`, + ].join("\n"); + + for (const pr of run.pull_requests) { + const botComment = await findBotComment(pr.number); + if (botComment) await upsertComment(pr.number, passedMD); + } + return; + } + + const failedJobs = ( + await ctx.octokit.rest.actions.listJobsForWorkflowRun({ + owner, + repo, + run_id: run.id, + filter: "latest", + }) + ).data.jobs.filter((job) => job.conclusion === "failure"); + + if (failedJobs.length === 0) return; + + const sections = await Promise.all( + failedJobs.map(async (job) => { + let log = ""; + + try { + const { data } = + await ctx.octokit.rest.actions.downloadJobLogsForWorkflowRun({ + owner, + repo, + job_id: job.id, + }); + log = tailOfLog(String(data), lines); + } catch { + log = "Logs could not be retrieved."; + } + + return [ + "
", + `${job.name}view job`, + "", + "```text", + log, + "```", + "", + "
", + ].join("\n"); + }), + ); + + const summaryMD = [ + commentMark, + "> [!WARNING]", + `> CI failed on [\`${run.name}\`](${run.html_url}). Output of the failing job${failedJobs.length > 1 ? "s" : ""} below (last ${lines} lines).`, + "", + ...sections, + ].join("\n"); + + for (const pr of run.pull_requests) { + await upsertComment(pr.number, summaryMD); + } + }, +}); diff --git a/src/app/hooks/pasteCI/schema.ts b/src/app/hooks/pasteCI/schema.ts new file mode 100644 index 0000000..3151570 --- /dev/null +++ b/src/app/hooks/pasteCI/schema.ts @@ -0,0 +1,6 @@ +import z from "zod"; + +export const pasteCISchema = z.object({ + enabled: z.boolean().default(false), + lines: z.number().int().min(1).max(200).default(50), +}); diff --git a/src/schemas/hooks.ts b/src/schemas/hooks.ts index 3094f4f..3818510 100644 --- a/src/schemas/hooks.ts +++ b/src/schemas/hooks.ts @@ -5,6 +5,7 @@ import { conventionalCommitsSchema } from "../app/hooks/conventionalCommits/sche import { dcoSchema } from "../app/hooks/dco/schema.js"; import { deleteMergedBranchSchema } from "../app/hooks/deleteMergedBranch/schema.js"; import { labelSchema } from "../app/hooks/label/schema.js"; +import { pasteCISchema } from "../app/hooks/pasteCI/schema.js"; import { unfurlSchema } from "../app/hooks/unfurl/schema.js"; import { wipSchema } from "../app/hooks/wip/schema.js"; @@ -21,4 +22,5 @@ export const hooksSchema = z.object({ dco: dcoSchema.default(dcoSchema.parse({})), assign: assignSchema.default(assignSchema.parse({})), label: labelSchema.default(labelSchema.parse({})), + pasteCI: pasteCISchema.default(pasteCISchema.parse({})), }); diff --git a/tests/app/hooks/pasteCI.test.ts b/tests/app/hooks/pasteCI.test.ts new file mode 100644 index 0000000..54e425f --- /dev/null +++ b/tests/app/hooks/pasteCI.test.ts @@ -0,0 +1,236 @@ +import type { Context } from "probot"; +import { describe, expect, it, vi } from "vitest"; +import pasteCIHook from "../../../src/app/hooks/pasteCI/index.js"; +import * as configModule from "../../../src/lib/getConfig.js"; +import { defaultConfig, type Config } from "../../../src/schemas/config.js"; + +function createMockContext({ + conclusion = "failure", + pullRequests = [{ number: 1 }], + jobs = [] as { + id: number; + name: string; + conclusion: string; + html_url: string; + }[], + log = "", + existingComments = [] as { + id: number; + user: { type: string }; + body: string; + }[], +} = {}) { + const listJobsMock = vi.fn().mockResolvedValue({ data: { jobs } }); + const downloadLogsMock = vi.fn().mockResolvedValue({ data: log }); + const listCommentsMock = vi + .fn() + .mockResolvedValue({ data: existingComments }); + const createCommentMock = vi.fn().mockResolvedValue({ data: { id: 1 } }); + const updateCommentMock = vi.fn().mockResolvedValue({}); + + const mockCtx = { + payload: { + workflow_run: { + id: 777, + name: "CI", + html_url: "https://github.com/testowner/testrepo/actions/runs/777", + conclusion, + pull_requests: pullRequests, + }, + }, + repo: () => ({ owner: "testowner", repo: "testrepo" }), + octokit: { + rest: { + actions: { + listJobsForWorkflowRun: listJobsMock, + downloadJobLogsForWorkflowRun: downloadLogsMock, + }, + issues: { + listComments: listCommentsMock, + createComment: createCommentMock, + updateComment: updateCommentMock, + }, + }, + }, + } as unknown as Context<"workflow_run.completed">; + + return { + mockCtx, + listJobsMock, + downloadLogsMock, + listCommentsMock, + createCommentMock, + updateCommentMock, + }; +} + +function withConfig(overrides: Partial): Config { + return { + ...defaultConfig, + hooks: { + ...defaultConfig.hooks, + pasteCI: { + enabled: true, + lines: 50, + ...overrides, + }, + }, + }; +} + +const failedJob = { + id: 10, + name: "test", + conclusion: "failure", + html_url: "https://github.com/testowner/testrepo/actions/runs/777/job/10", +}; + +describe("pasteCI hook", () => { + it("registers workflow_run.completed", () => { + expect(pasteCIHook.events).toEqual(["workflow_run.completed"]); + }); + + it("does nothing when disabled", async () => { + const { mockCtx, listJobsMock, createCommentMock } = createMockContext(); + vi.spyOn(configModule, "getConfig").mockResolvedValue( + withConfig({ enabled: false }), + ); + + await pasteCIHook.callback(mockCtx); + + expect(listJobsMock).not.toHaveBeenCalled(); + expect(createCommentMock).not.toHaveBeenCalled(); + }); + + it("does nothing for runs without an attached pull request", async () => { + const { mockCtx, createCommentMock } = createMockContext({ + pullRequests: [], + }); + vi.spyOn(configModule, "getConfig").mockResolvedValue(withConfig({})); + + await pasteCIHook.callback(mockCtx); + + expect(createCommentMock).not.toHaveBeenCalled(); + }); + + it("pastes the failing job log tail as a PR comment", async () => { + const { mockCtx, createCommentMock, downloadLogsMock } = createMockContext({ + jobs: [failedJob], + log: "2026-08-18T10:00:00.000Z line one\n2026-08-18T10:00:01.000Z Error: it broke\n", + }); + vi.spyOn(configModule, "getConfig").mockResolvedValue(withConfig({})); + + await pasteCIHook.callback(mockCtx); + + expect(downloadLogsMock).toHaveBeenCalledWith( + expect.objectContaining({ job_id: 10 }), + ); + expect(createCommentMock).toHaveBeenCalledWith( + expect.objectContaining({ + issue_number: 1, + body: expect.stringContaining("Error: it broke"), + }), + ); + // Log timestamps are stripped for readability + const body = createCommentMock.mock.calls[0][0].body as string; + expect(body).not.toContain("2026-08-18T10:00:01.000Z"); + expect(body).toContain(""); + }); + + it("only pastes the last configured number of lines", async () => { + const log = Array.from({ length: 100 }, (_, i) => `line ${i + 1}`).join( + "\n", + ); + const { mockCtx, createCommentMock } = createMockContext({ + jobs: [failedJob], + log, + }); + vi.spyOn(configModule, "getConfig").mockResolvedValue( + withConfig({ lines: 10 }), + ); + + await pasteCIHook.callback(mockCtx); + + const body = createCommentMock.mock.calls[0][0].body as string; + expect(body).toContain("line 100"); + expect(body).not.toContain("line 90\n"); + }); + + it("updates the existing bot comment instead of creating a new one", async () => { + const { mockCtx, createCommentMock, updateCommentMock } = createMockContext( + { + jobs: [failedJob], + log: "boom", + existingComments: [ + { + id: 999, + user: { type: "Bot" }, + body: "\nold output", + }, + ], + }, + ); + vi.spyOn(configModule, "getConfig").mockResolvedValue(withConfig({})); + + await pasteCIHook.callback(mockCtx); + + expect(updateCommentMock).toHaveBeenCalledWith( + expect.objectContaining({ comment_id: 999 }), + ); + expect(createCommentMock).not.toHaveBeenCalled(); + }); + + it("refreshes an existing comment when CI passes again, without creating one", async () => { + const { mockCtx, createCommentMock, updateCommentMock } = createMockContext( + { + conclusion: "success", + existingComments: [ + { + id: 999, + user: { type: "Bot" }, + body: "\nold failure output", + }, + ], + }, + ); + vi.spyOn(configModule, "getConfig").mockResolvedValue(withConfig({})); + + await pasteCIHook.callback(mockCtx); + + expect(updateCommentMock).toHaveBeenCalledWith( + expect.objectContaining({ + comment_id: 999, + body: expect.stringContaining("passing again"), + }), + ); + expect(createCommentMock).not.toHaveBeenCalled(); + }); + + it("stays silent on success when no comment exists", async () => { + const { mockCtx, createCommentMock, updateCommentMock } = createMockContext( + { conclusion: "success" }, + ); + vi.spyOn(configModule, "getConfig").mockResolvedValue(withConfig({})); + + await pasteCIHook.callback(mockCtx); + + expect(createCommentMock).not.toHaveBeenCalled(); + expect(updateCommentMock).not.toHaveBeenCalled(); + }); + + it("still lists the failed job when its logs cannot be retrieved", async () => { + const { mockCtx, createCommentMock, downloadLogsMock } = createMockContext({ + jobs: [failedJob], + }); + downloadLogsMock.mockRejectedValue(new Error("410 Gone")); + vi.spyOn(configModule, "getConfig").mockResolvedValue(withConfig({})); + + await pasteCIHook.callback(mockCtx); + + expect(createCommentMock).toHaveBeenCalledWith( + expect.objectContaining({ + body: expect.stringContaining("Logs could not be retrieved."), + }), + ); + }); +});