workflows: dispatch release issue workflows against the release branch (#4422) When release commands (such as `/create-rc`, `/process-backports`, `/sync-changelog`, or `/promote`) are triggered via comments on a release tracking issue, `on_comment.yaml` runs on `main` and previously invoked reusable workflows from `main` via `uses:`. When `main` diverges from the `release/X.Y` branch, workflow and script changes on `main` can fail against the older release branch. To run the release branch's own workflow definitions, extract the `release/X.Y` branch name from the release issue title in `on_comment.py` and dispatch the post-branch release workflows (`create-rc`, `process-backports`, `sync-changelog`, and `promote`) on that branch using `gh workflow run --ref`.
diff --git a/.github/workflows/on_comment.py b/.github/workflows/on_comment.py index 2687972..ac15168 100755 --- a/.github/workflows/on_comment.py +++ b/.github/workflows/on_comment.py
@@ -9,6 +9,8 @@ import subprocess import sys +_RELEASE_TITLE_RE = re.compile(r"Release (\d+\.\d+)\.\d+", re.IGNORECASE) + def _load_event_data() -> dict: """Loads event JSON payload from GITHUB_EVENT_PATH.""" @@ -95,6 +97,15 @@ ) +def _get_release_branch(event: dict) -> str | None: + """Extracts the release/X.Y branch name from the issue title.""" + issue_data = event.get("issue") or {} + title = str(issue_data.get("title") or "") + if m := _RELEASE_TITLE_RE.search(title): + return f"release/{m.group(1)}" + return None + + def _react_negative(repo: str, comment_id: str) -> None: """Logs error and adds a negative reaction to the comment.""" print("::error::No PRs specified for backport.") @@ -125,6 +136,15 @@ "gholms", ] ) +_BRANCH_DISPATCHED_COMMANDS = frozenset( + { + _Command.CREATE_RC, + _Command.PROCESS_BACKPORTS, + _Command.SYNC_CHANGELOG, + _Command.ADD_BACKPORTS, + _Command.PROMOTE, + } +) def _is_command_allowed( @@ -305,12 +325,22 @@ _write_github_env("issue_number", issue_number) if has_release_label: + release_branch = _get_release_branch(event) + if release_branch: + _write_github_output("release_branch", release_branch) command = _process_release_issue_comment( comment_body=comment_body, issue_number=issue_number, repo=repo, comment_id=comment_id, ) + if command in _BRANCH_DISPATCHED_COMMANDS and not release_branch: + issue_title = str((event.get("issue") or {}).get("title") or "") + print( + f"::error::Could not determine release branch for issue" + f" #{issue_number} from title: '{issue_title}'." + ) + return 1 elif has_backport_label: command = _process_backport_issue_comment( comment_body=comment_body,
diff --git a/.github/workflows/on_comment.yaml b/.github/workflows/on_comment.yaml index 4950416..fb6589c 100644 --- a/.github/workflows/on_comment.yaml +++ b/.github/workflows/on_comment.yaml
@@ -25,6 +25,7 @@ issue_number: ${{ steps.parse.outputs.issue_number }} pr_number: ${{ steps.parse.outputs.pr_number }} backports: ${{ steps.parse.outputs.backports }} + release_branch: ${{ steps.parse.outputs.release_branch }} steps: - uses: actions/checkout@v7 - uses: actions/setup-python@v7 @@ -45,11 +46,23 @@ call_create_rc: needs: parse_comment if: needs.parse_comment.outputs.command == 'create-rc' - uses: ./.github/workflows/release_create_rc.yaml - with: - issue: ${{ needs.parse_comment.outputs.issue_number }} - comment_id: "${{ github.event.comment.id }}" - secrets: inherit + runs-on: ubuntu-latest + permissions: + actions: write + steps: + - name: "Dispatch: Release: Create RC" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + BRANCH: ${{ needs.parse_comment.outputs.release_branch }} + ISSUE: ${{ needs.parse_comment.outputs.issue_number }} + COMMENT_ID: "${{ github.event.comment.id }}" + run: | + gh workflow run release_create_rc.yaml \ + --repo "$REPO" \ + --ref "$BRANCH" \ + --raw-field "issue=$ISSUE" \ + --raw-field "comment_id=$COMMENT_ID" call_prepare_complete: needs: parse_comment @@ -90,36 +103,82 @@ call_process_backports_after_add: needs: [parse_comment, call_add_backports] if: needs.parse_comment.outputs.command == 'add-backports' - uses: ./.github/workflows/release_process_backports.yaml - with: - issue: ${{ needs.parse_comment.outputs.issue_number }} - comment_id: "${{ github.event.comment.id }}" - secrets: inherit + runs-on: ubuntu-latest + permissions: + actions: write + steps: + - name: "Dispatch: Release: Process Backports" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + BRANCH: ${{ needs.parse_comment.outputs.release_branch }} + ISSUE: ${{ needs.parse_comment.outputs.issue_number }} + COMMENT_ID: "${{ github.event.comment.id }}" + run: | + gh workflow run release_process_backports.yaml \ + --repo "$REPO" \ + --ref "$BRANCH" \ + --raw-field "issue=$ISSUE" \ + --raw-field "comment_id=$COMMENT_ID" call_process_backports_only: needs: parse_comment if: needs.parse_comment.outputs.command == 'process-backports' - uses: ./.github/workflows/release_process_backports.yaml - with: - issue: ${{ needs.parse_comment.outputs.issue_number }} - comment_id: "${{ github.event.comment.id }}" - secrets: inherit + runs-on: ubuntu-latest + permissions: + actions: write + steps: + - name: "Dispatch: Release: Process Backports" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + BRANCH: ${{ needs.parse_comment.outputs.release_branch }} + ISSUE: ${{ needs.parse_comment.outputs.issue_number }} + COMMENT_ID: "${{ github.event.comment.id }}" + run: | + gh workflow run release_process_backports.yaml \ + --repo "$REPO" \ + --ref "$BRANCH" \ + --raw-field "issue=$ISSUE" \ + --raw-field "comment_id=$COMMENT_ID" call_sync_changelog: needs: parse_comment if: needs.parse_comment.outputs.command == 'sync-changelog' - uses: ./.github/workflows/release_sync_changelog.yaml - with: - issue: ${{ needs.parse_comment.outputs.issue_number }} - secrets: inherit + runs-on: ubuntu-latest + permissions: + actions: write + steps: + - name: "Dispatch: Release: Sync Changelog" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + BRANCH: ${{ needs.parse_comment.outputs.release_branch }} + ISSUE: ${{ needs.parse_comment.outputs.issue_number }} + run: | + gh workflow run release_sync_changelog.yaml \ + --repo "$REPO" \ + --ref "$BRANCH" \ + --raw-field "issue=$ISSUE" call_promote: needs: parse_comment if: needs.parse_comment.outputs.command == 'promote' - uses: ./.github/workflows/release_promote.yaml - with: - issue: ${{ needs.parse_comment.outputs.issue_number }} - secrets: inherit + runs-on: ubuntu-latest + permissions: + actions: write + steps: + - name: "Dispatch: Release: Promote" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO: ${{ github.repository }} + BRANCH: ${{ needs.parse_comment.outputs.release_branch }} + ISSUE: ${{ needs.parse_comment.outputs.issue_number }} + run: | + gh workflow run release_promote.yaml \ + --repo "$REPO" \ + --ref "$BRANCH" \ + --raw-field "issue=$ISSUE" call_backport_prepare: needs: parse_comment
diff --git a/.github/workflows/release_promote.yaml b/.github/workflows/release_promote.yaml index a65eb69..eb44a5f 100644 --- a/.github/workflows/release_promote.yaml +++ b/.github/workflows/release_promote.yaml
@@ -5,7 +5,11 @@ inputs: version: description: 'The final version to release (e.g., 0.38.0)' - required: true + required: false + type: string + issue: + description: 'The tracking issue number' + required: false type: string workflow_call: inputs:
diff --git a/tests/workflows/on_comment_test.py b/tests/workflows/on_comment_test.py index 09738c3..1fd7dc5 100644 --- a/tests/workflows/on_comment_test.py +++ b/tests/workflows/on_comment_test.py
@@ -41,6 +41,7 @@ self, *, issue_number: int | str = 4175, + issue_title: str = "Release 2.5.0", comment_id: int | str = 5772601429, comment_url: str = "", repo: str = "bazel-contrib/rules_python", @@ -58,8 +59,13 @@ } if user_login: comment["user"] = {"login": user_login} + issue: dict[str, object] = {} + if issue_number: + issue["number"] = issue_number + if issue_title: + issue["title"] = issue_title payload = { - "issue": {"number": issue_number} if issue_number else {}, + "issue": issue, "comment": comment, "repository": {"full_name": repo} if repo else {}, } @@ -125,6 +131,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "create-rc", } assert gha_env.read_env() == {"issue_number": "100"} @@ -138,6 +145,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "prepare-complete", "pr_number": "200", } @@ -152,6 +160,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "prepare-complete", } assert gha_env.read_env() == {"issue_number": "100"} @@ -165,6 +174,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "create-release-branch", } assert gha_env.read_env() == {"issue_number": "100"} @@ -178,6 +188,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "prepare", } assert gha_env.read_env() == {"issue_number": "100"} @@ -191,6 +202,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "process-backports", } assert gha_env.read_env() == {"issue_number": "100"} @@ -204,6 +216,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "add-backports", "backports": "1,2,3", } @@ -218,6 +231,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "add-backports", "backports": "#123,#567", } @@ -234,6 +248,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "none", } assert gha_env.read_env() == {"issue_number": "100"} @@ -254,6 +269,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "sync-changelog", } assert gha_env.read_env() == {"issue_number": "100"} @@ -267,6 +283,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "promote", } assert gha_env.read_env() == {"issue_number": "100"} @@ -280,6 +297,7 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "none", } assert gha_env.read_env() == {"issue_number": "100"} @@ -294,11 +312,27 @@ ) assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "prepare", } assert gha_env.read_env() == {"issue_number": "100"} +def test_release_issue_missing_release_branch_fails(monkeypatch, gha_env, capsys): + gha_env.set_event(issue_title="Invalid title") + rc = _run_comment( + monkeypatch, + "/create-rc", + has_release_label="true", + ) + assert rc == 1 + captured = capsys.readouterr() + assert ( + "::error::Could not determine release branch for issue #100 from" + " title: 'Invalid title'." in captured.out + ) + + def test_backport_issue_prepare(monkeypatch, gha_env): _run_comment( monkeypatch, @@ -417,6 +451,7 @@ assert gha_env.read_outputs() == { "issue_number": "42", + "release_branch": "release/2.5", "command": "create-rc", } assert gha_env.read_env() == {"issue_number": "42"} @@ -547,6 +582,7 @@ assert rc == 0 assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "add-backports", "backports": "#123,#456", } @@ -621,6 +657,7 @@ assert rc == 0 assert gha_env.read_outputs() == { "issue_number": "100", + "release_branch": "release/2.5", "command": "promote", }
diff --git a/tests/workflows/workflow_permissions_test.py b/tests/workflows/workflow_permissions_test.py index b1c7057..994e94d 100644 --- a/tests/workflows/workflow_permissions_test.py +++ b/tests/workflows/workflow_permissions_test.py
@@ -171,3 +171,40 @@ assert (set(publish_roots) | set(gazelle_roots) | set(sphinxdocs_roots)) == set( bcr_config["moduleRoots"] ) + + +def test_release_workflows_dispatch_on_target_ref(): + """Validates that post-branch release steps in on_comment.yaml use workflow_dispatch.""" + rf = runfiles.CreateOrRaise() + workflows_dir = rf.root() / "rules_python/.github/workflows" + + on_comment = yaml.safe_load( + (workflows_dir / "on_comment.yaml").read_text(encoding="utf-8") + ) + assert "release_branch" in on_comment["jobs"]["parse_comment"]["outputs"] + + branch_dispatched_jobs = { + # keep-sorted start + "call_create_rc": "release_create_rc.yaml", + "call_process_backports_after_add": "release_process_backports.yaml", + "call_process_backports_only": "release_process_backports.yaml", + "call_promote": "release_promote.yaml", + "call_sync_changelog": "release_sync_changelog.yaml", + # keep-sorted end + } + for job_name, expected_wf in branch_dispatched_jobs.items(): + job = on_comment["jobs"][job_name] + assert "uses" not in job, f"{job_name} should use workflow_dispatch, not uses" + assert job.get("permissions") == {"actions": "write"} + steps = job["steps"] + assert len(steps) == 1 + assert steps[0]["name"].startswith("Dispatch: Release: ") + assert f"gh workflow run {expected_wf}" in steps[0]["run"] + assert '--repo "$REPO"' in steps[0]["run"] + assert '--ref "$BRANCH"' in steps[0]["run"] + assert '--raw-field "issue=$ISSUE"' in steps[0]["run"] + assert steps[0]["env"]["REPO"] == "${{ github.repository }}" + assert ( + steps[0]["env"]["BRANCH"] + == "${{ needs.parse_comment.outputs.release_branch }}" + )