Skip to content

Commit

Permalink
Merge pull request #83 from anovadox/handle-repository-error
Browse files Browse the repository at this point in the history
return None in case of a github exception for get_active_date
  • Loading branch information
zkoppert authored Feb 5, 2024
2 parents 63c1ec2 + 5261bba commit f002d1e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
34 changes: 19 additions & 15 deletions stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,25 @@ def get_active_date(repo):
A date object representing the last activity date of the repository.
"""
activity_method = os.getenv("ACTIVITY_METHOD", "pushed").lower()
if activity_method == "default_branch_updated":
commit = repo.branch(repo.default_branch).commit
active_date = parse(commit.commit.as_dict()["committer"]["date"])
elif activity_method == "pushed":
last_push_str = repo.pushed_at # type: ignored
if last_push_str is None:
return None
active_date = parse(last_push_str)
else:
raise ValueError(
f"""
ACTIVITY_METHOD environment variable has unsupported value: '{activity_method}'.
Allowed values are: 'pushed' and 'default_branch_updated'
"""
)
try:
if activity_method == "default_branch_updated":
commit = repo.branch(repo.default_branch).commit
active_date = parse(commit.commit.as_dict()["committer"]["date"])
elif activity_method == "pushed":
last_push_str = repo.pushed_at # type: ignored
if last_push_str is None:
return None
active_date = parse(last_push_str)
else:
raise ValueError(
f"""
ACTIVITY_METHOD environment variable has unsupported value: '{activity_method}'.
Allowed values are: 'pushed' and 'default_branch_updated'
"""
)
except github3.exceptions.GitHubException:
print(f"{repo.html_url} had an exception trying to get the activity date.")
return None
return active_date


Expand Down
24 changes: 24 additions & 0 deletions test_stale_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import github3.github
from stale_repos import (
auth_to_github,
get_active_date,
get_inactive_repos,
is_repo_exempt,
output_to_json,
Expand Down Expand Up @@ -470,6 +471,29 @@ def test_write_to_markdown(self):
mock_file.__enter__.return_value.assert_has_calls(expected_calls)


@patch.dict(os.environ, {"ACTIVITY_METHOD": "default_branch_updated"})
class GetActiveDateTestCase(unittest.TestCase):
"""
Unit test case for the get_active_date() function.
This test case class verifies that get_active_date will return None if
github3 throws any kind of exception.
"""

def test_returns_none_for_exception(self):
"""Test that get will return None if github3 throws any kind of exception."""
repo = MagicMock(
name="repo", default_branch="main", spec=["branch", "html_url"]
)

repo.branch.side_effect = github3.exceptions.NotFoundError(
resp=MagicMock(status_code=404)
)
result = get_active_date(repo)

assert result is None


class OutputToJson(unittest.TestCase):
"""
Unit test case for the output_to_json() function.
Expand Down

0 comments on commit f002d1e

Please sign in to comment.