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

chore: add owner and repository names to errors #249

Merged
merged 1 commit into from
Apr 23, 2024
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
35 changes: 21 additions & 14 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 @@ -240,27 +244,28 @@ def get_per_issue_metrics(
return issues_with_metrics, num_issues_open, num_issues_closed


def get_owner(
def get_owner_and_repository(
search_query: str,
) -> Union[str, None]:
"""Get the owner from the search query.
) -> dict:
"""Get the owner and repository from the search query.

Args:
search_query (str): The search query used to search for issues.

Returns:
Union[str, None]: The owner.
dict: A dictionary of owner and repository.

"""
search_query_split = search_query.split(" ")
owner = None
result = {}
for item in search_query_split:
if "repo:" in item and "/" in item:
owner = item.split(":")[1].split("/")[0]
result["owner"] = item.split(":")[1].split("/")[0]
result["repository"] = item.split(":")[1].split("/")[1]
if "org:" in item or "owner:" in item or "user:" in item:
owner = item.split(":")[1]
result["owner"] = item.split(":")[1]

return owner
return result


def main():
Expand Down Expand Up @@ -297,8 +302,10 @@ def main():
max_comments_eval = int(env_vars.max_comments_eval)
heavily_involved_cutoff = int(env_vars.heavily_involved_cutoff)

# Get the repository owner and name from the search query
owner = get_owner(search_query)
# Get the owner and repository from the search query
owner_and_repository = get_owner_and_repository(search_query)
owner = owner_and_repository.get("owner")
repository = owner_and_repository.get("repository")

if owner is None:
raise ValueError(
Expand All @@ -323,7 +330,7 @@ def main():
write_to_markdown(None, None, None, None, None, None, None, None)
return
else:
issues = search_issues(search_query, github_connection)
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
38 changes: 37 additions & 1 deletion test_issue_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
IssueWithMetrics,
auth_to_github,
get_env_vars,
get_owner_and_repository,
get_per_issue_metrics,
measure_time_to_close,
measure_time_to_first_response,
Expand Down Expand Up @@ -54,10 +55,45 @@ 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 TestGetOwnerAndRepository(unittest.TestCase):
"""Unit tests for the get_owner_and_repository function.

This class contains unit tests for the get_owner_and_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_owner_with_owner_and_repo_in_query: Test get both owner and repo.
test_get_owner_and_repository_with_repo_in_query: Test get just owner.
test_get_owner_and_repository_without_either_in_query: Test get neither.

"""

def test_get_owner_with_owner_and_repo_in_query(self):
"""Test get both owner and repo."""
result = get_owner_and_repository("repo:owner1/repo1")
self.assertEqual(result.get("owner"), "owner1")
self.assertEqual(result.get("repository"), "repo1")

def test_get_owner_and_repository_with_repo_in_query(self):
"""Test get just owner."""
result = get_owner_and_repository("org:owner1")
self.assertEqual(result.get("owner"), "owner1")
self.assertIsNone(result.get("repository"))

def test_get_owner_and_repository_without_either_in_query(self):
"""Test get neither."""
result = get_owner_and_repository("is:blah")
self.assertIsNone(result.get("owner"))
self.assertIsNone(result.get("repository"))


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

Expand Down
Loading