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