Skip to content

Commit 49b6eae

Browse files
author
Glenn Snyder
committed
adding support for looping over large number projects
1 parent 768d3e3 commit 49b6eae

File tree

1 file changed

+50
-30
lines changed

1 file changed

+50
-30
lines changed

examples/get_project_versions_with_unconfirmed_snippet_matches.py

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import csv
55
import json
66
import logging
7+
import math
78
import sys
89
from pprint import pprint
910

@@ -36,36 +37,55 @@
3637
writer = csv.DictWriter(csv_file, fieldnames=columns)
3738
writer.writeheader()
3839

39-
for project in hub.get_projects(limit=args.limit).get('items', []):
40-
for version in hub.get_project_versions(project).get('items', []):
41-
snippet_counts_url = version['_meta']['href'] + "/snippet-counts"
42-
#
43-
# As of v2020.6.0 the Content-Type returned is application/vnd.blackducksoftware.internal-1+json;charset=UTF-8
44-
# so I'm accepting any content type to allow the GET to work flexibly
45-
#
46-
custom_headers = {'Accept': '*/*'}
47-
snippet_counts = hub.execute_get(snippet_counts_url, custom_headers=custom_headers).json()
40+
num_projects = math.inf
41+
projects_offset = 0
42+
first = True
4843

49-
snippets_present = snippet_counts.get('snippetScanPresent', False)
50-
unconfirmed_snippets = snippets_present and snippet_counts.get('unreviewedCount', 0) > 0
51-
number_unconfirmed = snippet_counts.get('unreviewedCount', 0)
52-
number_confirmed = snippet_counts.get('reviewedCount', 0)
53-
number_ignored = snippet_counts.get('ignoredCount', 0)
54-
total = snippet_counts.get('totalCount', 0)
55-
assert (number_unconfirmed + number_confirmed + number_ignored) == total
44+
while projects_offset < num_projects:
45+
parameters = {'offset': projects_offset}
46+
projects_json = hub.get_projects(limit=args.limit, parameters=parameters)
47+
if first:
48+
num_projects = projects_json.get('totalCount', 0)
49+
first = False
50+
51+
logging.debug(f"Retreived {len(projects_json.get('items', []))} from offset {projects_offset} of {num_projects} projects.")
52+
for project in projects_json.get('items', []):
53+
versions_json = hub.get_project_versions(project)
54+
versions_total = versions_json['totalCount']
55+
logging.debug(f"Retrieved {len(versions_json.get('items', []))} of {versions_total} versions for project {project['name']}.")
56+
for version in versions_json.get('items', []):
57+
snippet_counts_url = version['_meta']['href'] + "/snippet-counts"
58+
#
59+
# As of v2020.6.0 the Content-Type returned is application/vnd.blackducksoftware.internal-1+json;charset=UTF-8
60+
# so I'm accepting any content type to allow the GET to work flexibly
61+
#
62+
custom_headers = {'Accept': '*/*'}
63+
snippet_counts = hub.execute_get(snippet_counts_url, custom_headers=custom_headers).json()
64+
65+
snippets_present = snippet_counts.get('snippetScanPresent', False)
66+
unconfirmed_snippets = snippets_present and snippet_counts.get('unreviewedCount', 0) > 0
67+
number_unconfirmed = snippet_counts.get('unreviewedCount', 0)
68+
number_confirmed = snippet_counts.get('reviewedCount', 0)
69+
number_ignored = snippet_counts.get('ignoredCount', 0)
70+
total = snippet_counts.get('totalCount', 0)
71+
assert (number_unconfirmed + number_confirmed + number_ignored) == total
72+
73+
if snippets_present or args.all:
74+
# url_html = f"<a href={version['_meta']['href']}>{version['versionName']}</a>"
75+
url_html = f"=hyperlink(\"{version['_meta']['href']}/components\")"
76+
row = {
77+
'Project Name': project['name'],
78+
'Version Name': version['versionName'],
79+
'Version URL': url_html,
80+
'Has Snippets': snippets_present,
81+
'Un-confirmed': number_unconfirmed,
82+
'Confirmed': number_confirmed,
83+
'Ignored': number_ignored,
84+
'Total': total
85+
}
86+
logging.debug(f"Version {version['versionName']} in project {project['name']} has snippets or the --all flag was used, writing row to {args.csv_file}")
87+
writer.writerow(row)
88+
89+
projects_offset += args.limit
5690

57-
if snippets_present or args.all:
58-
# url_html = f"<a href={version['_meta']['href']}>{version['versionName']}</a>"
59-
url_html = f"=hyperlink(\"{version['_meta']['href']}/components\")"
60-
row = {
61-
'Project Name': project['name'],
62-
'Version Name': version['versionName'],
63-
'Version URL': url_html,
64-
'Has Snippets': snippets_present,
65-
'Un-confirmed': number_unconfirmed,
66-
'Confirmed': number_confirmed,
67-
'Ignored': number_ignored,
68-
'Total': total
69-
}
70-
writer.writerow(row)
7191

0 commit comments

Comments
 (0)