Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ jobs:

- name: Tests
run: npm run test

- name: Post PR CI failure comment
if: always()
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: npx tsx bin/post-pr-ci-failure-comment/index.ts
4 changes: 4 additions & 0 deletions bin/post-pr-ci-failure-comment/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const GITHUB_ACTIONS_BOT_ID = 41898282;
export const DOCS_BASE_URL = "https://developers.cloudflare.com";
export const CONTENT_BASE_PATH = "src/content";
export const PREVIEW_URL_REGEX = /^\*\*Preview URL:\*\* (.*)$/m;
101 changes: 101 additions & 0 deletions bin/post-pr-ci-failure-comment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import type { PullRequestEvent } from "@octokit/webhooks-types";

import { GITHUB_ACTIONS_BOT_ID } from "./constants";

async function run(): Promise<void> {
try {
if (!process.env.GITHUB_TOKEN) {
core.setFailed(`Could not find GITHUB_TOKEN in env`);
process.exit();
}

const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
const payload = github.context.payload as PullRequestEvent;

const { owner, repo } = github.context.repo;
const pullRequestNumber = payload.number;
const runId = github.context.runId;

const { data: run } = await octokit.rest.actions.listJobsForWorkflowRun({
owner,
repo,
run_id: runId,
});

const job = run.jobs.findLast((job) => job.name === "Compiles");

if (!job) {
core.setFailed(`Could not find a job called 'Compiles'`);
process.exit();
}

const failedStep = job.steps?.find((step) => step.conclusion === "failure");

if (failedStep) {
core.info(`Found failed step ${failedStep.name}`);
}

const conclusion = failedStep ? "failure" : "success";

const { data: comments } = await octokit.rest.issues.listComments({
owner,
repo,
issue_number: pullRequestNumber,
per_page: 100,
});

const existingComment = comments.find(
(comment) =>
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
comment.body?.includes("**CI run failed:**"),
);

if (existingComment) {
core.info(`Found existing comment with ID ${existingComment.id}`);
} else {
core.info(`No existing comment found`);
}

const url = `https://github.com/${owner}/${repo}/actions/runs/${runId}/job/${job.id}`;
const comment = `**CI run failed:** [build logs](${url})`;

if (conclusion === "failure") {
if (existingComment) {
core.info(
`Updating ${existingComment.id} with ${JSON.stringify(comment)}`,
);
await octokit.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body: comment,
});
} else {
core.info(`Creating new comment with ${JSON.stringify(comment)}`);
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: pullRequestNumber,
body: comment,
});
}
} else if (conclusion === "success" && existingComment) {
core.info(`Removing ${existingComment.id}`);
await octokit.rest.issues.deleteComment({
owner,
repo,
issue_number: pullRequestNumber,
comment_id: existingComment.id,
});
}
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
}
process.exit();
}
}

run();
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@expressive-code/plugin-collapsible-sections": "0.40.2",
"@iarna/toml": "2.2.5",
"@marsidev/react-turnstile": "1.1.0",
"@octokit/webhooks-types": "7.6.1",
"@stoplight/json-schema-tree": "4.0.0",
"@types/hast": "3.0.4",
"@types/he": "1.2.3",
Expand Down
Loading