Skip to content

Commit

Permalink
Fix error saving JSON output with empty repo enum results. (#42)
Browse files Browse the repository at this point in the history
* Fix error saving JSON output with empty repo enum results.

* Formatting.
  • Loading branch information
AdnaneKhan authored Sep 29, 2024
1 parent 5dd5a9e commit 1a53be3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
7 changes: 4 additions & 3 deletions gatox/enumerate/enumerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def self_enumeration(self):
Returns:
bool: False if the PAT is not valid for enumeration.
(list, list): Tuple containing list of orgs and list of repos.
"""

self.__setup_user_info()
Expand Down Expand Up @@ -330,12 +331,13 @@ def enumerate_repos(self, repo_names: list):
Args:
repo_names (list): Repository name in {Org/Owner}/Repo format.
"""
repo_wrappers = []
if not self.__setup_user_info():
return False
return repo_wrappers

if len(repo_names) == 0:
Output.error("The list of repositories was empty!")
return
return repo_wrappers

Output.info(
f"Querying and caching workflow YAML files "
Expand All @@ -344,7 +346,6 @@ def enumerate_repos(self, repo_names: list):
queries = GqlQueries.get_workflow_ymls_from_list(repo_names)
self.__query_graphql_workflows(queries)

repo_wrappers = []
try:
for repo in repo_names:
repo_obj = self.enumerate_repo_only(repo, len(repo_names) > 100)
Expand Down
6 changes: 4 additions & 2 deletions gatox/models/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ def add_organizations(self, organizations: list[Organization]):
Args:
organizations (List[Organization]): List of org wrappers.
"""
self.organizations = organizations
if organizations:
self.organizations = organizations

def add_repositories(self, repositories: list[Repository]):
"""Add list of organization wrapper objects.
Args:
organizations (List[Organization]): List of org wrappers.
"""
self.repositories = repositories
if repositories:
self.repositories = repositories

def set_user_details(self, user_details):
"""_summary_
Expand Down
15 changes: 15 additions & 0 deletions unit_test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ def test_enum_self(mock_enumerate):
mock_enumerate.assert_called_once()


@mock.patch("gatox.models.execution.Execution.add_repositories")
@mock.patch("gatox.models.execution.Execution.add_organizations")
@mock.patch("gatox.enumerate.enumerate.Enumerator.self_enumeration")
def test_enum_self_json_empty(mock_enumerate, mock_executor_org, mock_executor_repo):
"""Test enum command using the self enumerattion."""

mock_enumerate.return_value = ([], ["repo1", "repo2"])

cli.cli(["enum", "-s", "-oJ", "test.json"])
mock_enumerate.assert_called_once()

mock_executor_org.assert_called_with([])
mock_executor_repo.assert_called_with(["repo1", "repo2"])


@mock.patch("gatox.cli.cli.Enumerator")
def test_enum_org(mock_enumerate):
"""Test enum command using the organization enumerattion."""
Expand Down
25 changes: 25 additions & 0 deletions unit_test/test_enumerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,28 @@ def test_unscoped_token(mock_api, capfd):
out, _ = capfd.readouterr()
assert "Self-enumeration requires the repo scope!" in escape_ansi(out)
assert status is False


@patch("gatox.enumerate.enumerate.Api")
def test_enum_self_no_repos(mock_api, capfd):
gh_enumeration_runner = Enumerator(
"ghp_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
socks_proxy=None,
http_proxy=None,
output_yaml=False,
skip_log=True,
output_json="test.json",
)

mock_api.return_value.is_app_token.return_value = False
mock_api.return_value.check_user.return_value = {
"user": "testUser",
"scopes": ["repo"],
}

orgs, repos = gh_enumeration_runner.self_enumeration()

assert orgs == []
assert repos == []

out, _ = capfd.readouterr()

0 comments on commit 1a53be3

Please sign in to comment.