Skip to content

Commit 9ca8ba7

Browse files
committed
use sensible defaults, fix broken requests
1 parent eb88c12 commit 9ca8ba7

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

examples/client/generate_sbom.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ class FailedReportDownload(Exception):
4545
parser.add_argument("token_file", help="containing access token")
4646
parser.add_argument("project_name")
4747
parser.add_argument("version_name")
48-
parser.add_argument("-z", "--zip_file_name", default="reports.zip")
49-
parser.add_argument("-t", "--type", type=str, nargs='?', default="SPDX_22", choices=["SPDX_22", "CYCLONEDX_13"], help="Choose the type of SBOM report")
48+
parser.add_argument("-t", "--type", type=str, nargs='?', default="SPDX_23", choices=["SPDX_22", "SPDX_23", "CYCLONEDX_13", "CYCLONEDX_14"], help="Choose the type of SBOM report")
5049
parser.add_argument('-r', '--retries', default=4, type=int, help="How many times to retry downloading the report, i.e. wait for the report to be generated")
51-
parser.add_argument('-s', '--sleep_time', default=5, type=int, help="The amount of time to sleep in-between (re-)tries to download the report")
50+
parser.add_argument('-s', '--sleep_seconds', default=60, type=int, help="The amount of time to sleep in-between (re-)tries to download the report")
51+
parser.add_argument('--include-subprojects', dest='include_subprojects', action='store_false', help="whether subprojects should be included")
5252
parser.add_argument('--no-verify', dest='verify', action='store_false', help="disable TLS certificate verification")
5353

5454
args = parser.parse_args()
@@ -75,8 +75,8 @@ def download_report(bd_client, location, filename, retries=args.retries):
7575
logging.error("Ruh-roh, not sure what happened here")
7676
else:
7777
logging.debug(f"Failed to retrieve report {report_id}, report status: {report_status}")
78-
logging.debug("Probably not ready yet, waiting 5 seconds then retrying...")
79-
time.sleep(args.sleep_time)
78+
logging.debug(f"Probably not ready yet, waiting {sleep_seconds} seconds then retrying...")
79+
time.sleep(args.sleep_seconds)
8080
retries -= 1
8181
download_report(bd_client, location, filename, retries)
8282
else:
@@ -105,16 +105,19 @@ def download_report(bd_client, location, filename, retries=args.retries):
105105

106106
post_data = {
107107
'reportFormat': "JSON",
108-
'reportType': 'SBOM',
109-
'sbomType': args.type,
108+
'sbomType': args.type,
109+
'includeSubprojects': args.include_subprojects
110110
}
111111
sbom_reports_url = version['_meta']['href'] + "/sbom-reports"
112112

113+
bd.session.headers["Content-Type"] = "application/vnd.blackducksoftware.report-4.json"
113114
r = bd.session.post(sbom_reports_url, json=post_data)
115+
if (r.status_code == 403):
116+
logging.debug("Authorization Error - Please ensure the token you are using has write permissions!")
114117
r.raise_for_status()
115118
location = r.headers.get('Location')
116119
assert location, "Hmm, this does not make sense. If we successfully created a report then there needs to be a location where we can get it from"
117120

118121
logging.debug(f"Created SBOM report of type {args.type} for project {args.project_name}, version {args.version_name} at location {location}")
119-
download_report(bd, location, args.zip_file_name)
122+
download_report(bd, location, f"{args.project_name}({args.version_name}).zip")
120123

0 commit comments

Comments
 (0)