Skip to content

Commit 1fddbdd

Browse files
author
Shane Wright
committed
Clean up some error messages.
Clarify the purpose of some variables. Handle SPDX names with space chars Fix edge case w/UNKNOWN versions
1 parent bf8d5fd commit 1fddbdd

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

examples/client/parse_spdx.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ def poll_for_upload(sbom_name):
153153
sleep_time = 10
154154
matched_scan = False
155155

156+
# Replace any spaces in the name with a dash to match BD
157+
sbom_name = sbom_name.replace(' ', '-')
158+
156159
# TODO also check for api/projects/<ver>/versions/<ver>/codelocations
157160
# -- status - operationNameCode = ServerScanning, operationName=Scanning, status
158161
# -- should be COMPLETED, not IN_PROGRESS
159162
# -- operatinName: Scanning
160163
# Search for the latest scan matching our SBOM
161164
# This might be a risk for a race condition
162-
# TODO Annoyingly, the sbom_name is not necessarily precisely our document
163-
# name! Found a case where BD swaps a space for a "-" in the
164-
# document name. Need to be more general in the match.
165165
params = {
166166
'q': [f"name:{sbom_name}"],
167167
'sort': ["updatedAt: ASC"]
@@ -237,7 +237,7 @@ def upload_sbom_file(filename, project, version):
237237
try:
238238
pprint(response.json()['errorMessage'])
239239
except:
240-
logging.error(f"Status code {response.status_code}")
240+
logging.error(f"Status code: {response.status_code}")
241241
sys.exit(1)
242242

243243
# Lookup the given pURL in the BD KB.
@@ -282,6 +282,9 @@ def find_comp_in_bom(compname, compver, projver):
282282
if comp['componentName'].lower() != compname.lower():
283283
# The BD API search is inexact. Force our match to be precise.
284284
continue
285+
if compver == "UNKNOWN":
286+
# We did not have a version specified in the first place
287+
return True
285288
# Check component name + version name
286289
try:
287290
if comp['componentVersionName'].lower() == compver.lower():
@@ -370,15 +373,10 @@ def create_cust_comp(name, version, license):
370373
}
371374
response = bd.session.post("api/components", json=data)
372375
logging.debug(response)
373-
if response.status_code == 412:
374-
# Shouldn't be possible. We checked for existence earlier.
375-
logging.error(f"Component {name} already exists")
376-
sys.exit(1)
377-
378376
if response.status_code != 201:
379377
# Shouldn't be possible. We checked for existence earlier.
380378
logging.error(response.json()['errors'][0]['errorMessage'])
381-
logging.error(f"Status code {response.status_code}")
379+
logging.error(f"Status code: {response.status_code}")
382380
sys.exit(1)
383381

384382
# Should be guaranteed 1 version because we just created it!
@@ -427,7 +425,7 @@ def add_to_sbom(proj_version_url, comp_ver_url):
427425
response = bd.session.post(proj_version_url + "/components", json=data)
428426
if (response.status_code != 200):
429427
logging.error(response.json()['errors'][0]['errorMessage'])
430-
logging.error(f"Status code {response.status_code}")
428+
logging.error(f"Status code: {response.status_code}")
431429
sys.exit(1)
432430

433431

@@ -521,9 +519,14 @@ def add_to_sbom(proj_version_url, comp_ver_url):
521519
# We hope we'll have an external reference (pURL), but we might not.
522520
extref = None
523521
purlmatch = False
522+
# matchname/matchver can change, depending on the KB lookup step.
523+
# These are stored separately so that we have the original names available.
524524
matchname = package.name
525+
if package.version is None:
526+
# Default in case one is not specified in SPDX
527+
package.version = "UNKNOWN"
525528
matchver = package.version
526-
print(f"Processing SPDX package: {matchname} {matchver}....")
529+
print(f"Processing SPDX package: {matchname} version: {matchver}....")
527530
# Tracking unique package name + version from spdx file
528531
packages[matchname+matchver] = packages.get(matchname+matchver, 0) + 1
529532

0 commit comments

Comments
 (0)