diff --git a/issue_metrics.py b/issue_metrics.py index 8f5a4d2..a94a88f 100644 --- a/issue_metrics.py +++ b/issue_metrics.py @@ -151,28 +151,32 @@ def write_to_markdown(issues_with_metrics, average_time_to_first_response, file= Args: issues_with_metrics (list of tuple): A list of tuples containing a GitHub issue - and its time to first response. + and its time to first response. average_time_to_first_response (datetime.timedelta): The average time to first - response for the issues. + response for the issues. file (file object, optional): The file object to write to. If not provided, - a file named "issue_metrics.md" will be created. + a file named "issue_metrics.md" will be created. Returns: None. """ - issues_with_metrics.sort(key=lambda x: x[1], reverse=True) - with file or open("issue_metrics.md", "w", encoding="utf-8") as file: - file.write("# Issue Metrics\n\n") - file.write( - f"Average time to first response: {average_time_to_first_response}\n" - ) - file.write(f"Number of issues: {len(issues_with_metrics)}\n\n") - file.write("| Title | URL | TTFR |\n") - file.write("| --- | --- | ---: |\n") - for title, url, ttfr in issues_with_metrics: - file.write(f"| {title} | {url} | {ttfr} |\n") - print("Wrote issue metrics to issue_metrics.md") + if not issues_with_metrics and not average_time_to_first_response: + with file or open("issue_metrics.md", "w", encoding="utf-8") as file: + file.write("no issues found for the given search criteria\n\n") + else: + issues_with_metrics.sort(key=lambda x: x[1], reverse=True) + with file or open("issue_metrics.md", "w", encoding="utf-8") as file: + file.write("# Issue Metrics\n\n") + file.write( + f"Average time to first response: {average_time_to_first_response}\n" + ) + file.write(f"Number of issues: {len(issues_with_metrics)}\n\n") + file.write("| Title | URL | TTFR |\n") + file.write("| --- | --- | ---: |\n") + for title, url, ttfr in issues_with_metrics: + file.write(f"| {title} | {url} | {ttfr} |\n") + print("Wrote issue metrics to issue_metrics.md") def main(): @@ -208,7 +212,11 @@ def main(): # Search for issues issues = search_issues(repo_url, issue_search_query, github_connection) + if len(issues.items) <= 0: + print("No issues found") + write_to_markdown(None, None) + return # Find the time to first response issues_with_ttfr = measure_time_to_first_response(issues) average_time_to_first_response = get_average_time_to_first_response( diff --git a/test_issue_metrics.py b/test_issue_metrics.py index ade529f..eae5dee 100644 --- a/test_issue_metrics.py +++ b/test_issue_metrics.py @@ -221,6 +221,7 @@ class TestMain(unittest.TestCase): Methods: test_main: Test that main runs without errors. + test_main_no_issues_found: Test that main handles when no issues are found """ @@ -248,10 +249,13 @@ def test_main( mock_auth_to_github.return_value = mock_connection # Set up the mock search_issues function - mock_issues = [ - MagicMock(title="Issue 1"), - MagicMock(title="Issue 2"), - ] + mock_issues = MagicMock( + items=[ + MagicMock(title="Issue 1"), + MagicMock(title="Issue 2"), + ] + ) + mock_search_issues.return_value = mock_issues # Set up the mock measure_time_to_first_response function @@ -281,6 +285,37 @@ def test_main( # Remove the markdown file created by main os.remove("issue_metrics.md") + @patch("issue_metrics.auth_to_github") + @patch("issue_metrics.search_issues") + @patch("issue_metrics.write_to_markdown") + @patch.dict( + os.environ, + { + "ISSUE_SEARCH_QUERY": "is:open", + "REPOSITORY_URL": "https://github.com/user/repo", + }, + ) + def test_main_no_issues_found( + self, + mock_write_to_markdown, + mock_search_issues, + mock_auth_to_github, + ): + """Test that main writes 'No issues found' to the + console and calls write_to_markdown with None.""" + + # Set up the mock GitHub connection object + mock_connection = MagicMock() + mock_auth_to_github.return_value = mock_connection + + # Set up the mock search_issues function to return an empty list of issues + mock_issues = MagicMock(items=[]) + mock_search_issues.return_value = mock_issues + + # Call main and check that it writes 'No issues found' + issue_metrics.main() + mock_write_to_markdown.assert_called_once_with(None, None) + if __name__ == "__main__": unittest.main()