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

handle and test no issues found case #10

Merged
merged 1 commit into from
Jun 2, 2023
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
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()