Skip to content

Commit

Permalink
chore: add owner and repository names to errors
Browse files Browse the repository at this point in the history
Add owner and repository info to error messages

Signed-off-by: jmeridth <[email protected]>
  • Loading branch information
jmeridth committed Apr 23, 2024
1 parent eb98ac5 commit dc78fc2
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 5 deletions.
34 changes: 30 additions & 4 deletions issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


def search_issues(
search_query: str, github_connection: github3.GitHub
search_query: str, github_connection: github3.GitHub, owner: str, repository: str
) -> List[github3.search.IssueSearchResult]: # type: ignore
"""
Searches for issues/prs/discussions in a GitHub repository that match
Expand All @@ -52,6 +52,8 @@ def search_issues(
Args:
search_query (str): The search query to use for finding issues/prs/discussions.
github_connection (github3.GitHub): A connection to the GitHub API.
owner (str): The owner of the repository to search in.
repository (str): The repository to search in.
Returns:
List[github3.search.IssueSearchResult]: A list of issues that match the search query.
Expand All @@ -67,11 +69,13 @@ def search_issues(
issues.append(issue)
except github3.exceptions.ForbiddenError:
print(
"You do not have permission to view this repository; Check you API Token."
f"You do not have permission to view this repository '{repository}'; Check your API Token."
)
sys.exit(1)
except github3.exceptions.NotFoundError:
print("The repository could not be found; Check the repository owner and name.")
print(
f"The repository could not be found; Check the repository owner and name: '{owner}/{repository}"
)
sys.exit(1)
except github3.exceptions.ConnectionError:
print(
Expand Down Expand Up @@ -227,6 +231,27 @@ def get_per_issue_metrics(
return issues_with_metrics, num_issues_open, num_issues_closed


def get_repository(
search_query: str,
) -> Union[str, None]:
"""Get the repository from the search query.
Args:
search_query (str): The search query used to search for issues.
Returns:
Union[str, None]: The repository.
"""
search_query_split = search_query.split(" ")
repository = None
for item in search_query_split:
if "repo:" in item and "/" in item:
repository = item.split(":")[1].split("/")[1]

return repository


def get_owner(
search_query: str,
) -> Union[str, None]:
Expand Down Expand Up @@ -310,7 +335,8 @@ def main():
write_to_markdown(None, None, None, None, None, None, None, None)
return
else:
issues = search_issues(search_query, github_connection)
repository = get_repository(search_query)
issues = search_issues(search_query, github_connection, owner, repository)
if len(issues) <= 0:
print("No issues found")
write_to_markdown(None, None, None, None, None, None, None, None)
Expand Down
72 changes: 71 additions & 1 deletion test_issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
IssueWithMetrics,
auth_to_github,
get_env_vars,
get_owner,
get_per_issue_metrics,
get_repository,
measure_time_to_close,
measure_time_to_first_response,
search_issues,
Expand Down Expand Up @@ -54,10 +56,78 @@ def test_search_issues(self):
mock_connection.search_issues.return_value = mock_issues

# Call search_issues and check that it returns the correct issues
issues = search_issues("is:open", mock_connection)
issues = search_issues(
"is:open", mock_connection, "fakeowner", "fakerepository"
)
self.assertEqual(issues, mock_issues)


class TestGetOwner(unittest.TestCase):
"""Unit tests for the get_owner function.
This class contains unit tests for the get_owner function in the
issue_metrics module. The tests use the unittest module and the unittest.mock
module to mock the GitHub API and test the function in isolation.
Methods:
test_get_owner_with_org_in_query: Test that get_owner returns the owner.
test_get_owner_with_repo_in_query: Test that get_owner returns the owner.
test_get_owner_without_owner_present: Test that get_owner returns None.
"""

def test_get_owner_with_org_in_query(self):
"""Test that get_owner returns the correct owner."""
# Call search_issues and check that it returns the correct issues
owner = get_owner("org:owner1")
self.assertEqual(owner, "owner1")

def test_get_owner_with_repo_in_query(self):
"""Test that get_owner returns the correct owner."""
# Call search_issues and check that it returns the correct issues
owner = get_owner("repo:owner1/repo1")
self.assertEqual(owner, "owner1")

def test_get_owner_without_owner_present(self):
"""Test that get_owner returns None."""
# Call search_issues and check that it returns the correct issues
owner = get_owner("is:open")
self.assertIsNone(owner)


class TestGetRepository(unittest.TestCase):
"""Unit tests for the get_repository function.
This class contains unit tests for the get_repository function in the
issue_metrics module. The tests use the unittest module and the unittest.mock
module to mock the GitHub API and test the function in isolation.
Methods:
test_get_repository_with_org_in_query: Test that get_repository returns None.
test_get_repository_with_repo_in_query: Test that get_repository returns the repository.
test_get_repository_without_repository_present: Test that get_repository returns None.
"""

def test_get_repository_with_org_in_query(self):
"""Test that get_repository returns None."""
# Call search_issues and check that it returns the correct issues
repository = get_repository("org:owner1")
self.assertIsNone(repository)

def test_get_repository_with_repo_in_query(self):
"""Test that get_repository returns the correct owner."""
# Call search_issues and check that it returns the correct issues
repository = get_repository("repo:owner1/repo1")
self.assertEqual(repository, "repo1")

def test_get_repository_without_repository_present(self):
"""Test that get_repository returns None."""
# Call search_issues and check that it returns the correct issues
repository = get_repository("is:open")
self.assertIsNone(repository)


class TestAuthToGithub(unittest.TestCase):
"""Test the auth_to_github function."""

Expand Down

0 comments on commit dc78fc2

Please sign in to comment.