Skip to content

Commit e7ce002

Browse files
author
Glenn Snyder
authored
Merge pull request #105 from blackducksoftware/gsnyder/versions-with-unconfirmed-snippets
Adding example to show how to retrieve project-versions with unconfirmed snippets
2 parents 3a150df + 49b6eae commit e7ce002

File tree

3 files changed

+101
-1
lines changed

3 files changed

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

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)