2021-05-11 16:30:13 +00:00
|
|
|
const cp = require('child_process')
|
|
|
|
const fetch = require('node-fetch')
|
2021-05-07 16:36:52 +00:00
|
|
|
|
2021-05-11 16:30:13 +00:00
|
|
|
const checks = require('./checks')
|
2021-05-07 16:36:52 +00:00
|
|
|
|
2021-05-11 16:30:13 +00:00
|
|
|
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
|
2021-05-07 16:36:52 +00:00
|
|
|
|
2021-05-11 16:30:13 +00:00
|
|
|
function getCurrentCommitSha () {
|
2021-05-07 16:36:52 +00:00
|
|
|
return cp
|
2021-05-11 16:30:13 +00:00
|
|
|
.execSync('git rev-parse HEAD')
|
2021-05-07 16:36:52 +00:00
|
|
|
.toString()
|
2021-05-11 16:30:13 +00:00
|
|
|
.trim()
|
2021-05-07 16:36:52 +00:00
|
|
|
}
|
|
|
|
// The SHA provied by GITHUB_SHA is the merge (PR) commit.
|
|
|
|
// We need to get the current commit sha ourself.
|
2021-05-11 16:30:13 +00:00
|
|
|
const sha = getCurrentCommitSha()
|
2021-05-07 16:36:52 +00:00
|
|
|
|
2021-05-11 16:30:13 +00:00
|
|
|
async function setStatus (context, state, description) {
|
2021-05-07 16:36:52 +00:00
|
|
|
return fetch(`https://api.github.com/repos/${owner}/${repo}/statuses/${sha}`, {
|
|
|
|
method: 'POST',
|
|
|
|
body: JSON.stringify({
|
|
|
|
state,
|
|
|
|
description,
|
2021-05-11 16:30:13 +00:00
|
|
|
context
|
2021-05-07 16:36:52 +00:00
|
|
|
}),
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
|
2021-05-11 16:30:13 +00:00
|
|
|
'Content-Type': 'application/json'
|
|
|
|
}
|
|
|
|
})
|
2021-05-07 16:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2021-05-11 16:30:13 +00:00
|
|
|
console.log(`Starting status checks for commit ${sha}`)
|
2021-05-07 16:36:52 +00:00
|
|
|
|
|
|
|
// Run in parallel
|
|
|
|
await Promise.all(
|
|
|
|
checks.map(async check => {
|
2021-05-11 16:30:13 +00:00
|
|
|
const { name, callback } = check
|
2021-05-07 16:36:52 +00:00
|
|
|
|
2021-05-11 16:30:13 +00:00
|
|
|
await setStatus(name, 'pending', 'Running check..')
|
2021-05-07 16:36:52 +00:00
|
|
|
|
|
|
|
try {
|
2021-05-11 16:30:13 +00:00
|
|
|
const response = await callback()
|
|
|
|
await setStatus(name, 'success', response)
|
2021-05-07 16:36:52 +00:00
|
|
|
} catch (err) {
|
2021-05-11 16:30:13 +00:00
|
|
|
const message = err ? err.message : 'Something went wrong'
|
|
|
|
await setStatus(name, 'failure', message)
|
2021-05-07 16:36:52 +00:00
|
|
|
}
|
2021-05-11 16:30:13 +00:00
|
|
|
})
|
|
|
|
)
|
2021-05-07 16:36:52 +00:00
|
|
|
|
2021-05-11 16:30:13 +00:00
|
|
|
console.log('Finished status checks')
|
|
|
|
})()
|