From ca0f27dc06e07225491a37adca043c2928004f4b Mon Sep 17 00:00:00 2001 From: michal Date: Tue, 19 Jul 2022 14:28:52 +0300 Subject: [PATCH 1/3] update click version --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 01f93a9a..7fa280ce 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ PyYAML==6.0 -click~=7.1.2 +click==8.1.0 colorama~=0.4.3 pathspec~=0.8.0 typing~=3.7.4.3 From ce1db5be40138cb0bfd3af43af6bd58b5caa2280 Mon Sep 17 00:00:00 2001 From: michal Date: Tue, 19 Jul 2022 14:56:04 +0300 Subject: [PATCH 2/3] delete old printer --- cli/code_scanner.py | 1 - cli/printer.py | 82 --------------------------------------------- 2 files changed, 83 deletions(-) delete mode 100644 cli/printer.py diff --git a/cli/code_scanner.py b/cli/code_scanner.py index 09fc6300..24e4fcdb 100644 --- a/cli/code_scanner.py +++ b/cli/code_scanner.py @@ -7,7 +7,6 @@ from typing import Optional from git import Repo, NULL_TREE, InvalidGitRepositoryError from sys import getsizeof -from cli import printer from cli.printers import ResultsPrinter from typing import List, Dict from cli.models import Document, DocumentDetections diff --git a/cli/printer.py b/cli/printer.py deleted file mode 100644 index 4f396059..00000000 --- a/cli/printer.py +++ /dev/null @@ -1,82 +0,0 @@ -import click -import math -from cli.models import DocumentDetections -from cli.config import config -from cli.consts import SECRET_SCAN_TYPE -from cli.utils.string_utils import obfuscate_text - -import colorama - - -@click.pass_context -def print_detections(context: click.Context, detection_details: DocumentDetections): - lines_to_display = config['result_printer']['lines_to_display'] - show_secret = context.obj['show_secret'] - scan_type = context.obj['scan_type'] - document = detection_details.document - for detection in detection_details.detections: - detection_name = detection.type if scan_type == SECRET_SCAN_TYPE else detection.message - detection_sha = detection.detection_details.get('sha512') - detection_sha_message = f'\nSecret SHA: {detection_sha}' if detection_sha else '' - detection_commit_id = detection.detection_details.get('commit_id') - detection_commit_id_message = f'\nCommit SHA: {detection_commit_id}' if detection_commit_id else '' - click.echo( - f'⛔ Found issue of type: {click.style(detection_name, fg="bright_red", bold=True)} (rule ID: {detection.detection_rule_id}) in file: {click.format_filename(detection_details.document.path)} ' + - f'{detection_sha_message}{detection_commit_id_message} ⛔ ') - - detection_line = try_get_int("line" if scan_type == SECRET_SCAN_TYPE else "line_in_file", - detection.detection_details) - detection_position = try_get_int("start_position", detection.detection_details) - - lines = detection_details.document.content.splitlines() - start_line = detection_line - math.ceil(lines_to_display / 2) - if start_line < 0: - start_line = 0 - - click.echo() - for i in range(lines_to_display): - current_line_index = start_line + i - if current_line_index >= len(lines): - break - - current_line = lines[current_line_index] - if current_line_index == detection_line: - position = get_position_in_line(detection_details.document.content, detection_position) - click.echo( - f"{get_line_number_style(current_line_index+1)} {get_line_style(current_line, document.is_git_diff_format, position, detection.detection_details.get('length'), show_secret, scan_type)}") - else: - click.echo( - f'{get_line_number_style(current_line_index+1)} {get_line_style(current_line, document.is_git_diff_format, scan_type=scan_type)}') - click.echo() - - -def get_position_in_line(text: str, position: int) -> int: - return position - text.rfind('\n', 0, position) - 1 - - -def get_line_number_style(line_number: int): - return f'{click.style(str(line_number), fg="white", bold=False)} {click.style("|", fg="red", bold=False)}' - - -def get_line_style(line: str, is_git_diff: bool, start_position: int = -1, - length: int = None, show_secret: bool = False, scan_type: str = 'secret'): - if start_position >= 0 and scan_type == SECRET_SCAN_TYPE: - violation = line[start_position: start_position + length] - if not show_secret: - violation = obfuscate_text(violation) - return f'{get_line_style(line[0: start_position], is_git_diff, scan_type=scan_type)}{click.style(violation, underline=True, bold=False)}{get_line_style(line[start_position + length:], is_git_diff, scan_type=scan_type)}' - - if not is_git_diff: - return click.style(line, fg='white', bold=False) - - if line.startswith('+'): - return click.style(line, fg='green', bold=False) - - if line.startswith('-'): - return click.style(line, fg='red', bold=False) - - return click.style(line, fg='white', bold=False) - - -def try_get_int(key, dict): - return dict[key] if key in dict else -1 From 81ada45aca35e2e8ac21124463bb67741fe9e8da Mon Sep 17 00:00:00 2001 From: michal Date: Tue, 19 Jul 2022 14:57:54 +0300 Subject: [PATCH 3/3] remove unused code --- cli/code_scanner.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cli/code_scanner.py b/cli/code_scanner.py index 24e4fcdb..4bde5632 100644 --- a/cli/code_scanner.py +++ b/cli/code_scanner.py @@ -230,11 +230,6 @@ def exclude_irrelevant_scan_results(document_detections_list: List[DocumentDetec return relevant_document_detections_list -def print_file_result(document: Document, detections): - printer.print_detections( - detection_details=DocumentDetections(detections=detections, document=document)) - - def get_diff_file_path(file): return file.b_path if file.b_path else file.a_path