Skip to content

Commit 7c3aadd

Browse files
author
Glenn Snyder
authored
Merge pull request #123 from nichollsdave/wait_for_bom_scans
Wait for multiple code locations e.g. BDIO, Signature and Snippet scanning
2 parents 7cdab4c + c6addab commit 7c3aadd

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

examples/wait_for_scan_results.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,58 @@ def __init__(self, hub, scan_location_name, max_checks=10, check_delay=5, snippe
3737
def wait_for_scan_completion(self):
3838
scan_locations = self.hub.get_codelocations(parameters={'q':f'name:{self.scan_location_name}'}).get('items', [])
3939

40-
scan_location = scan_locations[0]
40+
logging.debug(f"Scan locations found: {len(scan_locations)}")
4141

4242
remaining_checks = self.max_checks
43-
scans_url = self.hub.get_link(scan_location, "scans")
44-
45-
if self.snippet_scan:
46-
logging.debug("Looking for snippet scan which means there will be 2 expected scans")
47-
number_expected_newer_scans = 2
48-
else:
49-
logging.debug("Not looking for snippet scan which means there will be 1 expected scans")
50-
number_expected_newer_scans = 1
5143

5244
while remaining_checks > 0:
53-
scans = self.hub.execute_get(scans_url).json().get('items', [])
5445

55-
newer_scans = list(filter(lambda s: arrow.get(s['updatedAt']) > self.start_time, scans))
56-
logging.debug(f"Found {len(newer_scans)} newer scans")
46+
for scan_location in scan_locations:
5747

58-
expected_scans_seen = len(newer_scans) == number_expected_newer_scans
59-
logging.debug(f"expected_scans_seen: {expected_scans_seen}")
60-
61-
if expected_scans_seen and all([s['status'] in ['COMPLETE', 'FAILURE'] for s in newer_scans]):
62-
logging.info("Scans have finished processing")
63-
if all([s['status'] == 'COMPLETE' for s in newer_scans]):
64-
return ScanMonitor.SUCCESS
48+
scans_url = self.hub.get_link(scan_location, "scans")
49+
50+
scans = self.hub.execute_get(scans_url).json().get('items', [])
51+
52+
newer_scans = list(filter(lambda s: arrow.get(s['updatedAt']) > self.start_time, scans))
53+
54+
if (len(newer_scans) > 0):
55+
if len(newer_scans) > 0 and self.snippet_scan:
56+
# We are snippet scanning, we need to check if we should be waiting for another scan. Only the case if one of them is FS or if one is SNIPPET. If one is BDIO then it will not have snippet.
57+
fs_scans = list(filter(lambda s: s['scanType'] == 'FS', newer_scans))
58+
snippet_scans = list(filter(lambda s: s['scanType'] == 'SNIPPET', newer_scans))
59+
if len(fs_scans) > 0 or len(snippet_scans) > 0:
60+
# This is a candicate for snippet scan
61+
expected_scans_seen = len(fs_scans) > 0 and len(snippet_scans) > 0
62+
logging.debug(f"Snippet scanning - candidate code location - newer scans {len(newer_scans)}, expected_scans_seen: {expected_scans_seen} for {scan_location['name']}")
63+
else:
64+
# This is another type of scan.
65+
expected_scans_seen = True
66+
logging.debug(f"Snippet scanning - non snippet code location - newer scans {len(newer_scans)}, expected_scans_seen: {expected_scans_seen} for {scan_location['name']}")
67+
68+
else:
69+
# We have one or more newer scans
70+
expected_scans_seen = True
71+
logging.debug(f"Not Snippet scanning - newer scans {len(newer_scans)}, expected_scans_seen: {expected_scans_seen} for {scan_location['name']}")
6572
else:
66-
return ScanMonitor.FAILURE
67-
else:
68-
remaining_checks -= 1
69-
logging.debug(f"Sleeping for {self.check_delay} seconds before checking again. {remaining_checks} remaining")
70-
time.sleep(self.check_delay)
73+
logging.debug(f"No newer scans found for {scan_location['name']}")
74+
expected_scans_seen = False
75+
76+
if expected_scans_seen and all([s['status'] in ['COMPLETE', 'FAILURE'] for s in newer_scans]):
77+
logging.info(f"Scans have finished processing for {scan_location['name']}")
78+
if all([s['status'] == 'COMPLETE' for s in newer_scans]):
79+
# All scans for this code location are complete, remove from the list we are waiting on.
80+
scan_locations.remove(scan_location)
81+
else:
82+
return ScanMonitor.FAILURE
83+
84+
if len(scan_locations) == 0:
85+
# All code locations are complete.
86+
return ScanMonitor.SUCCESS
87+
88+
remaining_checks -= 1
89+
logging.info(f"Waiting for {len(scan_locations)} code locations. Sleeping for {self.check_delay} seconds before checking again. {remaining_checks} remaining")
90+
time.sleep(self.check_delay)
91+
7192

7293
return ScanMonitor.TIMED_OUT
7394

0 commit comments

Comments
 (0)