Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(ci): fail a fork's CI gate when the fork changes CI workflows (#9932)
teoc319fa754667
diff --git a/.agents/skills/run-ci/SKILL.md b/.agents/skills/run-ci/SKILL.mdindex 31852631b1..7a307859fa 100644--- a/.agents/skills/run-ci/SKILL.md+++ b/.agents/skills/run-ci/SKILL.md@@ -76,12 +76,17 @@ To read the state once, look at the `CI gate` row of `gh pr checks <pr>`:## Fork PRs-A maintainer adds the label after reading the diff, workflow files included:-the run uses the fork's copies. The fork's token can't set statuses, so-`CI Gate for Forks` removes the label and sets `CI gate` to `Running on <sha>`-on the head the label went on, then copies the run's verdict onto that head.+A maintainer adds the label after reading the diff: the run executes the+fork's code. The fork's token can't set statuses, so `CI Gate for Forks`+removes the label and sets `CI gate` to `Running on <sha>` on the head the+label went on, then copies the verdict of the `pr-ci.yml` run onto that head.Re-running a fork's failed jobs doesn't update `CI gate`; add the label again.+A fork run uses the fork's workflow files, so a fork that changes `.github/` or+`tooling/xtask/crates/xtask_workflows` gets `CI gate` failure (`fork changes CI+workflows`). To test it, a maintainer reviews it, pushes it to a branch here,+and runs CI on a PR from that branch.+## While the switch is in progressThe checks still run on every push as well, and `main` still requires theirdiff --git a/.github/workflows/ci-gate-fork.yml b/.github/workflows/ci-gate-fork.ymlindex 9ba9c9c3ea..3c142a75a5 100644--- a/.github/workflows/ci-gate-fork.yml+++ b/.github/workflows/ci-gate-fork.yml@@ -18,6 +18,7 @@ jobs:name: Take a fork's CI requestruns-on: ubuntu-latestpermissions:+ contents: readissues: writepull-requests: writestatuses: write@@ -39,6 +40,10 @@ jobs:// fork after a fork PR's run, whose read-only token could not set the status//// Statuses go on the PR head commit, so a later push starts without one.+ //+ // A fork PR's run uses the workflow files of its merge commit, so a fork that+ // edits them decides its own verdict. Both fork steps fail closed instead: the+ // fork's CI sources must be the trees it branched from (`ciSourceChanges`).import { readFileSync } from 'node:fs';@@ -49,11 +54,19 @@ jobs:// Job names in `PR CI`; the fork step reads them back from the finished run.export const START_JOB = 'Start';export const GATE_JOB = 'CI gate';+ // The workflow whose jobs the fork step trusts, and the trees that define it:+ // the generated workflows and the Rust that generates them.+ export const PR_CI_PATH = '.github/workflows/pr-ci.yml';+ export const CI_SOURCES = ['.github', 'tooling/xtask/crates/xtask_workflows'] as const;+ export const FORK_CHANGES_CI =+ 'fork changes CI workflows: a maintainer must review and run CI from a branch';type State = 'pending' | 'success' | 'failure' | 'error';export type Needs = Record<string, { result: string }>;export type RunJob = { name: string; conclusion: string | null };type Outcome = { state: State; description: string } | null;+ /// Git tree SHA of each CI source at a commit; null where the path is missing.+ export type CiTrees = Record<string, string | null>;function env(name: string): string {const value = process.env[name];@@ -139,6 +152,25 @@ jobs:return null;}+ /// The CI sources whose tree differs between a fork head and the commit it+ /// branched from. A source missing from either side counts as changed.+ export function ciSourceChanges(head: CiTrees, base: CiTrees): string[] {+ return CI_SOURCES.filter((path) => !head[path] || !base[path] || head[path] !== base[path]);+ }++ /// Why a finished fork run can't be trusted, or null. The workflow is matched+ /// by name, so a fork can add another file named `PR CI`; and even `pr-ci.yml`+ /// is the fork's copy unless the CI sources are unchanged.+ export function forkRunDistrust(runPath: string, changes: string[]): string | null {+ if (changes.length > 0) {+ return FORK_CHANGES_CI;+ }+ if (runPath !== PR_CI_PATH) {+ return `The run came from ${runPath}, not ${PR_CI_PATH}`;+ }+ return null;+ }+async function setStatus(sha: string, state: State, description: string, targetUrl: string) {await github('POST', `/repos/${repo()}/statuses/${sha}`, {state,@@ -165,6 +197,41 @@ jobs:return status?.state === 'pending' && status.description.startsWith(RUNNING);}+ /// The tree SHA at `path` in a commit, walking one directory at a time.+ async function treeAt(commit: string, path: string): Promise<string | null> {+ let tree: string = (await github('GET', `/repos/${repo()}/git/commits/${commit}`)).tree.sha;+ for (const part of path.split('/')) {+ const { tree: entries }: { tree: { path: string; type: string; sha: string }[] } = await github(+ 'GET',+ `/repos/${repo()}/git/trees/${tree}`,+ );+ const entry = entries.find((each) => each.path === part && each.type === 'tree');+ if (!entry) {+ return null;+ }+ tree = entry.sha;+ }+ return tree;+ }++ async function ciTrees(commit: string): Promise<CiTrees> {+ const trees: CiTrees = {};+ for (const path of CI_SOURCES) {+ trees[path] = await treeAt(commit, path);+ }+ return trees;+ }++ /// The CI sources a fork head changed since it branched from `base`.+ async function forkCiChanges(base: string, head: string): Promise<string[]> {+ const compare = await github('GET', `/repos/${repo()}/compare/${base}...${head}`);+ const changes = ciSourceChanges(await ciTrees(head), await ciTrees(compare.merge_base_commit.sha));+ if (changes.length > 0) {+ console.log(`${head.slice(0, 10)} changes ${changes.join(', ')}`);+ }+ return changes;+ }+async function removeLabel(pr: string) {await github('DELETE', `/repos/${repo()}/issues/${pr}/labels/${encodeURIComponent(label())}`);}@@ -250,6 +317,13 @@ jobs:const pr = env('PR_NUMBER');await removeLabel(pr);++ // The run the label starts would use the fork's own workflow files.+ if ((await forkCiChanges(env('BASE_REF'), head)).length > 0) {+ await setStatus(head, 'failure', FORK_CHANGES_CI, env('PR_CHECKS_URL'));+ return;+ }+await setStatus(head, 'pending', `${RUNNING} ${head.slice(0, 7)}`, env('PR_CHECKS_URL'));}@@ -266,6 +340,14 @@ jobs:return;}+ // Checked again here, from the run itself: the request step's view could be+ // stale, and the run's path is only known now.+ const distrust = forkRunDistrust(run.path, await forkCiChanges(env('BASE_REF'), head));+ if (distrust) {+ await setStatus(head, 'failure', distrust, run.html_url);+ return;+ }+const jobs: RunJob[] = [];for (let page = 1; ; page++) {const { jobs: batch } = await github(@@ -309,6 +391,7 @@ jobs:GH_TOKEN: ${{ github.token }}PR_NUMBER: ${{ github.event.pull_request.number }}HEAD_SHA: ${{ github.event.pull_request.head.sha }}+ BASE_REF: ${{ github.event.pull_request.base.ref }}PR_CHECKS_URL: ${{ github.event.pull_request.html_url }}/checksreport:if: github.event_name == 'workflow_run' && github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.head_repository.full_name != github.repository@@ -316,6 +399,7 @@ jobs:runs-on: ubuntu-latestpermissions:actions: read+ contents: readstatuses: writetimeout-minutes: 5steps:@@ -335,6 +419,10 @@ jobs:// fork after a fork PR's run, whose read-only token could not set the status//// Statuses go on the PR head commit, so a later push starts without one.+ //+ // A fork PR's run uses the workflow files of its merge commit, so a fork that+ // edits them decides its own verdict. Both fork steps fail closed instead: the+ // fork's CI sources must be the trees it branched from (`ciSourceChanges`).import { readFileSync } from 'node:fs';@@ -345,11 +433,19 @@ jobs:// Job names in `PR CI`; the fork step reads them back from the finished run.export const START_JOB = 'Start';export const GATE_JOB = 'CI gate';+ // The workflow whose jobs the fork step trusts, and the trees that define it:+ // the generated workflows and the Rust that generates them.+ export const PR_CI_PATH = '.github/workflows/pr-ci.yml';+ export const CI_SOURCES = ['.github', 'tooling/xtask/crates/xtask_workflows'] as const;+ export const FORK_CHANGES_CI =+ 'fork changes CI workflows: a maintainer must review and run CI from a branch';type State = 'pending' | 'success' | 'failure' | 'error';export type Needs = Record<string, { result: string }>;export type RunJob = { name: string; conclusion: string | null };type Outcome = { state: State; description: string } | null;+ /// Git tree SHA of each CI source at a commit; null where the path is missing.+ export type CiTrees = Record<string, string | null>;function env(name: string): string {const value = process.env[name];@@ -435,6 +531,25 @@ jobs:return null;}+ /// The CI sources whose tree differs between a fork head and the commit it+ /// branched from. A source missing from either side counts as changed.+ export function ciSourceChanges(head: CiTrees, base: CiTrees): string[] {+ return CI_SOURCES.filter((path) => !head[path] || !base[path] || head[path] !== base[path]);+ }++ /// Why a finished fork run can't be trusted, or null. The workflow is matched+ /// by name, so a fork can add another file named `PR CI`; and even `pr-ci.yml`+ /// is the fork's copy unless the CI sources are unchanged.+ export function forkRunDistrust(runPath: string, changes: string[]): string | null {+ if (changes.length > 0) {+ return FORK_CHANGES_CI;+ }+ if (runPath !== PR_CI_PATH) {+ return `The run came from ${runPath}, not ${PR_CI_PATH}`;+ }+ return null;+ }+async function setStatus(sha: string, state: State, description: string, targetUrl: string) {await github('POST', `/repos/${repo()}/statuses/${sha}`, {state,@@ -461,6 +576,41 @@ jobs:return status?.state === 'pending' && status.description.startsWith(RUNNING);}+ /// The tree SHA at `path` in a commit, walking one directory at a time.+ async function treeAt(commit: string, path: string): Promise<string | null> {+ let tree: string = (await github('GET', `/repos/${repo()}/git/commits/${commit}`)).tree.sha;+ for (const part of path.split('/')) {+ const { tree: entries }: { tree: { path: string; type: string; sha: string }[] } = await github(+ 'GET',+ `/repos/${repo()}/git/trees/${tree}`,+ );+ const entry = entries.find((each) => each.path === part && each.type === 'tree');+ if (!entry) {+ return null;+ }+ tree = entry.sha;+ }+ return tree;+ }++ async function ciTrees(commit: string): Promise<CiTrees> {+ const trees: CiTrees = {};+ for (const path of CI_SOURCES) {+ trees[path] = await treeAt(commit, path);+ }+ return trees;+ }++ /// The CI sources a fork head changed since it branched from `base`.+ async function forkCiChanges(base: string, head: string): Promise<string[]> {+ const compare = await github('GET', `/repos/${repo()}/compare/${base}...${head}`);+ const changes = ciSourceChanges(await ciTrees(head), await ciTrees(compare.merge_base_commit.sha));+ if (changes.length > 0) {+ console.log(`${head.slice(0, 10)} changes ${changes.join(', ')}`);+ }+ return changes;+ }+async function removeLabel(pr: string) {await github('DELETE', `/repos/${repo()}/issues/${pr}/labels/${encodeURIComponent(label())}`);}@@ -546,6 +696,13 @@ jobs:const pr = env('PR_NUMBER');await removeLabel(pr);++ // The run the label starts would use the fork's own workflow files.+ if ((await forkCiChanges(env('BASE_REF'), head)).length > 0) {+ await setStatus(head, 'failure', FORK_CHANGES_CI, env('PR_CHECKS_URL'));+ return;+ }+await setStatus(head, 'pending', `${RUNNING} ${head.slice(0, 7)}`, env('PR_CHECKS_URL'));}@@ -562,6 +719,14 @@ jobs:return;}+ // Checked again here, from the run itself: the request step's view could be+ // stale, and the run's path is only known now.+ const distrust = forkRunDistrust(run.path, await forkCiChanges(env('BASE_REF'), head));+ if (distrust) {+ await setStatus(head, 'failure', distrust, run.html_url);+ return;+ }+const jobs: RunJob[] = [];for (let page = 1; ; page++) {const { jobs: batch } = await github(@@ -603,3 +768,4 @@ jobs:CI_GATE_CONTEXT: CI gateCI_GATE_LABEL: run-ciGH_TOKEN: ${{ github.token }}+ BASE_REF: maindiff --git a/.github/workflows/ci-gate-reminder.yml b/.github/workflows/ci-gate-reminder.ymlindex 913569bb22..26877a0493 100644--- a/.github/workflows/ci-gate-reminder.yml+++ b/.github/workflows/ci-gate-reminder.yml@@ -34,6 +34,10 @@ jobs:// fork after a fork PR's run, whose read-only token could not set the status//// Statuses go on the PR head commit, so a later push starts without one.+ //+ // A fork PR's run uses the workflow files of its merge commit, so a fork that+ // edits them decides its own verdict. Both fork steps fail closed instead: the+ // fork's CI sources must be the trees it branched from (`ciSourceChanges`).import { readFileSync } from 'node:fs';@@ -44,11 +48,19 @@ jobs:// Job names in `PR CI`; the fork step reads them back from the finished run.export const START_JOB = 'Start';export const GATE_JOB = 'CI gate';+ // The workflow whose jobs the fork step trusts, and the trees that define it:+ // the generated workflows and the Rust that generates them.+ export const PR_CI_PATH = '.github/workflows/pr-ci.yml';+ export const CI_SOURCES = ['.github', 'tooling/xtask/crates/xtask_workflows'] as const;+ export const FORK_CHANGES_CI =+ 'fork changes CI workflows: a maintainer must review and run CI from a branch';type State = 'pending' | 'success' | 'failure' | 'error';export type Needs = Record<string, { result: string }>;export type RunJob = { name: string; conclusion: string | null };type Outcome = { state: State; description: string } | null;+ /// Git tree SHA of each CI source at a commit; null where the path is missing.+ export type CiTrees = Record<string, string | null>;function env(name: string): string {const value = process.env[name];@@ -134,6 +146,25 @@ jobs:return null;}+ /// The CI sources whose tree differs between a fork head and the commit it+ /// branched from. A source missing from either side counts as changed.+ export function ciSourceChanges(head: CiTrees, base: CiTrees): string[] {+ return CI_SOURCES.filter((path) => !head[path] || !base[path] || head[path] !== base[path]);+ }++ /// Why a finished fork run can't be trusted, or null. The workflow is matched+ /// by name, so a fork can add another file named `PR CI`; and even `pr-ci.yml`+ /// is the fork's copy unless the CI sources are unchanged.+ export function forkRunDistrust(runPath: string, changes: string[]): string | null {+ if (changes.length > 0) {+ return FORK_CHANGES_CI;+ }+ if (runPath !== PR_CI_PATH) {+ return `The run came from ${runPath}, not ${PR_CI_PATH}`;+ }+ return null;+ }+async function setStatus(sha: string, state: State, description: string, targetUrl: string) {await github('POST', `/repos/${repo()}/statuses/${sha}`, {state,@@ -160,6 +191,41 @@ jobs:return status?.state === 'pending' && status.description.startsWith(RUNNING);}+ /// The tree SHA at `path` in a commit, walking one directory at a time.+ async function treeAt(commit: string, path: string): Promise<string | null> {+ let tree: string = (await github('GET', `/repos/${repo()}/git/commits/${commit}`)).tree.sha;+ for (const part of path.split('/')) {+ const { tree: entries }: { tree: { path: string; type: string; sha: string }[] } = await github(+ 'GET',+ `/repos/${repo()}/git/trees/${tree}`,+ );+ const entry = entries.find((each) => each.path === part && each.type === 'tree');+ if (!entry) {+ return null;+ }+ tree = entry.sha;+ }+ return tree;+ }++ async function ciTrees(commit: string): Promise<CiTrees> {+ const trees: CiTrees = {};+ for (const path of CI_SOURCES) {+ trees[path] = await treeAt(commit, path);+ }+ return trees;+ }++ /// The CI sources a fork head changed since it branched from `base`.+ async function forkCiChanges(base: string, head: string): Promise<string[]> {+ const compare = await github('GET', `/repos/${repo()}/compare/${base}...${head}`);+ const changes = ciSourceChanges(await ciTrees(head), await ciTrees(compare.merge_base_commit.sha));+ if (changes.length > 0) {+ console.log(`${head.slice(0, 10)} changes ${changes.join(', ')}`);+ }+ return changes;+ }+async function removeLabel(pr: string) {await github('DELETE', `/repos/${repo()}/issues/${pr}/labels/${encodeURIComponent(label())}`);}@@ -245,6 +311,13 @@ jobs:const pr = env('PR_NUMBER');await removeLabel(pr);++ // The run the label starts would use the fork's own workflow files.+ if ((await forkCiChanges(env('BASE_REF'), head)).length > 0) {+ await setStatus(head, 'failure', FORK_CHANGES_CI, env('PR_CHECKS_URL'));+ return;+ }+await setStatus(head, 'pending', `${RUNNING} ${head.slice(0, 7)}`, env('PR_CHECKS_URL'));}@@ -261,6 +334,14 @@ jobs:return;}+ // Checked again here, from the run itself: the request step's view could be+ // stale, and the run's path is only known now.+ const distrust = forkRunDistrust(run.path, await forkCiChanges(env('BASE_REF'), head));+ if (distrust) {+ await setStatus(head, 'failure', distrust, run.html_url);+ return;+ }+const jobs: RunJob[] = [];for (let page = 1; ; page++) {const { jobs: batch } = await github(diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.ymlindex 3005e39842..de19166951 100644--- a/.github/workflows/pr-ci.yml+++ b/.github/workflows/pr-ci.yml@@ -43,6 +43,10 @@ jobs:// fork after a fork PR's run, whose read-only token could not set the status//// Statuses go on the PR head commit, so a later push starts without one.+ //+ // A fork PR's run uses the workflow files of its merge commit, so a fork that+ // edits them decides its own verdict. Both fork steps fail closed instead: the+ // fork's CI sources must be the trees it branched from (`ciSourceChanges`).import { readFileSync } from 'node:fs';@@ -53,11 +57,19 @@ jobs:// Job names in `PR CI`; the fork step reads them back from the finished run.export const START_JOB = 'Start';export const GATE_JOB = 'CI gate';+ // The workflow whose jobs the fork step trusts, and the trees that define it:+ // the generated workflows and the Rust that generates them.+ export const PR_CI_PATH = '.github/workflows/pr-ci.yml';+ export const CI_SOURCES = ['.github', 'tooling/xtask/crates/xtask_workflows'] as const;+ export const FORK_CHANGES_CI =+ 'fork changes CI workflows: a maintainer must review and run CI from a branch';type State = 'pending' | 'success' | 'failure' | 'error';export type Needs = Record<string, { result: string }>;export type RunJob = { name: string; conclusion: string | null };type Outcome = { state: State; description: string } | null;+ /// Git tree SHA of each CI source at a commit; null where the path is missing.+ export type CiTrees = Record<string, string | null>;function env(name: string): string {const value = process.env[name];@@ -143,6 +155,25 @@ jobs:return null;}+ /// The CI sources whose tree differs between a fork head and the commit it+ /// branched from. A source missing from either side counts as changed.+ export function ciSourceChanges(head: CiTrees, base: CiTrees): string[] {+ return CI_SOURCES.filter((path) => !head[path] || !base[path] || head[path] !== base[path]);+ }++ /// Why a finished fork run can't be trusted, or null. The workflow is matched+ /// by name, so a fork can add another file named `PR CI`; and even `pr-ci.yml`+ /// is the fork's copy unless the CI sources are unchanged.+ export function forkRunDistrust(runPath: string, changes: string[]): string | null {+ if (changes.length > 0) {+ return FORK_CHANGES_CI;+ }+ if (runPath !== PR_CI_PATH) {+ return `The run came from ${runPath}, not ${PR_CI_PATH}`;+ }+ return null;+ }+async function setStatus(sha: string, state: State, description: string, targetUrl: string) {await github('POST', `/repos/${repo()}/statuses/${sha}`, {state,@@ -169,6 +200,41 @@ jobs:return status?.state === 'pending' && status.description.startsWith(RUNNING);}+ /// The tree SHA at `path` in a commit, walking one directory at a time.+ async function treeAt(commit: string, path: string): Promise<string | null> {+ let tree: string = (await github('GET', `/repos/${repo()}/git/commits/${commit}`)).tree.sha;+ for (const part of path.split('/')) {+ const { tree: entries }: { tree: { path: string; type: string; sha: string }[] } = await github(+ 'GET',+ `/repos/${repo()}/git/trees/${tree}`,+ );+ const entry = entries.find((each) => each.path === part && each.type === 'tree');+ if (!entry) {+ return null;+ }+ tree = entry.sha;+ }+ return tree;+ }++ async function ciTrees(commit: string): Promise<CiTrees> {+ const trees: CiTrees = {};+ for (const path of CI_SOURCES) {+ trees[path] = await treeAt(commit, path);+ }+ return trees;+ }++ /// The CI sources a fork head changed since it branched from `base`.+ async function forkCiChanges(base: string, head: string): Promise<string[]> {+ const compare = await github('GET', `/repos/${repo()}/compare/${base}...${head}`);+ const changes = ciSourceChanges(await ciTrees(head), await ciTrees(compare.merge_base_commit.sha));+ if (changes.length > 0) {+ console.log(`${head.slice(0, 10)} changes ${changes.join(', ')}`);+ }+ return changes;+ }+async function removeLabel(pr: string) {await github('DELETE', `/repos/${repo()}/issues/${pr}/labels/${encodeURIComponent(label())}`);}@@ -254,6 +320,13 @@ jobs:const pr = env('PR_NUMBER');await removeLabel(pr);++ // The run the label starts would use the fork's own workflow files.+ if ((await forkCiChanges(env('BASE_REF'), head)).length > 0) {+ await setStatus(head, 'failure', FORK_CHANGES_CI, env('PR_CHECKS_URL'));+ return;+ }+await setStatus(head, 'pending', `${RUNNING} ${head.slice(0, 7)}`, env('PR_CHECKS_URL'));}@@ -270,6 +343,14 @@ jobs:return;}+ // Checked again here, from the run itself: the request step's view could be+ // stale, and the run's path is only known now.+ const distrust = forkRunDistrust(run.path, await forkCiChanges(env('BASE_REF'), head));+ if (distrust) {+ await setStatus(head, 'failure', distrust, run.html_url);+ return;+ }+const jobs: RunJob[] = [];for (let page = 1; ; page++) {const { jobs: batch } = await github(@@ -485,6 +566,10 @@ jobs:// fork after a fork PR's run, whose read-only token could not set the status//// Statuses go on the PR head commit, so a later push starts without one.+ //+ // A fork PR's run uses the workflow files of its merge commit, so a fork that+ // edits them decides its own verdict. Both fork steps fail closed instead: the+ // fork's CI sources must be the trees it branched from (`ciSourceChanges`).import { readFileSync } from 'node:fs';@@ -495,11 +580,19 @@ jobs:// Job names in `PR CI`; the fork step reads them back from the finished run.export const START_JOB = 'Start';export const GATE_JOB = 'CI gate';+ // The workflow whose jobs the fork step trusts, and the trees that define it:+ // the generated workflows and the Rust that generates them.+ export const PR_CI_PATH = '.github/workflows/pr-ci.yml';+ export const CI_SOURCES = ['.github', 'tooling/xtask/crates/xtask_workflows'] as const;+ export const FORK_CHANGES_CI =+ 'fork changes CI workflows: a maintainer must review and run CI from a branch';type State = 'pending' | 'success' | 'failure' | 'error';export type Needs = Record<string, { result: string }>;export type RunJob = { name: string; conclusion: string | null };type Outcome = { state: State; description: string } | null;+ /// Git tree SHA of each CI source at a commit; null where the path is missing.+ export type CiTrees = Record<string, string | null>;function env(name: string): string {const value = process.env[name];@@ -585,6 +678,25 @@ jobs:return null;}+ /// The CI sources whose tree differs between a fork head and the commit it+ /// branched from. A source missing from either side counts as changed.+ export function ciSourceChanges(head: CiTrees, base: CiTrees): string[] {+ return CI_SOURCES.filter((path) => !head[path] || !base[path] || head[path] !== base[path]);+ }++ /// Why a finished fork run can't be trusted, or null. The workflow is matched+ /// by name, so a fork can add another file named `PR CI`; and even `pr-ci.yml`+ /// is the fork's copy unless the CI sources are unchanged.+ export function forkRunDistrust(runPath: string, changes: string[]): string | null {+ if (changes.length > 0) {+ return FORK_CHANGES_CI;+ }+ if (runPath !== PR_CI_PATH) {+ return `The run came from ${runPath}, not ${PR_CI_PATH}`;+ }+ return null;+ }+async function setStatus(sha: string, state: State, description: string, targetUrl: string) {await github('POST', `/repos/${repo()}/statuses/${sha}`, {state,@@ -611,6 +723,41 @@ jobs:return status?.state === 'pending' && status.description.startsWith(RUNNING);}+ /// The tree SHA at `path` in a commit, walking one directory at a time.+ async function treeAt(commit: string, path: string): Promise<string | null> {+ let tree: string = (await github('GET', `/repos/${repo()}/git/commits/${commit}`)).tree.sha;+ for (const part of path.split('/')) {+ const { tree: entries }: { tree: { path: string; type: string; sha: string }[] } = await github(+ 'GET',+ `/repos/${repo()}/git/trees/${tree}`,+ );+ const entry = entries.find((each) => each.path === part && each.type === 'tree');+ if (!entry) {+ return null;+ }+ tree = entry.sha;+ }+ return tree;+ }++ async function ciTrees(commit: string): Promise<CiTrees> {+ const trees: CiTrees = {};+ for (const path of CI_SOURCES) {+ trees[path] = await treeAt(commit, path);+ }+ return trees;+ }++ /// The CI sources a fork head changed since it branched from `base`.+ async function forkCiChanges(base: string, head: string): Promise<string[]> {+ const compare = await github('GET', `/repos/${repo()}/compare/${base}...${head}`);+ const changes = ciSourceChanges(await ciTrees(head), await ciTrees(compare.merge_base_commit.sha));+ if (changes.length > 0) {+ console.log(`${head.slice(0, 10)} changes ${changes.join(', ')}`);+ }+ return changes;+ }+async function removeLabel(pr: string) {await github('DELETE', `/repos/${repo()}/issues/${pr}/labels/${encodeURIComponent(label())}`);}@@ -696,6 +843,13 @@ jobs:const pr = env('PR_NUMBER');await removeLabel(pr);++ // The run the label starts would use the fork's own workflow files.+ if ((await forkCiChanges(env('BASE_REF'), head)).length > 0) {+ await setStatus(head, 'failure', FORK_CHANGES_CI, env('PR_CHECKS_URL'));+ return;+ }+await setStatus(head, 'pending', `${RUNNING} ${head.slice(0, 7)}`, env('PR_CHECKS_URL'));}@@ -712,6 +866,14 @@ jobs:return;}+ // Checked again here, from the run itself: the request step's view could be+ // stale, and the run's path is only known now.+ const distrust = forkRunDistrust(run.path, await forkCiChanges(env('BASE_REF'), head));+ if (distrust) {+ await setStatus(head, 'failure', distrust, run.html_url);+ return;+ }+const jobs: RunJob[] = [];for (let page = 1; ; page++) {const { jobs: batch } = await github(diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/ci_gate_fork.rs b/tooling/xtask/crates/xtask_workflows/src/workflows/ci_gate_fork.rsindex 4e73d91a21..f0491f683f 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/ci_gate_fork.rs+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/ci_gate_fork.rs@@ -5,8 +5,12 @@//! out the fork's code. When the label goes on, `request` removes it and opens//! a pending `CI gate` request on the head it was added at. When the fork's run//! ends, `report` copies its verdict, but only onto a commit with an open-//! request: a fork token can't set statuses, so a run on any other commit, such-//! as one pushed with a rewritten workflow, reports nothing.+//! request: a fork token can't set statuses, so a run on any other commit+//! reports nothing.+//!+//! The fork's run uses the fork's workflow files, so both jobs fail closed when+//! the fork changed `.github` or the workflow generator, and `report` trusts+//! only a run of `pr-ci.yml`.use gh_workflow::{Event, Expression, Job, Level, Permissions, PullRequestTarget, PullRequestType, Workflow,@@ -46,6 +50,7 @@ fn request() -> Job {.runs_on("ubuntu-latest").timeout_minutes(5u32).permissions(Permissions {+ contents: Some(Level::Read),issues: Some(Level::Write),pull_requests: Some(Level::Write),statuses: Some(Level::Write),@@ -55,6 +60,7 @@ fn request() -> Job {pr_ci::gate_step("Open the CI gate request", "fork-request").add_env(("PR_NUMBER", "${{ github.event.pull_request.number }}")).add_env(("HEAD_SHA", "${{ github.event.pull_request.head.sha }}"))+ .add_env(("BASE_REF", "${{ github.event.pull_request.base.ref }}")).add_env(("PR_CHECKS_URL","${{ github.event.pull_request.html_url }}/checks",@@ -72,8 +78,10 @@ fn report() -> Job {.timeout_minutes(5u32).permissions(Permissions {actions: Some(Level::Read),+ contents: Some(Level::Read),statuses: Some(Level::Write),..Default::default()})- .add_step(pr_ci::gate_step("Set the CI gate status", "fork"))+ // `PR CI` runs only for pull requests into `main`.+ .add_step(pr_ci::gate_step("Set the CI gate status", "fork").add_env(("BASE_REF", "main")))}diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.test.ts b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.test.tsindex 09d0493412..b55e816782 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.test.ts+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.test.ts@@ -2,7 +2,17 @@import assert from 'node:assert/strict';import { describe, test } from 'node:test';-import { forkOutcome, GATE_JOB, gateOutcome, isOpenRequest, START_JOB } from './ci_gate.ts';+import {+ ciSourceChanges,+ FORK_CHANGES_CI,+ forkOutcome,+ forkRunDistrust,+ GATE_JOB,+ gateOutcome,+ isOpenRequest,+ PR_CI_PATH,+ START_JOB,+} from './ci_gate.ts';const HEAD = '0123456789abcdef0123456789abcdef01234567';@@ -131,3 +141,45 @@ describe('isOpenRequest', () => {assert.equal(isOpenRequest(undefined), false);});});++describe('ciSourceChanges', () => {+ const base = { '.github': 'aaa', 'tooling/xtask/crates/xtask_workflows': 'bbb' };++ test('a fork that left the CI sources alone changes nothing', () => {+ assert.deepEqual(ciSourceChanges({ ...base }, base), []);+ });++ test('names each source whose tree differs', () => {+ assert.deepEqual(ciSourceChanges({ ...base, '.github': 'ccc' }, base), ['.github']);+ assert.deepEqual(+ ciSourceChanges({ '.github': 'ccc', 'tooling/xtask/crates/xtask_workflows': 'ddd' }, base),+ ['.github', 'tooling/xtask/crates/xtask_workflows'],+ );+ });++ test('a source missing on either side counts as changed', () => {+ assert.deepEqual(ciSourceChanges({ ...base, '.github': null }, base), ['.github']);+ assert.deepEqual(+ ciSourceChanges(base, { ...base, 'tooling/xtask/crates/xtask_workflows': null }),+ ['tooling/xtask/crates/xtask_workflows'],+ );+ });+});++describe('forkRunDistrust', () => {+ test('trusts a pr-ci.yml run of a fork that left the CI sources alone', () => {+ assert.equal(forkRunDistrust(PR_CI_PATH, []), null);+ });++ test('a fork that changed the CI sources fails, whatever its jobs say', () => {+ assert.equal(forkRunDistrust(PR_CI_PATH, ['.github']), FORK_CHANGES_CI);+ assert.equal(forkRunDistrust('.github/workflows/other.yml', ['.github']), FORK_CHANGES_CI);+ });++ test('another workflow named PR CI is not trusted', () => {+ assert.equal(+ forkRunDistrust('.github/workflows/other.yml', []),+ `The run came from .github/workflows/other.yml, not ${PR_CI_PATH}`,+ );+ });+});diff --git a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.ts b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.tsindex 7e50caf2e2..8ffddb7488 100644--- a/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.ts+++ b/tooling/xtask/crates/xtask_workflows/src/workflows/scripts/ci_gate.ts@@ -11,6 +11,10 @@// fork after a fork PR's run, whose read-only token could not set the status//// Statuses go on the PR head commit, so a later push starts without one.+//+// A fork PR's run uses the workflow files of its merge commit, so a fork that+// edits them decides its own verdict. Both fork steps fail closed instead: the+// fork's CI sources must be the trees it branched from (`ciSourceChanges`).import { readFileSync } from 'node:fs';@@ -21,11 +25,19 @@ const RUNNING = 'Running on';// Job names in `PR CI`; the fork step reads them back from the finished run.export const START_JOB = 'Start';export const GATE_JOB = 'CI gate';+// The workflow whose jobs the fork step trusts, and the trees that define it:+// the generated workflows and the Rust that generates them.+export const PR_CI_PATH = '.github/workflows/pr-ci.yml';+export const CI_SOURCES = ['.github', 'tooling/xtask/crates/xtask_workflows'] as const;+export const FORK_CHANGES_CI =+ 'fork changes CI workflows: a maintainer must review and run CI from a branch';type State = 'pending' | 'success' | 'failure' | 'error';export type Needs = Record<string, { result: string }>;export type RunJob = { name: string; conclusion: string | null };type Outcome = { state: State; description: string } | null;+/// Git tree SHA of each CI source at a commit; null where the path is missing.+export type CiTrees = Record<string, string | null>;function env(name: string): string {const value = process.env[name];@@ -111,6 +123,25 @@ export function forkOutcome(jobs: RunJob[], head: string, labelName: string): Oureturn null;}+/// The CI sources whose tree differs between a fork head and the commit it+/// branched from. A source missing from either side counts as changed.+export function ciSourceChanges(head: CiTrees, base: CiTrees): string[] {+ return CI_SOURCES.filter((path) => !head[path] || !base[path] || head[path] !== base[path]);+}++/// Why a finished fork run can't be trusted, or null. The workflow is matched+/// by name, so a fork can add another file named `PR CI`; and even `pr-ci.yml`+/// is the fork's copy unless the CI sources are unchanged.+export function forkRunDistrust(runPath: string, changes: string[]): string | null {+ if (changes.length > 0) {+ return FORK_CHANGES_CI;+ }+ if (runPath !== PR_CI_PATH) {+ return `The run came from ${runPath}, not ${PR_CI_PATH}`;+ }+ return null;+}+async function setStatus(sha: string, state: State, description: string, targetUrl: string) {await github('POST', `/repos/${repo()}/statuses/${sha}`, {state,@@ -137,6 +168,41 @@ export function isOpenRequest(status: { state: string; description: string } | ureturn status?.state === 'pending' && status.description.startsWith(RUNNING);}+/// The tree SHA at `path` in a commit, walking one directory at a time.+async function treeAt(commit: string, path: string): Promise<string | null> {+ let tree: string = (await github('GET', `/repos/${repo()}/git/commits/${commit}`)).tree.sha;+ for (const part of path.split('/')) {+ const { tree: entries }: { tree: { path: string; type: string; sha: string }[] } = await github(+ 'GET',+ `/repos/${repo()}/git/trees/${tree}`,+ );+ const entry = entries.find((each) => each.path === part && each.type === 'tree');+ if (!entry) {+ return null;+ }+ tree = entry.sha;+ }+ return tree;+}++async function ciTrees(commit: string): Promise<CiTrees> {+ const trees: CiTrees = {};+ for (const path of CI_SOURCES) {+ trees[path] = await treeAt(commit, path);+ }+ return trees;+}++/// The CI sources a fork head changed since it branched from `base`.+async function forkCiChanges(base: string, head: string): Promise<string[]> {+ const compare = await github('GET', `/repos/${repo()}/compare/${base}...${head}`);+ const changes = ciSourceChanges(await ciTrees(head), await ciTrees(compare.merge_base_commit.sha));+ if (changes.length > 0) {+ console.log(`${head.slice(0, 10)} changes ${changes.join(', ')}`);+ }+ return changes;+}+async function removeLabel(pr: string) {await github('DELETE', `/repos/${repo()}/issues/${pr}/labels/${encodeURIComponent(label())}`);}@@ -222,6 +288,13 @@ async function forkRequest() {const pr = env('PR_NUMBER');await removeLabel(pr);++ // The run the label starts would use the fork's own workflow files.+ if ((await forkCiChanges(env('BASE_REF'), head)).length > 0) {+ await setStatus(head, 'failure', FORK_CHANGES_CI, env('PR_CHECKS_URL'));+ return;+ }+await setStatus(head, 'pending', `${RUNNING} ${head.slice(0, 7)}`, env('PR_CHECKS_URL'));}@@ -238,6 +311,14 @@ async function fork() {return;}+ // Checked again here, from the run itself: the request step's view could be+ // stale, and the run's path is only known now.+ const distrust = forkRunDistrust(run.path, await forkCiChanges(env('BASE_REF'), head));+ if (distrust) {+ await setStatus(head, 'failure', distrust, run.html_url);+ return;+ }+const jobs: RunJob[] = [];for (let page = 1; ; page++) {const { jobs: batch } = await github(