-
Notifications
You must be signed in to change notification settings - Fork 37
allow bots to automerge PR if all checks pass #7845
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
Draft
tomasmatus
wants to merge
6
commits into
cockpit-project:main
Choose a base branch
from
tomasmatus:automerge-bots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d29fe3e
github.py: add methods to approve PR and get author info
tomasmatus 9ad919e
add initial auto-merge capability
tomasmatus cd06817
debug
tomasmatus 7847b7b
get PR info from run-queue structure
tomasmatus 9604f39
more debug
tomasmatus 4e0a820
add mock for statuses
tomasmatus 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 hidden or 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,85 @@ | ||
| # This file is part of Cockpit. | ||
| # | ||
| # Copyright (C) 2025 Red Hat, Inc. | ||
| # | ||
| # Cockpit is free software; you can redistribute it and/or modify it | ||
| # under the terms of the GNU Lesser General Public License as published by | ||
| # the Free Software Foundation; either version 2.1 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Cockpit is distributed in the hope that it will be useful, but | ||
| # WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| # Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Cockpit; If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| import logging | ||
|
|
||
| from lib.aio.jsonutil import get_int, get_str | ||
| from task import github | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # TODO: verify if this is always the same | ||
| GITHUB_CI = { | ||
| 'login': 'github-actions[bot]', | ||
| 'id': 41898282, | ||
| } | ||
|
|
||
| COCKPITUOUS = { | ||
| 'login': 'cockpituous', | ||
| 'id': 14330603, | ||
| } | ||
|
|
||
|
|
||
| def is_ci_bot(api: github.GitHub, pr: int) -> bool: | ||
| author = api.get_author(pr) | ||
| print(author) | ||
| login = get_str(author, 'login') | ||
| login_id = get_int(author, 'id') | ||
|
|
||
| return ((login == GITHUB_CI['login'] and login_id == GITHUB_CI['id']) or | ||
| (login == COCKPITUOUS['login'] and login_id == COCKPITUOUS['id'])) | ||
|
|
||
|
|
||
| def all_checks_pass(api: github.GitHub, commit_hash: str) -> bool: | ||
| statuses = api.statuses(commit_hash) | ||
| print(f"STATUSES: {statuses}") | ||
|
|
||
| print("Checking statuses:") | ||
| if len(statuses) == 0: | ||
| print("No statuses found for commit %s", commit_hash) | ||
| return False | ||
|
|
||
| for context in statuses: | ||
| status = statuses[context] | ||
| status_state = get_str(status, 'state') | ||
| print("Status for context '%s': %s", context, status_state) | ||
| if status_state != 'success': | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def auto_merge_bots_pr(repo: str, pr: int, sha: str) -> None: | ||
| api = github.GitHub(repo=repo) | ||
|
|
||
| print(f"is_cu_bot: {is_ci_bot(api, pr)}") | ||
| # Make sure that the PR was made by cockpituous or github actions | ||
| # if not is_ci_bot(api, pr): | ||
| # logger.info("PR not made by CI bot, skipping automerge") | ||
| # return | ||
|
|
||
| # check that all checks are green | ||
| all_pass = all_checks_pass(api, sha) | ||
| print(f"all_checks_pass: {all_pass}") | ||
| if not all_pass: | ||
| print("Not every check has passed, skipping automerge") | ||
| return | ||
|
|
||
| logger.info("All checks green, can automerge") | ||
| print("All checks green, can automerge") | ||
| # merge the PR | ||
| api.approve_pr(pr, sha) | ||
This file contains hidden or 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 hidden or 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 hidden or 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
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.
Check notice
Code scanning / CodeQL
Commented-out code Note