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

Add implementation for Members, Sources and Invitations endpoints #131

Merged
merged 20 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ddd3401
feat(client): add members related endpoints
GG-Yanne Dec 5, 2024
d322eaa
feat(client): add teams related endpoints
GG-Yanne Dec 5, 2024
8e9748d
feat(client): add sources related endpoints
GG-Yanne Dec 6, 2024
8555586
feat(client): add invitation related endpoints
GG-Yanne Dec 6, 2024
a932163
docs(changelog): added changelog for teams, members, invitations and …
GG-Yanne Dec 6, 2024
b5f7f6b
fix(models): changed wrong keyword argument for parameters schema and…
GG-Yanne Dec 6, 2024
009bcad
refactor(client): delete methods now return None in case deletion was…
GG-Yanne Dec 11, 2024
f59f4a1
refactor(models): remove many arguments in from_dict and move list se…
GG-Yanne Dec 11, 2024
a672070
fix(models): mark last_scan as optional for a scan object
GG-Yanne Dec 11, 2024
27bc067
test(client): changed error message to detail
GG-Yanne Dec 11, 2024
3e17f92
refactor(models): move schema, parameters and paginated response to m…
GG-Yanne Dec 11, 2024
463062c
test(client): change client tests to allow tests to run on any workspace
GG-Yanne Dec 13, 2024
90f3f02
refactor(client): rename list team source to better match public api doc
GG-Yanne Dec 13, 2024
4a06722
refactor(models): prefix all post_load method with make
GG-Yanne Dec 18, 2024
994f987
refactor(client): rename "update" function model's argument to payload
GG-Yanne Dec 18, 2024
1ff53dc
docs(tests): add documentation around limitations of initial workplac…
GG-Yanne Dec 18, 2024
20b1624
test(client): move get functions to utils file
GG-Yanne Dec 18, 2024
b87bc42
fix(client): explicit add status code for all response
GG-Yanne Dec 19, 2024
3efc288
fix(models_utils): paginated data now also receives status code when …
GG-Yanne Dec 19, 2024
5133106
refactor(client): change query_parameters to parameters for list_members
GG-Yanne Dec 20, 2024
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
65 changes: 65 additions & 0 deletions pygitguardian/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@
SecretIncident,
SecretScanPreferences,
ServerMetadata,
Source,
SourceParameters,
Team,
TeamInvitation,
TeamInvitationParameter,
TeamMember,
TeamMemberParameter,
TeamSourceParameters,
TeamsParameter,
UpdateMember,
UpdateTeam,
UpdateTeamSource,
)
from .sca_models import (
ComputeSCAFilesResult,
Expand Down Expand Up @@ -1213,3 +1217,64 @@ def delete_team_member(
return response.status_code

return load_detail(response)

def list_sources(
self,
parameters: Optional[SourceParameters] = None,
extra_headers: Optional[Dict[str, str]] = None,
) -> Union[Detail, CursorPaginatedResponse[Source]]:
response = self.get(
endpoint="sources",
data=parameters.to_dict() if parameters else {},
extra_headers=extra_headers,
)

obj: Union[Detail, CursorPaginatedResponse[Source]]
if is_ok(response):
obj = CursorPaginatedResponse[Source].from_response(response, Source)
else:
obj = load_detail(response)

obj.status_code
return obj

def list_teams_sources(
self,
team_id: int,
parameters: Optional[TeamSourceParameters] = None,
extra_headers: Optional[Dict[str, str]] = None,
) -> Union[Detail, CursorPaginatedResponse[Source]]:
response = self.get(
endpoint=f"teams/{team_id}/sources",
data=parameters.to_dict() if parameters else {},
extra_headers=extra_headers,
)

obj: Union[Detail, CursorPaginatedResponse[Source]]
if is_ok(response):
obj = CursorPaginatedResponse[Source].from_response(response, Source)
else:
obj = load_detail(response)

obj.status_code
return obj

def update_team_source(
self,
team_sources: UpdateTeamSource,
extra_headers: Optional[Dict[str, str]] = None,
) -> Union[Detail, int]:
team_id = team_sources.team_id
data = team_sources.to_dict()
del data["team_id"]

response = self.post(
endpoint=f"teams/{team_id}/sources",
data=data,
extra_headers=extra_headers,
)

if response.status_code == 204:
return 204

return load_detail(response)
2 changes: 1 addition & 1 deletion pygitguardian/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DEFAULT_BASE_URI = "http://localhost:3000/exposed"
DEFAULT_BASE_URI = "https://api.gitguardian.com"
DEFAULT_API_VERSION = "v1"
DEFAULT_TIMEOUT = 20.0 # 20s default timeout

Expand Down
112 changes: 101 additions & 11 deletions pygitguardian/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,25 +982,70 @@ class Feedback(Base, FromDictMixin):
answers: List[Answer]


@dataclass
class SecretIncidentStats(Base, FromDictMixin):
total: int
severity_breakdown: dict[Severity, int]


@dataclass
class SecretIncidentsBreakdown(Base, FromDictMixin):
open_secret_incidents: SecretIncidentStats
closed_secret_incidents: SecretIncidentStats


ScanStatus = Literal[
"pending",
"running",
"canceled",
"failed",
"too_large",
"timeout",
"pending_timeout",
"finished",
]


@dataclass
class Scan(Base, FromDictMixin):
date: datetime
status: ScanStatus
failing_reason: str
commits_scanned: int
branches_scanned: int
duration: str


SourceHealth = Literal["safe", "unknown", "at_risk"]
SourceCriticality = Literal["critical", "high", "medium", "low", "unknown"]


@dataclass
class Source(Base, FromDictMixin):
id: int
url: str
type: str
full_name: str
health: Literal["safe", "unknown", "at_risk"]
health: SourceHealth
default_branch: Optional[str]
default_branch_head: Optional[str]
open_incidents_count: int
closed_incidents_count: int
secret_incidents_breakdown: Dict[str, Any] # TODO: add SecretIncidentsBreakdown
secret_incidents_breakdown: SecretIncidentsBreakdown
visibility: Visibility
external_id: str
source_criticality: str
last_scan: Optional[Dict[str, Any]] # TODO: add LastScan
source_criticality: SourceCriticality
last_scan: Scan
monitored: bool


SourceSchema = cast(
Type[BaseSchema],
marshmallow_dataclass.class_schema(Source, base_schema=BaseSchema),
)
Source.SCHEMA = SourceSchema()


@dataclass
class OccurrenceMatch(Base, FromDictMixin):
"""
Expand Down Expand Up @@ -1103,14 +1148,14 @@ class AccessLevel(str, Enum):
RESTRICTED = "restricted"


class PaginationParameter(Base, FromDictMixin):
class PaginationParameter(ToDictMixin):
"""Pagination mixin used for endpoints that support pagination."""

cursor: str = ""
per_page: int = 20


class SearchParameter(Base, FromDictMixin):
class SearchParameter(ToDictMixin):
search: Optional[str] = None


Expand Down Expand Up @@ -1142,7 +1187,7 @@ def from_response(


@dataclass
class MembersParameters(PaginationParameter, SearchParameter, Base, FromDictMixin):
class MembersParameters(PaginationParameter, SearchParameter, ToDictMixin):
"""
Members query parameters
"""
Expand Down Expand Up @@ -1239,7 +1284,7 @@ class DeleteMember(Base, FromDictMixin):
DeleteMember.SCHEMA = DeleteMemberSchema()


class TeamsParameter(PaginationParameter, SearchParameter, Base, FromDictMixin):
class TeamsParameter(PaginationParameter, SearchParameter, ToDictMixin):
is_global: Optional[bool] = None


Expand Down Expand Up @@ -1311,7 +1356,7 @@ class IncidentPermission(str, Enum):


@dataclass
class TeamInvitationParameter(PaginationParameter, Base, FromDictMixin):
class TeamInvitationParameter(PaginationParameter, ToDictMixin):
invitation_id: Optional[int] = None
is_team_leader: Optional[bool] = None
incident_permission: Optional[IncidentPermission] = None
Expand Down Expand Up @@ -1387,7 +1432,7 @@ class Meta:
CreateTeamInvitation.SCHEMA = CreateTeamInvitationSchema()


class TeamMemberParameter(PaginationParameter, SearchParameter, Base, FromDictMixin):
class TeamMemberParameter(PaginationParameter, SearchParameter, ToDictMixin):
is_team_leader: Optional[bool] = None
incident_permission: Optional[IncidentPermission] = None
member_id: Optional[int] = None
Expand Down Expand Up @@ -1440,7 +1485,7 @@ def return_team_membership(self, data: dict[str, Any], **kwargs: dict[str, Any])


@dataclass
class CreateTeamMemberParameter(Base, FromDictMixin):
class CreateTeamMemberParameter(ToDictMixin):
send_email: bool


Expand Down Expand Up @@ -1475,3 +1520,48 @@ def return_create_team_membership(


CreateTeamMember.SCHEMA = CreateTeamMemberSchema()


@dataclass
class TeamSourceParameters(PaginationParameter, SearchParameter, ToDictMixin):
last_scan_status: Optional[ScanStatus] = None
type: Optional[str] = None
health: Optional[SourceHealth] = None
type: Optional[str] = None
ordering: Optional[Literal["last_scan_date", "-last_scan_date"]] = None
visibility: Optional[Visibility] = None
external_id: Optional[str] = None


TeamSourceParametersSchema = cast(
Type[BaseSchema],
marshmallow_dataclass.class_schema(TeamSourceParameters, base_schema=BaseSchema),
)
TeamSourceParameters.SCHEMA = TeamSourceParametersSchema()


@dataclass
class UpdateTeamSource(Base, FromDictMixin):
team_id: int
sources_to_add: list[int]
sources_to_remove: list[int]


UpdateTeamSourceSchema = cast(
Type[BaseSchema],
marshmallow_dataclass.class_schema(UpdateTeamSource, base_schema=BaseSchema),
)
UpdateTeamSource.SCHEMA = UpdateTeamSourceSchema()


@dataclass
class SourceParameters(TeamSourceParameters):
source_criticality: Optional[SourceCriticality] = None
monitored: Optional[bool] = None


SourceParametersSchema = cast(
Type[BaseSchema],
marshmallow_dataclass.class_schema(SourceParameters, base_schema=BaseSchema),
)
SourceParameters.SCHEMA = SourceParametersSchema()
122 changes: 122 additions & 0 deletions tests/cassettes/test_add_team_sources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
interactions:
- request:
body: '{"sources_to_add": [126], "sources_to_remove": []}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '50'
Content-Type:
- application/json
User-Agent:
- pygitguardian/1.18.0 (Darwin;py3.11.8)
method: POST
uri: https://api.gitguardian.com/v1/teams/9/sources
response:
body:
string: ''
headers:
Access-Control-Expose-Headers:
- X-App-Version
Allow:
- GET, POST, HEAD, OPTIONS
Connection:
- keep-alive
Content-Length:
- '0'
Cross-Origin-Opener-Policy:
- same-origin
Date:
- Fri, 06 Dec 2024 12:46:14 GMT
Referrer-Policy:
- same-origin
Server:
- nginx/1.24.0
Vary:
- Cookie
X-App-Version:
- dev
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-ID:
- 05e58838d26c922add8312a362dba69c
X-SCA-Engine-Version:
- 2.2.0
X-Secrets-Engine-Version:
- 2.127.0
status:
code: 204
message: No Content
- request:
body: type=azure_devops
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
Content-Length:
- '17'
Content-Type:
- application/x-www-form-urlencoded
User-Agent:
- pygitguardian/1.18.0 (Darwin;py3.11.8)
method: GET
uri: https://api.gitguardian.com/v1/teams/9/sources
response:
body:
string: '[{"id":124,"type":"azure_devops","full_name":"gg-integration-test /
gg-test / default_branch","health":"at_risk","source_criticality":"unknown","default_branch":"test","default_branch_head":null,"open_incidents_count":19,"closed_incidents_count":0,"last_scan":{"date":"2024-12-06T12:27:52.346395Z","status":"finished","failing_reason":"","commits_scanned":23,"duration":"0.437131","branches_scanned":1,"progress":100},"monitored":true,"visibility":"private","external_id":"05b69081-f346-4022-8784-198f50aed182","secret_incidents_breakdown":{"open_secret_incidents":{"total":19,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":19}},"closed_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}}},"url":"https://dev.azure.com/gg-integration-test/gg-test/_git/default_branch"},{"id":125,"type":"azure_devops","full_name":"gg-integration-test
/ gg-test / gg-test","health":"at_risk","source_criticality":"unknown","default_branch":"main","default_branch_head":null,"open_incidents_count":19,"closed_incidents_count":0,"last_scan":{"date":"2024-12-06T12:27:52.346418Z","status":"finished","failing_reason":"","commits_scanned":43,"duration":"0.492489","branches_scanned":1,"progress":100},"monitored":true,"visibility":"private","external_id":"7655868e-bb15-47ab-bd62-abd97212e5e8","secret_incidents_breakdown":{"open_secret_incidents":{"total":19,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":19}},"closed_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}}},"url":"https://dev.azure.com/gg-integration-test/gg-test/_git/gg-test"},{"id":126,"type":"azure_devops","full_name":"gg-integration-test
/ gg-test / huge_repo","health":"safe","source_criticality":"unknown","default_branch":"master","default_branch_head":null,"open_incidents_count":0,"closed_incidents_count":0,"last_scan":{"date":"2024-12-06T12:27:52.346442Z","status":"finished","failing_reason":"","commits_scanned":1007,"duration":"1.102781","branches_scanned":1,"progress":100},"monitored":true,"visibility":"private","external_id":"455c4e0c-6dc3-48ce-a0e7-819d9a8d7523","secret_incidents_breakdown":{"open_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}},"closed_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}}},"url":"https://dev.azure.com/gg-integration-test/gg-test/_git/huge_repo"},{"id":127,"type":"azure_devops","full_name":"gg-integration-test
/ gg-test / new_repo","health":"safe","source_criticality":"unknown","default_branch":"master","default_branch_head":null,"open_incidents_count":0,"closed_incidents_count":0,"last_scan":{"date":"2024-12-06T12:27:52.346464Z","status":"finished","failing_reason":"","commits_scanned":1,"duration":"0.216094","branches_scanned":1,"progress":100},"monitored":true,"visibility":"private","external_id":"f9b583fb-dcfd-46ec-8938-44b427d3e596","secret_incidents_breakdown":{"open_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}},"closed_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}}},"url":"https://dev.azure.com/gg-integration-test/gg-test/_git/new_repo"},{"id":128,"type":"azure_devops","full_name":"gg-integration-test
/ gg-test / t e s t i n g","health":"at_risk","source_criticality":"unknown","default_branch":"main","default_branch_head":null,"open_incidents_count":110,"closed_incidents_count":0,"last_scan":{"date":"2024-12-06T12:27:52.346299Z","status":"finished","failing_reason":"","commits_scanned":113,"duration":"7.597231","branches_scanned":2,"progress":100},"monitored":true,"visibility":"private","external_id":"8a132329-0c77-4efc-a18a-882bda6ab28b","secret_incidents_breakdown":{"open_secret_incidents":{"total":110,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":110}},"closed_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}}},"url":"https://dev.azure.com/gg-integration-test/gg-test/_git/t%20e%20s%20t%20i%20n%20g"},{"id":129,"type":"azure_devops","full_name":"gg-integration-test
/ gg-test / test abc","health":"at_risk","source_criticality":"unknown","default_branch":"main","default_branch_head":null,"open_incidents_count":28,"closed_incidents_count":0,"last_scan":{"date":"2024-12-06T12:27:52.346367Z","status":"finished","failing_reason":"","commits_scanned":55,"duration":"0.623082","branches_scanned":1,"progress":100},"monitored":true,"visibility":"private","external_id":"2aa16b64-2fb2-4639-afeb-70c0c4e8a267","secret_incidents_breakdown":{"open_secret_incidents":{"total":28,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":28}},"closed_secret_incidents":{"total":0,"severity_breakdown":{"critical":0,"high":0,"medium":0,"low":0,"info":0,"unknown":0}}},"url":"https://dev.azure.com/gg-integration-test/gg-test/_git/test%20abc"}]'
headers:
Access-Control-Expose-Headers:
- X-App-Version
Allow:
- GET, POST, HEAD, OPTIONS
Connection:
- keep-alive
Content-Length:
- '5158'
Content-Type:
- application/json
Cross-Origin-Opener-Policy:
- same-origin
Date:
- Fri, 06 Dec 2024 12:46:14 GMT
Link:
- ''
Referrer-Policy:
- same-origin
Server:
- nginx/1.24.0
Vary:
- Cookie
X-App-Version:
- dev
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Per-Page:
- '20'
X-Request-ID:
- 235112590e3f298f700971bb283acb6c
X-SCA-Engine-Version:
- 2.2.0
X-Secrets-Engine-Version:
- 2.127.0
status:
code: 200
message: OK
version: 1
Loading