Skip to content

Commit 768d3e3

Browse files
author
Glenn Snyder
committed
fixing issue when getting snippet counts and adding initial version of program to get all versions with un-confirmed snippets
1 parent 3a150df commit 768d3e3

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+

examples/get_snippet_match_counts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424

2525
snippet_counts_url = version['_meta']['href'] + "/snippet-counts"
2626

27-
snippet_counts = hub.execute_get(snippet_counts_url).json()
27+
#
28+
# As of v2020.6.0 the Content-Type returned is application/vnd.blackducksoftware.internal-1+json;charset=UTF-8
29+
# so I'm accepting any content type to allow the GET to work flexibly
30+
#
31+
custom_headers = {'Accept': '*/*'}
32+
snippet_counts = hub.execute_get(snippet_counts_url, custom_headers=custom_headers).json()
2833

2934
del snippet_counts['_meta']
3035

out.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Project Name,Version Name,Version URL,Has Snippets,Un-confirmed,Confirmed,Ignored,Total
2+
protex,snippets,"=hyperlink(""https://ec2-18-217-189-8.us-east-2.compute.amazonaws.com/api/projects/2871cddb-01b9-4bad-a5e5-38884ab3b6da/versions/ce2832c0-371b-4689-94d8-1c88faeed038/components"")",True,25,22,0,47
3+
protex,snippets-diff-path,"=hyperlink(""https://ec2-18-217-189-8.us-east-2.compute.amazonaws.com/api/projects/2871cddb-01b9-4bad-a5e5-38884ab3b6da/versions/6c5a2d92-d6bd-4d88-8697-a68d29284fae/components"")",True,47,0,0,47
4+
protex,snippets2,"=hyperlink(""https://ec2-18-217-189-8.us-east-2.compute.amazonaws.com/api/projects/2871cddb-01b9-4bad-a5e5-38884ab3b6da/versions/d940b4f7-7e24-43a1-88c2-5f4d89d7bf7e/components"")",True,25,22,0,47

0 commit comments

Comments
 (0)