-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
399 lines (363 loc) · 10.8 KB
/
index.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");
const { spawn, execSync } = require("child_process");
const { Webhooks } = require("@octokit/webhooks");
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
userAgent: "v8-riscv CI v1.0.2",
});
const webhooks = new Webhooks({
secret: process.env.WEBHOOK_SECRET,
});
// Create the logs directory if it does not already exist
var logDir = "./logs";
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
// Read the configuration file
var config = JSON.parse(fs.readFileSync("config.json"));
// When the PR is opened or edited by an approved user, run the build and test
webhooks.on("pull_request", ({ id, name, payload }) => {
console.log(`Handling pull request.`);
handlePullRequest(payload);
});
// When a PR is approved by an approved user, run the build and test
webhooks.on("pull_request_review", ({ id, name, payload }) => {
console.log(`Handling pr review.`);
handlePullRequestReview(payload);
});
webhooks.on("push", ({ id, name, payload }) => {
handlePush(payload);
});
// When a "/retest" comment is added, re-trigger the tests
webhooks.on("issue_comment", ({ id, name, payload }) => {
console.log(`Handling comment on PRs.`);
handleComment(payload);
});
function runPrecheck(prNum, sha) {
return new Promise((resolve, reject) => {
let timestamp = new Date().toISOString();
var logfile = `${prNum}-precheck-${timestamp}.log`;
var logStream = fs.createWriteStream(`./logs/${logfile}`);
sendStatus(prNum, sha, "precheck", "pending", logfile);
console.log(`Pre-check PR #${prNum}`);
var precheck = spawn("docker", [
"build",
"-t",
`${config.owner}/${config.repo}:${prNum}`,
"--build-arg",
`pr_num=${prNum}`,
"--build-arg",
`sha=${sha}`,
"--target=v8-precheck",
".",
], {
env: { ...process.env, DOCKER_BUILDKIT: 1 }
});
precheck.stdout.pipe(logStream);
precheck.stderr.pipe(logStream);
precheck.on("close", function (code) {
if (code != 0) {
sendStatus(prNum, sha, "precheck", "failure", logfile);
resolve(code);
} else {
sendStatus(prNum, sha, "precheck", "success", logfile);
resolve(0);
}
});
});
}
function runBuild(prNum, sha) {
return new Promise((resolve, reject) => {
let timestamp = new Date().toISOString();
console.log(`Build PR #${prNum}`);
logfile = `${prNum}-build-${timestamp}.log`;
logStream = fs.createWriteStream(`./logs/${logfile}`);
sendStatus(prNum, sha, "build", "pending", logfile);
var build = spawn("docker", [
"build",
"-t",
`${config.owner}/${config.repo}:${prNum}`,
"--build-arg",
`pr_num=${prNum}`,
"--build-arg",
`sha=${sha}`,
"--target=v8-build",
".",
], {
env: { ...process.env, DOCKER_BUILDKIT: 1 }
});
build.stdout.pipe(logStream);
build.stderr.pipe(logStream);
build.on("close", function (code) {
if (code != 0) {
sendStatus(prNum, sha, "build", "failure", logfile);
resolve(code);
} else {
sendStatus(prNum, sha, "build", "success", logfile);
resolve(0);
}
});
});
}
function runRun(prNum, sha) {
return new Promise((resolve, reject) => {
let timestamp = new Date().toISOString();
console.log(`Run PR #${prNum}`);
logfile = `${prNum}-run-${timestamp}.log`;
logStream = fs.createWriteStream(`./logs/${logfile}`);
sendStatus(prNum, sha, "run", "pending", logfile);
var run = spawn("docker", [
"build",
"-t",
`${config.owner}/${config.repo}:${prNum}`,
"--build-arg",
`pr_num=${prNum}`,
"--build-arg",
`sha=${sha}`,
".",
], {
env: { ...process.env, DOCKER_BUILDKIT: 1 }
});
run.stdout.pipe(logStream);
run.stderr.pipe(logStream);
run.on("close", function (code) {
if (code != 0) {
sendStatus(prNum, sha, "run", "failure", logfile);
resolve(code);
} else {
sendStatus(prNum, sha, "run", "success", logfile);
resolve(0);
}
});
});
}
function runAndReportStatus(prNum, sha) {
runPrecheck(prNum, sha).then(code => {
return runBuild(prNum, sha);
}).then(code => {
if (code == 0) {
return runRun(prNum, sha);
}
}).then(code => {
if (code == 0) {
// On success, delete the docker containers/images
cleanupDocker(`${config.owner}/${config.repo}:${prNum}`);
}
});
}
async function buildAndRelease(sha) {
let timestamp = new Date().toISOString();
var logfile = `${timestamp}-${sha}-release.log`;
var logStream = fs.createWriteStream(`./logs/${logfile}`);
console.log("Generate release");
var build = spawn("docker", [
"build",
"-f",
"Dockerfile.release",
"-t",
`${config.owner}/${config.repo}:RELEASE`,
".",
]);
build.stdout.pipe(logStream);
build.stderr.pipe(logStream);
build.on("close", async function (code) {
if (code != 0) {
console.log(" Release failed. Review logs.");
} else {
console.log(" Release build successful!");
execSync(`bash release.sh ${sha}`);
console.log(" Created RPM");
// Delete the LATEST release (if it exists)
try {
let latest = await octokit.repos.getReleaseByTag({
owner: config.owner,
repo: config.repo,
tag: "LATEST",
});
await octokit.repos.deleteRelease({
owner: config.owner,
repo: config.repo,
release_id: latest.data.id,
});
console.log(" Deleted old release");
} catch (err) {
console.log(" Error deleting old release:", err);
}
// Create the new release
var release;
try {
release = await octokit.repos.createRelease({
owner: config.owner,
repo: config.repo,
tag_name: "LATEST",
name: "LATEST",
target_commitish: sha,
});
} catch (err) {
console.log(" Error releasing build:", err);
return;
}
console.log(" Created release");
// Upload the RPM to the release
let filename = fs.readFileSync("rpm-file.txt", "utf8");
let name = path.basename(filename);
try {
let response = await octokit.repos.uploadReleaseAsset({
owner: config.owner,
repo: config.repo,
release_id: release.data.id,
name: name,
data: fs.readFileSync(filename),
url: release.data.upload_url,
headers: {
"content-type": "application/tar+gzip",
},
});
} catch (err) {
console.log(" Error uploading asset:", err);
}
console.log(" Uploaded asset");
}
});
}
async function isMember(org, user) {
try {
await octokit.orgs.checkMembershipForUser({ org: org, username: user });
} catch {
return false;
}
return true;
}
async function sendStatus(prNum, sha, context, state, logfile) {
console.log(`Send ${state} for ${context} to PR #${prNum}`);
octokit.repos.createCommitStatus({
owner: config.owner,
repo: config.repo,
sha: sha,
state: state,
target_url: `${process.env.BASE_URL}/logs/${logfile}`,
context: context,
});
}
async function handlePullRequest(payload) {
let member = await isMember(config.memberGroup, payload.pull_request.user.login);
if (
payload.repository.full_name != `${config.owner}/${config.repo}` ||
!(
payload.action == "opened" ||
payload.action == "edited" ||
payload.action == "synchronize" ||
payload.action == "closed"
) ||
!member
) {
console.log(
`Ignoring PR #${payload.number} ${payload.action} by ${payload.pull_request.user.login}`
);
return;
}
if (payload.action == "closed") {
cleanupDocker(`${payload.repository.full_name}:${payload.number}`);
return;
}
console.log(`Testing ${payload.repository.full_name} PR #${payload.number}`);
runAndReportStatus(payload.number, payload.pull_request.head.sha);
}
async function handlePullRequestReview(payload) {
let reviewerIsMember = await isMember(
config.memberGroup,
payload.review.user.login
);
let ownerIsMember = await isMember(
config.memberGroup,
payload.pull_request.user.login
);
if (
payload.repository.full_name != `${config.owner}/${config.repo}` ||
payload.review.state != "approved" ||
// If the reviewer is not approved
!reviewerIsMember ||
// If the PR user is approved (already tested)
ownerIsMember
) {
console.log(
`Ignoring ${payload.repository.full_name} PR #${payload.pull_request.number}: ${payload.review.user.login} ${payload.review.state}`
);
return;
}
console.log(
`Testing ${payload.repository.full_name} PR #${payload.pull_request.number}`
);
runAndReportStatus(
payload.pull_request.number,
payload.pull_request.head.sha
);
}
async function handlePush(payload) {
if (
payload.repository.full_name != `${config.owner}/${config.repo}` ||
payload.ref != "refs/heads/riscv64"
) {
console.log(
`Ignoring push to ${payload.repository.full_name}:${payload.ref}`
);
return;
}
console.log(
`Releasing ${payload.repository.full_name} SHA #${payload.after}`
);
buildAndRelease(payload.after);
}
async function handleComment(payload) {
let commenterIsMember = await isMember(
config.memberGroup,
payload.sender.login
);
if (
payload.repository.full_name != `${config.owner}/${config.repo}` ||
!commenterIsMember ||
!payload.comment.body.startsWith("/retest") ||
!payload.issue.pull_request
) {
console.log(`Ignoring comment on ${payload.issue.number}`);
return;
}
console.log(
`Re-testing ${payload.repository.full_name} PR #${payload.issue.number}`
);
try {
let pr = await octokit.pulls.get({
owner: config.owner,
repo: config.repo,
pull_number: payload.issue.number,
});
if (payload.comment.body == "/retest-precheck") {
runPrecheck(payload.issue.number, pr.data.head.sha);
} else if (payload.comment.body == "/retest-build") {
runBuild(payload.issue.number, pr.data.head.sha);
} else if (payload.comment.body == "/retest-run") {
runRun(payload.issue.number, pr.data.head.sha);
} else {
runAndReportStatus(payload.issue.number, pr.data.head.sha);
}
} catch (err) {
console.log(" Error re-starting tests:", err);
}
}
function cleanupDocker(tag) {
try {
execSync(`docker rm $(docker ps -a -q --filter ancestor=${tag})`);
execSync(`docker rmi ${tag}`);
} catch {
console.log(`TODO: better housekeeping`);
}
}
app.use("/hooks", webhooks.middleware);
app.use("/logs", express.static("logs"));
const server = app.listen(8000);