dsa-connectors/status-checks/index.ts

57 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-12-05 05:23:21 +00:00
import * as cp from "child_process";
import fetch from "node-fetch";
2021-12-05 22:34:43 +00:00
import checks from "./checks";
2021-12-05 05:23:21 +00:00
const [owner, repo] = String(process.env.GITHUB_REPOSITORY).split("/");
2021-12-05 05:23:21 +00:00
function getCurrentCommitSha() {
return cp
.execSync("git rev-parse HEAD")
.toString()
.trim();
}
// The SHA provied by GITHUB_SHA is the merge (PR) commit.
// We need to get the current commit sha ourself.
const sha = getCurrentCommitSha();
2021-12-05 23:37:37 +00:00
async function setStatus(context: any, state: string, description: string) {
2021-12-05 05:23:21 +00:00
return fetch(
`https://api.github.com/repos/${owner}/${repo}/statuses/${sha}`,
{
method: "POST",
body: JSON.stringify({
state,
description,
context,
}),
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
"Content-Type": "application/json",
},
}
);
}
(async () => {
console.log(`Starting status checks for commit ${sha}`);
// Run in parallel
await Promise.all(
2021-12-05 22:01:27 +00:00
checks.map(async (check: { name: any; callback: any }) => {
2021-12-05 05:23:21 +00:00
const { name, callback } = check;
await setStatus(name, "pending", "Running check..");
try {
const response = await callback();
await setStatus(name, "success", response);
} catch (err: any) {
2021-12-05 05:23:21 +00:00
const message = err ? err.message : "Something went wrong";
await setStatus(name, "failure", message);
}
})
);
console.log("Finished status checks");
})();