|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * "Humans write English, the bot writes translations" — as a machine rule. |
| 4 | + * |
| 5 | + * English is the only authored language (AGENTS.md). Translations are produced |
| 6 | + * in a separate periodic pass by a dedicated account. Both halves of that split |
| 7 | + * have to be enforced or neither holds: |
| 8 | + * |
| 9 | + * - A content PR that also hand-edits six locale siblings is the cost this |
| 10 | + * design removes — 86% of the diff in a typical docs PR used to be |
| 11 | + * translation churn. Left as a convention it comes back on the first |
| 12 | + * rushed PR. |
| 13 | + * - A translation PR that also edits English (or anything outside |
| 14 | + * `content/docs/`) is a generated-content PR carrying an unreviewed |
| 15 | + * behavioural change. An agent with an editor will "helpfully" fix a typo, |
| 16 | + * repair a link, or restructure a table while translating. |
| 17 | + * |
| 18 | + * So: the translation account may ONLY touch locale artifacts, and everyone |
| 19 | + * else may only touch everything else. |
| 20 | + * |
| 21 | + * The discriminator is the PR author's login, not a label — a label can be |
| 22 | + * forgotten or edited, an author cannot be forged. Set the repo variable |
| 23 | + * `TRANSLATION_BOT_LOGIN` to the dedicated account. Until it is set the check |
| 24 | + * reports and passes, so this can land before the account exists. |
| 25 | + * |
| 26 | + * Usage: |
| 27 | + * node .github/scripts/check-translation-ownership.mjs --actor <login> --files <list> |
| 28 | + * |
| 29 | + * <list> is a file containing one changed path per line (`git diff --name-only`). |
| 30 | + */ |
| 31 | +import { readFileSync } from 'node:fs'; |
| 32 | +import { join, dirname, resolve, relative } from 'node:path'; |
| 33 | +import { fileURLToPath } from 'node:url'; |
| 34 | + |
| 35 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 36 | +const ROOT = resolve(HERE, '../..'); |
| 37 | +const I18N = join(ROOT, 'apps/docs/lib/i18n.ts'); |
| 38 | + |
| 39 | +function locales() { |
| 40 | + const m = readFileSync(I18N, 'utf8').match(/languages:\s*\[([^\]]+)\]/); |
| 41 | + if (!m) throw new Error(`could not parse languages[] out of ${relative(ROOT, I18N)}`); |
| 42 | + return m[1] |
| 43 | + .split(',') |
| 44 | + .map((s) => s.trim().replace(/['"]/g, '')) |
| 45 | + .filter((l) => l && l !== 'en'); |
| 46 | +} |
| 47 | + |
| 48 | +const LOCALES = locales(); |
| 49 | + |
| 50 | +/** `content/docs/**\/*.<locale>.mdx` and `content/docs/**\/meta.<locale>.json`. */ |
| 51 | +function isTranslationArtifact(path) { |
| 52 | + if (!path.startsWith('content/docs/')) return false; |
| 53 | + return LOCALES.some((l) => path.endsWith(`.${l}.mdx`) || path.endsWith(`meta.${l}.json`)); |
| 54 | +} |
| 55 | + |
| 56 | +function main() { |
| 57 | + const argv = process.argv.slice(2); |
| 58 | + const value = (name) => { |
| 59 | + const i = argv.indexOf(`--${name}`); |
| 60 | + return i >= 0 ? argv[i + 1] : argv.find((a) => a.startsWith(`--${name}=`))?.split('=').slice(1).join('='); |
| 61 | + }; |
| 62 | + |
| 63 | + const actor = (value('actor') ?? '').trim(); |
| 64 | + const listFile = value('files'); |
| 65 | + if (!listFile) throw new Error('--files <path> is required'); |
| 66 | + |
| 67 | + const changed = readFileSync(resolve(ROOT, listFile), 'utf8') |
| 68 | + .split('\n') |
| 69 | + .map((s) => s.trim()) |
| 70 | + .filter(Boolean); |
| 71 | + |
| 72 | + const botLogin = (process.env.TRANSLATION_BOT_LOGIN ?? '').trim(); |
| 73 | + const isBot = botLogin !== '' && actor.toLowerCase() === botLogin.toLowerCase(); |
| 74 | + |
| 75 | + const artifacts = changed.filter(isTranslationArtifact); |
| 76 | + const others = changed.filter((p) => !isTranslationArtifact(p)); |
| 77 | + |
| 78 | + if (!botLogin) { |
| 79 | + console.log( |
| 80 | + '⚠ TRANSLATION_BOT_LOGIN is not set — ownership is not enforced yet.\n' + |
| 81 | + ' Set it to the dedicated translation account (Settings → Variables) to turn this on.', |
| 82 | + ); |
| 83 | + console.log(` This PR touches ${artifacts.length} translation artifact(s) and ${others.length} other file(s).`); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (isBot) { |
| 88 | + if (others.length) { |
| 89 | + console.error( |
| 90 | + `✗ translation PRs may only touch translation artifacts.\n` + |
| 91 | + ` @${actor} is the translation account, but this PR also changes:\n` + |
| 92 | + others.map((p) => ` ${p}`).join('\n') + |
| 93 | + `\n\n English sources and site code are authored by humans. Split them out.`, |
| 94 | + ); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | + console.log(`✓ translation PR by @${actor}: ${artifacts.length} artifact(s), nothing else touched.`); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + if (artifacts.length) { |
| 102 | + console.error( |
| 103 | + `✗ translations are generated, not hand-written.\n` + |
| 104 | + ` This PR edits ${artifacts.length} translation artifact(s):\n` + |
| 105 | + artifacts.map((p) => ` ${p}`).join('\n') + |
| 106 | + `\n\n Edit the English source instead — the translation pass will follow.\n` + |
| 107 | + ` See docs/TRANSLATION.md. To retire a page, delete its English source and\n` + |
| 108 | + ` the siblings go with it (the freshness gate reports them as orphaned).`, |
| 109 | + ); |
| 110 | + process.exit(1); |
| 111 | + } |
| 112 | + |
| 113 | + console.log(`✓ ${changed.length} file(s) changed, no translation artifacts touched.`); |
| 114 | +} |
| 115 | + |
| 116 | +main(); |
0 commit comments