From 573faf8a04ff68f59ed0be38b42b251e2586fac0 Mon Sep 17 00:00:00 2001 From: jguerreiro Date: Fri, 2 Jul 2021 18:15:17 +0200 Subject: [PATCH] fix(json): fix line display on JSON output --- ggshield/output/json/json_output.py | 2 +- ggshield/output/json/schemas.py | 18 ++++-------------- ggshield/utils.py | 14 +++++++++++--- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/ggshield/output/json/json_output.py b/ggshield/output/json/json_output.py index cb187e0405..8793bad072 100644 --- a/ggshield/output/json/json_output.py +++ b/ggshield/output/json/json_output.py @@ -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 diff --git a/ggshield/output/json/schemas.py b/ggshield/output/json/schemas.py index e831c7da0b..9be7a3ff38 100644 --- a/ggshield/output/json/schemas.py +++ b/ggshield/output/json/schemas.py @@ -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( @@ -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 diff --git a/ggshield/utils.py b/ggshield/utils.py index eca13e7594..3b9fc7b58b 100644 --- a/ggshield/utils.py +++ b/ggshield/utils.py @@ -134,7 +134,7 @@ 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. @@ -142,6 +142,7 @@ def update_policy_break_matches( :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 @@ -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):