Skip to content

Commit

Permalink
Merge pull request #10 from github/no-issues
Browse files Browse the repository at this point in the history
handle and test no issues found case
  • Loading branch information
zkoppert authored Jun 2, 2023
2 parents cf7141f + 71ee4dd commit 74f66de
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 19 deletions.
38 changes: 23 additions & 15 deletions issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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(
Expand Down
43 changes: 39 additions & 4 deletions test_issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 74f66de

Please sign in to comment.