Skip to content

Commit

Permalink
api: removed gh contributors unauthenticated fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo authored and kpsherva committed Oct 17, 2023
1 parent 9932fa6 commit 4910724
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
49 changes: 25 additions & 24 deletions invenio_github/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
RemoteAccountNotFound,
RepositoryAccessError,
RepositoryNotFoundError,
UnexpectedGithubResponse,
)


Expand Down Expand Up @@ -505,35 +506,35 @@ def user_identity(self):

@cached_property
def contributors(self):
"""Get list of contributors to a repository."""
"""Get list of contributors to a repository.
The list of contributors is fetched from Github API, filtered for type "User" and sorted by contributions.
:returns: a generator of objects that contains contributors information.
:raises UnexpectedGithubResponse: when Github API returns a status code other than 200.
"""
max_contributors = current_app.config.get("GITHUB_MAX_CONTRIBUTORS_NUMBER", 30)
contributors_iter = self.gh.api.repository_with_id(
self.repository_object.github_id
).contributors()
contributors = list(contributors_iter)
if contributors_iter.last_status == 200:

def get_author(contributor):
r = requests.get(contributor["url"])
if r.status_code == 200:
data = r.json()
return data

# Sort according to number of contributions
contributors = sorted(
contributors,
key=lambda x: x.as_dict()["contributions"],
).contributors(number=max_contributors)

status = contributors_iter.last_status
if status == 200:
# Sort by contributions and filter only users.
sorted_contributors = sorted(
(c for c in contributors_iter if c.type == "User"),
key=lambda x: x.contributions,
reverse=True,
)
max_contributors = current_app.config.get(
"GITHUB_MAX_CONTRIBUTORS_NUMBER", 30

# Expand contributors using `Contributor.refresh()`
contributors = [x.refresh().as_dict() for x in sorted_contributors]
return contributors
else:
# Contributors fetch failed
raise UnexpectedGithubResponse(
f"Github returned unexpected code: {status} for release {self.repository_object.github_id}"
)
contributors = [
get_author(x.as_dict())
for x in contributors[:max_contributors]
if x.as_dict()["type"] == "User"
]
contributors = filter(lambda x: x is not None, contributors)
return list(contributors)

# Helper functions

Expand Down
10 changes: 10 additions & 0 deletions invenio_github/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,13 @@ class ReleaseNotFound(GitHubError):
def __init__(self, message=None):
"""Constructor."""
super().__init__(message or self.message)


class UnexpectedGithubResponse(GitHubError):
"""Request to Github API returned an unexpected error."""

message = "Github API returned an unexpected error."

def __init__(self, message=None):
"""Constructor."""
super().__init__(message or self.message)
2 changes: 1 addition & 1 deletion invenio_github/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def release_gh_metadata_handler(release, ex):

def release_default_exception_handler(release, ex):
"""Default handler."""
release.release_object.errors = _get_err_obj("Unknown error occured.")
release.release_object.errors = _get_err_obj(ex.message)
db.session.commit()


Expand Down

0 comments on commit 4910724

Please sign in to comment.