Skip to content

Commit 332f66a

Browse files
committed
fix: issue accessing repo when not provided in query
Fixes #290 - [x] handle trying to access repository from dictionary when not present (use default of empty string) - [x] add test to ensure we cover this scenario - [x] handle issue where `time_to_close` variable was being access before possibly being declared Signed-off-by: jmeridth <jmeridth@gmail.com>
1 parent fbe4e7c commit 332f66a

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

issue_metrics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ def wait_for_api_refresh(iterator: github3.structs.SearchIterator):
9595
issues = []
9696
repos_and_owners_string = ""
9797
for item in owners_and_repositories:
98-
repos_and_owners_string += f"{item['owner']}/{item['repository']} "
98+
repos_and_owners_string += (
99+
f"{item.get('owner', '')}/{item.get('repository','')} "
100+
)
99101

100102
# Print the issue titles
101103
try:

test_issue_metrics.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ class TestSearchIssues(unittest.TestCase):
3636
module to mock the GitHub API and test the function in isolation.
3737
3838
Methods:
39-
test_search_issues: Test that search_issues returns the correct issues.
39+
test_search_issues_with_owner_and_repository: Test that search_issues with owner/repo returns the correct issues.
40+
test_search_issues_with_just_owner_or_org: Test that search_issues with just an owner/org returns the correct issues.
4041
4142
"""
4243

43-
def test_search_issues(self):
44-
"""Test that search_issues returns the correct issues."""
44+
def test_search_issues_with_owner_and_repository(self):
45+
"""Test that search_issues with owner/repo returns the correct issues."""
4546

4647
# Set up the mock GitHub connection object
4748
mock_issues = [
@@ -63,6 +64,30 @@ def test_search_issues(self):
6364
issues = search_issues("is:open", mock_connection, owners_and_repositories)
6465
self.assertEqual(issues, mock_issues)
6566

67+
def test_search_issues_with_just_owner_or_org(self):
68+
"""Test that search_issues with just an owner/org returns the correct issues."""
69+
70+
# Set up the mock GitHub connection object
71+
mock_issues = [
72+
MagicMock(title="Issue 1"),
73+
MagicMock(title="Issue 2"),
74+
MagicMock(title="Issue 3"),
75+
]
76+
77+
# simulating github3.structs.SearchIterator return value
78+
mock_search_result = MagicMock()
79+
mock_search_result.__iter__.return_value = iter(mock_issues)
80+
mock_search_result.ratelimit_remaining = 30
81+
82+
mock_connection = MagicMock()
83+
mock_connection.search_issues.return_value = mock_search_result
84+
85+
# Call search_issues and check that it returns the correct issues
86+
org = {"owner": "org1"}
87+
owners = [org]
88+
issues = search_issues("is:open", mock_connection, owners)
89+
self.assertEqual(issues, mock_issues)
90+
6691

6792
class TestGetOwnerAndRepository(unittest.TestCase):
6893
"""Unit tests for the get_owners_and_repositories function.
@@ -84,7 +109,7 @@ def test_get_owners_with_owner_and_repo_in_query(self):
84109
self.assertEqual(result[0].get("owner"), "owner1")
85110
self.assertEqual(result[0].get("repository"), "repo1")
86111

87-
def test_get_owner_and_repositories_with_repo_in_query(self):
112+
def test_get_owner_and_repositories_without_repo_in_query(self):
88113
"""Test get just owner."""
89114
result = get_owners_and_repositories("org:owner1")
90115
self.assertEqual(result[0].get("owner"), "owner1")

time_to_close.py

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def get_stats_time_to_close(
7575

7676
# Calculate the total time to close for all issues
7777
close_times = []
78+
total_time_to_close = None
7879
if issues_with_time_to_close:
7980
total_time_to_close = 0
8081
for issue in issues_with_time_to_close:

0 commit comments

Comments
 (0)