Skip to content

Commit c162f48

Browse files
author
Glenn Snyder
committed
narrowing to a specific project-version
1 parent 710fcd6 commit c162f48

File tree

1 file changed

+10
-37
lines changed

1 file changed

+10
-37
lines changed

examples/get_bom_computed_notifications.py

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,10 @@
2626

2727
from blackduck.HubRestApi import HubInstance, object_id
2828

29-
#
30-
# Example usage:
31-
#
32-
# To get all the vulnerability notices,
33-
# python examples/get_vulnerability_notifications.py > all_vuln_notifications.json
34-
#
35-
# To get all the vulnerability notices and save the date/time of the last run,
36-
# python examples/get_vulnerability_notifications.py -s > all_vuln_notifications.json
37-
#
38-
# To get all the vulnerability notices since the last run,
39-
# python examples/get_vulnerability_notifications.py -n `cat .last_run` > all_vuln_notifications.json
40-
#
41-
# To get all the vulnerability notices since a date/time,
42-
# python examples/get_vulnerability_notifications.py -n "March 29, 2019 12:00" > since_mar_29_at_noon_vuln_notifications.json
43-
#
44-
# To get all the vulnerability notices for a given project,
45-
# python examples/get_vulnerability_notifications.py -p my-project > all_vuln_notifications_for_my_project.json
46-
#
47-
# To get all the vulnerability notices for a given project and version,
48-
# python examples/get_vulnerability_notifications.py -p my-project -v 1.0 > all_vuln_notifications_for_my_project_v1.0.json
49-
#
50-
#
51-
5229

5330
parser = argparse.ArgumentParser("Retreive BOM computed notifications")
54-
parser.add_argument("-p", "--project", help="If supplied, filter the notifications to this project")
55-
parser.add_argument("-v", "--version", help="If supplied, filter the notifications to this version (requires a project)")
31+
parser.add_argument("project", help="The name of the project")
32+
parser.add_argument("version", help="The name of the version")
5633
parser.add_argument("-n", "--newer_than",
5734
default=None,
5835
type=str,
@@ -81,9 +58,11 @@
8158

8259
hub = HubInstance()
8360
current_user = hub.get_current_user()
61+
version = hub.get_project_version_by_name(args.project, args.version)
62+
version_url = version['_meta']['href']
8463

8564
# Construct the URL to either pull from the system or user account scope,
86-
# and then narrow the search to only include BOM computed
65+
# and then narrow the search to only include BOM computed notifications
8766
if args.system:
8867
notifications_url = "{}/api/notifications".format(hub.get_urlbase())
8968
else:
@@ -93,21 +72,15 @@
9372
notifications_url, args.limit)
9473

9574
if newer_than:
75+
# add to the URL to include startDate
9676
start_date = newer_than.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
9777
notifications_url += "&startDate=" + start_date
9878

79+
logging.debug(f"Retrieving BOM computed notifications using {notifications_url}")
9980
bom_computed_notifications = hub.execute_get(notifications_url).json().get('items', [])
10081

101-
# if newer_than:
102-
# bom_computed_notifications = list(
103-
# filter(lambda n: timestring.Date(n['createdAt']) > newer_than, bom_computed_notifications))
104-
if args.project:
105-
bom_computed_notifications = list(
106-
filter(lambda n: args.project in [apv['projectName'] for apv in n['content']['affectedProjectVersions']],
107-
bom_computed_notifications))
108-
if args.version:
109-
bom_computed_notifications = list(
110-
filter(lambda n: args.version in [apv['projectVersionName'] for apv in n['content']['affectedProjectVersions']],
111-
bom_computed_notifications))
82+
# filter to include only those notification pertaining to the specified project, version
83+
bom_computed_notifications = list(
84+
filter(lambda n: version_url == n['content']['projectVersion'], bom_computed_notifications))
11285

11386
print(json.dumps(bom_computed_notifications))

0 commit comments

Comments
 (0)