|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""check_labels.py""" |
| 3 | + |
| 4 | +from typing import Any, List |
| 5 | +from datetime import datetime, timedelta |
| 6 | + |
| 7 | +from export_pytorch_labels import get_pytorch_labels |
| 8 | +from gitutils import ( |
| 9 | + get_git_remote_name, |
| 10 | + get_git_repo_dir, |
| 11 | + GitRepo, |
| 12 | +) |
| 13 | +from trymerge import ( |
| 14 | + _fetch_url, |
| 15 | + gh_post_pr_comment, |
| 16 | + GitHubPR, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +BOT_AUTHORS = ["github-actions", "pytorchmergebot", "pytorch-bot"] |
| 21 | + |
| 22 | +ERR_MSG_TITLE = "This PR needs a label" |
| 23 | +ERR_MSG = ( |
| 24 | + f"# {ERR_MSG_TITLE}\n" |
| 25 | + "If your changes are user facing and intended to be a part of release notes, please use a label starting with `release notes:`.\n\n" # noqa: E501 pylint: disable=line-too-long |
| 26 | + "If not, please add the `topic: not user facing` label.\n\n" |
| 27 | + "For more information, see https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work." # noqa: E501 pylint: disable=line-too-long |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def get_release_notes_labels() -> List[str]: |
| 32 | + return [label for label in get_pytorch_labels() if label.lstrip().startswith("release notes:")] |
| 33 | + |
| 34 | + |
| 35 | +def delete_comment(comment_id: int) -> None: |
| 36 | + url = f"https://api.github.com/repos/pytorch/pytorch/issues/comments/{comment_id}" |
| 37 | + _fetch_url(url, method="DELETE") |
| 38 | + |
| 39 | + |
| 40 | +def has_required_labels(pr: GitHubPR) -> bool: |
| 41 | + pr_labels = pr.get_labels() |
| 42 | + |
| 43 | + # Check if PR is not user facing |
| 44 | + is_not_user_facing_pr = any(label.strip() == "topic: not user facing" for label in pr_labels) |
| 45 | + if is_not_user_facing_pr: |
| 46 | + return True |
| 47 | + |
| 48 | + # Check if bot has already posted a message within the past hour to include a release notes label |
| 49 | + for comment in pr.get_comments(): |
| 50 | + if comment.body_text.lstrip(" #").startswith(ERR_MSG_TITLE) and comment.author_login in BOT_AUTHORS: |
| 51 | + ts = datetime.strptime(comment.created_at, "%Y-%m-%dT%H:%M:%SZ") |
| 52 | + if (datetime.utcnow() - ts) < timedelta(hours=1): |
| 53 | + return True |
| 54 | + delete_comment(comment.database_id) |
| 55 | + break |
| 56 | + |
| 57 | + return any(label.strip() in get_release_notes_labels() for label in pr_labels) |
| 58 | + |
| 59 | + |
| 60 | +def parse_args() -> Any: |
| 61 | + from argparse import ArgumentParser |
| 62 | + parser = ArgumentParser("Check PR labels") |
| 63 | + parser.add_argument("pr_num", type=int) |
| 64 | + |
| 65 | + return parser.parse_args() |
| 66 | + |
| 67 | + |
| 68 | +def main() -> None: |
| 69 | + args = parse_args() |
| 70 | + repo = GitRepo(get_git_repo_dir(), get_git_remote_name()) |
| 71 | + org, project = repo.gh_owner_and_name() |
| 72 | + pr = GitHubPR(org, project, args.pr_num) |
| 73 | + |
| 74 | + try: |
| 75 | + if not has_required_labels(pr): |
| 76 | + print(ERR_MSG) |
| 77 | + gh_post_pr_comment(pr.org, pr.project, pr.pr_num, ERR_MSG) |
| 78 | + exit(1) |
| 79 | + except Exception as e: |
| 80 | + pass |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main() |
0 commit comments