Skip to content

Commit

Permalink
Fix copy command output SARIF JSON schema compliance (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
balgillo authored Sep 17, 2024
1 parent 02132ae commit aafa048
Show file tree
Hide file tree
Showing 8 changed files with 3,662 additions and 27 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
/.pytest_cache
.DS_Store
*.sarif
*.json
*.csv
.coverage
coverage.xml
Expand Down
221 changes: 208 additions & 13 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pyyaml = "^6.0.1"

[tool.poetry.dev-dependencies]
black = "^24.3.0"
jsonschema = "^4.23.0"
pylint = "^3.2"
pytest = "^8.3"
pytest-cov = "^5.0"
Expand Down
15 changes: 5 additions & 10 deletions sarif/operations/copy_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os

from sarif import loader, sarif_file
from sarif.sarif_file import SarifFileSet
from sarif.sarif_file import SarifFileSet, SarifFile


def generate_sarif(
Expand All @@ -17,7 +17,7 @@ def generate_sarif(
append_timestamp: bool,
sarif_tools_version: str,
cmdline: str,
):
) -> SarifFile:
"""
Generate a new SARIF file based on the input files
"""
Expand All @@ -26,7 +26,7 @@ def generate_sarif(
"version": "2.1.0",
"runs": [],
}
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
output_file_abs_path = os.path.abspath(output)
conversion_timestamp_iso8601 = now.isoformat()
conversion_timestamp_trendformat = now.strftime(sarif_file.DATETIME_FORMAT)
Expand All @@ -38,10 +38,7 @@ def generate_sarif(
continue
input_file_count += 1
input_file_path = input_file.get_abs_file_path()
input_file_stat = os.stat(input_file_path)
input_file_modified_iso8601 = datetime.datetime.fromtimestamp(
input_file_stat.st_mtime
).isoformat()
input_file_modified_iso8601 = input_file.mtime.isoformat()
for input_run in input_file.runs:
run_count += 1
# Create a shallow copy
Expand All @@ -60,9 +57,7 @@ def generate_sarif(
"properties": conversion_properties,
}
},
"invocation": {
"commandLine": cmdline,
},
"invocation": {"commandLine": cmdline, "executionSuccessful": True},
}
results = input_run.get_results()
filter_stats = input_run.get_filter_stats()
Expand Down
7 changes: 6 additions & 1 deletion sarif/sarif_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,13 @@ class SarifFile:
Class to hold SARIF data parsed from a file and provide accessors to the data.
"""

def __init__(self, file_path, data):
def __init__(self, file_path, data, mtime=None):
self.abs_file_path = os.path.abspath(file_path)
if mtime:
self.mtime = mtime
else:
stat = os.stat(file_path)
self.mtime = datetime.datetime.fromtimestamp(stat.st_mtime)
self.data = data
self.runs = [
SarifRun(self, run_index, run_data)
Expand Down
Loading

0 comments on commit aafa048

Please sign in to comment.