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