Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/catalog-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
8 changes: 6 additions & 2 deletions scripts/catalog-sync-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ async function main(): Promise<void> {
// 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"],
Expand All @@ -142,7 +146,7 @@ async function main(): Promise<void> {
if (!extraction.stderr) console.error(error);
if (ci) {
try {
reportCatalogBreak(gh, {
reportCatalogBreak(issueGh, {
commandCodeVersion: latestCc,
error,
workflowUrl: workflowUrl(),
Expand All @@ -164,7 +168,7 @@ async function main(): Promise<void> {
// 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;
Expand Down
11 changes: 10 additions & 1 deletion src/catalog-break-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
});
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/catalog-break-reporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, test } from "bun:test";
import {
ghEnvironment,
reportCatalogBreak,
resolveCatalogBreaks,
type GhRunner,
Expand All @@ -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",
Expand Down