mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2026-11-26 16:01:21 +00:00
* docs: move the user docs to the website and fix what the code contradicts The README is down to the pitch, a quick start and links. The docs/ pages that repeated the website (environment variables, sources, push targets, notifications, force-push protection, SSO, Nix) and the root NIX.md and certs/README.md are now short stubs pointing at the site, after moving their remaining true facts there. Checking those facts against the code also turned up site pages that were wrong: - SAML sign-in does not work from the UI, and the ACS URL was wrong - the OIDC provider discovery URL lives under /api/auth - force-push protection does not stop Gitea's own mirror schedule - a read-only /app/certs mount stops the container at startup - gitea-mirror-db on Nix does not touch the real database - several environment variable rows described the wrong behavior - DESTINATION_PROVIDER is not checked against the destination lock The link checker now covers every markdown file in the repository, and four screenshots nothing links to are gone. * ci: check the environment variable reference against the code scripts/check-env-docs.ts fails when the app reads a variable the reference page does not list, or the page lists one nothing reads. The variables that are read but deliberately undocumented (internal ones, and those stored but never used) are listed in the script with a reason, and the check also fails when one of those stops being read. * docs: bring the maintainer docs up to date CLAUDE.md, CONTRIBUTING.md, the development workflow, the SSO testing guide, the Nix packaging notes and the site's own README and CLAUDE.md described scripts, files, ports and behavior that no longer exist. They now match the code: Astro 7, multiple sources and destinations, the real CI steps, the test isolation rules, the TypeScript 7 type check workaround, and the AGPL license. GRACEFUL_SHUTDOWN.md and RECOVERY_IMPROVEMENTS.md are merged into SHUTDOWN_AND_RECOVERY.md. It also records two gaps: a job stopped by a clean shutdown is not resumed by recovery, and Docker's 10 second stop timeout is shorter than the 30 second shutdown. AGENTS.md now points at CLAUDE.md. * docs: changelog for the docs consolidation * ci: count the env config loader's envFlag reads in the env check
169 lines
7.0 KiB
TypeScript
169 lines
7.0 KiB
TypeScript
#!/usr/bin/env bun
|
|
/**
|
|
* Checks that the environment variable reference on the website matches the
|
|
* code. Fails when:
|
|
* - the app reads a variable the reference does not list, or
|
|
* - the reference lists a variable nothing reads.
|
|
*
|
|
* "Reads" means `process.env.NAME`, `import.meta.env.NAME`, `Bun.env.NAME`,
|
|
* `env.NAME` on a ProcessEnv parameter, the bracket forms, or a name passed
|
|
* to the env config loader's `envFlag` / `envFlagAny` helpers, in src/ and
|
|
* scripts/ outside tests, plus the variables docker-entrypoint.sh takes from
|
|
* the environment. The reference is every variable in the first column of a
|
|
* table in www/src/pages/docs/environment-variables.mdx.
|
|
*
|
|
* Run with `bun scripts/check-env-docs.ts`; exits 1 on a mismatch.
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
|
|
const root = path.resolve(import.meta.dir, "..");
|
|
const referencePage = "www/src/pages/docs/environment-variables.mdx";
|
|
|
|
/**
|
|
* Read by the code but deliberately left out of the reference. Each entry says
|
|
* why. The check also fails when an entry here is no longer read, so the list
|
|
* cannot go stale.
|
|
*/
|
|
const NOT_DOCUMENTED: Record<string, string> = {
|
|
ASTRO_NODE_AUTOSTART: "internal: scripts/runtime-server.ts sets it for the Astro node adapter",
|
|
DEPLOYMENT_MODE: "internal",
|
|
JWT_SECRET: "pre-v3 name of BETTER_AUTH_SECRET, still read as a fallback",
|
|
DATABASE_PATH: "only read in src/lib/db/adapter.ts, which nothing imports",
|
|
GITEA_REPAIR_PROGRESS_INTERVAL: "tuning for scripts/repair-mirrored-repos.ts",
|
|
GITEA_REPAIR_REQUEST_TIMEOUT_MS: "tuning for scripts/repair-mirrored-repos.ts",
|
|
// Stored by the env loader, but no code acts on the value.
|
|
GITHUB_TYPE: "stored, never used",
|
|
GITEA_CREATE_ORG: "stored, never used",
|
|
GITEA_FORK_STRATEGY: "stored, never used",
|
|
GITEA_TEMPLATE_OWNER: "stored, never used",
|
|
GITEA_TEMPLATE_REPO: "stored, never used",
|
|
SCHEDULE_AUTO_RETRY: "stored, never used",
|
|
SCHEDULE_CLEANUP_BEFORE_MIRROR: "stored, never used",
|
|
SCHEDULE_LOG_LEVEL: "stored, never used",
|
|
SCHEDULE_NOTIFY_ON_FAILURE: "stored, never used",
|
|
SCHEDULE_NOTIFY_ON_SUCCESS: "stored, never used",
|
|
SCHEDULE_RETRY_ATTEMPTS: "stored, never used",
|
|
SCHEDULE_RETRY_DELAY: "stored, never used",
|
|
SCHEDULE_TIMEOUT: "stored, never used",
|
|
// Only read by getMirrorStrategyConfig in src/lib/utils/mirror-strategies.ts,
|
|
// which nothing calls.
|
|
NON_MIRROR_STRATEGY: "dead code",
|
|
ORG_CREATION_RETRIES: "dead code",
|
|
ORG_CREATION_RETRY_DELAY: "dead code",
|
|
REPO_BATCH_SIZE: "dead code",
|
|
SEQUENTIAL_ORG_CREATION: "dead code",
|
|
SYNC_TIMEOUT: "dead code",
|
|
};
|
|
|
|
/**
|
|
* Listed in the reference but read by something other than the app code:
|
|
* Docker Compose, the Nix wrapper, or the runtime itself.
|
|
*/
|
|
const READ_ELSEWHERE: Record<string, string> = {
|
|
DOCKER_REGISTRY: "Docker Compose substitution",
|
|
DOCKER_IMAGE: "Docker Compose substitution",
|
|
DOCKER_TAG: "Docker Compose substitution",
|
|
PUID: "Docker Compose substitution",
|
|
PGID: "Docker Compose substitution",
|
|
NODE_EXTRA_CA_CERTS: "read by the Bun runtime; the entrypoint also sets it",
|
|
SERVER_CERT_PATH: "read by the Astro node adapter when dist/server/entry.mjs runs directly",
|
|
SERVER_KEY_PATH: "read by the Astro node adapter when dist/server/entry.mjs runs directly",
|
|
};
|
|
|
|
function trackedFiles(...patterns: string[]): string[] {
|
|
return Bun.spawnSync(["git", "ls-files", ...patterns], { cwd: root })
|
|
.stdout.toString()
|
|
.split("\n")
|
|
.filter(Boolean);
|
|
}
|
|
|
|
const read = new Map<string, string>();
|
|
const note = (name: string, file: string) => {
|
|
if (!read.has(name)) read.set(name, file);
|
|
};
|
|
|
|
const codeFiles = trackedFiles("src", "scripts", "astro.config.mjs").filter(
|
|
(file) =>
|
|
/\.(ts|tsx|js|mjs|astro)$/.test(file) &&
|
|
!/\.test\.(ts|tsx)$/.test(file) &&
|
|
!file.startsWith("src/tests/") &&
|
|
!file.startsWith("scripts/check-")
|
|
);
|
|
const envRead =
|
|
/(?:process\.env|import\.meta\.env|Bun\.env|(?<![.\w])env)(?:\.([A-Z][A-Z0-9_]+)|\[\s*["'`]([A-Z][A-Z0-9_]+)["'`]\s*\])/g;
|
|
// envFlag('NAME') and envFlagAny('A', 'B') in env-config-loader.ts.
|
|
const envHelperCall = /\benvFlag(?:Any)?\(([^)]*)\)/g;
|
|
const quotedName = /["'`]([A-Z][A-Z0-9_]+)["'`]/g;
|
|
for (const file of codeFiles) {
|
|
const text = readFileSync(path.join(root, file), "utf8");
|
|
for (const match of text.matchAll(envRead)) note(match[1] ?? match[2], file);
|
|
for (const call of text.matchAll(envHelperCall)) {
|
|
for (const name of call[1].matchAll(quotedName)) note(name[1], file);
|
|
}
|
|
}
|
|
|
|
// The entrypoint: a variable it reads before it ever assigns it comes from
|
|
// the environment, and ones it assigns first are its own locals. Function
|
|
// bodies run later, so a read there only counts when the script never
|
|
// assigns the variable at all.
|
|
const entrypoint = readFileSync(path.join(root, "docker-entrypoint.sh"), "utf8");
|
|
const assignment = /(?:^|[\s;&|(])(?:export\s+)?([A-Z][A-Z0-9_]+)=/;
|
|
const reads = (line: string) => [...line.matchAll(/\$\{?([A-Z][A-Z0-9_]+)/g)].map((m) => m[1]);
|
|
const seen = new Set<string>();
|
|
const assignedAnywhere = new Set<string>();
|
|
const readInFunctions = new Set<string>();
|
|
let inFunction = false;
|
|
for (const line of entrypoint.split("\n")) {
|
|
if (/^\s*#/.test(line)) continue;
|
|
if (/^\s*[A-Za-z_][A-Za-z0-9_]*\(\)\s*\{/.test(line)) inFunction = true;
|
|
const assigned = assignment.exec(line)?.[1];
|
|
if (assigned) assignedAnywhere.add(assigned);
|
|
if (inFunction) {
|
|
for (const name of reads(line)) readInFunctions.add(name);
|
|
if (/^\}/.test(line)) inFunction = false;
|
|
continue;
|
|
}
|
|
for (const name of reads(line)) {
|
|
if (!seen.has(name)) {
|
|
seen.add(name);
|
|
note(name, "docker-entrypoint.sh");
|
|
}
|
|
}
|
|
if (assigned) seen.add(assigned);
|
|
}
|
|
for (const name of readInFunctions) {
|
|
if (!assignedAnywhere.has(name)) note(name, "docker-entrypoint.sh");
|
|
}
|
|
|
|
const documented = new Set<string>();
|
|
for (const line of readFileSync(path.join(root, referencePage), "utf8").split("\n")) {
|
|
const name = /^\|\s*`([A-Z][A-Z0-9_]+)`/.exec(line)?.[1];
|
|
if (name) documented.add(name);
|
|
}
|
|
|
|
const problems: string[] = [];
|
|
for (const [name, file] of [...read].sort()) {
|
|
if (!documented.has(name) && !(name in NOT_DOCUMENTED)) {
|
|
problems.push(`${name} is read in ${file} but missing from ${referencePage}`);
|
|
}
|
|
}
|
|
for (const name of [...documented].sort()) {
|
|
if (!read.has(name) && !(name in READ_ELSEWHERE)) {
|
|
problems.push(`${name} is listed in ${referencePage} but nothing reads it`);
|
|
}
|
|
}
|
|
for (const name of Object.keys(NOT_DOCUMENTED)) {
|
|
if (!read.has(name)) problems.push(`${name} is in NOT_DOCUMENTED but nothing reads it any more`);
|
|
if (documented.has(name)) problems.push(`${name} is in NOT_DOCUMENTED but the reference lists it`);
|
|
}
|
|
for (const name of Object.keys(READ_ELSEWHERE)) {
|
|
if (!documented.has(name)) problems.push(`${name} is in READ_ELSEWHERE but the reference does not list it`);
|
|
}
|
|
|
|
if (problems.length) {
|
|
console.error(`${problems.length} environment variable mismatch(es):\n${problems.map((p) => ` ${p}`).join("\n")}`);
|
|
process.exit(1);
|
|
}
|
|
console.log(`Environment variable reference OK (${documented.size} documented, ${read.size} read)`);
|