Skip to content

Commit

Permalink
[fix] Fix crash in GiteaAPI api.get repos() when URL does not exist (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse committed Mar 8, 2021
1 parent cb7765a commit 672532e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/_repobee/ext/gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,14 @@ def get_repos(
) -> Iterable[plug.Repo]:
"""See :py:meth:`repobee_plug.PlatformAPI.get_repos`."""
if repo_urls:
return map(self._get_repo_by_url, repo_urls)
return [
repo
for repo in map(
lambda url: self._get_repo_by_url(url, ignore_errors=True),
repo_urls,
)
if repo is not None
]

endpoint = f"/orgs/{self._org_name}/repos"
response = self._request(
Expand All @@ -260,12 +267,17 @@ def get_repos(

return (self._wrap_repo(repo_data) for repo_data in response.json())

def _get_repo_by_url(self, url: str) -> plug.Repo:
def _get_repo_by_url(
self, url: str, ignore_errors: bool = False
) -> Optional[plug.Repo]:
error_msg = None if ignore_errors else f"failed to fetch repo at {url}"
endpoint = f"/repos/{self._org_name}/{self.extract_repo_name(url)}"
response = self._request(
requests.get, endpoint, error_msg=f"failed to fetch repo at {url}"
response = self._request(requests.get, endpoint, error_msg=error_msg)
return (
self._wrap_repo(response.json())
if response.status_code == 200
else None
)
return self._wrap_repo(response.json())

def assign_repo(
self, team: plug.Team, repo: plug.Repo, permission: plug.TeamPermission
Expand Down
18 changes: 18 additions & 0 deletions system_tests/gitea/test_gitea.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ def test_get_template_repos_by_urls(self, template_api):
expected_repo_names
)

def test_getting_repos_that_do_not_exist(
self, target_api, with_student_repos
):
# arrange
bad_repo_urls = [
url[:-5]
for url in target_api.get_repo_urls(
templates.TEMPLATE_REPO_NAMES,
team_names=giteamanager.STUDENT_TEAMS,
)
]

# act
repos = list(target_api.get_repos(bad_repo_urls))

# assert
assert repos == []


class TestAssignRepo:
"""Tests for the assign_repo function."""
Expand Down

0 comments on commit 672532e

Please sign in to comment.