From c2438c2b6ae11fbc203c59624cdf463d6d37f4e4 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 27 Mar 2026 15:17:17 +0100 Subject: [PATCH] ci: reorganize PR resolver scripts --- .github/workflows/entry-pull-request.yml | 31 ++++++++-- docs/contributing/pull-requests.md | 6 +- docs/contributing/workflow.md | 2 + scripts/meta/resolve/README.md | 4 +- scripts/meta/resolve/pr/README.md | 10 +++ scripts/meta/resolve/pr/branch_prefix.sh | 47 ++++++++++++++ .../{pr_merge_ref.sh => pr/merge_ref.sh} | 0 .../meta/resolve/{pr_scope.sh => pr/scope.sh} | 0 tests/integration/test_pr_branch_prefix.py | 61 +++++++++++++++++++ tests/integration/test_pull_request_scope.py | 2 +- 10 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 scripts/meta/resolve/pr/README.md create mode 100755 scripts/meta/resolve/pr/branch_prefix.sh rename scripts/meta/resolve/{pr_merge_ref.sh => pr/merge_ref.sh} (100%) rename scripts/meta/resolve/{pr_scope.sh => pr/scope.sh} (100%) create mode 100644 tests/integration/test_pr_branch_prefix.py diff --git a/.github/workflows/entry-pull-request.yml b/.github/workflows/entry-pull-request.yml index 7fa9ebe676..a024166cf9 100644 --- a/.github/workflows/entry-pull-request.yml +++ b/.github/workflows/entry-pull-request.yml @@ -45,11 +45,28 @@ jobs: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} REPOSITORY: ${{ github.repository }} PR_NUMBER: ${{ github.event.pull_request.number }} - run: ./scripts/meta/resolve/pr_scope.sh + run: ./scripts/meta/resolve/pr/scope.sh + + validate-pr-branch-prefix: + name: 🧭 Validate PR branch prefix + needs: detect-pr-scope + if: ${{ github.event_name == 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository }} + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6 + + - name: Validate branch prefix for detected scope + env: + PR_HEAD_REF: ${{ github.event.pull_request.head.ref }} + PR_SCOPE: ${{ needs.detect-pr-scope.outputs.scope }} + run: ./scripts/meta/resolve/pr/branch_prefix.sh pr-scope-gate: name: 🚦 Gate PR scope - needs: detect-pr-scope + needs: + - detect-pr-scope + - validate-pr-branch-prefix if: ${{ needs.detect-pr-scope.outputs.run_ci_orchestrator == 'false' }} runs-on: ubuntu-latest steps: @@ -60,14 +77,18 @@ jobs: echo "Lightweight PR scope '${{ needs.detect-pr-scope.outputs.scope }}' passed." ci-orchestrator: - needs: detect-pr-scope + needs: + - detect-pr-scope + - validate-pr-branch-prefix if: ${{ github.event_name == 'pull_request' && needs.detect-pr-scope.outputs.run_ci_orchestrator == 'true' }} uses: ./.github/workflows/ci-orchestrator.yml with: distros: "arch debian ubuntu fedora centos" prepare-fork-merge-ref: - needs: detect-pr-scope + needs: + - detect-pr-scope + - validate-pr-branch-prefix if: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name != github.repository && needs.detect-pr-scope.outputs.run_ci_orchestrator == 'true' }} runs-on: ubuntu-latest outputs: @@ -82,7 +103,7 @@ jobs: env: PR_NUMBER: ${{ github.event.pull_request.number }} PR_MERGE_SHA: ${{ github.event.pull_request.merge_commit_sha || '' }} - run: ./scripts/meta/resolve/pr_merge_ref.sh + run: ./scripts/meta/resolve/pr/merge_ref.sh build-ci-images-fork: needs: prepare-fork-merge-ref diff --git a/docs/contributing/pull-requests.md b/docs/contributing/pull-requests.md index a4d218f20d..04aa11aad7 100644 --- a/docs/contributing/pull-requests.md +++ b/docs/contributing/pull-requests.md @@ -19,9 +19,9 @@ The PR workflow classifies changes by the files that were touched: | Scope | Matching files | CI behavior | |---|---|---| -| `agents` | Only `AGENTS.md`, `CLAUDE.md`, `GEMINI.md`, or `docs/agents/*` | Skips `ci-orchestrator` and finishes green through the lightweight scope gate. | -| `documentation` | Only `.md` or `.rst` files outside the agent paths | Skips `ci-orchestrator` and finishes green through the lightweight scope gate. | -| `full` | Anything else, or a mix of agent, documentation, and non-documentation files | Runs the full `ci-orchestrator` pipeline. | +| `agents` | Only `AGENTS.md`, `CLAUDE.md`, `GEMINI.md`, or `docs/agents/*` | Requires the `agent` branch prefix, skips `ci-orchestrator`, and finishes green through the lightweight scope gate. | +| `documentation` | Only `.md` or `.rst` files outside the agent paths | Requires the `documentation` branch prefix, skips `ci-orchestrator`, and finishes green through the lightweight scope gate. | +| `full` | Anything else, or a mix of agent, documentation, and non-documentation files | Requires the `feature` or `fix` branch prefix and runs the full `ci-orchestrator` pipeline. | ## Pull Request Checklist diff --git a/docs/contributing/workflow.md b/docs/contributing/workflow.md index 9059d81a65..cdd9c8cfe7 100644 --- a/docs/contributing/workflow.md +++ b/docs/contributing/workflow.md @@ -36,4 +36,6 @@ Use these prefixes for branch names: | `agent` | Changes to agent instruction files such as `AGENTS.md` or files in `docs/agents/`. | `agent/debugging-guidance` | | `documentation` | Pure documentation-only changes. | `documentation/docker-guide` | +The pull-request workflow validates the branch prefix against the touched file scope and fails the job when they do not match. + More information about the contribution workflow is available [here](https://s.infinito.nexus/forking). diff --git a/scripts/meta/resolve/README.md b/scripts/meta/resolve/README.md index 23c9a9122f..7e3796279a 100644 --- a/scripts/meta/resolve/README.md +++ b/scripts/meta/resolve/README.md @@ -6,11 +6,11 @@ This directory contains scripts that resolve metadata and derived values used by - 🔍 Resolve identifiers, references, and computed inputs - 🗂️ Keep lookup and resolution logic grouped in one place -Examples in this folder: +Examples in this folder and its subfolders: - `repository/name.sh` resolves the concrete repository name once for callers - `repository/owner.sh` resolves the concrete repository owner once for callers - `distros.sh` is the SPOT for the canonical distro list used by workflows -- `pr_scope.sh` resolves whether a Pull Request is agents-only, documentation-only, or full scope +- See [pr/README.md](pr/README.md) for the Pull Request-specific resolvers. The scope of this folder is value resolution. Build, release, and wait-oriented behavior should stay in their dedicated script directories. diff --git a/scripts/meta/resolve/pr/README.md b/scripts/meta/resolve/pr/README.md new file mode 100644 index 0000000000..07b39cb3f5 --- /dev/null +++ b/scripts/meta/resolve/pr/README.md @@ -0,0 +1,10 @@ +# Pull Request Resolvers + +This directory contains Pull Request-specific resolver scripts used by the PR workflow. + +Examples in this folder: +- `scope.sh` resolves whether a Pull Request is agents-only, documentation-only, or full scope +- `branch_prefix.sh` validates that the PR branch prefix matches the detected scope +- `merge_ref.sh` resolves the merge ref used for forked PR workflows + +These scripts keep PR scope detection, branch-prefix validation, and fork merge-ref resolution together in one place. diff --git a/scripts/meta/resolve/pr/branch_prefix.sh b/scripts/meta/resolve/pr/branch_prefix.sh new file mode 100755 index 0000000000..614d3d4e53 --- /dev/null +++ b/scripts/meta/resolve/pr/branch_prefix.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +set -euo pipefail + +: "${PR_HEAD_REF:?Missing PR_HEAD_REF}" +: "${PR_SCOPE:?Missing PR_SCOPE}" + +matches_expected_prefix() { + local head_ref="$1" + shift + + local prefix + for prefix in "$@"; do + case "${head_ref}" in + "${prefix}" | "${prefix}"/*) + return 0 + ;; + esac + done + + return 1 +} + +case "${PR_SCOPE}" in +agents) + expected_prefixes=(agent) + ;; +documentation) + expected_prefixes=(documentation) + ;; +full) + expected_prefixes=(feature fix) + ;; +*) + echo "ERROR: Unsupported PR_SCOPE '${PR_SCOPE}'." >&2 + exit 1 + ;; +esac + +branch_prefix="${PR_HEAD_REF%%/*}" + +if matches_expected_prefix "${PR_HEAD_REF}" "${expected_prefixes[@]}"; then + echo "Validated branch prefix '${branch_prefix}' for scope '${PR_SCOPE}' (${expected_prefixes[*]})." + exit 0 +fi + +echo "ERROR: Branch '${PR_HEAD_REF}' does not match scope '${PR_SCOPE}'. Expected one of: ${expected_prefixes[*]}." >&2 +exit 1 diff --git a/scripts/meta/resolve/pr_merge_ref.sh b/scripts/meta/resolve/pr/merge_ref.sh similarity index 100% rename from scripts/meta/resolve/pr_merge_ref.sh rename to scripts/meta/resolve/pr/merge_ref.sh diff --git a/scripts/meta/resolve/pr_scope.sh b/scripts/meta/resolve/pr/scope.sh similarity index 100% rename from scripts/meta/resolve/pr_scope.sh rename to scripts/meta/resolve/pr/scope.sh diff --git a/tests/integration/test_pr_branch_prefix.py b/tests/integration/test_pr_branch_prefix.py new file mode 100644 index 0000000000..148cecb5d2 --- /dev/null +++ b/tests/integration/test_pr_branch_prefix.py @@ -0,0 +1,61 @@ +import os +import subprocess +import unittest +from pathlib import Path + + +REPO_ROOT = Path(__file__).resolve().parents[2] +SCRIPT_PATH = REPO_ROOT / "scripts" / "meta" / "resolve" / "pr" / "branch_prefix.sh" + + +class TestPullRequestBranchPrefix(unittest.TestCase): + def _run_script(self, *, scope: str, head_ref: str): + env = os.environ.copy() + env.update( + { + "PR_SCOPE": scope, + "PR_HEAD_REF": head_ref, + } + ) + + return subprocess.run( + ["bash", str(SCRIPT_PATH)], + cwd=REPO_ROOT, + env=env, + capture_output=True, + text=True, + ) + + def test_agents_scope_accepts_agent_prefix(self): + result = self._run_script(scope="agents", head_ref="agent/debugging-guidance") + self.assertEqual(result.returncode, 0) + self.assertIn("Validated branch prefix 'agent'", result.stdout) + + def test_documentation_scope_accepts_documentation_prefix(self): + result = self._run_script( + scope="documentation", head_ref="documentation/docker-guide" + ) + self.assertEqual(result.returncode, 0) + self.assertIn("Validated branch prefix 'documentation'", result.stdout) + + def test_full_scope_accepts_feature_prefix(self): + result = self._run_script(scope="full", head_ref="feature/add-matomo") + self.assertEqual(result.returncode, 0) + self.assertIn("Validated branch prefix 'feature'", result.stdout) + + def test_full_scope_accepts_fix_prefix(self): + result = self._run_script(scope="full", head_ref="fix/login-redirect") + self.assertEqual(result.returncode, 0) + self.assertIn("Validated branch prefix 'fix'", result.stdout) + + def test_mismatched_prefix_fails(self): + result = self._run_script(scope="documentation", head_ref="feature/add-docs") + self.assertNotEqual(result.returncode, 0) + self.assertIn( + "does not match scope 'documentation'", + result.stderr, + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/integration/test_pull_request_scope.py b/tests/integration/test_pull_request_scope.py index fc9651af7f..6b95f43993 100644 --- a/tests/integration/test_pull_request_scope.py +++ b/tests/integration/test_pull_request_scope.py @@ -8,7 +8,7 @@ from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[2] -SCRIPT_PATH = REPO_ROOT / "scripts" / "meta" / "resolve" / "pr_scope.sh" +SCRIPT_PATH = REPO_ROOT / "scripts" / "meta" / "resolve" / "pr" / "scope.sh" @unittest.skipUnless(