feat(github): add new service Organization (#6300)

Co-authored-by: MrCloudSec <hello@mistercloudsec.com>
This commit is contained in:
Hugo Pereira Brito
2025-05-14 10:40:26 +02:00
committed by GitHub
parent 9ecf570790
commit 484a773f5b
5 changed files with 75 additions and 1 deletions
+1 -1
View File
@@ -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.
@@ -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())
@@ -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
@@ -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"