Skip to content

Commit

Permalink
Merge pull request #380 from gitautoai/wes
Browse files Browse the repository at this point in the history
Fix a 422 error: 'Reviews may only be requested from collaborators. One or more of the users or teams you specified is not a collaborator of the guibranco/progressbar repository.' in add_reviewers()
  • Loading branch information
hiroshinishio authored Nov 18, 2024
2 parents 21e4879 + e15ae94 commit dc192b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 17 additions & 1 deletion services/github/pulls_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from config import GITHUB_API_URL, TIMEOUT, PER_PAGE
from services.github.create_headers import create_headers
from services.github.github_types import BaseArgs
from services.github.user_manager import check_user_is_collaborator
from utils.handle_exceptions import handle_exceptions


Expand All @@ -15,9 +16,24 @@ def add_reviewers(base_args: BaseArgs):
base_args["token"],
base_args["reviewers"],
)

# Check if the reviewers are collaborators because reviewers must be collaborators
valid_reviewers: list[str] = []
for reviewer in reviewers:
is_collaborator = check_user_is_collaborator(
owner=owner, repo=repo, user=reviewer, token=token
)
if is_collaborator:
valid_reviewers.append(reviewer)

# If no valid reviewers, return
if not valid_reviewers:
return

# Add the reviewers to the pull request
url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/pulls/{pr_number}/requested_reviewers"
headers = create_headers(token=token)
json = {"reviewers": reviewers}
json = {"reviewers": valid_reviewers}
response = requests.post(url=url, headers=headers, json=json, timeout=TIMEOUT)
response.raise_for_status()

Expand Down
14 changes: 14 additions & 0 deletions services/github/user_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import requests
from config import GITHUB_API_URL, TIMEOUT
from services.github.create_headers import create_headers
from utils.handle_exceptions import handle_exceptions


@handle_exceptions(default_return_value=False, raise_on_error=False)
def check_user_is_collaborator(owner: str, repo: str, user: str, token: str) -> bool:
"""https://docs.github.com/en/rest/collaborators/collaborators?apiVersion=2022-11-28#check-if-a-user-is-a-repository-collaborator"""
url = f"{GITHUB_API_URL}/repos/{owner}/{repo}/collaborators/{user}"
headers = create_headers(token=token)
response = requests.get(url=url, headers=headers, timeout=TIMEOUT)
response.raise_for_status()
return response.status_code == 204

0 comments on commit dc192b3

Please sign in to comment.