Skip to content

Commit 0ea61db

Browse files
authored
[Docs Site] Post PR comment when CI fails (#20386)
1 parent 5220003 commit 0ea61db

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ jobs:
5757

5858
- name: Tests
5959
run: npm run test
60+
61+
- name: Post PR CI failure comment
62+
if: always()
63+
continue-on-error: true
64+
env:
65+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66+
run: npx tsx bin/post-pr-ci-failure-comment/index.ts
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const GITHUB_ACTIONS_BOT_ID = 41898282;
2+
export const DOCS_BASE_URL = "https://developers.cloudflare.com";
3+
export const CONTENT_BASE_PATH = "src/content";
4+
export const PREVIEW_URL_REGEX = /^\*\*Preview URL:\*\* (.*)$/m;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import * as core from "@actions/core";
2+
import * as github from "@actions/github";
3+
import type { PullRequestEvent } from "@octokit/webhooks-types";
4+
5+
import { GITHUB_ACTIONS_BOT_ID } from "./constants";
6+
7+
async function run(): Promise<void> {
8+
try {
9+
if (!process.env.GITHUB_TOKEN) {
10+
core.setFailed(`Could not find GITHUB_TOKEN in env`);
11+
process.exit();
12+
}
13+
14+
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
15+
const payload = github.context.payload as PullRequestEvent;
16+
17+
const { owner, repo } = github.context.repo;
18+
const pullRequestNumber = payload.number;
19+
const runId = github.context.runId;
20+
21+
const { data: run } = await octokit.rest.actions.listJobsForWorkflowRun({
22+
owner,
23+
repo,
24+
run_id: runId,
25+
});
26+
27+
const job = run.jobs.findLast((job) => job.name === "Compiles");
28+
29+
if (!job) {
30+
core.setFailed(`Could not find a job called 'Compiles'`);
31+
process.exit();
32+
}
33+
34+
const failedStep = job.steps?.find((step) => step.conclusion === "failure");
35+
36+
if (failedStep) {
37+
core.info(`Found failed step ${failedStep.name}`);
38+
}
39+
40+
const conclusion = failedStep ? "failure" : "success";
41+
42+
const { data: comments } = await octokit.rest.issues.listComments({
43+
owner,
44+
repo,
45+
issue_number: pullRequestNumber,
46+
per_page: 100,
47+
});
48+
49+
const existingComment = comments.find(
50+
(comment) =>
51+
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
52+
comment.body?.includes("**CI run failed:**"),
53+
);
54+
55+
if (existingComment) {
56+
core.info(`Found existing comment with ID ${existingComment.id}`);
57+
} else {
58+
core.info(`No existing comment found`);
59+
}
60+
61+
const url = `https://github.com/${owner}/${repo}/actions/runs/${runId}/job/${job.id}`;
62+
const comment = `**CI run failed:** [build logs](${url})`;
63+
64+
if (conclusion === "failure") {
65+
if (existingComment) {
66+
core.info(
67+
`Updating ${existingComment.id} with ${JSON.stringify(comment)}`,
68+
);
69+
await octokit.rest.issues.updateComment({
70+
owner,
71+
repo,
72+
comment_id: existingComment.id,
73+
body: comment,
74+
});
75+
} else {
76+
core.info(`Creating new comment with ${JSON.stringify(comment)}`);
77+
await octokit.rest.issues.createComment({
78+
owner,
79+
repo,
80+
issue_number: pullRequestNumber,
81+
body: comment,
82+
});
83+
}
84+
} else if (conclusion === "success" && existingComment) {
85+
core.info(`Removing ${existingComment.id}`);
86+
await octokit.rest.issues.deleteComment({
87+
owner,
88+
repo,
89+
issue_number: pullRequestNumber,
90+
comment_id: existingComment.id,
91+
});
92+
}
93+
} catch (error) {
94+
if (error instanceof Error) {
95+
core.setFailed(error.message);
96+
}
97+
process.exit();
98+
}
99+
}
100+
101+
run();

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@expressive-code/plugin-collapsible-sections": "0.40.2",
4242
"@iarna/toml": "2.2.5",
4343
"@marsidev/react-turnstile": "1.1.0",
44+
"@octokit/webhooks-types": "7.6.1",
4445
"@stoplight/json-schema-tree": "4.0.0",
4546
"@types/hast": "3.0.4",
4647
"@types/he": "1.2.3",

0 commit comments

Comments
 (0)