Skip to content

Commit 3119bc4

Browse files
author
Glenn Snyder
committed
adding example to show using journal endpoint
1 parent ad155c3 commit 3119bc4

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

examples/client/quickstart.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,9 @@
3030
pprint(bd.list_resources(project))
3131
for version in bd.get_resource('versions', project):
3232
print(f"Version: {version['versionName']}")
33-
print("Exiting after printing first project and version")
34-
quit(0)
33+
for bom_component in bd.get_resource('components', version):
34+
print(f"BOM component: {bom_component['componentName']}:{bom_component['componentVersionName']}")
35+
# print(f"Version list_resources():")
36+
# pprint(bd.list_resources(version))
37+
# print("Exiting after printing first project and version")
38+
# quit(0)

examples/get_journal_events.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import json
5+
import logging
6+
import sys
7+
8+
from blackduck.HubRestApi import HubInstance, object_id
9+
10+
journal_filters = [
11+
"journalObjectType",
12+
"journalObjectNames",
13+
"journalTriggerNames",
14+
"journalDate",
15+
"journalAction",
16+
"journalTriggerType"
17+
]
18+
19+
# A partial list of the journal actions (aka events) we can filter on
20+
# To see a complete list go into the GUI, navigate to the activity page and
21+
# select the Events filter
22+
journal_actions = [
23+
'component_added',
24+
'component_deleted',
25+
'kb_component_deprecated',
26+
'kb_component_version_deprecated'
27+
]
28+
29+
# Sample invocation
30+
#
31+
# python examples/get_journal_events.py protex_tutorial -v 1.0 -f journalAction:component_added -f journalAction:component_deleted > journal_comp_adds_deletes.json
32+
#
33+
34+
parser = argparse.ArgumentParser("Retreive journal events for projects or project-versions")
35+
parser.add_argument("project")
36+
parser.add_argument("-v", "--version")
37+
parser.add_argument("-f", "--filter", nargs='+', action='append', help=f"Use the following filter keys to narrow the events returned ({journal_filters}). Specify the filter in the format filterKey:filterValue. ")
38+
39+
# TODO: Add a check on the journalFilterKey:journalFilterValue
40+
41+
args = parser.parse_args()
42+
43+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
44+
logging.getLogger("requests").setLevel(logging.WARNING)
45+
logging.getLogger("urllib3").setLevel(logging.WARNING)
46+
47+
hub = HubInstance()
48+
49+
project_or_version = hub.get_project_by_name(args.project)
50+
journal_url = hub.get_link(project_or_version, "project-journal")
51+
if args.version:
52+
version = hub.get_project_version_by_name(args.project, args.version)
53+
version_id = object_id(version)
54+
journal_url = f"{journal_url}/versions/{version_id}"
55+
56+
if args.filter:
57+
filter_str = "&filter=".join([f[0] for f in args.filter])
58+
journal_url = f"{journal_url}?limit=9999&filter={filter_str}"
59+
60+
journal_events = hub.execute_get(journal_url).json()
61+
62+
print(json.dumps(journal_events))

0 commit comments

Comments
 (0)