-
Notifications
You must be signed in to change notification settings - Fork 17
177 lines (163 loc) · 6.83 KB
/
ci-comment.yaml
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
name: CI Comment
on:
workflow_run:
workflows: ["CI"]
types:
- completed
jobs:
ci-comment:
name: Update PR body
if: >
${{ github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
env:
OPAMSOLVERTIMEOUT: 3600
SNAPSHOT: "snapshot-${{ matrix.snapshot }}"
steps:
- name: Checkout code (10 commits)
uses: actions/checkout@v2
with:
fetch-depth: 10
submodules: true
- name: 'Download artifact'
uses: actions/github-script@v7
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
const fs = require('fs');
const matchedArtifacts = artifacts.data.artifacts.filter((artifact) => {
return artifact.name.startsWith('pr');
});
for await (const artifact of matchedArtifacts) {
console.log('download artifact', artifact);
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
fs.writeFileSync(`${{github.workspace}}/pr-${artifact.id}.zip`, Buffer.from(download.data));
}
- name: Extract
run: |
shopt -s nullglob
for file in *.zip ; do
unzip "$file" -d "${file%.zip}"
done
for f in pr-*/comment.txt ; do
echo "# $f"
cat "$f"
done
- name: Update the PR body
uses: actions/github-script@v7
env:
SNAPSHOT_UPDATABLE: ${{ steps.test-update-snapshot.outputs.result }}
PACKAGES_TO_UPDATE: ${{ steps.test-update-snapshot.outputs.packages }}
with:
github-token: ${{ github.token }}
script: |
const fs = require('fs');
const dir = await fs.promises.opendir('.');
const snapshots = [];
const readFile = (fileName, d) => {
try {
return fs.readFileSync(fileName, 'utf8').trim();
} catch (e) {
console.log(e);
return d;
}
};
for await (const dirent of dir) {
if (dirent.isDirectory() && dirent.name.startsWith('pr-')) {
const dirname = dirent.name;
if (!fs.existsSync(`${dirname}/pull-number.txt`)) {
continue;
}
const pull_number = Number(fs.readFileSync(`${dirname}/pull-number.txt`, 'utf8'));
const run_id = Number(fs.readFileSync(`${dirname}/run-id.txt`, 'utf8'));
const snapshot = fs.readFileSync(`${dirname}/id.txt`, 'utf8').trim();
const matrix = fs.readFileSync(`${dirname}/matrix.txt`, 'utf8').trim();
const result = readFile(`${dirname}/result.txt`, undefined);
const packages = readFile(`${dirname}/packages.txt`, 'unknown');
const comment = readFile(`${dirname}/comment.txt`, undefined);
snapshots.push({pull_number, run_id, snapshot, matrix, result, packages, comment});
}
}
console.log('snapshots', snapshots);
if (snapshots.length === 0) {
console.log('No artifacts read');
return;
}
const pull_number = snapshots[0].pull_number;
const pullRequestId = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number
};
const pull_request = await github.rest.pulls.get(pullRequestId);
const actionHeader = '\n# Automatic follow-ups\n';
const actionText = (snapshot) => `Add to snapshot \`${snapshot}\``;
const body = pull_request.data.body.replace(/\r?\n/g, '\n');
const extractAction = (str) =>
str.replace(/^\[.\] /, "")
.replace(/ :: .*/, "")
.replace(/^~~(.*)~~.*/, "$1");
if (body) {
const splitted = body.split(actionHeader);
const actions = splitted[1]
? splitted[1]
.split('\n- ')
.map((action) => {
for (snapshot of snapshots) {
console.log('action', action, actionText(snapshot.snapshot), snapshot);
const extractedAction = extractAction(action);
if (extractedAction === actionText(snapshot.snapshot)) {
switch (snapshot.result) {
case 'updated':
return `[ ] ${extractedAction} :: ${snapshot.packages}`;
case 'no-updates':
return `~~${extractedAction}~~ (No updates)`;
case 'failed':
// return `~~${extractedAction}~~ ([Inconsistent](https://github.com/${context.repo.owner}/${context.repo.repo}/runs/${snapshot.run_id}?check_suite_focus=true))`;
return `~~${extractedAction}~~ (Inconsistent)`;
case 'unknown':
return `~~${extractedAction}~~ (Result unknown)`;
default:
if (snapshot.result.startsWith('inconsistent-snapshot ')) {
return `~~${extractedAction}~~ (${snapshot.result})`;
}
return `~~${extractedAction}~~ (Result unknown)`;
}
}
}
return action;
})
: [];
const replacedBody = `${splitted[0]}${actionHeader}${actions.join('\n- ')}`;
await github.rest.pulls.update({
...pullRequestId,
body: replacedBody
});
}
const comments = [
'# CI Summary'
];
for (snapshot of snapshots) {
if (snapshot.comment) {
comments.push(`## Matrix: ${snapshot.matrix}`);
comments.push(snapshot.comment);
}
}
if (comments.length) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pull_number,
body: comments.join('\n')
});
}