Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
PA-8697 Handle zero issue API response for SARIF output (#74)
Browse files Browse the repository at this point in the history
* handle zero issue api response

* version number

* change wording and fix if statement
  • Loading branch information
MichaelL-PA committed Mar 9, 2023
1 parent c125390 commit aadd8cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/cli/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.7.9
1.7.10
77 changes: 39 additions & 38 deletions src/cli/soos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1163,51 +1163,52 @@ def exec(context: SOOSContext, project_hash: str, branch_hash: str, scan_id: str

api_response: requests.Response = requests.get(url=url, headers=headers)
sarif_json_response = handle_response(api_response)
if type(sarif_json_response) is ErrorAPIResponse:
if sarif_json_response is None:
return
if type(sarif_json_response) is ErrorAPIResponse and sarif_json_response.code == "ApiValidationBadRequest":
SOOS.console_log(f"{sarif_json_response.message}")
return
elif type(sarif_json_response) is ErrorAPIResponse:
error_message = "A Generate SARIF Report API Exception Occurred"
SOOS.console_log(f"{error_message}\n{sarif_json_response.code}-{sarif_json_response.message}")
else:
SOOS.console_log("SARIF Report")
SOOS.console_log(str(sarif_json_response))

if sarif_json_response is None:
SOOS.console_log("This project contains no issues. There will be no SARIF upload.")
return
SOOS.console_log("Uploading SARIF Report to GitHub")
sarif_report_str = json.dumps(sarif_json_response)
compressed_sarif_response = base64.b64encode(gzip.compress(bytes(sarif_report_str, 'UTF-8')))

github_body_request = {
"commit_sha": context.commit_hash,
"ref": context.branch_name,
"sarif": compressed_sarif_response.decode(encoding='UTF-8'),
"started_at": ANALYSIS_START_TIME,
"tool_name": "SOOS SCA"
}

github_sarif_url = SOOSSARIFReport.generate_github_sarif_url(project_name=context.project_name)
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {context.github_pat}"}

sarif_github_response = requests.post(url=github_sarif_url, data=json.dumps(github_body_request),
headers=headers)

if sarif_github_response.status_code >= 400:
SOOSSARIFReport.handle_github_sarif_error(status=sarif_github_response.status_code,
json_response=sarif_github_response.json())
else:
SOOS.console_log("Uploading SARIF Report to GitHub")
sarif_report_str = json.dumps(sarif_json_response)
compressed_sarif_response = base64.b64encode(gzip.compress(bytes(sarif_report_str, 'UTF-8')))

github_body_request = {
"commit_sha": context.commit_hash,
"ref": context.branch_name,
"sarif": compressed_sarif_response.decode(encoding='UTF-8'),
"started_at": ANALYSIS_START_TIME,
"tool_name": "SOOS SCA"
}

github_sarif_url = SOOSSARIFReport.generate_github_sarif_url(project_name=context.project_name)
headers = {"Accept": "application/vnd.github.v3+json", "Authorization": f"token {context.github_pat}"}

sarif_github_response = requests.post(url=github_sarif_url, data=json.dumps(github_body_request),
headers=headers)

if sarif_github_response.status_code >= 400:
SOOSSARIFReport.handle_github_sarif_error(status=sarif_github_response.status_code,
json_response=sarif_github_response.json())
else:
sarif_id = sarif_github_response.json()["id"]
sarif_url = sarif_github_response.json()["url"]
github_sarif_report_status = requests.get(url=sarif_url, headers=headers)

if github_sarif_report_status.ok:
processing_status = github_sarif_report_status.json()[
"processing_status"] if "processing_status" in github_sarif_report_status.json() else None
errors = github_sarif_report_status.json()[
"errors"] if "errors" in github_sarif_report_status.json() else None
SOOS.console_log(f"Upload SARIF Report to Github Status: {processing_status}")
if errors is not None and len(errors) > 0:
SOOS.console_log(f"Errors: {str(errors)}")
sarif_id = sarif_github_response.json()["id"]
sarif_url = sarif_github_response.json()["url"]
github_sarif_report_status = requests.get(url=sarif_url, headers=headers)

if github_sarif_report_status.ok:
processing_status = github_sarif_report_status.json()[
"processing_status"] if "processing_status" in github_sarif_report_status.json() else None
errors = github_sarif_report_status.json()[
"errors"] if "errors" in github_sarif_report_status.json() else None
SOOS.console_log(f"Upload SARIF Report to Github Status: {processing_status}")
if errors is not None and len(errors) > 0:
SOOS.console_log(f"Errors: {str(errors)}")

except Exception as sarif_exception:
SOOS.console_log(f"ERROR: {str(sarif_exception)}")
Expand Down

0 comments on commit aadd8cf

Please sign in to comment.