|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +import json |
| 6 | +import logging |
| 7 | +import math |
| 8 | +import sys |
| 9 | +from pprint import pprint |
| 10 | + |
| 11 | +from blackduck.HubRestApi import HubInstance, object_id |
| 12 | + |
| 13 | +parser = argparse.ArgumentParser("Get a list of project-versions with un-confirmed snippet matches") |
| 14 | +parser.add_argument("csv_file", help="Provide the name of the CSV file to dump the list of project-versions into") |
| 15 | +parser.add_argument("-l", "--limit", type=int, default=100, help="Set the limit on the number of projects to retrieve at a time") |
| 16 | +parser.add_argument("-a", "--all", action='store_true', help="Output all the project-versions regardless of whether they have snippet matches, or un-confirmed snippet matches") |
| 17 | +args = parser.parse_args() |
| 18 | + |
| 19 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 20 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 21 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 22 | + |
| 23 | +hub = HubInstance() |
| 24 | + |
| 25 | +columns = [ |
| 26 | + 'Project Name', |
| 27 | + 'Version Name', |
| 28 | + 'Version URL', |
| 29 | + 'Has Snippets', |
| 30 | + 'Un-confirmed', |
| 31 | + 'Confirmed', |
| 32 | + 'Ignored', |
| 33 | + 'Total' |
| 34 | +] |
| 35 | + |
| 36 | +with open(args.csv_file, 'w') as csv_file: |
| 37 | + writer = csv.DictWriter(csv_file, fieldnames=columns) |
| 38 | + writer.writeheader() |
| 39 | + |
| 40 | + num_projects = math.inf |
| 41 | + projects_offset = 0 |
| 42 | + first = True |
| 43 | + |
| 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 |
| 90 | + |
| 91 | + |
0 commit comments