|
4 | 4 | import csv
|
5 | 5 | import json
|
6 | 6 | import logging
|
| 7 | +import math |
7 | 8 | import sys
|
8 | 9 | from pprint import pprint
|
9 | 10 |
|
|
36 | 37 | writer = csv.DictWriter(csv_file, fieldnames=columns)
|
37 | 38 | writer.writeheader()
|
38 | 39 |
|
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 |
48 | 43 |
|
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 |
56 | 90 |
|
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) |
71 | 91 |
|
0 commit comments