-
Notifications
You must be signed in to change notification settings - Fork 154
197 lines (181 loc) · 7.85 KB
/
Copy pathci-post-merge.yml
File metadata and controls
197 lines (181 loc) · 7.85 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
name: Post Merge Actions
on:
pull_request:
types: [closed]
workflow_run:
workflows: ["Run CI unittests"]
types: [completed]
permissions:
actions: read
contents: read
issues: write
pull-requests: read
jobs:
notify-ci-failure:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' || github.event.workflow_run.conclusion != 'success'
steps:
- name: Find failed CI run
id: failed-ci
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const GRAALVMBOT_LOGIN = "graalvmbot";
const TARGET_WORKFLOW_NAME = "Run CI unittests";
async function getPullRequest(prNumber) {
const {data: pr} = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
return pr;
}
async function wasClosedByGraalVmBot(prNumber) {
for await (const response of github.paginate.iterator(github.rest.issues.listEvents, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
})) {
for (const event of response.data) {
if (event.event === "closed" && event.actor && event.actor.login === GRAALVMBOT_LOGIN) {
return true;
}
}
}
return false;
}
async function isEligiblePullRequest(pr, checkCloseActor) {
if (!pr || !pr.number || pr.state !== "closed") {
console.log("PR is not closed. Skipping CI failure notification.");
return false;
}
if (checkCloseActor && !(await wasClosedByGraalVmBot(pr.number))) {
console.log(`PR #${pr.number} was not closed by ${GRAALVMBOT_LOGIN}. Skipping CI failure notification.`);
return false;
}
const assignees = pr.assignees || [];
if (assignees.length !== 1) {
console.log(`Expected exactly 1 assignee, found ${assignees.length}. Skipping CI failure notification.`);
return false;
}
return true;
}
async function alreadyCommented(prNumber, marker) {
for await (const response of github.paginate.iterator(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100,
})) {
if (response.data.some(comment => comment.body && comment.body.includes(marker))) {
return true;
}
}
return false;
}
let pr = null;
let failedRun = null;
let checkCloseActor = false;
if (context.eventName === "pull_request") {
pr = context.payload.pull_request;
const sender = context.payload.sender;
if (!sender || sender.login !== GRAALVMBOT_LOGIN) {
console.log(`PR closed by ${sender ? sender.login : "unknown"}, not ${GRAALVMBOT_LOGIN}. Skipping CI check.`);
return;
}
if (!(await isEligiblePullRequest(pr, false))) {
return;
}
const runsResp = await github.rest.actions.listWorkflowRuns({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: "ci-unittests.yml",
head_sha: pr.head.sha,
});
failedRun = runsResp.data.workflow_runs.find(run =>
run.name === TARGET_WORKFLOW_NAME &&
run.head_sha === pr.head.sha &&
run.status === "completed" &&
run.conclusion !== "success"
);
if (!failedRun) {
console.log("No completed failed CI workflow found for the PR yet. The workflow_run trigger will handle a later failure.");
return;
}
} else if (context.eventName === "workflow_run") {
const run = context.payload.workflow_run;
if (!run || run.name !== TARGET_WORKFLOW_NAME) {
console.log("Not the target workflow_run event.");
return;
}
if (run.conclusion === "success") {
console.log("CI workflow succeeded. No notification needed.");
return;
}
if (!run.pull_requests || run.pull_requests.length === 0) {
console.log("Workflow run has no associated pull request.");
return;
}
pr = await getPullRequest(run.pull_requests[0].number);
checkCloseActor = true;
failedRun = run;
} else {
console.log(`Unsupported event: ${context.eventName}`);
return;
}
if (!(await isEligiblePullRequest(pr, checkCloseActor))) {
return;
}
const marker = `<!-- graalpy-ci-post-merge-failure:${pr.head.sha} -->`;
if (await alreadyCommented(pr.number, marker)) {
console.log(`Failure notification was already posted for PR #${pr.number} and SHA ${pr.head.sha}.`);
return;
}
const assignees = pr.assignees || [];
core.setOutput('assignee', assignees[0].login);
core.setOutput('failed_run_url', failedRun.html_url);
core.setOutput('failed_run_id', failedRun.id);
core.setOutput('pr_number', pr.number);
core.setOutput('comment_marker', marker);
console.log(`Found failed CI workflow: ${failedRun.html_url}`);
- name: Download merged test report
if: ${{ steps.failed-ci.outputs.failed_run_url != '' }}
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
with:
name: merged_test_reports
path: report
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ steps.failed-ci.outputs.failed_run_id }}
continue-on-error: true
- name: Post failure comment
if: ${{ steps.failed-ci.outputs.failed_run_url != '' }}
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const assignee = '${{ steps.failed-ci.outputs.assignee }}';
const runUrl = '${{ steps.failed-ci.outputs.failed_run_url }}';
const prNumber = Number('${{ steps.failed-ci.outputs.pr_number }}');
const marker = '${{ steps.failed-ci.outputs.comment_marker }}';
let body = `${marker}\n@${assignee} - CI workflow failed: [View workflow](${runUrl})`;
try {
const reportPath = 'report/merged_test_reports.json';
if (fs.existsSync(reportPath)) {
const data = JSON.parse(fs.readFileSync(reportPath, 'utf8'));
const failed = data.map(t => t.name);
if (failed.length) {
const list = failed.map(n => `- ${n}`).join('\n');
body = `${marker}\n@${assignee} - CI workflow failed: [View workflow](${runUrl})\nFailed tests:\n\n${list}`;
}
}
} catch (e) {
console.log(`Error parsing test report: ${e}`);
}
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});