Skip to content

Commit 1e4d053

Browse files
authored
Approve all workflow runs for a specific contributor PR (#3876)
### What This PR adds the ability to approve workflow runs on forks on any current and subsequent changes automatically. This is a step above the "don't approve any workflow runs on forks automatically" that we have set up right now. The approval is triggered by commenting on a PR issue (`issue_comment`), or the usual PR activity (`pull_request_target`). It then looks through all comments on the PR, searching for the substring `@rerun-bot approve`. If it finds at least one, then it checks if the user that sent that comment is any of: - A repository owner - A member of the repository's organization - A repository collaborator If that is the case, then it approves all workflow runs on that pull request. PRs cannot modify this workflow or the script it uses to change how it works. The changes must first be merged into the default branch before having any effect, so this is safe for contributor PRs. ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/3876) (if applicable) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG - [PR Build Summary](https://build.rerun.io/pr/3876) - [Docs preview](https://rerun.io/preview/5b5366f30d2c75c24fc85a437fb6c976807beb57/docs) <!--DOCS-PREVIEW--> - [Examples preview](https://rerun.io/preview/5b5366f30d2c75c24fc85a437fb6c976807beb57/examples) <!--EXAMPLES-PREVIEW--> - [Recent benchmark results](https://ref.rerun.io/dev/bench/) - [Wasm size tracking](https://ref.rerun.io/dev/sizes/)
1 parent 3c912be commit 1e4d053

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

.github/workflows/auto_approve.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "Approve Workflow Runs"
2+
3+
on:
4+
pull_request_target:
5+
issue_comment:
6+
types: [created, edited]
7+
8+
permissions:
9+
actions: "write"
10+
11+
jobs:
12+
approve-workflow-runs:
13+
name: "Check for approval"
14+
runs-on: ubuntu-latest
15+
if: ${{ github.event_name == 'pull_request' || github.event.issue.pull_request }}
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v4
19+
20+
- name: Setup Python
21+
uses: actions/setup-python@v4
22+
23+
- name: Install dependencies
24+
run: |
25+
pip install PyGithub==1.59.0 requests>=2.31,<3
26+
27+
- name: Wait a few seconds
28+
run: |
29+
# Give GitHub a bit of time to synchronize everything
30+
sleep 10s
31+
32+
- name: Approve workflow runs
33+
run: |
34+
python3 scripts/ci/approve_workflow_runs.py \
35+
--github-token "${{ secrets.GITHUB_TOKEN }}" \
36+
--github-repository "rerun-io/rerun"
37+
--pr-number "${{ github.event.pull_request.number || github.event.issue.number }}"
38+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Script to auto-approve workflow runs if certain criteria are met.
5+
6+
Checks for a `@rerun-bot approve` comment made by an official Rerun team member,
7+
and approves any workflow runs with pending approval.
8+
9+
This is expected to be run by the `auto_approve.yml` GitHub workflow.
10+
11+
Requires the following packages:
12+
pip install PyGithub==1.59.0 requests>=2.31,<3
13+
"""
14+
from __future__ import annotations
15+
16+
import argparse
17+
18+
import requests
19+
from github import Github
20+
from github.WorkflowRun import WorkflowRun
21+
22+
APPROVAL = "@rerun-bot approve"
23+
24+
25+
def approve(github_token: str, workflow_run: WorkflowRun) -> None:
26+
print(f"approving {workflow_run.id}")
27+
requests.post(
28+
f"https://api.github.com/repos/rerun-io/rerun/actions/runs/{workflow_run.id}/approve",
29+
headers={
30+
"Accept": "application/vnd.github+json",
31+
"Authorization": f"Bearer {github_token}",
32+
"X-GitHub-Api-Version": "2022-11-28",
33+
},
34+
).raise_for_status()
35+
36+
37+
def main() -> None:
38+
parser = argparse.ArgumentParser(description="Generate a PR summary page")
39+
parser.add_argument("--github-token", required=True, help="GitHub token")
40+
parser.add_argument("--github-repository", required=True, help="GitHub repository")
41+
parser.add_argument("--pr-number", required=True, type=int, help="PR number")
42+
args = parser.parse_args()
43+
44+
gh = Github(args.github_token)
45+
repo = gh.get_repo(args.github_repository)
46+
pr = repo.get_pull(args.pr_number)
47+
48+
for comment in pr.get_issue_comments():
49+
if APPROVAL not in comment.body:
50+
continue
51+
52+
can_user_approve_workflows = (
53+
repo.owner.login == comment.user.login
54+
or repo.organization.has_in_members(comment.user)
55+
or repo.has_in_collaborators(comment.user)
56+
)
57+
if not can_user_approve_workflows:
58+
continue
59+
60+
print(f"found valid approval by {comment.user.login}")
61+
for workflow_run in repo.get_workflow_runs(branch=pr.head.ref):
62+
if workflow_run.status == "action_required" or workflow_run.conclusion == "action_required":
63+
approve(args.github_token, workflow_run)
64+
65+
# We only need one approval
66+
return
67+
68+
69+
if __name__ == "__main__":
70+
main()

0 commit comments

Comments
 (0)