Skip to content

Commit 95d854c

Browse files
authored
chore: add owner and repository names to errors (#249)
Add owner and repository info to error messages Signed-off-by: jmeridth <jmeridth@gmail.com>
1 parent e7731ae commit 95d854c

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

issue_metrics.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444

4545
def search_issues(
46-
search_query: str, github_connection: github3.GitHub
46+
search_query: str, github_connection: github3.GitHub, owner: str, repository: str
4747
) -> List[github3.search.IssueSearchResult]: # type: ignore
4848
"""
4949
Searches for issues/prs/discussions in a GitHub repository that match
@@ -52,6 +52,8 @@ def search_issues(
5252
Args:
5353
search_query (str): The search query to use for finding issues/prs/discussions.
5454
github_connection (github3.GitHub): A connection to the GitHub API.
55+
owner (str): The owner of the repository to search in.
56+
repository (str): The repository to search in.
5557
5658
Returns:
5759
List[github3.search.IssueSearchResult]: A list of issues that match the search query.
@@ -67,11 +69,13 @@ def search_issues(
6769
issues.append(issue)
6870
except github3.exceptions.ForbiddenError:
6971
print(
70-
"You do not have permission to view this repository; Check you API Token."
72+
f"You do not have permission to view this repository '{repository}'; Check your API Token."
7173
)
7274
sys.exit(1)
7375
except github3.exceptions.NotFoundError:
74-
print("The repository could not be found; Check the repository owner and name.")
76+
print(
77+
f"The repository could not be found; Check the repository owner and name: '{owner}/{repository}"
78+
)
7579
sys.exit(1)
7680
except github3.exceptions.ConnectionError:
7781
print(
@@ -240,27 +244,28 @@ def get_per_issue_metrics(
240244
return issues_with_metrics, num_issues_open, num_issues_closed
241245

242246

243-
def get_owner(
247+
def get_owner_and_repository(
244248
search_query: str,
245-
) -> Union[str, None]:
246-
"""Get the owner from the search query.
249+
) -> dict:
250+
"""Get the owner and repository from the search query.
247251
248252
Args:
249253
search_query (str): The search query used to search for issues.
250254
251255
Returns:
252-
Union[str, None]: The owner.
256+
dict: A dictionary of owner and repository.
253257
254258
"""
255259
search_query_split = search_query.split(" ")
256-
owner = None
260+
result = {}
257261
for item in search_query_split:
258262
if "repo:" in item and "/" in item:
259-
owner = item.split(":")[1].split("/")[0]
263+
result["owner"] = item.split(":")[1].split("/")[0]
264+
result["repository"] = item.split(":")[1].split("/")[1]
260265
if "org:" in item or "owner:" in item or "user:" in item:
261-
owner = item.split(":")[1]
266+
result["owner"] = item.split(":")[1]
262267

263-
return owner
268+
return result
264269

265270

266271
def main():
@@ -297,8 +302,10 @@ def main():
297302
max_comments_eval = int(env_vars.max_comments_eval)
298303
heavily_involved_cutoff = int(env_vars.heavily_involved_cutoff)
299304

300-
# Get the repository owner and name from the search query
301-
owner = get_owner(search_query)
305+
# Get the owner and repository from the search query
306+
owner_and_repository = get_owner_and_repository(search_query)
307+
owner = owner_and_repository.get("owner")
308+
repository = owner_and_repository.get("repository")
302309

303310
if owner is None:
304311
raise ValueError(
@@ -323,7 +330,7 @@ def main():
323330
write_to_markdown(None, None, None, None, None, None, None, None)
324331
return
325332
else:
326-
issues = search_issues(search_query, github_connection)
333+
issues = search_issues(search_query, github_connection, owner, repository)
327334
if len(issues) <= 0:
328335
print("No issues found")
329336
write_to_markdown(None, None, None, None, None, None, None, None)

test_issue_metrics.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
IssueWithMetrics,
2525
auth_to_github,
2626
get_env_vars,
27+
get_owner_and_repository,
2728
get_per_issue_metrics,
2829
measure_time_to_close,
2930
measure_time_to_first_response,
@@ -54,10 +55,45 @@ def test_search_issues(self):
5455
mock_connection.search_issues.return_value = mock_issues
5556

5657
# Call search_issues and check that it returns the correct issues
57-
issues = search_issues("is:open", mock_connection)
58+
issues = search_issues(
59+
"is:open", mock_connection, "fakeowner", "fakerepository"
60+
)
5861
self.assertEqual(issues, mock_issues)
5962

6063

64+
class TestGetOwnerAndRepository(unittest.TestCase):
65+
"""Unit tests for the get_owner_and_repository function.
66+
67+
This class contains unit tests for the get_owner_and_repository function in the
68+
issue_metrics module. The tests use the unittest module and the unittest.mock
69+
module to mock the GitHub API and test the function in isolation.
70+
71+
Methods:
72+
test_get_owner_with_owner_and_repo_in_query: Test get both owner and repo.
73+
test_get_owner_and_repository_with_repo_in_query: Test get just owner.
74+
test_get_owner_and_repository_without_either_in_query: Test get neither.
75+
76+
"""
77+
78+
def test_get_owner_with_owner_and_repo_in_query(self):
79+
"""Test get both owner and repo."""
80+
result = get_owner_and_repository("repo:owner1/repo1")
81+
self.assertEqual(result.get("owner"), "owner1")
82+
self.assertEqual(result.get("repository"), "repo1")
83+
84+
def test_get_owner_and_repository_with_repo_in_query(self):
85+
"""Test get just owner."""
86+
result = get_owner_and_repository("org:owner1")
87+
self.assertEqual(result.get("owner"), "owner1")
88+
self.assertIsNone(result.get("repository"))
89+
90+
def test_get_owner_and_repository_without_either_in_query(self):
91+
"""Test get neither."""
92+
result = get_owner_and_repository("is:blah")
93+
self.assertIsNone(result.get("owner"))
94+
self.assertIsNone(result.get("repository"))
95+
96+
6197
class TestAuthToGithub(unittest.TestCase):
6298
"""Test the auth_to_github function."""
6399

0 commit comments

Comments
 (0)