Files
renovate-test/scripts/forgejo-up.mjs
T

93 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Startet ein lokales Forgejo (Git-Server) in Podman/Docker, legt Bot-User, Token und
// Repository an und pusht den aktuellen Stand des lokalen Repos dorthin.
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import {
STATE_DIR, FORGEJO_IMAGE, FORGEJO_CONTAINER, FORGEJO_VOLUME, FORGEJO_PORT, FORGEJO_URL,
FORGEJO_USER, FORGEJO_PASSWORD, FORGEJO_REPO, GIT_REMOTE, log, run, containerEngine, main,
} from "./lib.mjs";
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function healthy() {
try {
return (await fetch(`${FORGEJO_URL}/api/healthz`)).ok;
} catch {
return false;
}
}
export async function forgejoUp() {
fs.mkdirSync(STATE_DIR, { recursive: true });
const engine = containerEngine();
const exists = !run(engine, ["container", "inspect", FORGEJO_CONTAINER], { capture: true, allowFail: true }).status;
if (!exists) {
log(`Starte Forgejo (${FORGEJO_IMAGE}) auf ${FORGEJO_URL} via ${engine}`);
run(engine, [
"run", "-d", "--name", FORGEJO_CONTAINER,
"-p", `${FORGEJO_PORT}:3000`,
"-v", `${FORGEJO_VOLUME}:/data`,
"-e", "USER_UID=1000", "-e", "USER_GID=1000",
"-e", "FORGEJO__security__INSTALL_LOCK=true",
"-e", `FORGEJO__server__ROOT_URL=${FORGEJO_URL}/`,
"-e", "FORGEJO__server__HTTP_PORT=3000",
"-e", "FORGEJO__database__DB_TYPE=sqlite3",
"-e", "FORGEJO__service__DISABLE_REGISTRATION=true",
"-e", "FORGEJO__repository__DEFAULT_BRANCH=main",
FORGEJO_IMAGE,
], { capture: true });
} else {
run(engine, ["start", FORGEJO_CONTAINER], { capture: true });
}
log("Warte auf Forgejo …");
for (let i = 0; i < 60 && !(await healthy()); i++) await sleep(1000);
if (!(await healthy())) throw new Error(`Forgejo unter ${FORGEJO_URL} nicht erreichbar`);
const forgejoCli = (...args) =>
run(engine, ["exec", "-u", "git", FORGEJO_CONTAINER, "forgejo", ...args], { capture: true }).stdout;
const users = forgejoCli("admin", "user", "list").split("\n").map((l) => l.trim().split(/\s+/)[1]);
if (!users.includes(FORGEJO_USER)) {
log(`Lege Admin-/Bot-User '${FORGEJO_USER}' an`);
forgejoCli("admin", "user", "create", "--admin", "--username", FORGEJO_USER,
"--password", FORGEJO_PASSWORD, "--email", "renovate@localhost", "--must-change-password=false");
}
const tokenFile = path.join(STATE_DIR, "token");
if (!fs.existsSync(tokenFile) || !fs.readFileSync(tokenFile, "utf8").trim()) {
log("Erzeuge Access-Token");
const token = forgejoCli("admin", "user", "generate-access-token", "--username", FORGEJO_USER,
"--token-name", `renovate-${Date.now()}`, "--scopes", "all", "--raw").trim();
fs.writeFileSync(tokenFile, token);
}
const token = fs.readFileSync(tokenFile, "utf8").trim();
const headers = { Authorization: `token ${token}`, "Content-Type": "application/json" };
const repoRes = await fetch(`${FORGEJO_URL}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}`, { headers });
if (repoRes.status === 401) throw new Error(`Token ungültig – ${tokenFile} löschen und erneut starten`);
if (!repoRes.ok) {
log(`Lege Repository ${FORGEJO_USER}/${FORGEJO_REPO} an`);
const res = await fetch(`${FORGEJO_URL}/api/v1/user/repos`, {
method: "POST",
headers,
body: JSON.stringify({ name: FORGEJO_REPO, default_branch: "main", private: false }),
});
if (!res.ok) throw new Error(`Repository anlegen fehlgeschlagen: ${res.status} ${await res.text()}`);
}
const remoteUrl = `http://${FORGEJO_USER}:${token}@localhost:${FORGEJO_PORT}/${FORGEJO_USER}/${FORGEJO_REPO}.git`;
const hasRemote = !run("git", ["remote", "get-url", GIT_REMOTE], { capture: true, allowFail: true }).status;
run("git", ["remote", hasRemote ? "set-url" : "add", GIT_REMOTE, remoteUrl]);
log("Pushe lokalen Branch 'main' nach Forgejo");
run("git", ["push", "-q", GIT_REMOTE, "main:main"]);
log(`Forgejo läuft: ${FORGEJO_URL} (Login: ${FORGEJO_USER} / ${FORGEJO_PASSWORD})`);
return token;
}
if (import.meta.url === pathToFileURL(process.argv[1]).href) main(forgejoUp);