|
26 | 26 |
|
27 | 27 | from blackduck.HubRestApi import HubInstance, object_id
|
28 | 28 |
|
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 |
| - |
52 | 29 |
|
53 | 30 | 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") |
56 | 33 | parser.add_argument("-n", "--newer_than",
|
57 | 34 | default=None,
|
58 | 35 | type=str,
|
|
81 | 58 |
|
82 | 59 | hub = HubInstance()
|
83 | 60 | current_user = hub.get_current_user()
|
| 61 | +version = hub.get_project_version_by_name(args.project, args.version) |
| 62 | +version_url = version['_meta']['href'] |
84 | 63 |
|
85 | 64 | # 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 |
87 | 66 | if args.system:
|
88 | 67 | notifications_url = "{}/api/notifications".format(hub.get_urlbase())
|
89 | 68 | else:
|
|
93 | 72 | notifications_url, args.limit)
|
94 | 73 |
|
95 | 74 | if newer_than:
|
| 75 | + # add to the URL to include startDate |
96 | 76 | start_date = newer_than.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
97 | 77 | notifications_url += "&startDate=" + start_date
|
98 | 78 |
|
| 79 | +logging.debug(f"Retrieving BOM computed notifications using {notifications_url}") |
99 | 80 | bom_computed_notifications = hub.execute_get(notifications_url).json().get('items', [])
|
100 | 81 |
|
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)) |
112 | 85 |
|
113 | 86 | print(json.dumps(bom_computed_notifications))
|
0 commit comments