|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | + |
| 5 | +from github import Github |
| 6 | + |
| 7 | + |
| 8 | +def main() -> None: |
| 9 | + token = os.environ.get("GITHUB_TOKEN") |
| 10 | + |
| 11 | + repo_owner = "pytorch" |
| 12 | + repo_name = "pytorch" |
| 13 | + pull_request_number = int(sys.argv[1]) |
| 14 | + |
| 15 | + g = Github(token) |
| 16 | + repo = g.get_repo(f"{repo_owner}/{repo_name}") |
| 17 | + pull_request = repo.get_pull(pull_request_number) |
| 18 | + pull_request_body = pull_request.body |
| 19 | + # PR without description |
| 20 | + if pull_request_body is None: |
| 21 | + return |
| 22 | + |
| 23 | + # get issue number from the PR body |
| 24 | + if not re.search(r"#\d{1,6}", pull_request_body): |
| 25 | + print("The pull request does not mention an issue.") |
| 26 | + return |
| 27 | + issue_number = int(re.findall(r"#(\d{1,6})", pull_request_body)[0]) |
| 28 | + issue = repo.get_issue(issue_number) |
| 29 | + issue_labels = issue.labels |
| 30 | + docathon_label_present = any( |
| 31 | + label.name == "docathon-2026" for label in issue_labels |
| 32 | + ) |
| 33 | + |
| 34 | + # if the issue has a docathon label, add all labels from the issue to the PR. |
| 35 | + if not docathon_label_present: |
| 36 | + print("The 'docathon-2026' label is not present in the issue.") |
| 37 | + return |
| 38 | + pull_request_labels = pull_request.get_labels() |
| 39 | + pull_request_label_names = [label.name for label in pull_request_labels] |
| 40 | + issue_label_names = [label.name for label in issue_labels] |
| 41 | + labels_to_add = [ |
| 42 | + label |
| 43 | + for label in issue_label_names |
| 44 | + if label not in pull_request_label_names and label != "actionable" |
| 45 | + ] |
| 46 | + if not labels_to_add: |
| 47 | + print("The pull request already has the same labels.") |
| 48 | + return |
| 49 | + pull_request.add_to_labels(*labels_to_add) |
| 50 | + print("Labels added to the pull request!") |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + main() |
0 commit comments