All checks were successful
Deploy Indiekit Server / deploy (push) Successful in 1m15s
Gitea Contents API commits don't trigger on:push CI workflows. Patches action.js to fire a workflow_dispatch to giersig.eu/indiekit-blog after every create/update/delete/undelete so the Eleventy build runs immediately after a post is published. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
103 lines
4.0 KiB
JavaScript
103 lines
4.0 KiB
JavaScript
/**
|
|
* Patch: fire a Gitea workflow_dispatch after each Micropub create/update/delete/undelete.
|
|
*
|
|
* Gitea Contents API commits (what store-github does) do NOT trigger `on: push`
|
|
* CI workflows. This patch fires a workflow_dispatch to the blog repo after each
|
|
* successful Micropub action so the Eleventy build runs immediately.
|
|
*
|
|
* Env vars read at runtime (all have defaults):
|
|
* GITEA_BASE_URL Internal Gitea API base, e.g. http://10.100.0.90:3000/api/v1/
|
|
* GITEA_CONTENT_USER Org/user that owns the blog repo (e.g. giersig.eu)
|
|
* GITEA_CONTENT_REPO Blog repo name (e.g. indiekit-blog)
|
|
* GH_CONTENT_TOKEN Gitea PAT with Actions write permission
|
|
* GITEA_DISPATCH_WORKFLOW Workflow filename, default deploy.yml
|
|
* GITEA_DISPATCH_REF Branch to dispatch on, default main
|
|
*/
|
|
|
|
import { access, readFile, writeFile } from "node:fs/promises";
|
|
|
|
const TARGET = "node_modules/@indiekit/endpoint-micropub/lib/controllers/action.js";
|
|
const MARKER = "// [patch] micropub-gitea-dispatch";
|
|
|
|
const HELPER = `${MARKER}
|
|
async function _dispatchGiteaBuild() {
|
|
try {
|
|
const base = (process.env.GITEA_BASE_URL || "http://10.100.0.90:3000/api/v1/").replace(/\\/+$/, "");
|
|
const owner = process.env.GITEA_CONTENT_USER || "giersig.eu";
|
|
const repo = process.env.GITEA_CONTENT_REPO || "indiekit-blog";
|
|
const token = process.env.GH_CONTENT_TOKEN || process.env.GITHUB_TOKEN || "";
|
|
const workflow = process.env.GITEA_DISPATCH_WORKFLOW || "deploy.yml";
|
|
const ref = process.env.GITEA_DISPATCH_REF || "main";
|
|
if (!token) { console.warn("[micropub-gitea-dispatch] No token — skipping dispatch"); return; }
|
|
const url = \`\${base}/repos/\${owner}/\${repo}/actions/workflows/\${workflow}/dispatches\`;
|
|
const res = await fetch(url, {
|
|
method: "POST",
|
|
headers: { "Authorization": \`token \${token}\`, "Content-Type": "application/json" },
|
|
body: JSON.stringify({ ref }),
|
|
});
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(() => "");
|
|
console.warn(\`[micropub-gitea-dispatch] Dispatch failed \${res.status}: \${text}\`);
|
|
} else {
|
|
console.log(\`[micropub-gitea-dispatch] Dispatched \${workflow} on \${owner}/\${repo}@\${ref}\`);
|
|
}
|
|
} catch (err) {
|
|
console.warn("[micropub-gitea-dispatch] Dispatch error:", err.message);
|
|
}
|
|
}
|
|
`;
|
|
|
|
// Inject the dispatch call before the final response line, and only when
|
|
// content was actually written (i.e. content is defined and has a status).
|
|
const OLD_RESPONSE = ` response
|
|
.status(content.status)
|
|
.location(content.location)
|
|
.json(content.json);`;
|
|
|
|
const NEW_RESPONSE = ` _dispatchGiteaBuild().catch(() => {});
|
|
response
|
|
.status(content.status)
|
|
.location(content.location)
|
|
.json(content.json);`;
|
|
|
|
async function exists(filePath) {
|
|
try { await access(filePath); return true; } catch { return false; }
|
|
}
|
|
|
|
if (!(await exists(TARGET))) {
|
|
console.log(`[postinstall] micropub-gitea-dispatch: target not found (${TARGET}), skipping`);
|
|
process.exit(0);
|
|
}
|
|
|
|
const source = await readFile(TARGET, "utf8");
|
|
|
|
if (source.includes(MARKER)) {
|
|
console.log("[postinstall] micropub-gitea-dispatch: already patched");
|
|
process.exit(0);
|
|
}
|
|
|
|
if (!source.includes(OLD_RESPONSE)) {
|
|
console.warn("[postinstall] micropub-gitea-dispatch: response snippet not found — skipping");
|
|
process.exit(0);
|
|
}
|
|
|
|
// Insert helper block after the last import statement
|
|
const allImportMatches = [...source.matchAll(/^import\s/gm)];
|
|
let insertAt = 0;
|
|
if (allImportMatches.length > 0) {
|
|
const lastImportStart = allImportMatches.at(-1).index;
|
|
const afterLastImport = source.slice(lastImportStart);
|
|
const fromMatch = afterLastImport.match(/from\s+["'][^"']+["']\s*;\s*\n/);
|
|
if (fromMatch) {
|
|
insertAt = lastImportStart + fromMatch.index + fromMatch[0].length;
|
|
}
|
|
}
|
|
|
|
const updated =
|
|
source.slice(0, insertAt) +
|
|
"\n" + HELPER + "\n" +
|
|
source.slice(insertAt).replace(OLD_RESPONSE, NEW_RESPONSE);
|
|
|
|
await writeFile(TARGET, updated, "utf8");
|
|
console.log(`[postinstall] micropub-gitea-dispatch: patched ${TARGET}`);
|