Skip to content

Commit

Permalink
fix(json): fix line display on JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
Jguer committed Jul 5, 2021
1 parent 5cc6ec0 commit 573faf8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion ggshield/output/json/json_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def flattened_policy_break(
"total_occurrences": len(policy_breaks),
}
for policy_break in policy_breaks:
update_policy_break_matches(policy_break.matches, lines, is_patch)
update_policy_break_matches(policy_break.matches, lines, is_patch, True)
flattened_dict["occurrences"].extend(policy_break.matches)

return flattened_dict
18 changes: 4 additions & 14 deletions ggshield/output/json/schemas.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
from marshmallow import fields
from marshmallow.schema import Schema
from pygitguardian.models import MatchSchema
from pygitguardian.models import BaseSchema, MatchSchema


class FlattenedPolicyBreak(Schema):
class FlattenedPolicyBreak(BaseSchema):
policy = fields.String(required=True)
occurrences = fields.List(fields.Nested(MatchSchema), required=True)
break_type = fields.String(data_key="type", required=True)
ignore_sha = fields.String(required=True)
total_occurrences = fields.Integer(required=True)

class Meta:
ordered = True


class JSONResultSchema(Schema):
class JSONResultSchema(BaseSchema):
mode = fields.String(required=True)
filename = fields.String(required=True)
incidents = fields.List(fields.Nested(FlattenedPolicyBreak), required=True)
total_incidents = fields.Integer(required=True)
total_occurrences = fields.Integer(required=True)

class Meta:
ordered = True


class JSONScanCollectionSchema(Schema):
class JSONScanCollectionSchema(BaseSchema):
id = fields.String()
type = fields.String()
results = fields.List(
Expand All @@ -36,6 +29,3 @@ class JSONScanCollectionSchema(Schema):
total_incidents = fields.Integer(required=True)
total_occurrences = fields.Integer(required=True)
secrets_engine_version = fields.String(required=False)

class Meta:
ordered = True
14 changes: 11 additions & 3 deletions ggshield/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ def get_lines_from_patch(content: str, filemode: Filemode) -> Iterable[Line]:


def update_policy_break_matches(
matches: List[Match], lines: List[Line], is_patch: bool
matches: List[Match], lines: List[Line], is_patch: bool, user_display: bool = False
) -> None:
"""
Update secrets object with secret line and indexes in line.
:param secrets: List of secrets sorted by start index
:param lines: List of content lines with indexes (post_index and pre_index)
:param is_patch: True if is patch from git, False if file
:param user_display: Get line results as if treating the complete file
"""
index = 0
line_index = 0
Expand All @@ -165,10 +166,17 @@ def update_policy_break_matches(
line_index += 1
len_line = len(lines[line_index].content) + 1 + int(is_patch)

if user_display:
match.line_start = (
lines[start_line].pre_index or lines[start_line].post_index
)
match.line_end = lines[line_index].pre_index or lines[line_index].post_index
else:
match.line_start = start_line
match.line_end = line_index

match.index_start = start_index
match.index_end = match.index_end - index - int(is_patch) + 1
match.line_start = start_line
match.line_end = line_index


class SupportedCI(Enum):
Expand Down

0 comments on commit 573faf8

Please sign in to comment.