This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
296 lines (245 loc) · 7.91 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
const { createAppAuth } = require("@octokit/auth-app")
const assert = require("assert")
const fs = require("fs")
const shell = require("shelljs")
var { benchmarkRuntime, benchRustup } = require("./bench")
const githubCommentLimitLength = 65536
const githubCommentLimitTruncateMessage = "<truncated>..."
let isTerminating = false
let appFatalLogger = undefined
for (const event of ["uncaughtException", "unhandledRejection"]) {
process.on(event, function (error, origin) {
if (isTerminating) {
return
}
isTerminating = true
try {
if (appFatalLogger) {
appFatalLogger({ event, error, origin })
}
} catch (error) {
console.error({ level: "error", event, error, origin, exception })
}
process.exit(1)
})
}
module.exports = (app) => {
if (process.env.DEBUG) {
app.log("Running in debug mode")
}
appFatalLogger = app.log.fatal
const baseBranch = process.env.BASE_BRANCH || "master"
app.log.debug(`base branch: ${baseBranch}`)
const appId = parseInt(process.env.APP_ID)
assert(appId)
const installationId = parseInt(process.env.INSTALLATION_ID);
assert(installationId);
const clientId = process.env.CLIENT_ID
assert(clientId)
const clientSecret = process.env.CLIENT_SECRET
assert(clientSecret)
const privateKeyPath = process.env.PRIVATE_KEY_PATH
assert(privateKeyPath)
const privateKey = fs.readFileSync(privateKeyPath).toString()
assert(privateKey)
const bbRepo = process.env.BB_REPO;
assert(bbRepo)
const bbRepoOwner = process.env.BB_REPO_OWNER;
assert(bbRepoOwner)
const bbAppId = parseInt(process.env.BB_APP_ID)
assert(bbAppId)
const bbInstallationId = parseInt(process.env.BB_INSTALLATION_ID)
assert(bbInstallationId)
const bbClientId = process.env.BB_CLIENT_ID
assert(bbClientId)
const bbClientSecret = process.env.BB_CLIENT_SECRET
assert(bbClientSecret)
const bbPrivateKeyPath = process.env.BB_PRIVATE_KEY_PATH
assert(bbPrivateKeyPath)
const bbPrivateKey = fs.readFileSync(bbPrivateKeyPath).toString()
assert(bbPrivateKey)
const authInstallation = createAppAuth({
appId,
privateKey,
clientId,
clientSecret,
})
const bbAuthInstallation = createAppAuth({
appId: bbAppId,
privateKey: bbPrivateKey,
clientId: bbClientId,
clientSecret: bbClientSecret,
});
app.on("issue_comment", async (context) => {
let commentText = context.payload.comment.body
const triggerCommand = "/bench"
if (
!context.payload.issue.hasOwnProperty("pull_request") ||
context.payload.action !== "created" ||
!commentText.startsWith(triggerCommand)
) {
return
}
try {
const sourceInstallationId = (context.payload.installation || {}).id
if (!sourceInstallationId) {
await context.octokit.issues.createComment(
context.issue({
body: `Error: Installation id was missing from webhook payload`,
}),
)
app.log.error("Installation id was missing from webhook payload");
return
} else if (sourceInstallationId != installationId) {
console.log(`Warning: ignoring payload from irrelevant installation ${sourceInstallationId}`);
return;
}
const getPushDomain = async function () {
const token = (
await authInstallation({ type: "installation", installationId })
).token
const url = `https://x-access-token:${token}@github.com`
return { url, token }
}
const getBBPushDomain = async function () {
const token = (
await bbAuthInstallation({ type: "installation", installationId: bbInstallationId })
).token
const url = `https://x-access-token:${token}@github.com`
return { url, token }
}
const repo = context.payload.repository.name
const owner = context.payload.repository.owner.login
const pull_number = context.payload.issue.number
// Capture `<action>` in `/bench <action> <extra>`
let [action, ...extra] = commentText.slice(triggerCommand.length).trim().split(" ")
extra = extra.join(" ").trim()
let pr = await context.octokit.pulls.get({ owner, repo, pull_number })
const contributor = pr.data.head.user.login
const branch = pr.data.head.ref
app.log.debug(`branch: ${branch}`)
var { stdout: toolchain, code: toolchainError } = shell.exec(
"rustup show active-toolchain --verbose",
{ silent: false },
)
if (toolchainError) {
await context.octokit.issues.createComment(
context.issue({
body: "ERROR: Failed to query the currently active Rust toolchain",
}),
)
app.log.fatal("ERROR: Failed to query the currently active Rust toolchain");
return
} else {
toolchain = toolchain.trim()
}
// generate a unique branch for our PR
const bbBranch = `${branch}-benchbot-job-${new Date().getTime()}`;
const initialInfo = `Starting benchmark for branch: ${branch} (vs ${baseBranch})\nPR branch will be ${bbBranch}\n\nToolchain: \n${toolchain}\n\n Comment will be updated.`
let comment_id = undefined
app.log(initialInfo)
const issueComment = context.issue({ body: initialInfo })
const issue_comment = await context.octokit.issues.createComment(
issueComment,
)
comment_id = issue_comment.data.id
let config = {
owner,
contributor,
repo,
bbRepo,
bbRepoOwner,
bbBranch,
branch,
baseBranch,
id: action,
extra,
getPushDomain,
getBBPushDomain,
}
// kick off the build/run process...
let report
if (action == "runtime" || action == "xcm") {
report = await benchmarkRuntime(app, config, context.octokit)
} else if (action == "rustup") {
report = await benchRustup(app, config)
} else {
report = {
isError: true,
message: "Unsupported action",
error: `unsupported action: ${action}`,
};
}
if (process.env.DEBUG) {
console.log(report)
return
}
if (report.isError) {
app.log.error(report.message)
if (report.error) {
app.log.error(report.error)
}
const output = `${report.message}${report.error ? `: ${report.error.toString()}` : ""
}`
/*
await context.octokit.issues.updateComment({
owner,
repo,
comment_id,
body: `Error running benchmark: **${branch}**\n\n<details><summary>stdout</summary>${output}</details>`,
})
*/
return
}
let { title, output, extraInfo, benchCommand } = report
const bodyPrefix = `
Benchmark **${title}** for branch "${branch}" with command ${benchCommand}
Toolchain: ${toolchain}
<details>
<summary>Results</summary>
\`\`\`
`.trim()
const bodySuffix = `
\`\`\`
</details>
`.trim()
const padding = 16
const formattingLength =
bodyPrefix.length + bodySuffix.length + extraInfo.length + padding
const length = formattingLength + output.length
if (length >= githubCommentLimitLength) {
output = `${output.slice(
0,
githubCommentLimitLength -
(githubCommentLimitTruncateMessage.length + formattingLength),
)}${githubCommentLimitTruncateMessage}`
}
const body = `
${bodyPrefix}
${output}
${bodySuffix}
${extraInfo}
`.trim()
await context.octokit.issues.updateComment({
owner,
repo,
comment_id,
body,
})
} catch (error) {
console.log(error);
app.log.fatal({
error,
repo,
owner,
pull_number,
msg: "Caught exception in issue_comment's handler",
})
await context.octokit.issues.createComment(
context.issue({
body: `Exception caught: \`${error.message}\`\n${error.stack}`,
}),
)
}
})
}