All checks were successful
Deploy / deploy (pull_request) Successful in 7s
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
const token = process.env.GITHUB_TOKEN;
|
|
const server = process.env.GITHUB_SERVER_URL;
|
|
const repo = process.env.GITHUB_REPOSITORY;
|
|
const pr = process.env.PR_NUMBER;
|
|
const sha = process.env.GITHUB_SHA;
|
|
const marker = process.env.MARKER ?? "<!-- amalgolopoly-preview -->";
|
|
const shortSha = sha ? sha.slice(0, 7) : null;
|
|
const body =
|
|
process.env.BODY ??
|
|
[
|
|
marker,
|
|
"### Preview deployment",
|
|
`https://amalgolopoly.lschaefer.xyz/pr-${pr}/`,
|
|
shortSha
|
|
? `\nDeployed commit: [\`${shortSha}\`](${server}/${repo}/commit/${sha})`
|
|
: "",
|
|
]
|
|
.filter(Boolean)
|
|
.join("\n");
|
|
|
|
if (!token || !server || !repo || !pr) {
|
|
console.error(
|
|
"GITHUB_TOKEN, GITHUB_SERVER_URL, GITHUB_REPOSITORY, and PR_NUMBER are required",
|
|
);
|
|
process.exit(1);
|
|
}
|
|
|
|
const api = `${server}/api/v1/repos/${repo}`;
|
|
const headers = {
|
|
Authorization: `token ${token}`,
|
|
"Content-Type": "application/json",
|
|
};
|
|
|
|
async function req(method, url, data) {
|
|
const res = await fetch(url, { method, headers, body: data });
|
|
if (!res.ok) {
|
|
throw new Error(`${method} ${url}: ${res.status} ${await res.text()}`);
|
|
}
|
|
return res.status === 204 ? null : res.json();
|
|
}
|
|
|
|
const comments = await req("GET", `${api}/issues/${pr}/comments?limit=100`);
|
|
const existing = comments.find((c) => (c.body || "").includes(marker));
|
|
const payload = JSON.stringify({ body });
|
|
|
|
if (existing) {
|
|
await req("PATCH", `${api}/issues/comments/${existing.id}`, payload);
|
|
console.log(`Updated preview comment ${existing.id}`);
|
|
} else {
|
|
await req("POST", `${api}/issues/${pr}/comments`, payload);
|
|
console.log("Created preview comment");
|
|
}
|