Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
publish-site: read the owner from a worktree and a host alias; never skip the account check (#527)
nyblnet06059773c927
diff --git a/docs/RELEASING.md b/docs/RELEASING.mdindex 5d60d870..8d5c6732 100644--- a/docs/RELEASING.md+++ b/docs/RELEASING.md@@ -188,7 +188,11 @@ matters. Neither needs the signing key.after. So: `cd ~/devel/bento` (or any checkout under `~/devel`) and runthe publish from there, pointing `BENTO_SITE_DIR` at the site repo. Acollaborator with release rights who is not the owner can be allowed with- `BENTO_RELEASE_ACCOUNTS=name1,name2`.+ `BENTO_RELEASE_ACCOUNTS=name1,name2`. The repo owner is read from the+ `origin` remote — of the checkout, or of the main gitdir when the checkout+ is a worktree (a detached release worktree included). A checkout with no+ remote at all is **refused**, not skipped; `BENTO_RELEASE_OWNER=<owner>`+ is the way out for a copied tree.This mirrors the assembled `site/` tree into `../bento-site` (or`$BENTO_SITE_DIR`) and pushes it. **`site/` is fully generated — never editdiff --git a/scripts/gh-account.mjs b/scripts/gh-account.mjsindex af67a83a..90cee1fa 100644--- a/scripts/gh-account.mjs+++ b/scripts/gh-account.mjs@@ -19,9 +19,13 @@// live while the repo shows no release./** The owner of a GitHub remote: [email protected]:owner/repo.git,- * https://github.com/owner/repo(.git), ssh://[email protected]/owner/repo. */+ * https://github.com/owner/repo(.git), ssh://[email protected]/owner/repo —+ * and an ssh HOST ALIAS for github.com (`[email protected]:owner/repo`,+ * the per-account trick in ~/.ssh/config that `url.insteadOf` rewrites+ * remotes to): that is what the maintainer's own remotes read as, and+ * the reason the v1.2.2 gate "could not read the repo owner". */export function ownerOfRemote(url) {- const m = /github\.com[:/]([^/\s]+)\/([^/\s]+?)(?:\.git)?\/?$/i.exec(String(url ?? '').trim())+ const m = /github\.com[\w.-]*[:/]([^/\s]+)\/([^/\s]+?)(?:\.git)?\/?$/i.exec(String(url ?? '').trim())return m ? m[1] : null}@@ -52,6 +56,42 @@ export function accountMayRelease(account, owner, collaborators = []) {return a === owner.toLowerCase() || collaborators.some((c) => String(c).toLowerCase() === a)}+/**+ * The origin remote's URL for the repo a directory belongs to — a worktree+ * included. `git remote get-url` answers from the common gitdir already, but+ * a DETACHED worktree made for a release build (`git worktree add /tmp/rel+ * vX.Y.Z --detach`, RELEASING.md) has no upstream to consult, and a `git+ * archive`/plain-copy checkout has no remote at all; v1.2.2 was built in a+ * worktree and the gate found nothing and skipped — a check that skips is a+ * check that does not exist. So: the remote from the directory, then from+ * its common gitdir, then the explicit BENTO_RELEASE_OWNER; `capture` runs+ * git and throws on failure (injected so the rig can drive this without a+ * repo).+ */+export function repoRemote(dir, capture, env = process.env) {+ const tryGit = (args) => { try { return String(capture('git', args) ?? '').trim() } catch { return '' } }+ const direct = tryGit(['-C', dir, 'remote', 'get-url', 'origin'])+ if (direct) return { url: direct, via: 'origin' }+ const common = tryGit(['-C', dir, 'rev-parse', '--path-format=absolute', '--git-common-dir'])+ if (common) {+ const fromCommon = tryGit(['--git-dir', common, 'remote', 'get-url', 'origin'])+ if (fromCommon) return { url: fromCommon, via: 'common gitdir' }+ const cfg = tryGit(['config', '--file', common.replace(/\/$/, '') + '/config', '--get', 'remote.origin.url'])+ if (cfg) return { url: cfg, via: 'common config' }+ }+ if (env.BENTO_RELEASE_OWNER) return { url: `https://github.com/${env.BENTO_RELEASE_OWNER}/x`, via: 'BENTO_RELEASE_OWNER' }+ return null+}++/** The sentence when the owner cannot be read: refuse, with the way out. */+export function noOwnerMessage(dir) {+ return [+ `could not read the repository owner: ${dir} has no origin remote (a detached or copied checkout?).`,+ ` Nothing was published. The gh account check cannot run without knowing whose repo this is, and a check that skips is a check that does not exist.`,+ ` Fix: run the publish from a checkout with an origin remote (any worktree under ~/devel), or set BENTO_RELEASE_OWNER=<github owner>.`,+ ].join('\n')+}+/** The sentence publish-site prints on a mismatch: what gh is, what it must* be, and the exact command — from a path the shell maps to the right* profile. */diff --git a/scripts/publish-site.mjs b/scripts/publish-site.mjsindex 37db2c9f..0afc11f6 100755--- a/scripts/publish-site.mjs+++ b/scripts/publish-site.mjs@@ -31,7 +31,7 @@ import { fileURLToPath } from 'node:url'import { gatePackIndex } from './sign-packs.mjs'import { walk, plannedDeletions, groupDeletions, supersededPacks } from './site-inventory.mjs'import { APPS, RELEASE_MARKER, tagFor } from './apps.mjs'-import { accountMayRelease, activeAccount, mismatchMessage, ownerOfRemote } from './gh-account.mjs'+import { accountMayRelease, activeAccount, mismatchMessage, noOwnerMessage, ownerOfRemote, repoRemote } from './gh-account.mjs'const root = dirname(dirname(fileURLToPath(import.meta.url)))const site = join(root, 'site')@@ -330,11 +330,13 @@ if (!existsSync(join(site, 'guestbook.bento.html'))) {// cases. --dry skips it (a dry run publishes nothing either way); every real// publish runs it, because every real publish ends in the release step.if (!dry) {- const owner = ownerOfRemote((() => { try { return capture('git', ['-C', root, 'remote', 'get-url', 'origin']) } catch { return '' } })())+ const remote = repoRemote(root, (cmd, a) => capture(cmd, a, { stdio: ['ignore', 'pipe', 'ignore'] }))+ const owner = remote ? ownerOfRemote(remote.url) : nullconst status = (() => { try { return capture('gh', ['auth', 'status'], { stdio: ['ignore', 'pipe', 'pipe'] }) } catch (e) { return String(e?.stdout ?? '') + String(e?.stderr ?? '') } })()const account = activeAccount(status)const allowed = (process.env.BENTO_RELEASE_ACCOUNTS ?? '').split(',').map((s) => s.trim()).filter(Boolean)- if (!owner) console.warn('⚠ could not read the repo owner from origin — skipping the gh account check')+ // never skip: an owner that cannot be read is a refusal, not a warning+ if (!owner) die(noOwnerMessage(root))else if (!accountMayRelease(account, owner, allowed)) {die(mismatchMessage({ account, owner, repoRoot: root.startsWith(process.env.HOME ?? '') ? root : '~/devel/bento', cmd: `node scripts/publish-site.mjs ${args.map((a) => (/\s/.test(a) ? JSON.stringify(a) : a)).join(' ')}` }))} else console.log(`• gh account: ${account} ✓ (may release on ${owner})`)diff --git a/scripts/test-publish-account.ts b/scripts/test-publish-account.tsindex 4801b1d9..c5251f94 100644--- a/scripts/test-publish-account.ts+++ b/scripts/test-publish-account.ts@@ -14,8 +14,11 @@// under ~/devel. And the gate sits BEFORE the rsync in publish-site.mjs —// the whole point: the site must never go live and the release then fail.-import { readFileSync } from 'node:fs'-import { accountMayRelease, activeAccount, mismatchMessage, ownerOfRemote } from './gh-account.mjs'+import { readFileSync, mkdtempSync, rmSync, writeFileSync, cpSync } from 'node:fs'+import { execFileSync } from 'node:child_process'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { accountMayRelease, activeAccount, mismatchMessage, noOwnerMessage, ownerOfRemote, repoRemote } from './gh-account.mjs'let failures = 0, checks = 0const ok = (cond: boolean, msg: string) => { checks++; if (!cond) { failures++; console.log(` FAIL ${msg}`) } else console.log(` ok ${msg}`) }@@ -24,6 +27,7 @@ console.log('\nownerOfRemote')ok(ownerOfRemote('[email protected]:nyblnet/bento.git') === 'nyblnet', 'ssh scp form')ok(ownerOfRemote('https://github.com/nyblnet/bento') === 'nyblnet' && ownerOfRemote('https://github.com/nyblnet/bento.git/') === 'nyblnet', 'https, with and without .git and a trailing slash')ok(ownerOfRemote('ssh://[email protected]/Some-Org/repo.git') === 'Some-Org', 'ssh url form, dashes kept')+ok(ownerOfRemote('[email protected]:nyblnet/bento.git') === 'nyblnet' && ownerOfRemote('[email protected]:acme/x') === 'acme', 'an ssh host ALIAS for github.com (url.insteadOf per-account setups) — what the v1.2.2 gate could not read')ok(ownerOfRemote('https://gitlab.com/x/y') === null && ownerOfRemote('') === null && ownerOfRemote(undefined) === null, 'not GitHub, empty, undefined → null')console.log('\nactiveAccount')@@ -58,6 +62,31 @@ ok(/Nothing was published/.test(msg), 'says nothing was published')ok(/cd \/Users\/you\/devel\/bento && node scripts\/publish-site\.mjs "release v1\.2\.3"/.test(msg), 'gives the exact command from the repo root')ok(/gh auth switch --user nyblnet/.test(msg), 'and the switch alternative')+console.log('\nrepoRemote — from a worktree, a detached worktree, a copy')+{+ // a real repo with an origin, and two worktrees of it; git is the capture+ const tmp = mkdtempSync(join(tmpdir(), 'bento-gate-'))+ const git = (...a: string[]) => execFileSync('git', a, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], env: { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_SYSTEM: '/dev/null' } }).trim()+ const capture = (cmd: string, a: string[]) => execFileSync(cmd, a, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()+ const main = join(tmp, 'main')+ git('init', '-q', '-b', 'main', main)+ git('-C', main, 'config', 'user.email', 'r@x'); git('-C', main, 'config', 'user.name', 'r')+ writeFileSync(join(main, 'f'), '1'); git('-C', main, 'add', 'f'); git('-C', main, 'commit', '-q', '-m', 'one')+ git('-C', main, 'remote', 'add', 'origin', '[email protected]:nyblnet/bento.git')+ git('-C', main, 'tag', 'v9.9.9')+ const wt = join(tmp, 'wt'); git('-C', main, 'worktree', 'add', '-q', wt, '-b', 'feature')+ const rel = join(tmp, 'rel'); git('-C', main, 'worktree', 'add', '-q', '--detach', rel, 'v9.9.9')+ ok(ownerOfRemote(repoRemote(main, capture)!.url) === 'nyblnet', 'the main checkout: origin read')+ ok(ownerOfRemote(repoRemote(wt, capture)!.url) === 'nyblnet', 'a worktree (its .git is a FILE pointing at the main gitdir): origin read')+ const r = repoRemote(rel, capture)+ ok(!!r && ownerOfRemote(r.url) === 'nyblnet', `a DETACHED release worktree (git worktree add --detach vX.Y.Z, the RELEASING.md recipe): origin read (via ${r?.via})`)+ const copy = join(tmp, 'copy'); cpSync(main, copy, { recursive: true }); git('-C', copy, 'remote', 'remove', 'origin')+ ok(repoRemote(copy, capture, {}) === null, 'a checkout with no remote at all → null (no guess)')+ ok(ownerOfRemote(repoRemote(copy, capture, { BENTO_RELEASE_OWNER: 'nyblnet' })!.url) === 'nyblnet', 'unless BENTO_RELEASE_OWNER names the owner')+ ok(/no origin remote/.test(noOwnerMessage(copy)) && /Nothing was published/.test(noOwnerMessage(copy)) && /BENTO_RELEASE_OWNER/.test(noOwnerMessage(copy)), 'the refusal names the cause and the two ways out')+ try { rmSync(tmp, { recursive: true, force: true, maxRetries: 5 }) } catch { /* temp */ }+}+console.log('\npublish-site.mjs — the gate sits before the mirror'){const src = readFileSync(new URL('./publish-site.mjs', import.meta.url), 'utf8')@@ -66,8 +95,10 @@ console.log('\npublish-site.mjs — the gate sits before the mirror')const release = src.indexOf("run('gh', ['release', 'create'")ok(gate > 0 && mirror > 0 && gate < mirror, 'the account check runs BEFORE rsync')ok(release > mirror, 'and the release step stays after it (the order the check protects)')- ok(/if \(!dry\) \{\n const owner = ownerOfRemote/.test(src), 'a dry run skips the check (nothing is published either way); every real publish runs it')+ ok(/if \(!dry\) \{\n const remote = repoRemote\(root,/.test(src), 'a dry run skips the check (nothing is published either way); every real publish runs it')ok(/die\(mismatchMessage\(/.test(src), 'a mismatch dies with the message, not a warning')+ ok(/if \(!owner\) die\(noOwnerMessage\(root\)\)/.test(src) && !/skipping the gh account check/.test(src), 'an owner that cannot be read REFUSES — the gate never skips (v1.2.2 warned and proceeded)')+ ok(/repoRemote\(root,/.test(src), 'the remote is resolved through repoRemote (worktree-aware), not a bare get-url')}console.log(`\n${checks - failures}/${checks} checks passed`)