Skip to content

Commit 550a975

Browse files
authored
Merge pull request #13 from blackducksoftware/gsnyder/improving-affected-projects-retrieval
improving example code to retrieve affected project info given a CVE
2 parents d484605 + efd1575 commit 550a975

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

examples/print_vulnerability_affected_projects.py

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
their names, version(s), development phase, distribution, and last BOM update date
88
'''
99
import argparse
10-
from beautifultable import BeautifulTable
10+
# pip install terminaltables
11+
from terminaltables import AsciiTable
1112

1213
from blackduck.HubRestApi import HubInstance
1314
from pprint import pprint
@@ -21,24 +22,54 @@
2122
affected_projects = hub.get_vulnerability_affected_projects(args.vulnerability.upper())
2223

2324
if 'totalCount' in affected_projects and affected_projects['totalCount'] > 0:
24-
table = BeautifulTable()
25-
table.column_headers = ["project-name", "version", "phase", "distribution", "last-bom-update"]
25+
ttable = [[
26+
"project-name",
27+
"version",
28+
"phase",
29+
"distribution",
30+
"last-bom-update",
31+
"Owner Name",
32+
"Owner email"]]
33+
2634
for affected_project in affected_projects['items']:
27-
project_id = affected_project['project']['id']
28-
version_id = affected_project['release']['id']
35+
# Get the Owner info for the project
36+
project_json = hub.get_project_by_id(affected_project['project']['id'])
37+
if 'projectOwner' in project_json:
38+
owner_response = hub.execute_get(project_json['projectOwner'])
39+
owner_json = owner_response.json()
40+
if 'firstName' in owner_json and 'lastName' in owner_json:
41+
owner_name = owner_json['firstName'] + ' ' + owner_json['lastName']
42+
else:
43+
owner_name = "None supplied"
44+
if 'email' in owner_json:
45+
owner_email = owner_json['email']
46+
else:
47+
owner_email = "None supplied"
48+
else:
49+
owner_name = owner_email = "None supplied"
2950

3051
project_name = affected_project['project']['name']
3152
version = affected_project['release']['version']
3253

3354
# Development phase does not appear to be in the payload returned by the affected projects
3455
# endpoint so we need to fetch it from the project-version endpoint
56+
project_id = affected_project['project']['id']
57+
version_id = affected_project['release']['id']
58+
3559
project_version_info = hub.get_version_by_id(project_id, version_id)
3660

3761
phase = project_version_info['phase']
3862
distribution = project_version_info['distribution']
3963
last_bom_update = project_version_info['lastBomUpdateDate']
4064

41-
table.append_row([project_name, version, phase, distribution, last_bom_update])
42-
print(table)
65+
ttable.append([
66+
project_name,
67+
version,
68+
phase,
69+
distribution,
70+
last_bom_update,
71+
owner_name,
72+
owner_email])
73+
print(AsciiTable(ttable).table)
4374
else:
4475
print("No affected projects found for this vulnerability {}".format(args.vulnerability.upper()))

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# For the library itself
22
requests==2.20.0
33

4+
# for examples printing tables to the terminal
5+
terminaltables
6+
47
# for unit testing
58
pytest
69
requests-mock

0 commit comments

Comments
 (0)