This is an automated email from the ASF dual-hosted git repository. potiuk pushed a commit to branch fork-ci-approved-commit-approves-runs in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 71729b5060c15b0e28e3d01f0f23609e80c371a0 Author: Jarek Potiuk <[email protected]> AuthorDate: Mon Nov 16 04:07:34 2026 +0900 Approve the CI runs of a commit a maintainer approved A pull request whose code maintainers had already approved still waited for one of them to approve its CI runs by hand, as a pilot contributor pointed out on #74696. A maintainer who approved the exact head commit has reviewed all of it, CI changes included, which is what approving the runs asks of them. Generated-by: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01NyQq9U7i5HhjDgrQ9qd3gq --- contributing-docs/05_pull_requests.rst | 5 ++ dev/breeze/doc/ci/05_workflows.md | 10 +++ .../airflow_breeze/utils/workflow_run_approval.py | 53 ++++++++++++ dev/breeze/tests/test_workflow_run_approval.py | 97 ++++++++++++++++++++++ 4 files changed, 165 insertions(+) diff --git a/contributing-docs/05_pull_requests.rst b/contributing-docs/05_pull_requests.rst index d927b218f7c..fe076938ed1 100644 --- a/contributing-docs/05_pull_requests.rst +++ b/contributing-docs/05_pull_requests.rst @@ -284,6 +284,11 @@ The runs of PRs opened from the repositories of trusted stakeholders (``astronom ``aws-mwaa/upstream-to-airflow`` and ``VladaZakharova/airflow``) are always approved, so none of the rules above apply to them. +Once a maintainer approves your PR in a review of its latest commit, its waiting runs are approved +too, whatever the rules above say: the maintainer has read the change, CI changes included. A +review of an earlier commit does not count for a later push, and neither does an approval the +maintainer later replaced with a request for changes. + Each new push is checked again, because approval applies to one commit. Only runs started in the last 24 hours are considered, so older waiting runs stay with maintainers. If your PR does not qualify, it gets the ``need workflow approval`` label and a maintainer approves the runs after diff --git a/dev/breeze/doc/ci/05_workflows.md b/dev/breeze/doc/ci/05_workflows.md index ad967d30b89..c1be2a2fc51 100644 --- a/dev/breeze/doc/ci/05_workflows.md +++ b/dev/breeze/doc/ci/05_workflows.md @@ -148,6 +148,16 @@ rules above apply to them, and of the rules below only the 24-hour window does: approved even when several open PRs share the head commit, or when the runs started before the PR was opened, retargeted or relabelled. +The runs of a PR whose head commit a maintainer approved are approved whatever the PR changes, as +for a trusted stakeholder. The reviewer's latest verdict on the PR must be an `APPROVED` review of +that exact commit (a later `CHANGES_REQUESTED` or a dismissal withdraws it, a comment does not), and +the reviewer must have `write` or `admin` permission on the repository, as read from +`GET /repos/apache/airflow/collaborators/<login>/permission`; collaborators granted triage in +`.asf.yaml` have `read` and do not count. A maintainer who approved the exact commit has reviewed +every change in it, CI changes included, which is what approving its runs by hand asks of them. A +commit that is the head of several open PRs is still left for a maintainer, and only runs started +after the PR was opened and after its base branch last changed are approved. + Only runs created in the last 24 hours (`--max-run-age-hours`) are considered, so the workflow never approves a backlog of older waiting runs at once; those stay with maintainers. A commit that is the head of several open PRs is never approved automatically, because its runs cannot be told apart by diff --git a/dev/breeze/src/airflow_breeze/utils/workflow_run_approval.py b/dev/breeze/src/airflow_breeze/utils/workflow_run_approval.py index 40a72eba97e..bbbc572c42f 100644 --- a/dev/breeze/src/airflow_breeze/utils/workflow_run_approval.py +++ b/dev/breeze/src/airflow_breeze/utils/workflow_run_approval.py @@ -243,6 +243,44 @@ def decide_trusted_approval(*, stakeholder: str) -> ApprovalDecision: ) +# Review states that replace a reviewer's earlier verdict; a COMMENTED review leaves it standing. +REVIEW_VERDICT_STATES = frozenset({"APPROVED", "CHANGES_REQUESTED", "DISMISSED"}) +# Repository permissions of maintainers. Collaborators granted triage through .asf.yaml report `read`. +MAINTAINER_PERMISSIONS = frozenset({"admin", "write"}) + + +def find_head_approver(api: GitHubApi, pr: dict[str, Any]) -> str | None: + """ + A maintainer whose latest verdict on the PR approves its head commit; None without one. + + A maintainer who approved the exact head commit has reviewed every change in it, CI changes + included, which is what approving its runs by hand asks of them. An approval of an earlier + commit does not cover a later push, and a later request for changes or a dismissal withdraws it. + """ + latest: dict[str, dict[str, Any]] = {} + # Listed oldest first, so each reviewer's last verdict wins. + for review in api.get_reviews(pr["number"]): + login = (review.get("user") or {}).get("login") + if login and review.get("state") in REVIEW_VERDICT_STATES: + latest[login] = review + for login, review in latest.items(): + if ( + review["state"] == "APPROVED" + and review.get("commit_id") == pr["head"]["sha"] + and api.get_permission(login) in MAINTAINER_PERMISSIONS + ): + return login + return None + + +def decide_reviewed_approval(*, reviewer: str) -> ApprovalDecision: + return ApprovalDecision( + approve=True, + reasons=[f"{reviewer}, a maintainer, approved its head commit"], + decision_labels=frozenset(), + ) + + # Files CI uses that do not decide the build-info outputs: a change to them is reviewed with the rest # of the pull request, and only noted on it. CI_MODIFYING_PREFIXES = ( @@ -423,6 +461,19 @@ class GitHubApi: def get_open_pull_requests(self) -> list[dict[str, Any]]: return self._get_paginated("pulls?state=open&per_page=100") + def get_reviews(self, pr_number: int) -> list[dict[str, Any]]: + return self._get_paginated(f"pulls/{pr_number}/reviews?per_page=100") + + def get_permission(self, login: str) -> str: + """The user's permission on the repository: ``admin``, ``write``, ``read`` or ``none``.""" + response = self._session.get( + f"{GITHUB_API_URL}/repos/{self.github_repository}/collaborators/{login}/permission", timeout=60 + ) + if response.status_code == 404: + return "none" + response.raise_for_status() + return response.json().get("permission") or "none" + def approve_run(self, run_id: int) -> None: if self.report_only: console_print(f"[info]Report only: would approve workflow run {run_id}[/]") @@ -698,6 +749,8 @@ def process_waiting_runs(api: GitHubApi, *, created_since: datetime) -> list[Pul decision = ApprovalDecision( approve=False, reasons=["its head commit is the head of several open PRs"] ) + elif (reviewer := find_head_approver(api, pr)) is not None: + decision = decide_reviewed_approval(reviewer=reviewer) else: decision = decide_for_pull_request(api=api, pr=pr) pr_run_ids = run_ids diff --git a/dev/breeze/tests/test_workflow_run_approval.py b/dev/breeze/tests/test_workflow_run_approval.py index f1c9c4b47a2..79bcc4fddda 100644 --- a/dev/breeze/tests/test_workflow_run_approval.py +++ b/dev/breeze/tests/test_workflow_run_approval.py @@ -55,6 +55,7 @@ from airflow_breeze.utils.workflow_run_approval import ( decide_for_pull_request, escape_markdown, fetch_merge_commit, + find_head_approver, get_changed_files, process_waiting_runs, write_step_summary, @@ -1497,3 +1498,99 @@ def test_decide_for_pull_request_gates_an_older_pilot_pull_request_from_joining( mock_decide_approval.assert_not_called() else: assert decision is mock_decide_approval.return_value + + +def _review(login, state, commit_id="aaa"): + return {"user": {"login": login}, "state": state, "commit_id": commit_id} + + +PERMISSIONS = {"committer": "write", "admin": "admin", "triager": "read", "other-committer": "write"} + + [email protected]( + ("reviews", "expected"), + [ + pytest.param([_review("committer", "APPROVED")], "committer", id="approved-head"), + pytest.param([_review("admin", "APPROVED")], "admin", id="admin"), + pytest.param([_review("committer", "APPROVED", commit_id="old")], None, id="approved-earlier-commit"), + pytest.param( + [_review("committer", "APPROVED"), _review("committer", "CHANGES_REQUESTED")], + None, + id="changes-requested-after", + ), + pytest.param( + [_review("committer", "APPROVED"), _review("committer", "COMMENTED")], + "committer", + id="comment-after-keeps-approval", + ), + pytest.param([_review("committer", "DISMISSED")], None, id="dismissed"), + pytest.param([_review("triager", "APPROVED")], None, id="no-write-access"), + pytest.param( + [_review("triager", "APPROVED"), _review("other-committer", "APPROVED")], + "other-committer", + id="one-of-several-reviewers", + ), + pytest.param([{"user": None, "state": "APPROVED", "commit_id": "aaa"}], None, id="deleted-user"), + pytest.param([], None, id="no-reviews"), + ], +) +def test_find_head_approver(reviews, expected): + api = mock.MagicMock(spec=GitHubApi) + api.get_reviews.return_value = reviews + api.get_permission.side_effect = lambda login: PERMISSIONS[login] + + assert find_head_approver(api, _pr(10, "aaa")) == expected + api.get_reviews.assert_called_once_with(10) + + [email protected](f"{MODULE}.decide_for_pull_request", autospec=True) +def test_process_waiting_runs_approves_runs_of_a_head_commit_a_maintainer_approved(mock_decide): + api = _api_with_prs([_pr(10, "aaa", labels=(NEED_WORKFLOW_APPROVAL_LABEL,))], [_run(1, "aaa")]) + api.get_reviews.return_value = [_review("committer", "APPROVED")] + api.get_permission.return_value = "write" + + (result,) = process_waiting_runs(api, created_since=CREATED_SINCE) + + assert result.decision.approve is True + assert result.decision.reasons == ["committer, a maintainer, approved its head commit"] + mock_decide.assert_not_called() + api.approve_run.assert_called_once_with(1) + api.remove_label.assert_called_once_with(10, NEED_WORKFLOW_APPROVAL_LABEL) + + [email protected](f"{MODULE}.decide_for_pull_request", autospec=True) +def test_process_waiting_runs_does_not_approve_a_shared_head_commit_a_maintainer_approved(mock_decide): + api = _api_with_prs([_pr(10, "aaa"), _pr(11, "aaa")], [_run(1, "aaa")]) + api.get_reviews.return_value = [_review("committer", "APPROVED")] + api.get_permission.return_value = "write" + + results = process_waiting_runs(api, created_since=CREATED_SINCE) + + assert all(not result.decision.approve for result in results) + api.approve_run.assert_not_called() + + [email protected]( + ("status_code", "payload", "expected"), + [ + pytest.param(200, {"permission": "write"}, "write", id="write"), + pytest.param(200, {"permission": "read"}, "read", id="read"), + pytest.param(404, {}, "none", id="not-a-collaborator"), + ], +) +def test_get_permission(status_code, payload, expected): + api = GitHubApi(github_repository="apache/airflow", github_token="token", report_only=False) + with mock.patch.object(api, "_session", autospec=True) as session: + session.get.return_value.status_code = status_code + session.get.return_value.json.return_value = payload + assert api.get_permission("someone") == expected + + assert session.get.call_args.args[0].endswith("/repos/apache/airflow/collaborators/someone/permission") + + +def test_get_reviews_lists_the_pull_request_reviews(): + api = GitHubApi(github_repository="apache/airflow", github_token="token", report_only=False) + with mock.patch.object(api, "_get_paginated", autospec=True, return_value=[]) as get_paginated: + assert api.get_reviews(10) == [] + + get_paginated.assert_called_once_with("pulls/10/reviews?per_page=100")
