Skip to content

Commit f168733

Browse files
committed
workflows: Add a simple pull request subscription workflow
This new workflows will make it possible for people to subscribe to pull requests based on the files that are being modified. Whenever a pull request is created, this action will look through all of the teams whose name begins with pr-subscribers- and it will try to match the files changed in the pull request to a comma separated list of globs in the team's description. One limitation of this workflow is that it only runs when a pull request is created, so any files added in subsequent updates will not create notifications for the appropriate teams. This is planned to be fixed in a future update.
1 parent efe8aa2 commit f168733

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

.github/workflows/pr-subscriber.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: PR Subscriber
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
auto-subscribe:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Setup Automation Script
16+
run: |
17+
curl -O -L https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$GITHUB_SHA/llvm/utils/git/github-automation.py
18+
curl -O -L https://raw.githubusercontent.com/$GITHUB_REPOSITORY/$GITHUB_SHA/llvm/utils/git/requirements.txt
19+
chmod a+x github-automation.py
20+
pip install -r requirements.txt
21+
22+
- name: Update watchers
23+
run: |
24+
./github-automation.py \
25+
--token '${{ secrets.ISSUE_SUBSCRIBER_TOKEN }}' \
26+
pr-subscriber \
27+
--issue-number '${{ github.event.pull_request.number }}'

llvm/utils/git/github-automation.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# ==-------------------------------------------------------------------------==#
1010

1111
import argparse
12+
import fnmatch
1213
from git import Repo # type: ignore
1314
import github
1415
import os
@@ -65,6 +66,43 @@ def run(self) -> bool:
6566
return False
6667

6768

69+
class PRSubscriber:
70+
71+
class PRTeam:
72+
def __init__(self, slug: str, description: str):
73+
self.slug = slug
74+
self.globs = [x.strip() for x in description.split(",")]
75+
76+
def __init__(self, token: str, repo: str, pr_number: int):
77+
self.repo = github.Github(token).get_repo(repo)
78+
self.org = github.Github(token).get_organization(self.repo.organization.login)
79+
self.pr = self.repo.get_issue(pr_number).as_pull_request()
80+
self.teams = []
81+
for team in self.org.get_teams():
82+
if not team.name.startswith("pr-subscribers-"):
83+
continue
84+
self.teams.append(PRSubscriber.PRTeam(team.slug, team.description))
85+
86+
def run(self) -> bool:
87+
mentions = []
88+
for c in self.pr.get_commits():
89+
for f in c.files:
90+
print(f.filename)
91+
for t in self.teams:
92+
for g in t.globs:
93+
if len(fnmatch.filter([f.filename for f in c.files], g)):
94+
print('Matches {} for {}'.format(g, t.slug))
95+
mentions.append(t.slug)
96+
break
97+
98+
if not len(mentions):
99+
return False
100+
101+
comment = "\n".join(['@llvm/{}'.format(m) for m in mentions])
102+
self.pr.as_issue().create_comment(comment)
103+
return True
104+
105+
68106
def setup_llvmbot_git(git_dir="."):
69107
"""
70108
Configure the git repo in `git_dir` with the llvmbot account so
@@ -506,6 +544,9 @@ def execute_command(self) -> bool:
506544
issue_subscriber_parser.add_argument("--label-name", type=str, required=True)
507545
issue_subscriber_parser.add_argument("--issue-number", type=int, required=True)
508546

547+
pr_subscriber_parser = subparsers.add_parser("pr-subscriber")
548+
pr_subscriber_parser.add_argument("--issue-number", type=int, required=True)
549+
509550
release_workflow_parser = subparsers.add_parser("release-workflow")
510551
release_workflow_parser.add_argument(
511552
"--llvm-project-dir",
@@ -551,6 +592,11 @@ def execute_command(self) -> bool:
551592
args.token, args.repo, args.issue_number, args.label_name
552593
)
553594
issue_subscriber.run()
595+
elif args.command == "pr-subscriber":
596+
pr_subscriber = PRSubscriber(
597+
args.token, args.repo, args.issue_number
598+
)
599+
pr_subscriber.run()
554600
elif args.command == "release-workflow":
555601
release_workflow = ReleaseWorkflow(
556602
args.token,

0 commit comments

Comments
 (0)