-
Notifications
You must be signed in to change notification settings - Fork 12
/
check-runs.js
224 lines (196 loc) · 6.09 KB
/
check-runs.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
const getOrCreateCheckRun = async (context, token, owner, repo, ref, checkRunName, title, summary, text, detailsURL) => {
const githubApiRequest = require('./github-api-request')
// is there an existing check-run we can re-use?
const { check_runs } = await githubApiRequest(
context,
token,
'GET',
`/repos/${owner}/${repo}/commits/${ref}/check-runs`
)
const filtered = check_runs
.filter(e => e.name === checkRunName && e.conclusion === null).map(e => {
return {
id: e.id,
status: e.status,
output: e.output
}
})
if (filtered.length > 0) {
// ensure that the check_run is set to status "in progress"
if (filtered[0].status !== 'in_progress') {
console.log(await githubApiRequest(
context,
token,
'PATCH',
`/repos/${owner}/${repo}/check-runs/${filtered[0].id}`, {
status: 'in_progress',
details_url: detailsURL,
output: {
...filtered[0].output,
title: title || filtered[0].output.title,
summary: summary || filtered[0].output.summary,
text: text || filtered[0].output.text
}
}
))
}
process.stderr.write(`Returning existing ${filtered[0].id}`)
return filtered[0].id
}
const { id } = await githubApiRequest(
context,
token,
'POST',
`/repos/${owner}/${repo}/check-runs`, {
name: checkRunName,
head_sha: ref,
status: 'in_progress',
details_url: detailsURL,
output: {
title,
summary,
text
}
}
)
return id
}
const updateCheckRun = async (context, token, owner, repo, checkRunId, appendText, conclusion, title, summary) => {
const githubApiRequest = require('./github-api-request')
let { output } = await githubApiRequest(
context,
token,
'GET',
`/repos/${owner}/${repo}/check-runs/${checkRunId}`
)
if (title) output.title = title
if (summary) output.summary = summary
if (appendText) output.text = [output.text, appendText].join('\n')
const statusUpdate = conclusion ? { status: 'completed', conclusion } : {}
await githubApiRequest(
context,
token,
'PATCH',
`/repos/${owner}/${repo}/check-runs/${checkRunId}`, {
output,
...statusUpdate
}
)
return output.text
}
const crypto = require('crypto')
const getPublicKey = (privateKey) => crypto.createPublicKey(privateKey)
const decryptChunk = (encrypted, privateKey) => crypto.privateDecrypt(privateKey, Buffer.from(encrypted, 'base64url')).toString('utf-8')
const decrypt = (encrypted, privateKey) => {
return JSON.parse(encrypted
.split('/')
.map(e => decryptChunk(e, privateKey))
.join(''))
}
const encryptChunk = (chunk, publicKey) => crypto.publicEncrypt(publicKey, chunk).toString('base64url')
const maxChunkLength = 200
const encrypt = (data, publicKey) => {
const json = JSON.stringify(data)
const chunked = []
let i = 0
while (i < json.length) {
const i2 = i + maxChunkLength
if (i2 < json.length) chunked.push(json.substring(i, i2))
else chunked.push(json.substring(i))
i = i2
}
return chunked
.map(e => encryptChunk(e, publicKey))
.join('/')
}
const initCheckRunState = async (context, setSecret, appId, privateKey, owner, repo, stateFile) => {
const fs = require('fs')
if (!stateFile) stateFile = `${process.env.RUNNER_TEMP || process.env.TEMP || '/tmp'}/check-run.state`
const state = fs.existsSync(stateFile)
? JSON.parse(decrypt(fs.readFileSync(stateFile).toString(), privateKey))
: {}
if (appId && typeof(appId) === 'string') appId = parseInt(appId)
if (!state.appId) {
if (!appId) throw new Error(`Need an appId`)
state.appId = appId
} else if (appId && state.appId !== appId) throw new Error(`Expected appId ${state.appId}, got ${appId}`)
if (!state.owner) {
if (!owner) throw new Error(`Need an owner`)
state.owner = owner
} else if (owner && state.owner !== owner) throw new Error(`Expected owner ${state.owner}, got ${owner}`)
if (!state.repo) {
if (!repo) throw new Error(`Need a repository`)
state.repo = repo
} else if (repo && state.repo !== repo) throw new Error(`Expected repo ${state.repo}, got ${repo}`)
state.store = () => {
fs.writeFileSync(stateFile, encrypt(JSON.stringify(state), getPublicKey(privateKey)))
}
state.refreshToken = async () => {
if (state.expiresAt
&& Date.parse(state.expiresAt) - Date.now() > 5 * 60 * 1000) return
const getAppInstallationId = require('./get-app-installation-id')
const installationId = await getAppInstallationId(
context,
state.appId,
privateKey,
state.owner,
state.repo
)
const getInstallationAccessToken = require('./get-installation-access-token')
const { expiresAt, token: accessToken } = await getInstallationAccessToken(
context,
state.appId,
privateKey,
installationId
)
if (setSecret) setSecret(accessToken)
state.expiresAt = expiresAt
state.accessToken = accessToken
state.store()
}
state.get = async (ref, checkRunName, title, summary, text, detailsURL) => {
await state.refreshToken()
state.id = await(getOrCreateCheckRun(
context,
state.accessToken,
state.owner,
state.repo,
ref,
checkRunName,
title,
summary,
text,
detailsURL
))
state.ref = ref
state.checkRunName = checkRunName
state.title = title
state.summary = summary
state.text = text
state.detailsURL = detailsURL
state.store()
}
state.update = async (appendText, conclusion, title, summary) => {
if (!state.id) throw new Error('Need to `get()` Check Run before calling `update()`')
await state.refreshToken()
state.text = await updateCheckRun(
context,
state.accessToken,
state.owner,
state.repo,
state.id,
appendText,
conclusion,
title,
summary
)
state.conclusion = conclusion
}
state.store()
return state
}
module.exports = {
getOrCreateCheckRun,
updateCheckRun,
initCheckRunState
}