diff --git a/.github/workflows/worker-version-upgrade.lock.yml b/.github/workflows/worker-version-upgrade.lock.yml index 88b99aa21..939dd91cb 100644 --- a/.github/workflows/worker-version-upgrade.lock.yml +++ b/.github/workflows/worker-version-upgrade.lock.yml @@ -1,4 +1,4 @@ -# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"8da589511e1413ea0d84ab82a748d2a1cb11cddec70861ec5a55932d28afa462","body_hash":"ee5c7ca429dc819894ac6931fa62a2d96aa596bcde626d0ec82761179c3a5b4e","compiler_version":"v1.2.2","strict":true,"agent_id":"copilot","engine_versions":{"copilot":"1.0.103"}} +# gh-aw-metadata: {"schema_version":"v4","frontmatter_hash":"a9ab3952426ab1c17a1cc11b3b836c151af30de2b173983b604a26cea2d3f5cc","body_hash":"ee5c7ca429dc819894ac6931fa62a2d96aa596bcde626d0ec82761179c3a5b4e","compiler_version":"v1.2.2","strict":true,"agent_id":"copilot","engine_versions":{"copilot":"1.0.103"}} # gh-aw-manifest: {"version":1,"secrets":["COPILOT_GITHUB_TOKEN","GH_AW_AGENT_TOKEN","GH_AW_DEFAULT_OTLP_ENDPOINT","GH_AW_DEFAULT_OTLP_HEADERS","GH_AW_GITHUB_MCP_SERVER_TOKEN","GH_AW_GITHUB_TOKEN","GITHUB_TOKEN"],"actions":[{"repo":"actions/checkout","sha":"3d3c42e5aac5ba805825da76410c181273ba90b1","version":"v7.0.1"},{"repo":"actions/download-artifact","sha":"9000827ccba6bdab643e8b6fd33ac0654aef8333","version":"v8.0.2"},{"repo":"actions/github-script","sha":"3a2844b7e9c422d3c10d287c895573f7108da1b3","version":"v9.0.0"},{"repo":"actions/setup-node","sha":"949feb2413d6458794dcd2491c4babbbce0c15c1","version":"v7.1.0"},{"repo":"actions/upload-artifact","sha":"cf430e030ddbb5b0abf93d22962f4752f3646cd9","version":"v7.0.2"},{"repo":"github/gh-aw-actions/setup","sha":"53d0749e19424a918acd82b4d140264ce48f2f14","version":"v1.2.2"}],"containers":[{"image":"ghcr.io/github/gh-aw-firewall/agent:0.29.11","digest":"sha256:8774a73c351395b41d2ad0af4ddb4689007c67de3cd08bd9f43bb8a0e300690e","pinned_image":"ghcr.io/github/gh-aw-firewall/agent:0.29.11@sha256:8774a73c351395b41d2ad0af4ddb4689007c67de3cd08bd9f43bb8a0e300690e"},{"image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.29.11","digest":"sha256:88d2d0e315a504d93fd4fb8847fd0f6b7f4469f41a6fc6047b3dc9654c56ab2c","pinned_image":"ghcr.io/github/gh-aw-firewall/api-proxy:0.29.11@sha256:88d2d0e315a504d93fd4fb8847fd0f6b7f4469f41a6fc6047b3dc9654c56ab2c"},{"image":"ghcr.io/github/gh-aw-firewall/squid:0.29.11","digest":"sha256:861739ecf924dd2db5e5fb652db2ad82aaafd1615623297ddabc8f41a36b2847","pinned_image":"ghcr.io/github/gh-aw-firewall/squid:0.29.11@sha256:861739ecf924dd2db5e5fb652db2ad82aaafd1615623297ddabc8f41a36b2847"},{"image":"ghcr.io/github/gh-aw-mcpg:v0.5.6","digest":"sha256:342cd624e1f179d91be2de7a146fc4f387092e62e6c8a47506d7932e113d2311","pinned_image":"ghcr.io/github/gh-aw-mcpg:v0.5.6@sha256:342cd624e1f179d91be2de7a146fc4f387092e62e6c8a47506d7932e113d2311"},{"image":"ghcr.io/github/gh-aw-node","digest":"sha256:8735428c2d4e602f90781ba0c571c400e8ff93c7ee1508a75d71f87e34e415cc","pinned_image":"ghcr.io/github/gh-aw-node@sha256:8735428c2d4e602f90781ba0c571c400e8ff93c7ee1508a75d71f87e34e415cc"},{"image":"ghcr.io/github/github-mcp-server:v2.1.0","digest":"sha256:f6056fc10bfffcc8c81a6417a2e9ca12808647e8dd6049642a4feaadee0e0f8a","pinned_image":"ghcr.io/github/github-mcp-server:v2.1.0@sha256:f6056fc10bfffcc8c81a6417a2e9ca12808647e8dd6049642a4feaadee0e0f8a"}],"mcp_servers":[{"name":"github","tools":["get_commit","get_file_contents","get_latest_release","get_me","get_pull_request","get_pull_request_comments","get_pull_request_diff","get_pull_request_files","get_pull_request_review_comments","get_pull_request_reviews","get_pull_request_status","get_release_by_tag","get_tag","issue_read","list_branches","list_commits","list_issue_types","list_issues","list_pull_requests","list_releases","list_starred_repositories","list_tags","pull_request_read","search_code","search_issues","search_pull_requests","search_repositories"]},{"name":"safeoutputs","tools":["assign_to_agent","noop","report_incomplete","report_missing_data","report_missing_tool"]}],"threat_detection":{"mode":"enabled"},"admission":false,"agent_metadata":{"03cbf8931deff21d5116":{"name":"Agentic Workflows"}}} # This file was automatically generated by gh-aw (v1.2.2). DO NOT EDIT. To debug this workflow, load the skill at https://github.com/github/gh-aw/blob/main/debug.md # @@ -60,14 +60,6 @@ on: types: - opened - edited - # steps: # Steps injected into pre-activation job - # - env: - # LABELS: ${{ toJSON(github.event.issue.labels.*.name) }} - # id: label_check - # if: github.event_name != 'workflow_dispatch' - # name: Check worker update label - # run: | - # echo "$LABELS" | grep -q '"type: worker-update"' workflow_dispatch: inputs: aw_context: @@ -101,7 +93,7 @@ jobs: name: activation needs: pre_activation if: > - needs.pre_activation.outputs.activated == 'true' && (github.event_name == 'workflow_dispatch' || needs.pre_activation.outputs.label_check_result == 'success') + needs.pre_activation.outputs.activated == 'true' && (github.event_name == 'workflow_dispatch' || contains(github.event.issue.labels.*.name, format('type:{0}worker-update', ' '))) runs-on: ubuntu-slim # Permissions for the activation job (workflow permissions default to none). permissions: @@ -2163,12 +2155,13 @@ jobs: pre_activation: name: pre_activation + if: > + github.event_name == 'workflow_dispatch' || contains(github.event.issue.labels.*.name, format('type:{0}worker-update', ' ')) runs-on: ubuntu-slim env: GH_AW_RUNTIME_FEATURES: ${{ vars.GH_AW_RUNTIME_FEATURES }} outputs: activated: ${{ steps.check_membership.outputs.is_team_member == 'true' }} - label_check_result: ${{ steps.label_check.outcome }} matched_command: '' setup-parent-span-id: ${{ steps.setup.outputs.parent-span-id || steps.setup.outputs.span-id }} setup-span-id: ${{ steps.setup.outputs.span-id }} @@ -2201,13 +2194,6 @@ jobs: setupGlobals(core, github, context, exec, io, getOctokit); const { main } = require(path.join(actionsDir, 'check_membership.cjs')); await main(); - - name: Check worker update label - id: label_check - if: github.event_name != 'workflow_dispatch' - run: | - echo "$LABELS" | grep -q '"type: worker-update"' - env: - LABELS: ${{ toJSON(github.event.issue.labels.*.name) }} safe_outputs: name: safe_outputs diff --git a/.github/workflows/worker-version-upgrade.md b/.github/workflows/worker-version-upgrade.md index 1fa463a61..e59167a07 100644 --- a/.github/workflows/worker-version-upgrade.md +++ b/.github/workflows/worker-version-upgrade.md @@ -9,18 +9,9 @@ on: types: [opened, edited] names: ["type: worker-update"] workflow_dispatch: - # Workaround: gh-aw compiler bug — `names:` is commented out in the lock file - # and no label condition is injected. Use on.steps to enforce label filtering. - steps: - - name: Check worker update label - id: label_check - if: github.event_name != 'workflow_dispatch' - env: - LABELS: ${{ toJSON(github.event.issue.labels.*.name) }} - run: | - echo "$LABELS" | grep -q '"type: worker-update"' - -if: github.event_name == 'workflow_dispatch' || needs.pre_activation.outputs.label_check_result == 'success' + +# The compiler comments out `names:`; format avoids a colon-space in emitted YAML. +if: "github.event_name == 'workflow_dispatch' || contains(github.event.issue.labels.*.name, format('type:{0}worker-update', ' '))" permissions: contents: read diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ce13c577d..d965ae089 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -516,6 +516,18 @@ The daily schedules are explicit cron expressions preserving their existing UTC Review changes to `.github/aw/actions-lock.json` and generated workflow permissions, triggers, action pins, and handler configuration before merging. +To test the worker-version-upgrade gate locally, put [`act`](https://github.com/nektos/act) +on your `PATH` (tested with v0.2.82) and run from the repository root: + +```bash +SCOPE_TEST_WORKFLOW_ACT=1 pnpm exec vitest run scripts/worker-version-upgrade.test.ts +``` + +The opt-in test feeds synthetic issue and dispatch events into a temporary workflow +that copies the generated job conditions. It stubs membership and asserts which +jobs ran or skipped. It uses no Docker containers or GitHub/Copilot credentials, +and does not test the hosted membership lookup, agent, or write operations. + `.github/dependabot.yml` specifies namespaced labels for the root pnpm workspace, the separate website package, and the Rust gateway. Each entry has `open-pull-requests-limit: 0` to keep version-update PRs disabled; the required schedule does not enable those PRs. These labels also diff --git a/scripts/fixtures/worker-version-upgrade-events.json b/scripts/fixtures/worker-version-upgrade-events.json new file mode 100644 index 000000000..7e750ad26 --- /dev/null +++ b/scripts/fixtures/worker-version-upgrade-events.json @@ -0,0 +1,38 @@ +[ + { + "name": "unlabeled issue opened", + "eventName": "issues", + "payload": { "action": "opened", "issue": { "number": 1, "labels": [] } }, + "checksMembership": false + }, + { + "name": "issue with unrelated and near-match labels edited", + "eventName": "issues", + "payload": { + "action": "edited", + "issue": { "number": 1, "labels": [{ "name": "bug" }, { "name": "type: worker-update-extra" }] } + }, + "checksMembership": false + }, + { + "name": "issue opened with the worker update label", + "eventName": "issues", + "payload": { "action": "opened", "issue": { "number": 1, "labels": [{ "name": "type: worker-update" }] } }, + "checksMembership": true + }, + { + "name": "issue edited with the worker update label alongside another label", + "eventName": "issues", + "payload": { + "action": "edited", + "issue": { "number": 1, "labels": [{ "name": "bug" }, { "name": "type: worker-update" }] } + }, + "checksMembership": true + }, + { + "name": "manual dispatch without an issue", + "eventName": "workflow_dispatch", + "payload": { "ref": "refs/heads/main", "inputs": {} }, + "checksMembership": true + } +] diff --git a/scripts/worker-version-upgrade.test.ts b/scripts/worker-version-upgrade.test.ts new file mode 100644 index 000000000..7faac8466 --- /dev/null +++ b/scripts/worker-version-upgrade.test.ts @@ -0,0 +1,134 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { spawnSync } from "node:child_process"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { describe, expect, it } from "vitest"; + +const { parse }: { parse: (source: string) => unknown } = + createRequire(resolve("packages/shared/package.json"))("yaml"); + +/** A synthetic GitHub event and its expected label-gate outcome. */ +interface EventFixture { + /** Case name shown by Vitest. */ + name: string; + /** Event passed to act independently of the payload. */ + eventName: "issues" | "workflow_dispatch"; + /** Contents of the GitHub event file. */ + payload: Record; + /** Whether the event should reach the membership check. */ + checksMembership: boolean; +} + +const fixtures: EventFixture[] = JSON.parse(readFileSync( + new URL("./fixtures/worker-version-upgrade-events.json", import.meta.url), "utf8", +)); +const lock = parse(readFileSync(resolve(".github/workflows/worker-version-upgrade.lock.yml"), "utf8")) as { + on: { issues: { types: string[] }; workflow_dispatch: unknown }; + jobs: Record; + steps?: { id?: string; env?: Record }[]; + }>; +}; + +describe("Worker Version Upgrade activation", () => { + it("filters issue labels without failing pre-activation and preserves dispatch and membership checks", () => { + const source = readFileSync(".github/workflows/worker-version-upgrade.md", "utf8"); + const frontmatter = parse(source.split("---")[1]) as { + on: { issues: { types: string[]; names: string[] }; workflow_dispatch: null; steps?: unknown }; + if: string; + }; + const condition = "github.event_name == 'workflow_dispatch' || contains(github.event.issue.labels.*.name, format('type:{0}worker-update', ' '))"; + + // Array membership requires the complete label, whether alone or among other labels. + expect(frontmatter.on.issues).toEqual({ types: ["opened", "edited"], names: ["type: worker-update"] }); + expect(frontmatter.on.workflow_dispatch).toBeNull(); + expect(frontmatter.on.steps).toBeUndefined(); + expect(frontmatter.if).toBe(condition); + expect(lock.on.issues.types).toEqual(["opened", "edited"]); + expect(lock.on.workflow_dispatch).toBeDefined(); + expect(lock.jobs.pre_activation.if?.trim()).toBe(condition); + expect(lock.jobs.pre_activation.steps?.some((step) => step.id === "label_check")).toBe(false); + + // Dispatch bypasses the label filter, but both paths still require membership. + expect(lock.jobs.pre_activation.outputs?.activated).toBe("${{ steps.check_membership.outputs.is_team_member == 'true' }}"); + expect(lock.jobs.pre_activation.steps?.find((step) => step.id === "check_membership")?.env?.GH_AW_REQUIRED_ROLES) + .toBe("admin,maintainer,write"); + expect(lock.jobs.activation.needs).toBe("pre_activation"); + expect(lock.jobs.activation.if?.trim()).toBe(`needs.pre_activation.outputs.activated == 'true' && (${condition})`); + expect(lock.jobs.agent.needs).toBe("activation"); + }); + + describe.skipIf(process.env.SCOPE_TEST_WORKFLOW_ACT !== "1")("Worker Version Upgrade event fixtures (act)", () => { + const cases = fixtures.flatMap((fixture) => + (fixture.checksMembership ? [true, false] : [true]).map((member) => ({ ...fixture, member })), + ); + + it.each(cases)("$name (membership: $member)", ({ eventName, payload, checksMembership, member }) => { + const directory = mkdtempSync(join(tmpdir(), "scope-worker-upgrade-act-")); + try { + const { pre_activation: preActivation, activation, agent } = lock.jobs; + // Copy only the wiring under test. Never run production steps or load repository credentials. + const workflowPath = join(directory, "fixture-workflow.yml"); + writeFileSync(workflowPath, JSON.stringify({ + name: "Worker upgrade gate fixture", + on: { issues: lock.on.issues, workflow_dispatch: {} }, + jobs: { + pre_activation: { + "runs-on": "fixture", + if: preActivation.if, + outputs: { activated: preActivation.outputs?.activated }, + steps: [{ + id: "check_membership", + run: `printf 'is_team_member=%s\\n' '${member}' >> "$GITHUB_OUTPUT"`, + }], + }, + activation: { + "runs-on": "fixture", + if: activation.if, + needs: activation.needs, + steps: [{ run: ":" }], + }, + agent: { "runs-on": "fixture", if: agent.if, needs: agent.needs, steps: [{ run: ":" }] }, + }, + })); + const eventPath = join(directory, "event.json"); + writeFileSync(eventPath, JSON.stringify(payload)); + + // A disposable home and allowlisted environment keep act config and auth out of the run. + const result = spawnSync("act", [ + eventName, "--workflows", workflowPath, "--eventpath", eventPath, + "--platform", "fixture=-self-hosted", "--secret", "GITHUB_TOKEN=", + "--action-cache-path", join(directory, "cache"), "--no-cache-server", + "--container-daemon-socket", "-", "--json", "--verbose", + ], { + cwd: directory, + env: { PATH: process.env.PATH, HOME: directory, XDG_CONFIG_HOME: directory }, + encoding: "utf8", + timeout: 15_000, + }); + if (result.error) throw new Error("Could not run act; install act and put it on PATH", { cause: result.error }); + const log = result.stdout + result.stderr; + expect(result.status, log).toBe(0); + + // Check act's terminal status for each job; exit zero alone also permits all jobs to skip. + const outcomes = Object.fromEntries(log.trim().split("\n").filter(Boolean).flatMap((line) => { + const entry: { jobID?: string; jobResult?: string } = JSON.parse(line); + return entry.jobID && entry.jobResult ? [[entry.jobID, entry.jobResult]] : []; + })); + expect(outcomes, log).toEqual({ + pre_activation: checksMembership ? "success" : "skipped", + activation: checksMembership && member ? "success" : "skipped", + agent: checksMembership && member ? "success" : "skipped", + }); + } finally { + rmSync(directory, { recursive: true, force: true }); + } + }, 20_000); + }); +});