diff --git a/stale_repos.py b/stale_repos.py index fd2c597..e927bcc 100755 --- a/stale_repos.py +++ b/stale_repos.py @@ -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 diff --git a/test_stale_repos.py b/test_stale_repos.py index 266eb01..f56ca0e 100644 --- a/test_stale_repos.py +++ b/test_stale_repos.py @@ -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, @@ -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.