|
| 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