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