1name: Bot - CI Failure Notifier
2
3on:
4 workflow_run:
5 workflows: ["CI"]
6 types: [completed]
7
8jobs:
9 notify:
10 runs-on: ubuntu-latest
11 steps:
12 - name: Process CI Result
13 uses: actions/github-script@v9
14 with:
15 github-token: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
16 script: |
17 const run = context.payload.workflow_run;
18 const owner = context.repo.owner;
19 const repo = context.repo.repo;
20
21 // 1. Robustly find the PR number (works for forks too)
22 let pr_number;
23 if (run.pull_requests && run.pull_requests.length > 0) {
24 pr_number = run.pull_requests[0].number;
25 } else {
26 // Fallback: Find PR associated with the commit SHA
27 const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
28 owner,
29 repo,
30 commit_sha: run.head_sha
31 });
32 if (prs.length > 0) {
33 pr_number = prs[0].number;
34 }
35 }
36
37 if (!pr_number) {
38 console.log("No open PR found for this workflow run. Exiting.");
39 return;
40 }
41
42 // 2. Handle CI Failure -> Request Changes
43 if (run.conclusion === 'failure') {
44 const jobs = await github.rest.actions.listJobsForWorkflowRun({
45 owner, repo, run_id: run.id
46 });
47
48 const failedJobs = jobs.data.jobs.filter(j => j.conclusion === 'failure').map(j => j.name);
49
50 let message = "The CI workflow failed. Please fix the following issues locally and push again:\n\n";
51 let foundSpecificInstruction = false;
52
53 if (failedJobs.some(name => name.includes('lint'))) {
54 message += "- **Lint Failed**: Run `gofmt -w .` to format files and `go vet ./...` to check for vet errors.\n";
55 foundSpecificInstruction = true;
56 }
57 if (failedJobs.some(name => name.includes('mod-tidy'))) {
58 message += "- **Mod-tidy Failed**: Run `go mod tidy` locally and commit the resulting `go.mod` and `go.sum` changes.\n";
59 foundSpecificInstruction = true;
60 }
61 if (failedJobs.some(name => name.includes('nix'))) {
62 message += "- **Nix Failed**: Run `nix flake check --no-build` locally to find the issue with the flake.\n";
63 foundSpecificInstruction = true;
64 }
65
66 if (!foundSpecificInstruction) {
67 message += "Please check the [CI logs](" + run.html_url + ") for more details on the failure.";
68 }
69
70 // Submit an official "Request Changes" review
71 await github.rest.pulls.createReview({
72 owner,
73 repo,
74 pull_number: pr_number,
75 body: message,
76 event: 'REQUEST_CHANGES'
77 });
78
79 }
80 // 3. Handle CI Success -> Dismiss Reviews
81 else if (run.conclusion === 'success') {
82 const { data: botUser } = await github.rest.users.getAuthenticated();
83
84 const { data: reviews } = await github.rest.pulls.listReviews({
85 owner, repo, pull_number: pr_number
86 });
87
88 // Find active blocking reviews left by the bot account
89 const botReviews = reviews.filter(r =>
90 r.user.login === botUser.login &&
91 r.state === 'CHANGES_REQUESTED'
92 );
93
94 // Dismiss them
95 for (const review of botReviews) {
96 await github.rest.pulls.dismissReview({
97 owner,
98 repo,
99 pull_number: pr_number,
100 review_id: review.id,
101 message: 'The CI workflow has passed! Dismissing previous review.'
102 });
103 }
104 }