Skip to content

Commit f7844a8

Browse files
A program that will get last scan date, project phase, license risk, operational risk and vulnerability counts for a given project version and print them out at the end.
1 parent c9bd042 commit f7844a8

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import argparse
2+
import csv
3+
import glob
4+
import json
5+
import logging
6+
import os
7+
import shutil
8+
import requests
9+
import sys
10+
import time
11+
import timeit
12+
13+
import pandas
14+
from pandas.errors import EmptyDataError
15+
16+
from blackduck.HubRestApi import HubInstance
17+
18+
parser = argparse.ArgumentParser(
19+
"Get last scan date, project phase, license risk, operational risk and vulnerability counts for a given project version ")
20+
parser.add_argument("project")
21+
parser.add_argument("version")
22+
parser.add_argument('-v', '--verbose', action='store_true', default=False, help='turn on DEBUG logging')
23+
24+
args = parser.parse_args()
25+
26+
27+
def set_logging_level(log_level):
28+
logging.basicConfig(stream=sys.stderr, level=log_level)
29+
30+
31+
if args.verbose:
32+
set_logging_level(logging.DEBUG)
33+
else:
34+
set_logging_level(logging.INFO)
35+
36+
projname = args.project
37+
hub = HubInstance()
38+
rootDir = os.getcwd()
39+
40+
def get_info():
41+
project = hub.get_project_by_name(args.project)
42+
version = hub.get_version_by_name(project, args.version)
43+
phase_and_risk_info = {}
44+
45+
# get last scan date for project version
46+
code_location_url = hub.get_link(version, "codelocations")
47+
response = hub.execute_get(code_location_url)
48+
if response.status_code in [200, 201]:
49+
code_location_info = response.json().get('items', [])
50+
if code_location_info:
51+
updated_at = max([cl['updatedAt'] for cl in code_location_info])
52+
updated_at = updated_at.split(".")[0].split("T")
53+
most_recent_scan = " ".join(updated_at)
54+
phase_and_risk_info.update({'most_recent_scan_date': most_recent_scan})
55+
56+
# get project version phase
57+
project_version_phase = "No phase has been set" if not version['phase'] else version['phase']
58+
phase_and_risk_info.update({'project_version_phase': project_version_phase})
59+
response = hub.execute_get(version['_meta']['links'][2]['href'])
60+
if response.status_code in [200, 201]:
61+
phase_and_risk_info.update(response.json().get('categories'))
62+
else:
63+
phase_and_risk_info.update({"risk_info": "No Info Available"})
64+
print(json.dumps(phase_and_risk_info))
65+
66+
def main():
67+
start = timeit.default_timer()
68+
get_info()
69+
print("Time spent getting info: {} seconds".format(int(timeit.default_timer() - start)))
70+
71+
main()

0 commit comments

Comments
 (0)