From a74d37c68d79815a93cda1cc66566c7d212bcf4d Mon Sep 17 00:00:00 2001 From: jmeridth Date: Mon, 22 Apr 2024 22:56:19 -0500 Subject: [PATCH] chore: add owner and repository names to errors Add owner and repository info to error messages Signed-off-by: jmeridth --- issue_metrics.py | 34 +++++++++++++++++--- test_issue_metrics.py | 72 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/issue_metrics.py b/issue_metrics.py index 15f58ee..161deac 100644 --- a/issue_metrics.py +++ b/issue_metrics.py @@ -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 @@ -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. @@ -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( @@ -240,6 +244,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]: @@ -323,7 +348,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) diff --git a/test_issue_metrics.py b/test_issue_metrics.py index 582dc2e..ef24d98 100644 --- a/test_issue_metrics.py +++ b/test_issue_metrics.py @@ -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, @@ -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."""