Ungültiges Forgejo-Token automatisch erneuern
This commit is contained in:
1 parent
712acdf2e5
commit
1062890099
2 files changed
+6
-5
No files matched your search
@@ -56,18 +56,18 @@ export async function forgejoUp() {
|
|||||||
"--password", FORGEJO_PASSWORD, "--email", "renovate@localhost", "--must-change-password=false");
|
"--password", FORGEJO_PASSWORD, "--email", "renovate@localhost", "--must-change-password=false");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Token wiederverwenden, solange Forgejo es kennt (nach Neuaufsetzen von Forgejo ist es ungültig)
|
||||||
const tokenFile = path.join(STATE_DIR, "token");
|
const tokenFile = path.join(STATE_DIR, "token");
|
||||||
if (!fs.existsSync(tokenFile) || !fs.readFileSync(tokenFile, "utf8").trim()) {
|
let token = fs.existsSync(tokenFile) ? fs.readFileSync(tokenFile, "utf8").trim() : "";
|
||||||
|
if (!token || !(await fetch(`${FORGEJO_URL}/api/v1/user`, { headers: { Authorization: `token ${token}` } })).ok) {
|
||||||
log("Erzeuge Access-Token");
|
log("Erzeuge Access-Token");
|
||||||
const token = forgejoCli("admin", "user", "generate-access-token", "--username", FORGEJO_USER,
|
token = forgejoCli("admin", "user", "generate-access-token", "--username", FORGEJO_USER,
|
||||||
"--token-name", `renovate-${Date.now()}`, "--scopes", "all", "--raw").trim();
|
"--token-name", `renovate-${Date.now()}`, "--scopes", "all", "--raw").trim();
|
||||||
fs.writeFileSync(tokenFile, token);
|
fs.writeFileSync(tokenFile, token);
|
||||||
}
|
}
|
||||||
const token = fs.readFileSync(tokenFile, "utf8").trim();
|
|
||||||
const headers = { Authorization: `token ${token}`, "Content-Type": "application/json" };
|
const headers = { Authorization: `token ${token}`, "Content-Type": "application/json" };
|
||||||
|
|
||||||
const repoRes = await fetch(`${FORGEJO_URL}/api/v1/repos/${FORGEJO_USER}/${FORGEJO_REPO}`, { headers });
|
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) {
|
if (!repoRes.ok) {
|
||||||
log(`Lege Repository ${FORGEJO_USER}/${FORGEJO_REPO} an`);
|
log(`Lege Repository ${FORGEJO_USER}/${FORGEJO_REPO} an`);
|
||||||
const res = await fetch(`${FORGEJO_URL}/api/v1/user/repos`, {
|
const res = await fetch(`${FORGEJO_URL}/api/v1/user/repos`, {
|
||||||
|
|||||||
+2
-1
@@ -64,6 +64,7 @@ export function containerEngine() {
|
|||||||
export function main(fn) {
|
export function main(fn) {
|
||||||
fn().catch((err) => {
|
fn().catch((err) => {
|
||||||
console.error(`\x1b[1;31mFehler:\x1b[0m ${err.message}`);
|
console.error(`\x1b[1;31mFehler:\x1b[0m ${err.message}`);
|
||||||
process.exit(1);
|
// exitCode statt process.exit(): verhindert unter Windows einen libuv-Absturz bei offenen fetch-Handles
|
||||||
|
process.exitCode = 1;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user