Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return None in case of a github exception for get_active_date #83

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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