diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5f96edc07..648b5e090 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,7 +10,10 @@ permissions: contents: read concurrency: - group: tests-${{ github.ref }} + # A merged pull request can report the base ref on ``closed``. Keying PR + # events by number lets that close run cancel an older queued synchronize + # run for the same PR instead of consuming runners after the PR is closed. + group: tests-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true jobs: diff --git a/tests/test_tests_workflow_contract.py b/tests/test_tests_workflow_contract.py index 2fd145b0e..e6b48461a 100644 --- a/tests/test_tests_workflow_contract.py +++ b/tests/test_tests_workflow_contract.py @@ -1,15 +1,17 @@ -"""Regression contracts for the repository test workflow.""" +"""Regression contracts for the repository-local test workflow.""" from pathlib import Path -def test_pr_close_cancels_obsolete_test_runs_without_starting_jobs() -> None: - """A close event must cancel the same-PR run while scheduling no test work.""" - workflow = ( - Path(__file__).resolve().parents[1] / ".github/workflows/tests.yml" - ).read_text(encoding="utf-8") +_WORKFLOW = Path(__file__).parents[1] / ".github" / "workflows" / "tests.yml" + + +def test_pull_request_concurrency_survives_closed_ref_change() -> None: + """Key synchronize and closed events by PR number so close cancels stale work.""" + + workflow = _WORKFLOW.read_text(encoding="utf-8") assert "types: [opened, synchronize, reopened, closed]" in workflow - assert "group: tests-${{ github.ref }}" in workflow + assert "group: tests-${{ github.event.pull_request.number || github.ref }}" in workflow assert "cancel-in-progress: true" in workflow assert workflow.count("github.event.action != 'closed'") == 2