|
9 | 9 | # ==-------------------------------------------------------------------------==#
|
10 | 10 |
|
11 | 11 | import argparse
|
| 12 | +import fnmatch |
12 | 13 | from git import Repo # type: ignore
|
13 | 14 | import github
|
14 | 15 | import os
|
@@ -65,6 +66,43 @@ def run(self) -> bool:
|
65 | 66 | return False
|
66 | 67 |
|
67 | 68 |
|
| 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 | + |
68 | 106 | def setup_llvmbot_git(git_dir="."):
|
69 | 107 | """
|
70 | 108 | Configure the git repo in `git_dir` with the llvmbot account so
|
@@ -506,6 +544,9 @@ def execute_command(self) -> bool:
|
506 | 544 | issue_subscriber_parser.add_argument("--label-name", type=str, required=True)
|
507 | 545 | issue_subscriber_parser.add_argument("--issue-number", type=int, required=True)
|
508 | 546 |
|
| 547 | +pr_subscriber_parser = subparsers.add_parser("pr-subscriber") |
| 548 | +pr_subscriber_parser.add_argument("--issue-number", type=int, required=True) |
| 549 | + |
509 | 550 | release_workflow_parser = subparsers.add_parser("release-workflow")
|
510 | 551 | release_workflow_parser.add_argument(
|
511 | 552 | "--llvm-project-dir",
|
@@ -551,6 +592,11 @@ def execute_command(self) -> bool:
|
551 | 592 | args.token, args.repo, args.issue_number, args.label_name
|
552 | 593 | )
|
553 | 594 | 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() |
554 | 600 | elif args.command == "release-workflow":
|
555 | 601 | release_workflow = ReleaseWorkflow(
|
556 | 602 | args.token,
|
|
0 commit comments