Skip to content

Commit

Permalink
Add option to exclude diff in json report
Browse files Browse the repository at this point in the history
  • Loading branch information
matteogreek authored and lauraschauer committed Jul 24, 2023
1 parent afc8704 commit ad204dd
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 17 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fail_fast: true
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down Expand Up @@ -30,11 +30,11 @@ repos:
# - id: go-unit-tests
# - id: go-build
- repo: https://github.com/psf/black
rev: 19.10b0
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.6.4
rev: 5.12.0
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]
Expand Down
6 changes: 5 additions & 1 deletion prospector/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ def main(argv): # noqa: C901
return

report.generate_report(
results, advisory_record, config.report, config.report_filename
results,
advisory_record,
config.report,
config.report_filename,
config.report_diff,
)

execution_time = execution_statistics["core"]["execution time"][0]
Expand Down
2 changes: 2 additions & 0 deletions prospector/config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ enabled_rules:
report:
format: html
name: prospector-report
no_diff: False


# Log level: "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"
log_level: INFO
Expand Down
13 changes: 9 additions & 4 deletions prospector/core/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ def json_(
results: List[Commit],
advisory_record: AdvisoryRecord,
filename: str = "prospector-report.json",
no_diff: bool = False,
):
fn = filename if filename.endswith(".json") else f"{filename}.json"

data = {
"advisory_record": advisory_record.__dict__,
"commits": [r.as_dict(no_hash=True, no_rules=False) for r in results],
"commits": [
r.as_dict(no_hash=True, no_rules=False, no_diff=no_diff) for r in results
],
}
logger.info(f"Writing results to {fn}")
file = Path(fn)
Expand Down Expand Up @@ -102,17 +105,19 @@ def format_annotations(commit: Commit) -> str:
print(f"Found {count} candidates\nAdvisory record\n{advisory_record}")


def generate_report(results, advisory_record, report_type, report_filename):
def generate_report(
results, advisory_record, report_type, report_filename, report_diff=False
):
with ConsoleWriter("Generating report\n") as console:
match report_type:
case "console":
console_(results, advisory_record, get_level() < logging.INFO)
case "json":
json_(results, advisory_record, report_filename)
json_(results, advisory_record, report_filename, report_diff)
case "html":
html_(results, advisory_record, report_filename)
case "all":
json_(results, advisory_record, report_filename)
json_(results, advisory_record, report_filename, report_diff)
html_(results, advisory_record, report_filename)
case _:
logger.warning("Invalid report type specified, using 'console'")
Expand Down
10 changes: 6 additions & 4 deletions prospector/datamodel/commit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -85,15 +85,15 @@ def serialize_minhash(self):
def deserialize_minhash(self, binary_minhash):
self.minhash = decode_minhash(binary_minhash)

# TODO: can i delete this?
def as_dict(self, no_hash: bool = True, no_rules: bool = True):
def as_dict(
self, no_hash: bool = True, no_rules: bool = True, no_diff: bool = True
):
out = {
"commit_id": self.commit_id,
"repository": self.repository,
"timestamp": self.timestamp,
"hunks": self.hunks,
"message": self.message,
"diff": self.diff,
"changed_files": self.changed_files,
"message_reference_content": self.message_reference_content,
"jira_refs": self.jira_refs,
Expand All @@ -102,6 +102,8 @@ def as_dict(self, no_hash: bool = True, no_rules: bool = True):
"twins": self.twins,
"tags": self.tags,
}
if not no_diff:
out["diff"] = self.diff
if not no_hash:
out["minhash"] = encode_minhash(self.minhash)
if not no_rules:
Expand Down
25 changes: 20 additions & 5 deletions prospector/util/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def parse_cli_args(args):
help="Commit preprocessing only",
)

parser.add_argument("--pub-date", type=str, help="Publication date of the advisory")
parser.add_argument(
"--pub-date", type=str, help="Publication date of the advisory"
)

# Allow the user to manually supply advisory description
parser.add_argument("--description", type=str, help="Advisory description")
Expand Down Expand Up @@ -154,7 +156,9 @@ def parse_config_file(filename: str = "config.yaml"):
logger.error(f"Type error in {filename}: {e}")
except Exception as e:
# General exception catch block for any other exceptions
logger.error(f"An unexpected error occurred when parsing config.yaml: {e}")
logger.error(
f"An unexpected error occurred when parsing config.yaml: {e}"
)
else:
logger.error("No configuration file found, cannot proceed.")

Expand Down Expand Up @@ -202,7 +206,11 @@ class ConfigSchema:
enabled_rules: List[str] = MISSING
nvd_token: Optional[str] = None
database: DatabaseConfig = DatabaseConfig(
user="postgres", password="example", host="db", port=5432, dbname="postgres"
user="postgres",
password="example",
host="db",
port=5432,
dbname="postgres",
)
llm_service: Optional[LLMServiceConfig] = None
github_token: Optional[str] = None
Expand Down Expand Up @@ -230,6 +238,7 @@ def __init__(
backend: str,
report: ReportConfig,
report_filename: str,
report_diff: bool,
ping: bool,
log_level: str,
git_cache: str,
Expand All @@ -245,8 +254,12 @@ def __init__(
self.description = description
self.max_candidates = max_candidates
# self.tag_interval = tag_interval
self.version_interval = version_interval if version_interval else "None:None"
self.modified_files = modified_files.split(",") if modified_files else []
self.version_interval = (
version_interval if version_interval else "None:None"
)
self.modified_files = (
modified_files.split(",") if modified_files else []
)
self.filter_extensions = filter_extensions
self.keywords = keywords.split(",") if keywords else []
self.use_nvd = use_nvd
Expand All @@ -255,6 +268,7 @@ def __init__(
self.use_backend = use_backend
self.report = report
self.report_filename = report_filename
self.report_diff = report_diff
self.ping = ping
self.log_level = log_level
self.git_cache = git_cache
Expand Down Expand Up @@ -292,6 +306,7 @@ def get_configuration(argv):
use_backend=args.use_backend or conf.use_backend,
report=args.report or conf.report.format,
report_filename=args.report_filename or conf.report.name,
report_diff=conf.report.no_diff,
ping=args.ping,
git_cache=conf.git_cache,
enabled_rules=conf.enabled_rules,
Expand Down

0 comments on commit ad204dd

Please sign in to comment.