forked from paritytech/bench-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
235 lines (196 loc) · 6.01 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
const { createAppAuth } = require("@octokit/auth-app")
const assert = require("assert")
const fs = require("fs")
const shell = require("shelljs")
var { benchBranch, benchmarkRuntime } = 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 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) => {
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,
}
let report
if (action == "runtime" || action == "xcm") {
report = await benchmarkRuntime(app, config)
} else {
report = await benchBranch(app, config)
}
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}
<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) {
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}`,
}),
)
}
})
}