Ryujinx-Mako/ryujinx_mako/commands/update_reviewers.py
TSRBerry 628d977de2
Create a new python package for the current scripts (#1)
* infra: Add PR triage action (#5293)

This is a bare minimal triage action that handle big categories.

In the future we could also label all services correctly but
I didn't felt this was required for a first iteration.

* infra: Fix PR triage workflow glob patterns (#5297)

* Use glob patterns to match file paths

* Update ignored paths for releases

* Adjust build.yml as well

* Add names to auto-assign steps

* Fix developer team name

* Allow build workflows to run if workflows changed

* Fix action version (#5299)

* infra: Sync paths-ignore with release job and attempt to fix review assign

* infra: Fix PR triage once and for all (#5442)

Switch to a custom made python script that query GitHub API to grab latest state of the PR after label assign.

* infra: Fix team name in reviewer.yml

* infra: do not assign developers team for now

Hopefully fix PR triage for real...

* Introduce Mako to fix pr_triage workflow (#5574)

* pr_triage: Fix invalid workflow

* Don't assign reviewers to draft PRs

* Add team review request for developers team

* Introduce Mako to make team reviewers work

* Convert app and installation ids to int (#5587)

* infra: add missing quotes around @ developers in reviewers.yml

* Adjust README.md and LICENSE

* Remove unneeded workflow files

* Add basic .editorconfig

* Add basic requirements.txt

* Add dependabot.yml

* Add formatting workflow

* Move update_reviewers.py into a new python module

* Add reviewers.yml template

* Replace requirements.txt with poetry

* Fix typo

* Add setup-mako action

* Add constants to private module

* Migrate update_reviewers to subcommand

* Add NullHandler for root logger

* Add setup-git subcommand

* Group all subcommands

* Add main script for ryujinx_mako

* Create ryujinx-mako command

* Add more information to README.md

* Apply black formatting

* Make PR author check case-insensitive

---------

Co-authored-by: Mary <mary@mary.zone>
Co-authored-by: ryujinx-mako[bot] <142357924+ryujinx-mako[bot]@users.noreply.github.com>
2023-08-30 19:00:29 +02:00

92 lines
2.9 KiB
Python

from argparse import ArgumentParser, Namespace
from pathlib import Path
from github.Repository import Repository
from github.GithubException import GithubException
import yaml
from ryujinx_mako.commands._subcommand import GithubSubcommand
class UpdateReviewers(GithubSubcommand):
@staticmethod
def name() -> str:
return "update-reviewers"
@staticmethod
def description() -> str:
return "Update reviewers for the specified PR"
def __init__(self, parser: ArgumentParser):
self._reviewers = set()
self._team_reviewers = set()
parser.add_argument("repo_path", type=str)
parser.add_argument("pr_number", type=int)
parser.add_argument("config_path", type=Path)
super().__init__(parser)
@property
def reviewers_lower(self) -> list[str]:
return [x.lower() for x in self._reviewers]
def add_reviewers(self, new_entries: list[str]):
for reviewer in new_entries:
if reviewer.startswith("@"):
self._team_reviewers.add(reviewer[1:])
else:
self._reviewers.add(reviewer)
def update_reviewers(self, config, repo: Repository, pr_number: int) -> int:
pull_request = repo.get_pull(pr_number)
if not pull_request:
self.logger.error(f"Unknown PR #{pr_number}")
return 1
if pull_request.draft:
self.logger.warning("Not assigning reviewers for draft PRs")
return 0
pull_request_author = pull_request.user.login
for label in pull_request.labels:
if label.name in config:
self.add_reviewers(config[label.name])
if "default" in config:
self.add_reviewers(config["default"])
if pull_request_author.lower() in self.reviewers_lower:
self._reviewers.remove(self.reviewers_lower.index(pull_request_author.lower()))
try:
reviewers = list(self._reviewers)
team_reviewers = list(self._team_reviewers)
self.logger.info(
f"Attempting to assign reviewers ({reviewers}) "
f"and team_reviewers ({team_reviewers})"
)
pull_request.create_review_request(reviewers, team_reviewers)
return 0
except GithubException:
self.logger.exception(f"Cannot assign review request for PR #{pr_number}")
return 1
def run(self, args: Namespace):
repo = self.github.get_repo(args.repo_path)
if not repo:
self.logger.error("Repository not found!")
exit(1)
if not args.config_path.exists():
self.logger.error(f"Config '{args.config_path}' not found!")
exit(1)
with open(args.config_path, "r") as file:
config = yaml.safe_load(file)
exit(self.update_reviewers(config, repo, args.pr_number))