-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add script and workflow that is able to "cancel all CI runs for a specific PR/commit" #35975
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d386a44
Add a script and workflow for 'cancel running workflows'.
andreilitvin 784c2cf
Restyle
andreilitvin 44a2d59
Merge branch 'master' into cancel_runs_script
andy31415 c82f9c1
Rename parameter to make it clear it is a github token
andy31415 8846e49
Update scripts/tools/cancel_workflows_for_pr.py
andy31415 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,55 @@ | ||
# Copyright (c) 2024 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
name: Cancel workflow on PR | ||
on: | ||
workflow_dispatch: | ||
inputs: | ||
pull_request_id: | ||
description: 'PR number to consider' | ||
required: true | ||
type: number | ||
commit_sha: | ||
description: 'Cancel runs for this specific SHA' | ||
required: true | ||
type: string | ||
|
||
jobs: | ||
cancel_workflow: | ||
name: Report on pull requests | ||
|
||
runs-on: ubuntu-latest | ||
|
||
# Don't run on forked repos | ||
if: github.repository_owner == 'project-chip' | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.12' | ||
- name: Setup pip modules we use | ||
run: | | ||
pip install \ | ||
click \ | ||
coloredlogs \ | ||
pygithub \ | ||
&& echo "DONE installint python prerequisites" | ||
- name: Cancel runs | ||
run: | | ||
scripts/tools/cancel_workflows_for_pr.py \ | ||
--pull-request ${{ inputs.pull_request_id }} \ | ||
--commit-sha "${{ inputs.commit_sha }}" \ | ||
--gh-api-token "${{ secrets.GITHUB_TOKEN }}" |
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,87 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# Copyright (c) 2024 Project CHIP Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import logging | ||
|
||
import click | ||
import coloredlogs | ||
from github import Github | ||
|
||
__LOG_LEVELS__ = { | ||
"debug": logging.DEBUG, | ||
"info": logging.INFO, | ||
"warn": logging.WARN, | ||
"fatal": logging.FATAL, | ||
} | ||
|
||
REPOSITORY = "project-chip/connectedhomeip" | ||
|
||
|
||
class Canceller: | ||
def __init__(self, token): | ||
self.api = Github(token) | ||
self.repo = self.api.get_repo(REPOSITORY) | ||
|
||
def cancel_all_runs(self, pr_number, commit_sha, dry_run): | ||
pr = self.repo.get_pull(pr_number) | ||
logging.info("Examining PR '%s'", pr.title) | ||
for commit in pr.get_commits(): | ||
if commit.sha != commit_sha: | ||
logging.info("Skipping SHA '%s' as it was not selected", commit.sha) | ||
continue | ||
|
||
for check_suite in commit.get_check_suites(): | ||
for run in check_suite.get_check_runs(): | ||
if run.status in {"in_progress", "queued"}: | ||
if dry_run: | ||
logging.warning("DRY RUN: Will not stop run %s", run.name) | ||
else: | ||
logging.warning("Stopping run %s", run.name) | ||
self.repo.get_workflow_run(run.id).cancel() | ||
else: | ||
logging.info("Skip over run %s (%s)", run.name, run.status) | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--log-level", | ||
default="INFO", | ||
type=click.Choice(list(__LOG_LEVELS__.keys()), case_sensitive=False), | ||
help="Determines the verbosity of script output.", | ||
) | ||
@click.option("--pull-request", type=int, help="Pull request number to consider") | ||
@click.option("--commit-sha", help="Commit to look at when cancelling pull requests") | ||
@click.option("--gh-api-token", help="Github token to use") | ||
@click.option("--token-file", help="Read github token from the given file") | ||
@click.option("--dry-run", default=False, is_flag=True, help="Actually cancel or not") | ||
def main(log_level, pull_request, commit_sha, gh_api_token, token_file, dry_run): | ||
coloredlogs.install( | ||
level=__LOG_LEVELS__[log_level], fmt="%(asctime)s %(levelname)-7s %(message)s" | ||
) | ||
|
||
if gh_api_token: | ||
gh_token = gh_api_token | ||
elif token_file: | ||
gh_token = open(token_file, "rt").read().strip() | ||
else: | ||
raise Exception("Require a --gh-api-token or --token-file to access github") | ||
|
||
Canceller(gh_token).cancel_all_runs(pull_request, commit_sha, dry_run) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
Uh oh!
There was an error while loading. Please reload this page.