-
Notifications
You must be signed in to change notification settings - Fork 608
Add PR release note label checker bot #6835
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
6 commits
Select commit
Hold shift + click to select a range
99df003
Add release note label checker bot
jackzhxng 4cdcc84
Fix formatting (?)
jackzhxng 2fe35cc
Try to activate test on non ghstack pr
jackzhxng 2eff612
Fix indentation
huydhn fff7606
Resolve mising trymerge deps
jackzhxng 72ab2f9
Update .github/workflows/check-labels.yml
huydhn 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,62 @@ | ||
#!/usr/bin/env python3 | ||
"""Check whether a PR has required labels.""" | ||
|
||
import sys | ||
from typing import Any | ||
|
||
from github_utils import gh_delete_comment, gh_post_pr_comment | ||
from gitutils import get_git_remote_name, get_git_repo_dir, GitRepo | ||
from label_utils import has_required_labels, is_label_err_comment, LABEL_ERR_MSG | ||
from trymerge import GitHubPR | ||
|
||
|
||
def delete_all_label_err_comments(pr: "GitHubPR") -> None: | ||
for comment in pr.get_comments(): | ||
if is_label_err_comment(comment): | ||
gh_delete_comment(pr.org, pr.project, comment.database_id) | ||
|
||
|
||
def add_label_err_comment(pr: "GitHubPR") -> None: | ||
# Only make a comment if one doesn't exist already | ||
if not any(is_label_err_comment(comment) for comment in pr.get_comments()): | ||
gh_post_pr_comment(pr.org, pr.project, pr.pr_num, LABEL_ERR_MSG) | ||
|
||
|
||
def parse_args() -> Any: | ||
from argparse import ArgumentParser | ||
|
||
parser = ArgumentParser("Check PR labels") | ||
parser.add_argument("pr_num", type=int) | ||
# add a flag to return a non-zero exit code if the PR does not have the required labels | ||
parser.add_argument( | ||
"--exit-non-zero", | ||
action="store_true", | ||
help="Return a non-zero exit code if the PR does not have the required labels", | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def main() -> None: | ||
args = parse_args() | ||
repo = GitRepo(get_git_repo_dir(), get_git_remote_name()) | ||
org, project = repo.gh_owner_and_name() | ||
pr = GitHubPR(org, project, args.pr_num) | ||
|
||
try: | ||
if not has_required_labels(pr): | ||
print(LABEL_ERR_MSG) | ||
add_label_err_comment(pr) | ||
if args.exit_non_zero: | ||
sys.exit(1) | ||
else: | ||
delete_all_label_err_comments(pr) | ||
except Exception as e: | ||
if args.exit_non_zero: | ||
sys.exit(1) | ||
|
||
sys.exit(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,54 @@ | ||
name: Check Labels | ||
|
||
on: | ||
# We need pull_request_target to be able to post comments on PRs from forks. | ||
# Only allow pull_request_target when merging to main, not some historical branch. | ||
# | ||
# Make sure to don't introduce explicit checking out and installing/running | ||
# untrusted user code into this workflow! | ||
pull_request_target: | ||
types: [opened, synchronize, reopened, labeled, unlabeled] | ||
branches: [main] | ||
|
||
# To check labels on ghstack PRs. | ||
# Note: as pull_request doesn't trigger on PRs targeting main, | ||
# to test changes to the workflow itself one needs to create | ||
# a PR that targets a gh/**/base branch. | ||
pull_request: | ||
types: [opened, synchronize, reopened, labeled, unlabeled] | ||
branches: [gh/**/base] | ||
jackzhxng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
workflow_dispatch: | ||
inputs: | ||
pr_number: | ||
description: 'PR number to check labels for' | ||
required: true | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
check-labels: | ||
permissions: | ||
contents: read | ||
pull-requests: write | ||
name: Check labels | ||
if: github.repository_owner == 'pytorch' | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- uses: actions/checkout@v3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me double check the behavior of |
||
with: | ||
fetch-depth: 0 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.10' | ||
# Not the direct dependencies but the script uses trymerge | ||
- run: pip install pyyaml==6.0 rockset==1.0.3 | ||
- name: Check labels | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
PR_NUM: ${{ github.event.number || github.event.inputs.pr_number }} | ||
run: | | ||
set -ex | ||
python3 .github/scripts/check_labels.py --exit-non-zero "${PR_NUM}" |
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.
Can you create this PR from codev by exporting it using ghstack so you can test on a gh/ branch?
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.
#6930 for ghstack