Skip to content

Commit 2f6a8f9

Browse files
author
Shane Wright
committed
gracefully handle failure to locate scan info:
- add try/retry loop - print more status messages - print detailed debug data on failure - add some initializations and assertions for safety
1 parent 6182356 commit 2f6a8f9

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

examples/client/parse_spdx.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def poll_for_sbom_complete(sbom_name, proj_version_url):
238238
retries = MAX_RETRIES
239239
sleep_time = SLEEP
240240
matched_scan = False
241+
latest_url = None
241242
cl_url = None
242243

243244
# Replace any spaces in the name with a dash to match BD
@@ -248,32 +249,56 @@ def poll_for_sbom_complete(sbom_name, proj_version_url):
248249
'q': [f"name:{sbom_name}"],
249250
'sort': ["updatedAt: ASC"]
250251
}
251-
cls = bd.get_resource('codeLocations', params=params)
252-
for cl in cls:
252+
253+
while (retries):
254+
cls = bd.get_resource('codeLocations', params=params)
255+
retries -= 1
253256
if matched_scan:
257+
# Exit the while()
254258
break
255-
# Force exact match of: spdx_doc_name + " spdx/sbom"
256-
# BD appends the "spdx/sbom" string to the name.
257-
if cl['name'] != sbom_name + " spdx/sbom":
258-
continue
259-
260-
matched_scan = True
261-
cl_url = cl['_meta']['href']
262-
263-
for link in (cl['_meta']['links']):
264-
# Locate the scans URL to check for status
265-
if link['rel'] == "latest-scan":
266-
latest_url = link['href']
259+
# Save the CL data as we go for debugging
260+
backupcls = []
261+
for cl in cls:
262+
backupcls.append(cl)
263+
if matched_scan:
264+
# Exit the inner for()
267265
break
266+
print(f"Searching scans for {sbom_name}...")
267+
# Force exact match of: spdx_doc_name + " spdx/sbom"
268+
# BD appends the "spdx/sbom" string to the name.
269+
if cl['name'] != sbom_name + " spdx/sbom":
270+
# No match, keep searching
271+
print(f" {cl['name']} != {sbom_name}" + " spdx/sbom")
272+
continue
273+
274+
print(" Scan located")
275+
matched_scan = True
276+
cl_url = cl['_meta']['href']
277+
278+
print("Checking for latest-scan info...")
279+
for link in (cl['_meta']['links']):
280+
# Locate the scans URL to check for status
281+
if link['rel'] == "latest-scan":
282+
print(" Located latest-scan")
283+
latest_url = link['href']
284+
break
285+
286+
# We walked the list of code locations and didn't find a match
287+
if not matched_scan:
288+
print(f" Waiting to locate scan...")
289+
time.sleep(sleep_time)
268290

269291
if not matched_scan:
270292
logging.error(f"No scan found for SBOM: {sbom_name}")
293+
print("\nCodelocations API data:\n")
294+
pprint(backupcls)
271295
sys.exit(1)
272296

273297
assert latest_url, "Failed to locate latest-scan reference"
274298
assert cl_url, "Failed to locate codelocation reference"
275299

276300
# Wait for scanState = SUCCESS
301+
retries = MAX_RETRIES
277302
while (retries):
278303
json_data = bd.get_json(latest_url)
279304
retries -= 1
@@ -288,6 +313,7 @@ def poll_for_sbom_complete(sbom_name, proj_version_url):
288313
print(f"Waiting for scan completion, currently: {json_data['scanState']}")
289314
time.sleep(sleep_time)
290315

316+
assert json_data, "Failed to locate scanState data"
291317
# If there were ZERO matches, there will never be a notification of
292318
# BOM import success. Short-circuit the check and treat this as success.
293319
if json_data['matchCount'] == 0:

0 commit comments

Comments
 (0)