dsa-connectors/status-checks/index.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-05-11 16:30:13 +00:00
const cp = require('child_process')
const fetch = require('node-fetch')
2021-05-11 16:30:13 +00:00
const checks = require('./checks')
2021-05-11 16:30:13 +00:00
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
2021-05-11 16:30:13 +00:00
function getCurrentCommitSha () {
return cp
2021-05-11 16:30:13 +00:00
.execSync('git rev-parse HEAD')
.toString()
2021-05-11 16:30:13 +00:00
.trim()
}
// 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-11 16:30:13 +00:00
async function setStatus (context, state, description) {
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
}),
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
2021-05-11 16:30:13 +00:00
'Content-Type': 'application/json'
}
})
}
(async () => {
2021-05-11 16:30:13 +00:00
console.log(`Starting status checks for commit ${sha}`)
// Run in parallel
await Promise.all(
checks.map(async check => {
2021-05-11 16:30:13 +00:00
const { name, callback } = check
2021-05-11 16:30:13 +00:00
await setStatus(name, 'pending', 'Running check..')
try {
2021-05-11 16:30:13 +00:00
const response = await callback()
await setStatus(name, 'success', response)
} 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-11 16:30:13 +00:00
})
)
2021-05-11 16:30:13 +00:00
console.log('Finished status checks')
})()