From bb7138d9984d059d0aebf303a27f7d62c619cf54 Mon Sep 17 00:00:00 2001 From: Andrew Nova Date: Fri, 2 Feb 2024 22:10:30 -0500 Subject: [PATCH 1/4] return None in case of exception for get_active_date occasionally repo.get_branch can raise an exception: github3.exceptions.NotFoundError: 404 Branch not found even will called on the repos default branch --- stale_repos.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/stale_repos.py b/stale_repos.py index fd2c597..1b9a8a4 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 From a99c6d3de41abc708be79a8a1501d24f10f25a3f Mon Sep 17 00:00:00 2001 From: Andrew Nova Date: Mon, 5 Feb 2024 10:11:38 -0500 Subject: [PATCH 2/4] formatting for line length --- stale_repos.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stale_repos.py b/stale_repos.py index 1b9a8a4..e927bcc 100755 --- a/stale_repos.py +++ b/stale_repos.py @@ -164,9 +164,9 @@ def get_active_date(repo): else: raise ValueError( f""" - ACTIVITY_METHOD environment variable has unsupported value: '{activity_method}'. - Allowed values are: 'pushed' and 'default_branch_updated' - """ + 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.") From 6afa96b6b353955a0a73305d8e25790caa6dbbd2 Mon Sep 17 00:00:00 2001 From: Andrew Nova Date: Mon, 5 Feb 2024 10:27:44 -0500 Subject: [PATCH 3/4] add test case asserting exception handling behavior for get_active_date --- test_stale_repos.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test_stale_repos.py b/test_stale_repos.py index 266eb01..89bb695 100644 --- a/test_stale_repos.py +++ b/test_stale_repos.py @@ -30,6 +30,7 @@ is_repo_exempt, output_to_json, write_to_markdown, + get_active_date, ) @@ -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. From 5261bba6106e3e8e9990e43a6c36941991ec25b8 Mon Sep 17 00:00:00 2001 From: Zack Koppert Date: Mon, 5 Feb 2024 11:55:39 -0800 Subject: [PATCH 4/4] sort imports Signed-off-by: Zack Koppert --- test_stale_repos.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_stale_repos.py b/test_stale_repos.py index 89bb695..f56ca0e 100644 --- a/test_stale_repos.py +++ b/test_stale_repos.py @@ -26,11 +26,11 @@ import github3.github from stale_repos import ( auth_to_github, + get_active_date, get_inactive_repos, is_repo_exempt, output_to_json, write_to_markdown, - get_active_date, )