diff --git a/prowler/lib/check/models.py b/prowler/lib/check/models.py index 5e240f74cc..0ef3195b2a 100644 --- a/prowler/lib/check/models.py +++ b/prowler/lib/check/models.py @@ -556,7 +556,7 @@ class CheckReportGithub(Check_Report): resource: Any, resource_name: str = None, resource_id: str = None, - repository: str = None, + repository: str = "global", ) -> None: """Initialize the GitHub Check's finding information. diff --git a/prowler/providers/github/services/organization/__init__.py b/prowler/providers/github/services/organization/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/prowler/providers/github/services/organization/organization_client.py b/prowler/providers/github/services/organization/organization_client.py new file mode 100644 index 0000000000..8c05c87178 --- /dev/null +++ b/prowler/providers/github/services/organization/organization_client.py @@ -0,0 +1,6 @@ +from prowler.providers.common.provider import Provider +from prowler.providers.github.services.organization.organization_service import ( + Organization, +) + +organization_client = Organization(Provider.get_global_provider()) diff --git a/prowler/providers/github/services/organization/organization_service.py b/prowler/providers/github/services/organization/organization_service.py new file mode 100644 index 0000000000..64c2e7c0d8 --- /dev/null +++ b/prowler/providers/github/services/organization/organization_service.py @@ -0,0 +1,33 @@ +from pydantic import BaseModel + +from prowler.lib.logger import logger +from prowler.providers.github.lib.service.service import GithubService + + +class Organization(GithubService): + def __init__(self, provider): + super().__init__(__class__.__name__, provider) + self.organizations = self._list_organizations() + + def _list_organizations(self): + logger.info("Organization - Listing Organizations...") + organizations = {} + try: + for client in self.clients: + for org in client.get_user().get_orgs(): + organizations[org.id] = Org( + id=org.id, + name=org.login, + ) + except Exception as error: + logger.error( + f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}" + ) + return organizations + + +class Org(BaseModel): + """Model for Github Organization""" + + id: int + name: str diff --git a/tests/providers/github/services/organization/organization_service_test.py b/tests/providers/github/services/organization/organization_service_test.py new file mode 100644 index 0000000000..02dabf80ff --- /dev/null +++ b/tests/providers/github/services/organization/organization_service_test.py @@ -0,0 +1,35 @@ +from unittest.mock import patch + +from prowler.providers.github.services.organization.organization_service import ( + Org, + Organization, +) +from tests.providers.github.github_fixtures import set_mocked_github_provider + + +def mock_list_organizations(_): + return { + 1: Org( + id=1, + name="test-organization", + ), + } + + +@patch( + "prowler.providers.github.services.organization.organization_service.Organization._list_organizations", + new=mock_list_organizations, +) +class Test_Repository_Service: + def test_get_client(self): + repository_service = Organization(set_mocked_github_provider()) + assert repository_service.clients[0].__class__.__name__ == "Github" + + def test_get_service(self): + repository_service = Organization(set_mocked_github_provider()) + assert repository_service.__class__.__name__ == "Organization" + + def test_list_organizations(self): + repository_service = Organization(set_mocked_github_provider()) + assert len(repository_service.organizations) == 1 + assert repository_service.organizations[1].name == "test-organization"