Skip to content

Commit 29d4d24

Browse files
Svetlana Karslioglumalfet
Svetlana Karslioglu
andauthored
Propagate labels from the issue to the PR (#2357)
* Add a GH action that propagates labels from an issue with docathon label to the PR that references it. --------- Co-authored-by: Nikita Shulga <nshulga@meta.com>
1 parent 8878969 commit 29d4d24

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Docathon Labels Sync
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, edited]
6+
7+
jobs:
8+
check-labels:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Check if PR mentions an issue and get labels
13+
uses: actions/checkout@v2
14+
with:
15+
fetch-depth: 0
16+
- name: Set up Python
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: 3.x
20+
- name: Install dependencies
21+
run: |
22+
pip install requests
23+
pip install PyGithub
24+
- name: Run Python script
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
run: python ./.github/scripts/docathon-label-sync.py ${{ github.event.pull_request.number }}

0 commit comments

Comments
 (0)