"use strict"; // Chooses the tags to remove from Gitea's container registry (pulizia-docker.sh, mode «completa»). It only reads the // files it is given: no network, no writes, so it is tested with a JSON file. // // node versioni-registro.js ... // node versioni-registro.js senza-etichetta ... // // Each file is a page of the answer of GET /api/v1/packages/{owner}/container/febe of Gitea 28: a list of versions with // «type», «name», «version» and «created_at» (an empty page may be «[]» or «null»). Only the versions of the container // package «febe» count, and a version found in two pages counts once. Both forms print, oldest first and one per line, // «versioncreated». // // With a number N: the sha-<7 to 40 hex digits> tags beyond the N newest by creation date. Everything else always // stays: main, latest, the X.Y.Z releases (X.Y.Z-rc.N too), the digests (sha256:…), any other tag and a sha- tag // without a date that can be read. // // With «senza-etichetta»: the untagged versions, sha256:<64 hex digits>. They are the manifests of an image index, // which Gitea keeps as versions of their own with the configuration and the layers; nothing here removes them (Gitea // cannot be asked through this API whether a tag still uses one), the cleanup only says how many there are. // // If a file is not a JSON list it exits with 2 and prints nothing: when in doubt nothing is removed. // Messages are in Italian, like every message of the CI. const fs = require("fs"); const [mode, ...pages] = process.argv.slice(2); const untagged = mode === "senza-etichetta"; if (!(untagged || /^[0-9]+$/.test(mode || "")) || pages.length === 0) { console.error("Uso: node versioni-registro.js | senza-etichetta ..."); process.exit(2); } const shaTag = /^sha-[0-9a-f]{7,40}$/; const digest = /^sha256:[0-9a-f]{64}$/; const versions = new Map(); for (const file of pages) { let page; try { page = JSON.parse(fs.readFileSync(file, "utf8").replace(/^/, "")); } catch (error) { console.error(`${file}: ${error.message}`); process.exit(2); } if (page === null) { continue; } if (!Array.isArray(page)) { console.error(`${file}: non è un elenco di versioni`); process.exit(2); } for (const version of page) { if (version && version.type === "container" && version.name === "febe" && typeof version.version === "string") { versions.set(version.version, version); } } } const dated = (version) => ({ name: version.version, created: String(version.created_at), time: Date.parse(version.created_at), id: Number(version.id) || 0, }); let chosen; if (untagged) { chosen = [...versions.values()] .filter((version) => digest.test(version.version)) .map(dated) .sort((a, b) => (Number.isFinite(a.time) ? a.time : 0) - (Number.isFinite(b.time) ? b.time : 0) || a.id - b.id); } else { chosen = [...versions.values()] .filter((version) => shaTag.test(version.version)) .map(dated) .filter((version) => Number.isFinite(version.time)) .sort((a, b) => b.time - a.time || b.id - a.id) .slice(Number(mode)) .reverse(); } for (const version of chosen) { process.stdout.write(`${version.name}\t${version.created}\n`); }