Skip to content

Commit d72e5ad

Browse files
committed
feat(models): Add is_diff to ScanResult and diff_kind to PolicyBreak
1 parent b567ad4 commit d72e5ad

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pygitguardian/models.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ def __repr__(self) -> str:
255255
)
256256

257257

258+
class DiffKinds(str, Enum):
259+
ADDITION = "addition"
260+
DELETION = "deletion"
261+
CONTEXT = "context"
262+
263+
258264
class PolicyBreakSchema(BaseSchema):
259265
break_type = fields.String(data_key="type", required=True)
260266
policy = fields.String(required=True)
@@ -264,6 +270,9 @@ class PolicyBreakSchema(BaseSchema):
264270
matches = fields.List(fields.Nested(MatchSchema), required=True)
265271
is_excluded = fields.Boolean(required=False, load_default=False, dump_default=False)
266272
exclude_reason = fields.String(required=False, load_default=None, dump_default=None)
273+
diff_kind = fields.Enum(
274+
DiffKinds, by_value=True, required=False, load_default=None, dump_default=None
275+
)
267276

268277
@post_load
269278
def make_policy_break(self, data: Dict[str, Any], **kwargs: Any) -> "PolicyBreak":
@@ -290,6 +299,7 @@ def __init__(
290299
incident_url: Optional[str] = None,
291300
is_excluded: bool = False,
292301
exclude_reason: Optional[str] = None,
302+
diff_kind: Optional[DiffKinds] = None,
293303
**kwargs: Any,
294304
) -> None:
295305
super().__init__()
@@ -301,6 +311,7 @@ def __init__(
301311
self.matches = matches
302312
self.is_excluded = is_excluded
303313
self.exclude_reason = exclude_reason
314+
self.diff_kind = diff_kind
304315

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

322334
@post_load
323335
def make_scan_result(self, data: Dict[str, Any], **kwargs: Any) -> "ScanResult":
@@ -341,6 +353,7 @@ def __init__(
341353
policy_break_count: int,
342354
policy_breaks: List[PolicyBreak],
343355
policies: List[str],
356+
is_diff: bool,
344357
**kwargs: Any,
345358
) -> None:
346359
"""
@@ -350,11 +363,14 @@ def __init__(
350363
:type policy_breaks: List
351364
:param policies: string list of policies evaluated
352365
:type policies: List[str]
366+
:param is_diff: true if the document scanned is a diff
367+
:type is_diff: bool
353368
"""
354369
super().__init__()
355370
self.policy_break_count = policy_break_count
356371
self.policies = policies
357372
self.policy_breaks = policy_breaks
373+
self.is_diff = is_diff
358374

359375
@property
360376
def has_policy_breaks(self) -> bool:

tests/test_models.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ def test_document_handle_surrogates(self):
137137
"matches": [{"match": "hello", "type": "hello"}],
138138
"is_excluded": False,
139139
"exclude_reason": None,
140+
"diff_kind": None,
141+
},
142+
),
143+
(
144+
PolicyBreakSchema,
145+
PolicyBreak,
146+
{
147+
"type": "hello",
148+
"policy": "hello",
149+
"validity": "hey",
150+
"known_secret": True,
151+
"incident_url": "https://api.gitguardian.com/workspace/2/incidents/3",
152+
"matches": [{"match": "hello", "type": "hello"}],
153+
"is_excluded": False,
154+
"exclude_reason": None,
155+
"diff_kind": "addition",
140156
},
141157
),
142158
(
@@ -166,6 +182,16 @@ def test_document_handle_surrogates(self):
166182
ScanResult,
167183
{"policy_break_count": 1, "policy_breaks": [], "policies": []},
168184
),
185+
(
186+
ScanResultSchema,
187+
ScanResult,
188+
{
189+
"policy_break_count": 1,
190+
"policy_breaks": [],
191+
"policies": [],
192+
"is_diff": True,
193+
},
194+
),
169195
(
170196
DetailSchema,
171197
Detail,

0 commit comments

Comments
 (0)