Notify ntfy with workflow facts and test dispatch

This commit is contained in:
2026-09-21 12:14:37 -07:00
parent 4fcbd2d2d2
commit 2d56fb7f29
5 changed files with 123 additions and 8 deletions
+56
View File
@@ -0,0 +1,56 @@
const { readFileSync } = require('node:fs');
const { NTFY_TOPIC_URL, RUN_URL, STEP_OUTCOME, TEST_NOTIFICATION, WORKFLOW_KIND } = process.env;
const labels = {
mirror: ['Mirror sync', 'mirror-facts.json', [
['Organizations checked', 'orgsChecked'],
['GitHub repositories found', 'reposFound'],
['Organizations created', 'orgsCreated'],
['Org visibility changes', 'orgVisibilityChanged'],
['Org avatars updated', 'avatarsUpdated'],
['New mirrors', 'mirrorsCreated'],
['Repo visibility changes', 'repoVisibilityChanged'],
['Failures', 'failures']
]],
rename: ['Missing-mirror check', 'rename-facts.json', [
['Gitea organizations checked', 'orgsChecked'],
['GitHub mirrors found', 'mirrorsFound'],
['Renamed mirrors', 'renamed'],
['Organizations skipped', 'skippedOrgs'],
['Failures', 'failures']
]]
};
(async () => {
if (!NTFY_TOPIC_URL || !labels[WORKFLOW_KIND]) throw new Error('Notification URL or workflow kind missing');
const test = TEST_NOTIFICATION === 'true';
const [label, file, fields] = labels[WORKFLOW_KIND];
const title = test ? 'Gitea backup notification test' : `${label}: ${STEP_OUTCOME === 'success' ? 'succeeded' : 'failed'}`;
const lines = test ? ['Notification-only test. No mirror or rename ran.'] : [];
if (!test) {
try {
const facts = JSON.parse(readFileSync(file, 'utf8'));
lines.push(...fields.map(([name, key]) => `${name}: ${facts[key] ?? 0}`));
const changes = facts.changes || [];
if (changes.length) lines.push('Changes:', ...changes.slice(0, 10));
if (changes.length > 10) lines.push(`...and ${changes.length - 10} more; see run logs.`);
if (facts.fatal) lines.push('Fatal error; see run logs.');
} catch {
lines.push('No run summary available; see run logs.');
}
}
if (RUN_URL) lines.push(`Run: ${RUN_URL}`);
const res = await fetch(NTFY_TOPIC_URL, {
method: 'POST',
headers: { Priority: '2', Title: title, ...(RUN_URL && { Click: RUN_URL }) },
body: lines.join('\n'),
signal: AbortSignal.timeout(30_000)
});
if (!res.ok) throw new Error(`ntfy returned HTTP ${res.status}`);
console.log(`ntfy notification sent: ${title}`);
})().catch(error => {
console.error(`Notification failed: ${error.message}`);
process.exitCode = 1;
});