Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
A new scriv changelog fragment.

Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed

- A bullet item for the Removed category.

-->

### Added

- Add field is_diff to ScanResult
- Add field diff_kind to PolicyBreak

<!--
### Changed

- A bullet item for the Changed category.

-->
<!--
### Deprecated

- A bullet item for the Deprecated category.

-->
<!--
### Fixed

- A bullet item for the Fixed category.

-->
<!--
### Security

- A bullet item for the Security category.

-->
16 changes: 16 additions & 0 deletions pygitguardian/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ def __repr__(self) -> str:
)


class DiffKind(str, Enum):
ADDITION = "addition"
DELETION = "deletion"
CONTEXT = "context"


class PolicyBreakSchema(BaseSchema):
break_type = fields.String(data_key="type", required=True)
policy = fields.String(required=True)
Expand All @@ -264,6 +270,9 @@ class PolicyBreakSchema(BaseSchema):
matches = fields.List(fields.Nested(MatchSchema), required=True)
is_excluded = fields.Boolean(required=False, load_default=False, dump_default=False)
exclude_reason = fields.String(required=False, load_default=None, dump_default=None)
diff_kind = fields.Enum(
DiffKind, by_value=True, required=False, load_default=None, dump_default=None
)

@post_load
def make_policy_break(self, data: Dict[str, Any], **kwargs: Any) -> "PolicyBreak":
Expand All @@ -290,6 +299,7 @@ def __init__(
incident_url: Optional[str] = None,
is_excluded: bool = False,
exclude_reason: Optional[str] = None,
diff_kind: Optional[DiffKind] = None,
**kwargs: Any,
) -> None:
super().__init__()
Expand All @@ -301,6 +311,7 @@ def __init__(
self.matches = matches
self.is_excluded = is_excluded
self.exclude_reason = exclude_reason
self.diff_kind = diff_kind

@property
def is_secret(self) -> bool:
Expand All @@ -318,6 +329,7 @@ class ScanResultSchema(BaseSchema):
policy_break_count = fields.Integer(required=True)
policies = fields.List(fields.String(), required=True)
policy_breaks = fields.List(fields.Nested(PolicyBreakSchema), required=True)
is_diff = fields.Boolean(required=False, load_default=False, dump_default=None)

@post_load
def make_scan_result(self, data: Dict[str, Any], **kwargs: Any) -> "ScanResult":
Expand All @@ -341,6 +353,7 @@ def __init__(
policy_break_count: int,
policy_breaks: List[PolicyBreak],
policies: List[str],
is_diff: bool = False,
**kwargs: Any,
) -> None:
"""
Expand All @@ -350,11 +363,14 @@ def __init__(
:type policy_breaks: List
:param policies: string list of policies evaluated
:type policies: List[str]
:param is_diff: true if the document scanned is a diff
:type is_diff: bool
"""
super().__init__()
self.policy_break_count = policy_break_count
self.policies = policies
self.policy_breaks = policy_breaks
self.is_diff = is_diff

@property
def has_policy_breaks(self) -> bool:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,22 @@ def test_document_handle_surrogates(self):
"matches": [{"match": "hello", "type": "hello"}],
"is_excluded": False,
"exclude_reason": None,
"diff_kind": None,
},
),
(
PolicyBreakSchema,
PolicyBreak,
{
"type": "hello",
"policy": "hello",
"validity": "hey",
"known_secret": True,
"incident_url": "https://api.gitguardian.com/workspace/2/incidents/3",
"matches": [{"match": "hello", "type": "hello"}],
"is_excluded": False,
"exclude_reason": None,
"diff_kind": "addition",
},
),
(
Expand Down Expand Up @@ -166,6 +182,16 @@ def test_document_handle_surrogates(self):
ScanResult,
{"policy_break_count": 1, "policy_breaks": [], "policies": []},
),
(
ScanResultSchema,
ScanResult,
{
"policy_break_count": 1,
"policy_breaks": [],
"policies": [],
"is_diff": True,
},
),
(
DetailSchema,
Detail,
Expand Down
Loading