83 lines
2.8 KiB
JavaScript
83 lines
2.8 KiB
JavaScript
#!/usr/bin/env node
|
|
// Upserts a sticky PR comment on Gitea identified by the marker
|
|
// `<!-- bundle-budget-report -->`. Reads bundle-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: bundle-comment.md
|
|
|
|
import fs from "node:fs";
|
|
|
|
const MARKER = "<!-- bundle-budget-report -->";
|
|
|
|
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 || "bundle-comment.md";
|
|
|
|
if (!token || !repo || !prNumber) {
|
|
console.error("post-bundle-budget-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-bundle-budget-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",
|
|
};
|
|
|
|
// 1) list comments, find existing by marker
|
|
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 updateUrl = `https://${host}/api/v1/repos/${repo}/issues/comments/${existing.id}`;
|
|
const res = await fetch(updateUrl, {
|
|
method: "PATCH",
|
|
headers,
|
|
body: JSON.stringify({ body }),
|
|
});
|
|
if (!res.ok) {
|
|
console.error(`Gitea update-comment failed: ${res.status}`);
|
|
process.exit(0);
|
|
}
|
|
console.log(`Updated bundle-budget comment #${existing.id}`);
|
|
} else {
|
|
const res = await fetch(apiBase, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify({ body }),
|
|
});
|
|
if (!res.ok) {
|
|
console.error(`Gitea create-comment failed: ${res.status}`);
|
|
process.exit(0);
|
|
}
|
|
const created = await res.json();
|
|
console.log(`Created bundle-budget comment #${created.id}`);
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
// Soft-fail: PR comment is informational, never fail the workflow on transient API errors.
|
|
process.exit(0);
|
|
});
|