-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automerge every 4 hours any labeled PR with passing tests #675
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cbe0f06
Automerge every 15-min any PR with passing tests labeled with 'autome…
addyess 29f1a6e
Make sure the bot can approve the PRs too
addyess fec52d5
Update Bot information only if git email currently unset
addyess 4a4bac8
consistently use private key secret to setup ssh git-remote
addyess 1abd2ec
Rename secret to BOT_SSH_KEY
addyess 8a74c29
Reimagine auto-merge scripts as python
addyess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Auto-merge Successful PRs | ||
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: "0 */4 * * *" # Every 4 hours | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
jobs: | ||
merge-successful-prs: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Harden Runner | ||
uses: step-security/harden-runner@v2 | ||
with: | ||
egress-policy: audit | ||
- name: Checking out repo | ||
uses: actions/checkout@v4 | ||
with: | ||
ssh-key: ${{ secrets.BOT_SSH_KEY }} | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.12' | ||
- name: Auto-merge pull requests if all status checks pass | ||
run: | | ||
build-scripts/hack/auto-merge-successful-prs.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/bin/env python3 | ||
|
||
import shlex | ||
import subprocess | ||
import json | ||
|
||
LABEL = "automerge" | ||
APPROVE_MSG = "All status checks passed for PR #{}." | ||
|
||
|
||
def sh(cmd: str) -> str: | ||
"""Run a shell command and return its output.""" | ||
_pipe = subprocess.PIPE | ||
result = subprocess.run(shlex.split(cmd), stdout=_pipe, stderr=_pipe, text=True) | ||
if result.returncode != 0: | ||
raise Exception(f"Error running command: {cmd}\nError: {result.stderr}") | ||
return result.stdout.strip() | ||
|
||
|
||
def get_pull_requests() -> list: | ||
"""Fetch open pull requests matching some label.""" | ||
prs_json = sh("gh pr list --state open --json number,labels") | ||
prs = json.loads(prs_json) | ||
return [pr for pr in prs if any(label["name"] == LABEL for label in pr["labels"])] | ||
|
||
|
||
def check_pr_passed(pr_number) -> bool: | ||
"""Check if all status checks passed for the given PR.""" | ||
checks_json = sh(f"gh pr checks {pr_number} --json bucket") | ||
checks = json.loads(checks_json) | ||
return all(check["bucket"] == "pass" for check in checks) | ||
|
||
|
||
def approve_and_merge_pr(pr_number) -> None: | ||
"""Approve and merge the PR.""" | ||
print(APPROVE_MSG.format(pr_number) + "Proceeding with merge...") | ||
sh(f'gh pr review {pr_number} --approve -b "{APPROVE_MSG.format(pr_number)}"') | ||
sh(f"gh pr merge {pr_number} --auto --squash") | ||
|
||
|
||
def process_pull_requests(): | ||
"""Process the PRs and merge if checks have passed.""" | ||
prs = get_pull_requests() | ||
|
||
for pr in prs: | ||
pr_number: int = pr["number"] | ||
|
||
if check_pr_passed(pr_number): | ||
approve_and_merge_pr(pr_number) | ||
else: | ||
print(f"Status checks have not passed for PR #{pr_number}. Skipping merge.") | ||
|
||
|
||
if __name__ == "__main__": | ||
process_pull_requests() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,12 @@ | |
|
||
DIR="$(realpath "$(dirname "${0}")")" | ||
|
||
# Configure git author | ||
git config user.email [email protected] | ||
git config user.name k8s-bot | ||
# Configure git author if unset | ||
addyess marked this conversation as resolved.
Show resolved
Hide resolved
|
||
git_email=$(git config --default "" user.email) | ||
if [ -z "${git_email}" ]; then | ||
git config user.email [email protected] | ||
git config user.name 'k8s-team-ci (CDK Bot)' | ||
fi | ||
|
||
# Remove unrelated tests | ||
rm "${DIR}/../../../tests/integration/tests/test_cilium_e2e.py" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,11 @@ | |
DIR="$(realpath "$(dirname "${0}")")" | ||
|
||
# Configure git author | ||
git config user.email [email protected] | ||
git config user.name k8s-bot | ||
git_email=$(git config --default "" user.email) | ||
if [ -z "${git_email}" ]; then | ||
git config user.email [email protected] | ||
git config user.name 'k8s-team-ci (CDK Bot)' | ||
fi | ||
|
||
# Apply strict patch | ||
git am "${DIR}/0001-Strict-patch.patch" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it took some digging, but i believe i confirmed this wasn't doing anything because the branch on the runner was checked out using an SSH key -- the token isn't necessary here to push to the branch. Instead it relies on the ssh key already being part of the local git config.