79 lines
2.7 KiB
JavaScript
79 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
|
// Upserts a sticky PR comment on Gitea identified by COMMENT_MARKER
|
|
// (default: `<!-- coverage-report -->`). Reads coverage-comment.md from the
|
|
// workflow's cwd and updates an existing comment if found, otherwise
|
|
// creates a new one.
|
|
//
|
|
// Environment:
|
|
// GITEA_TOKEN required. PAT with repository write scope.
|
|
// GITEA_HOST default: git.tool-ctrl.de
|
|
// REPO "owner/repo" (provided by Gitea workflow context).
|
|
// PR_NUMBER PR number to post on.
|
|
// COMMENT_PATH optional, default: coverage-comment.md
|
|
// COMMENT_MARKER optional, default: <!-- coverage-report -->
|
|
|
|
import fs from "node:fs";
|
|
|
|
async function main() {
|
|
const token = process.env.GITEA_TOKEN;
|
|
const host = process.env.GITEA_HOST || "git.tool-ctrl.de";
|
|
const repo = process.env.REPO;
|
|
const prNumber = process.env.PR_NUMBER;
|
|
const commentPath = process.env.COMMENT_PATH || "coverage-comment.md";
|
|
const marker = process.env.COMMENT_MARKER || "<!-- coverage-report -->";
|
|
|
|
if (!token || !repo || !prNumber) {
|
|
console.error("post-coverage-gitea-comment: missing GITEA_TOKEN / REPO / PR_NUMBER env vars");
|
|
process.exit(0); // soft-skip: workflow stays green
|
|
}
|
|
if (!fs.existsSync(commentPath)) {
|
|
console.error(`post-coverage-gitea-comment: ${commentPath} not found, skipping`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const body = fs.readFileSync(commentPath, "utf8");
|
|
const apiBase = `https://${host}/api/v1/repos/${repo}/issues/${prNumber}/comments`;
|
|
const headers = {
|
|
Authorization: `token ${token}`,
|
|
"Content-Type": "application/json",
|
|
Accept: "application/json",
|
|
};
|
|
|
|
const listRes = await fetch(apiBase, { headers });
|
|
if (!listRes.ok) {
|
|
console.error(`Gitea list-comments failed: ${listRes.status}`);
|
|
process.exit(0);
|
|
}
|
|
const comments = await listRes.json();
|
|
const existing = Array.isArray(comments) ? comments.find((c) => typeof c.body === "string" && c.body.includes(marker)) : null;
|
|
|
|
if (existing) {
|
|
const updateRes = await fetch(`https://${host}/api/v1/repos/${repo}/issues/comments/${existing.id}`, {
|
|
method: "PATCH",
|
|
headers,
|
|
body: JSON.stringify({ body }),
|
|
});
|
|
if (!updateRes.ok) {
|
|
console.error(`Gitea update-comment failed: ${updateRes.status}`);
|
|
process.exit(0);
|
|
}
|
|
console.log(`coverage comment updated (id=${existing.id})`);
|
|
} else {
|
|
const createRes = await fetch(apiBase, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify({ body }),
|
|
});
|
|
if (!createRes.ok) {
|
|
console.error(`Gitea create-comment failed: ${createRes.status}`);
|
|
process.exit(0);
|
|
}
|
|
console.log("coverage comment created");
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exit(0);
|
|
});
|