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