diff --git a/.github/workflows/controller.yml b/.github/workflows/controller.yml index 1e0bd275..5238552a 100644 --- a/.github/workflows/controller.yml +++ b/.github/workflows/controller.yml @@ -11,9 +11,18 @@ name: controller # else -> dry run, the plan goes to the job summary. # AGENT_PAUSE `true` -> kill switch: nothing is read, proposed or changed. # To turn it off entirely, disable this workflow in the Actions tab. +# +# It also runs on the three PR lifecycle events, so a new PR is labelled now +# rather than at the next cron tick. The old half-hourly schedule declared 48 +# slots a day and in practice ran twice (2026-11-19: 3 runs; 2026-11-20: 2), +# so immediacy comes from these events and manual dispatch, and the cron is a +# four-a-day backstop only. A `pull_request` run from a fork head carries a +# read-only token, so both jobs skip it: the fallback cron still labels fork PRs. on: + pull_request: + types: [opened, ready_for_review, reopened] schedule: - - cron: '7,37 * * * *' + - cron: '17 */6 * * *' workflow_dispatch: concurrency: @@ -27,6 +36,11 @@ jobs: name: plan (read only) runs-on: ubuntu-latest timeout-minutes: 10 + # A read-only token is all this job needs, but a fork head's run must not + # hand `apply` a live plan, so it is skipped like `apply` is. + if: >- + github.event_name != 'pull_request' + || github.event.pull_request.head.repo.full_name == github.repository permissions: contents: read issues: read @@ -50,7 +64,11 @@ jobs: apply: name: apply labels (live only) needs: plan - if: needs.plan.outputs.live == 'true' && vars.CONTROLLER_LIVE == 'true' && vars.AGENT_PAUSE != 'true' + if: >- + needs.plan.outputs.live == 'true' && vars.CONTROLLER_LIVE == 'true' + && vars.AGENT_PAUSE != 'true' + && (github.event_name != 'pull_request' + || github.event.pull_request.head.repo.full_name == github.repository) runs-on: ubuntu-latest timeout-minutes: 10 permissions: diff --git a/changelog.d/1187-controller-pr-trigger.md b/changelog.d/1187-controller-pr-trigger.md new file mode 100644 index 00000000..ea83c15a --- /dev/null +++ b/changelog.d/1187-controller-pr-trigger.md @@ -0,0 +1,7 @@ +Changed +The label-only controller now also runs on the `pull_request` `opened`, +`ready_for_review` and `reopened` events, so a new PR is labelled immediately +instead of waiting for a `schedule` tick. The cron moves from every 30 minutes +(48 declared slots, in practice two or three runs a day) to `17 */6 * * *`, a +backstop only; a `pull_request` run from a fork head is skipped by both jobs +because it cannot hold a write token (#1187). diff --git a/tests/test_process_controller.py b/tests/test_process_controller.py index 1b4384b7..1fa80312 100644 --- a/tests/test_process_controller.py +++ b/tests/test_process_controller.py @@ -18,7 +18,12 @@ ROOT = Path(__file__).resolve().parents[1] SCRIPTS = ROOT / ".github" / "scripts" WORKFLOW = ROOT / ".github" / "workflows" / "controller.yml" +WORKFLOWS = ROOT / ".github" / "workflows" CONFIG = json.loads((ROOT / ".github" / "agent" / "controller.json").read_text()) +# A `pull_request` run on a fork head gets a read-only GITHUB_TOKEN, so neither +# job may run for it: `plan` would hand `apply` a live plan it cannot carry out. +SAME_REPO_GUARD = ("github.event_name != 'pull_request' " + "|| github.event.pull_request.head.repo.full_name == github.repository") def _mod(): @@ -268,8 +273,13 @@ def test_workflow_shape(): text = WORKFLOW.read_text(encoding="utf-8") wf = yaml.safe_load(text) on = wf.get("on", wf.get(True)) - assert set(on) == {"schedule", "workflow_dispatch"} - assert on["schedule"] == [{"cron": "7,37 * * * *"}], "every 30 minutes" + assert set(on) == {"schedule", "workflow_dispatch", "pull_request"} + assert on["schedule"] == [{"cron": "17 */6 * * *"}], ( + "four slots a day: the half-hourly cron only ran two or three times a day, " + "so immediacy comes from the pull_request events, not the schedule" + ) + assert on["pull_request"] == {"types": ["opened", "ready_for_review", "reopened"]}, \ + "label a PR when it becomes reviewable, not on every push to its head" assert wf["permissions"] == {} plan, apply = wf["jobs"]["plan"], wf["jobs"]["apply"] assert plan["permissions"] == {"contents": "read", "issues": "read", "pull-requests": "read"} @@ -282,6 +292,24 @@ def test_workflow_shape(): assert "${{" not in runs, "values reach the shell only through env" +def test_a_fork_head_never_reaches_the_label_jobs(): + wf = yaml.safe_load(WORKFLOW.read_text(encoding="utf-8")) + for name in ("plan", "apply"): + assert SAME_REPO_GUARD in wf["jobs"][name]["if"], ( + f"{name} must skip a pull_request from a fork: that token cannot write" + ) + + +def test_no_workflow_uses_pull_request_target(): + offenders = [] + for path in sorted(WORKFLOWS.glob("*.yml")): + wf = yaml.safe_load(path.read_text(encoding="utf-8")) + on = wf.get("on", wf.get(True)) or {} + if "pull_request_target" in on: + offenders.append(path.name) + assert not offenders, f"pull_request_target runs with a writable token on untrusted code: {offenders}" + + def test_labels_json_has_the_labels_the_controller_manages(): labels = ROOT / ".github" / "labels.json" if not labels.exists():