|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Monthly check behind /tld-directory. Run by a homelab timer; see |
| 5 | + * scripts/domain-tools/systemd/. |
| 6 | + * |
| 7 | + * Refreshes the datasets the directory reads in a throwaway checkout of main. |
| 8 | + * If a real fact changed, it opens one pull request for review. If only dates |
| 9 | + * and version numbers moved, it does nothing. Any failure exits non-zero, which |
| 10 | + * the automation monitor shows. |
| 11 | + * |
| 12 | + * --dry-run stops before the commit. --base <ref> checks out another ref. |
| 13 | + */ |
| 14 | + |
| 15 | +import { execFileSync } from "node:child_process"; |
| 16 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 17 | +import { tmpdir } from "node:os"; |
| 18 | +import path from "node:path"; |
| 19 | + |
| 20 | +const BRANCH = "chore/tld-directory-refresh"; |
| 21 | +const DATA_DIR = "lib/domain/data"; |
| 22 | +const DATA_FILES = ["tlds.json", "rdap-bootstrap.json", "rdap-overrides.json", "whois-servers.json", "whois-reviewed.json", "tld-directory.json"]; |
| 23 | +// These move on most runs without any fact changing. |
| 24 | +const VOLATILE_KEYS = new Set(["fetchedAt", "version", "publication", "tldListVersion"]); |
| 25 | + |
| 26 | +const args = process.argv.slice(2); |
| 27 | +const dryRun = args.includes("--dry-run"); |
| 28 | +const baseIndex = args.indexOf("--base"); |
| 29 | +const base = baseIndex === -1 ? "origin/main" : args[baseIndex + 1]; |
| 30 | + |
| 31 | +const run = (command, commandArgs, cwd) => |
| 32 | + execFileSync(command, commandArgs, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "inherit"], maxBuffer: 1 << 28 }); |
| 33 | + |
| 34 | +const facts = (text) => |
| 35 | + JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(text)).filter(([key]) => !VOLATILE_KEYS.has(key)))); |
| 36 | + |
| 37 | +function listChange(before, after) { |
| 38 | + const was = new Set(JSON.parse(before).tlds); |
| 39 | + const now = new Set(JSON.parse(after).tlds); |
| 40 | + return { added: [...now].filter((tld) => !was.has(tld)), removed: [...was].filter((tld) => !now.has(tld)) }; |
| 41 | +} |
| 42 | + |
| 43 | +function refresh(tree) { |
| 44 | + const script = (name) => path.join("scripts/domain-tools", name); |
| 45 | + const node = process.execPath; |
| 46 | + run(node, ["--experimental-strip-types", script("refresh-datasets.mjs")], tree); |
| 47 | + // That script also refreshes data the directory doesn't read. Leave it for its own release. |
| 48 | + const others = run("git", ["diff", "--name-only", "--", DATA_DIR], tree) |
| 49 | + .split("\n") |
| 50 | + .filter((file) => file && !DATA_FILES.includes(path.basename(file))); |
| 51 | + if (others.length) run("git", ["checkout", "--", ...others], tree); |
| 52 | + run(node, ["--experimental-strip-types", script("refresh-tld-directory.mjs")], tree); |
| 53 | +} |
| 54 | + |
| 55 | +function changedFiles(tree) { |
| 56 | + return DATA_FILES.filter((file) => { |
| 57 | + const relative = path.posix.join(DATA_DIR, file); |
| 58 | + const before = run("git", ["show", `HEAD:${relative}`], tree); |
| 59 | + return facts(before) !== facts(readFileSync(path.join(tree, relative), "utf8")); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function pullRequestBody(tree, changed) { |
| 64 | + const tlds = path.posix.join(DATA_DIR, "tlds.json"); |
| 65 | + const { added, removed } = listChange(run("git", ["show", `HEAD:${tlds}`], tree), readFileSync(path.join(tree, tlds), "utf8")); |
| 66 | + const lines = ["The monthly TLD directory check found changes in IANA's data.", "", "Changed files:", ...changed.map((file) => `- \`${file}\``)]; |
| 67 | + if (added.length) lines.push("", `New endings: ${added.map((tld) => `\`.${tld}\``).join(", ")}. Check whether they need reviewed RDAP or WHOIS servers.`); |
| 68 | + if (removed.length) lines.push("", `Removed endings: ${removed.map((tld) => `\`.${tld}\``).join(", ")}.`); |
| 69 | + lines.push("", "Review the data diff and merge to update /tld-directory. Merging deploys."); |
| 70 | + return lines.join("\n"); |
| 71 | +} |
| 72 | + |
| 73 | +function publish(tree, changed) { |
| 74 | + // Read the old list for the description before the commit moves HEAD. |
| 75 | + const body = pullRequestBody(tree, changed); |
| 76 | + const title = `Update TLD directory data (${new Date().toISOString().slice(0, 7)})`; |
| 77 | + run("git", ["add", "--", ...DATA_FILES.map((file) => path.posix.join(DATA_DIR, file))], tree); |
| 78 | + run("git", ["commit", "--quiet", "-m", title], tree); |
| 79 | + // Stay detached: a local branch would outlive this checkout and block next month's run. |
| 80 | + run("git", ["push", "--quiet", "--force", "origin", `HEAD:refs/heads/${BRANCH}`], tree); |
| 81 | + const open = run("gh", ["pr", "list", "--head", BRANCH, "--state", "open", "--json", "number", "--jq", "length"], tree).trim(); |
| 82 | + if (open !== "0") { |
| 83 | + run("gh", ["pr", "edit", BRANCH, "--title", title, "--body", body], tree); |
| 84 | + console.log(`Updated the open pull request on ${BRANCH}.`); |
| 85 | + return; |
| 86 | + } |
| 87 | + const url = run("gh", ["pr", "create", "--base", "main", "--head", BRANCH, "--title", title, "--body", body], tree).trim(); |
| 88 | + console.log(`Opened ${url}`); |
| 89 | +} |
| 90 | + |
| 91 | +const repo = run("git", ["rev-parse", "--show-toplevel"], process.cwd()).trim(); |
| 92 | +const parent = mkdtempSync(path.join(tmpdir(), "stack127-tld-")); |
| 93 | +const tree = path.join(parent, "tree"); |
| 94 | + |
| 95 | +try { |
| 96 | + if (baseIndex === -1) run("git", ["fetch", "--quiet", "origin", "main"], repo); |
| 97 | + run("git", ["worktree", "add", "--quiet", "--detach", tree, base], repo); |
| 98 | + refresh(tree); |
| 99 | + const changed = changedFiles(tree); |
| 100 | + if (!changed.length) { |
| 101 | + console.log("No TLD directory facts changed. Nothing to do."); |
| 102 | + } else if (dryRun) { |
| 103 | + console.log(`Dry run. Would open a pull request:\n${pullRequestBody(tree, changed)}`); |
| 104 | + } else { |
| 105 | + publish(tree, changed); |
| 106 | + } |
| 107 | +} finally { |
| 108 | + try { |
| 109 | + run("git", ["worktree", "remove", "--force", tree], repo); |
| 110 | + } finally { |
| 111 | + rmSync(parent, { recursive: true, force: true }); |
| 112 | + } |
| 113 | +} |
0 commit comments