This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
282 lines (241 loc) · 7.87 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
const { createAppAuth } = require("@octokit/auth-app")
const assert = require("assert")
const fs = require("fs")
const shell = require("shelljs")
const Mutex = require("async-mutex").Mutex
var { benchBranch, benchmarkRuntime, benchRustup } = require("./bench")
const githubCommentLimitLength = 65536
const githubCommentLimitTruncateMessage = "<truncated>..."
let isTerminating = false
let logFatal = undefined
let pendingPayloadCount = 0
for (const event of ["uncaughtException", "unhandledRejection"]) {
process.on(event, function (error, origin) {
if (isTerminating) {
return
}
isTerminating = true
try {
if (logFatal) {
logFatal({ event, error, origin })
}
} catch (error) {
console.error({ level: "error", event, error, origin, exception })
}
process.exit(1)
})
}
const mutex = new Mutex()
module.exports = (app) => {
if (process.env.DEBUG) {
app.log("Running in debug mode")
}
// Crash the server on Probot failures or errors
// We retain the original error handlers on logError and logFatal so that the
// application can still report errors on the expected channels
// This is necessary to work around problems in reconnection issues from our
// event source
// (https://github.com/paritytech/bench-bot/issues/83#issuecomment-1024283664)
// FIXME: This is suboptimal and we should not have to stop the application in
// case of errors
// The server will automatically restarted upon exit in ./run
let isWaitingForExitingWhenFree = false
const exitWhenFree = async function() {
const isFree = await mutex.runExclusive(function () {
return pendingPayloadCount === 0
})
if (isFree) {
process.exit(1)
} else {
await exitWhenFree()
}
}
const logThenExit = function(log) {
return async function(...args) {
log(...args)
// only exit the application after the current events have been processed
// we only need to register this action once since exitWhenFree calls itself
if (isWaitingForExitingWhenFree) {
return
}
isWaitingForExitingWhenFree = true
await exitWhenFree()
}
}
const logError = app.log.error
app.log.error = logThenExit(logError)
logFatal = app.log.fatal
app.log.fatal = logThenExit(logFatal)
const baseBranch = process.env.BASE_BRANCH || "master"
app.log.debug(`base branch: ${baseBranch}`)
const appId = parseInt(process.env.APP_ID)
assert(appId)
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 authInstallation = createAppAuth({
appId,
privateKey,
clientId,
clientSecret,
})
app.on("issue_comment", async (context) => {
pendingPayloadCount++
await mutex.runExclusive(async function() {
let commentText = context.payload.comment.body
if (
!context.payload.issue.hasOwnProperty("pull_request") ||
context.payload.action !== "created" ||
!commentText.startsWith("/bench")
) {
return
}
try {
const installationId = (context.payload.installation || {}).id
if (!installationId) {
await context.octokit.issues.createComment(
context.issue({
body: `Error: Installation id was missing from webhook payload`,
}),
)
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 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 = commentText.split(" ").splice(1, 1).join(" ").trim()
// Capture all `<extra>` text in `/bench <action> <extra>`
let extra = commentText.split(" ").splice(2).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",
}),
)
return
} else {
toolchain = toolchain.trim()
}
const initialInfo = `Starting benchmark for branch: ${branch} (vs ${baseBranch})\n\nToolchain: \n${toolchain}\n\n Comment will be updated.`
let comment_id = undefined
if (process.env.DEBUG) {
app.log(initialInfo)
} else {
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,
branch,
baseBranch,
id: action,
extra,
getPushDomain,
logFatal
}
let report
if (action == "runtime" || action == "xcm") {
report = await benchmarkRuntime(app, config)
} else if (action == "rustup") {
report = await benchRustup(app, config)
} else {
report = await benchBranch(app, config)
}
if (process.env.DEBUG) {
console.log(report)
return
}
if (report.isError) {
logError(report.message)
if (report.error) {
logError(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) {
logFatal({
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}`,
}),
)
}
})
pendingPayloadCount--
})
}