diff --git a/.github/workflows/open-maintenance-pr.yaml b/.github/workflows/open-maintenance-pr.yaml index cdbe4a8..329a32e 100644 --- a/.github/workflows/open-maintenance-pr.yaml +++ b/.github/workflows/open-maintenance-pr.yaml @@ -46,12 +46,25 @@ jobs: contents: read outputs: patch-sha256: ${{ steps.digest.outputs.sha256 }} + base-sha: ${{ steps.base.outputs.sha }} steps: - name: 📥 Check out repository uses: actions/checkout@v7.0.1 with: - ref: ${{ github.sha }} + ref: refs/heads/${{ inputs.base }} + fetch-depth: 0 persist-credentials: false + - id: base + name: 🔍 Verify the patch source is in the target branch + env: + SOURCE_SHA: ${{ github.sha }} + run: | + set -euo pipefail + if ! git merge-base --is-ancestor "$SOURCE_SHA" HEAD; then + echo "::error::The commit that generated the patch is no longer in the target branch" >&2 + exit 1 + fi + echo "sha=$(git rev-parse HEAD)" >>"$GITHUB_OUTPUT" - name: 📩 Download maintenance patch uses: actions/download-artifact@v8.0.2 with: @@ -147,7 +160,7 @@ jobs: - name: 📥 Check out repository uses: actions/checkout@v7.0.1 with: - ref: ${{ github.sha }} + ref: ${{ needs.validate.outputs.base-sha }} fetch-depth: 0 persist-credentials: false - name: 📩 Download maintenance patch @@ -190,10 +203,16 @@ jobs: - name: ⬆️ Push maintenance branch env: GH_TOKEN: ${{ steps.token.outputs.token }} + BASE: ${{ inputs.base }} BRANCH: ${{ inputs.branch }} run: | set -euo pipefail gh auth setup-git --hostname github.com --force + base_ref=$(git ls-remote --exit-code origin "refs/heads/$BASE") + if [[ "${base_ref%%$'\t'*}" != "$(git rev-parse HEAD^)" ]]; then + echo "::error::The target branch advanced after patch validation" >&2 + exit 1 + fi echo "Pushing $BRANCH, replacing any earlier maintenance commit (force with lease)" git push --force-with-lease origin "HEAD:refs/heads/$BRANCH" - name: 📝 Write maintenance pull request body diff --git a/tests/open_maintenance_pr/test_open.py b/tests/open_maintenance_pr/test_open.py index f241f1c..ead1f4b 100644 --- a/tests/open_maintenance_pr/test_open.py +++ b/tests/open_maintenance_pr/test_open.py @@ -173,6 +173,76 @@ def test_writer_rejects_patch_that_differs_from_validated_digest(repository: Pat assert result.returncode != 0 +@pytest.mark.parametrize( + ("checkout", "returncode", "diagnostic", "output_keys"), + [ + pytest.param(("-b", "next"), 0, "", ("sha",), id="advanced"), + pytest.param(("--orphan", "replacement"), 1, "no longer in the target branch", (), id="rewritten"), + ], +) +def test_validate_selects_the_current_target( + repository: Path, + tmp_path: Path, + checkout: tuple[str, str], + returncode: int, + diagnostic: str, + output_keys: tuple[str, ...], +) -> None: + source = run(repository, "git", "rev-parse", "HEAD").stdout.strip() + run(repository, "git", "checkout", *checkout) + run(repository, "git", "commit", "--allow-empty", "-m", "Advance base") + result, outputs = run_workflow_step( + _WORKFLOW, + "validate", + "🔍 Verify the patch source is in the target branch", + cwd=repository, + runner_temp=tmp_path, + values={"github.sha": source}, + env={}, + ) + assert result.returncode == returncode, result.stderr + assert diagnostic in result.stderr + assert outputs == dict.fromkeys(output_keys, run(repository, "git", "rev-parse", "HEAD").stdout.strip()) + + +@pytest.mark.parametrize( + ("target", "returncode", "diagnostic", "published"), + [ + pytest.param( + "HEAD^", 0, "Pushing automation/maintenance", "{commit}\trefs/heads/automation/maintenance\n", id="current" + ), + pytest.param("HEAD", 1, "target branch advanced", "", id="advanced"), + ], +) +def test_push_requires_the_validated_target( + repository: Path, tmp_path: Path, target: str, returncode: int, diagnostic: str, published: str +) -> None: + remote = tmp_path / "remote.git" + run(repository, "git", "clone", "--bare", ".", str(remote)) + run(repository, "git", "remote", "add", "origin", str(remote)) + run(repository, "git", "switch", "-c", "maintenance") + (repository / "pylock.toml").write_text("lock-version = '2.0'\n") + run(repository, "git", "commit", "-am", "Update lock") + run(repository, "git", "push", "origin", f"{target}:refs/heads/main") + gh = tmp_path / "gh" + gh.write_text("#!/bin/sh\nexit 0\n") + gh.chmod(stat.S_IRWXU) + result, _ = run_workflow_step( + _WORKFLOW, + "open", + "⬆️ Push maintenance branch", + cwd=repository, + runner_temp=tmp_path, + values={"inputs.base": "main", "inputs.branch": "automation/maintenance"}, + env={"GH_TOKEN": "", "PATH": f"{tmp_path}:{os.environ['PATH']}"}, + ) + remote_branch = run(repository, "git", "ls-remote", "origin", "refs/heads/automation/maintenance").stdout + assert result.returncode == returncode, result.stderr + assert diagnostic in result.stdout + result.stderr + commit = run(repository, "git", "rev-parse", "HEAD").stdout.strip() + assert remote_branch == published.format(commit=commit) + + def test_existing_pull_request_is_updated(repository: Path, tmp_path: Path) -> None: result, outputs, calls = open_pull_request( repository, tmp_path, [pull(17, repo="attacker/fork"), pull(18)], "skip news\n\ndependencies\n"