From 25b97a28b0c3c65daeb2079caa69c2c9beff227d Mon Sep 17 00:00:00 2001 From: Cristhofer Pincetti Date: Fri, 4 Dec 2026 15:48:50 -0300 Subject: [PATCH] chore(ci): use built-in token for catalog issue writes Workit-Session: ses_dbdda4daaffexKPxS5Z9LKhpI2 --- .github/workflows/catalog-sync.yml | 5 ++++- scripts/catalog-sync-ci.ts | 8 ++++++-- src/catalog-break-reporter.ts | 11 ++++++++++- tests/unit/catalog-break-reporter.test.ts | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/.github/workflows/catalog-sync.yml b/.github/workflows/catalog-sync.yml index 9b12105..8ddb04a 100644 --- a/.github/workflows/catalog-sync.yml +++ b/.github/workflows/catalog-sync.yml @@ -48,7 +48,7 @@ jobs: - name: Ensure automation labels env: - GH_TOKEN: ${{ secrets.RELEASE_SYNC_TOKEN }} + GH_TOKEN: ${{ github.token }} run: | gh label create catalog-break --force --color B60205 \ --description "Catalog model extraction failed in CI" @@ -60,4 +60,7 @@ jobs: CI: "true" FORCE: ${{ inputs.force }} GH_TOKEN: ${{ secrets.RELEASE_SYNC_TOKEN }} + # Issue writes use the built-in token, whose issues: write permission + # is granted above; the PAT is only for PR creation (so CI runs). + CATALOG_ISSUES_TOKEN: ${{ github.token }} run: bun run scripts/catalog-sync-ci.ts diff --git a/scripts/catalog-sync-ci.ts b/scripts/catalog-sync-ci.ts index 3d2c1b2..3ff0dd1 100644 --- a/scripts/catalog-sync-ci.ts +++ b/scripts/catalog-sync-ci.ts @@ -126,7 +126,11 @@ async function main(): Promise { // untouched and routes here through the catalog-break issue path. const generatedAt = bundledGeneratedAt(); const ci = process.env.CI === "true"; + // PR creation needs the PAT so GitHub starts CI on the branch. Issue writes + // use the workflow's GITHUB_TOKEN (issues: write); the PAT may not carry + // Issues permission, and an addComment denial must not mask a healthy sync. const gh = githubCli(ROOT); + const issueGh = githubCli(ROOT, process.env.CATALOG_ISSUES_TOKEN); const extraction = spawnSync( process.execPath, ["run", "scripts/sync-models.ts", "--", "--remote"], @@ -142,7 +146,7 @@ async function main(): Promise { if (!extraction.stderr) console.error(error); if (ci) { try { - reportCatalogBreak(gh, { + reportCatalogBreak(issueGh, { commandCodeVersion: latestCc, error, workflowUrl: workflowUrl(), @@ -164,7 +168,7 @@ async function main(): Promise { // future release tag that semantic-release has not created yet. const commit = git("rev-parse", "HEAD"); const tags = git("tag", "--merged", commit, "--list", "v[0-9]*", "--sort=-version:refname"); - resolveCatalogBreaks(gh, { commit, tag: tags.split("\n")[0] || null }); + resolveCatalogBreaks(issueGh, { commit, tag: tags.split("\n")[0] || null }); } catch (issueErr) { console.error("failed to resolve catalog-break issues", issueErr); process.exitCode = 1; diff --git a/src/catalog-break-reporter.ts b/src/catalog-break-reporter.ts index 83dbe02..16790f2 100644 --- a/src/catalog-break-reporter.ts +++ b/src/catalog-break-reporter.ts @@ -11,12 +11,21 @@ import { export type GhRunner = (args: readonly string[], stdin?: string) => string; -export function githubCli(cwd: string): GhRunner { +export function ghEnvironment( + token: string | undefined, + base: NodeJS.ProcessEnv = process.env, +): NodeJS.ProcessEnv { + return token ? { ...base, GH_TOKEN: token, GITHUB_TOKEN: token } : base; +} + +export function githubCli(cwd: string, token?: string): GhRunner { + const env = ghEnvironment(token); return (args, input) => execFileSync("gh", args, { cwd, input, encoding: "utf-8", + env, maxBuffer: 16 * 1024 * 1024, stdio: [input === undefined ? "ignore" : "pipe", "pipe", "inherit"], }); diff --git a/tests/unit/catalog-break-reporter.test.ts b/tests/unit/catalog-break-reporter.test.ts index a12e8d5..466ce5c 100644 --- a/tests/unit/catalog-break-reporter.test.ts +++ b/tests/unit/catalog-break-reporter.test.ts @@ -1,5 +1,6 @@ import { describe, expect, test } from "bun:test"; import { + ghEnvironment, reportCatalogBreak, resolveCatalogBreaks, type GhRunner, @@ -18,6 +19,24 @@ type Issue = { duplicateOf?: number; }; +describe("ghEnvironment", () => { + test("uses the issue token for both gh variables when supplied", () => { + const env = ghEnvironment("issues-token", { + GH_TOKEN: "release-sync-pat", + GITHUB_TOKEN: "release-sync-pat", + PATH: "/bin", + }); + expect(env.GH_TOKEN).toBe("issues-token"); + expect(env.GITHUB_TOKEN).toBe("issues-token"); + expect(env.PATH).toBe("/bin"); + }); + + test("leaves the base environment untouched without an issue token", () => { + const base = { GH_TOKEN: "release-sync-pat" }; + expect(ghEnvironment(undefined, base)).toBe(base); + }); +}); + const failure = { commandCodeVersion: "1.93.7", error: "Could not evaluate model catalog: Q_ is not defined",