Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/
/.idea/
/examples/.restconfig.json
27 changes: 27 additions & 0 deletions blackduck/HubRestApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ def get_projects(self, limit=100, parameters={}):
url = self._get_projects_url() + self._get_parameter_string(parameters)
headers['Accept'] = 'application/vnd.blackducksoftware.project-detail-4+json'
response = requests.get(url, headers=headers, verify = not self.config['insecure'])
## @SMELL need to gracfully handle not finding the project
jsondata = response.json()
return jsondata

Expand Down Expand Up @@ -1240,6 +1241,32 @@ def get_project_info(self, project_name, link_name):
else:
return {} # nada

def get_project_violation_status(self, project_name, version):

project = self.get_project_by_name(project_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you consider using self.get_project_version_by_name? would replace use of get_project_by_name and the subsequent code to retrieve the version if present, e.g. this method would then become,

def get_project_violation_status(self, project_name, version):
version = self.get_project_version_by_name(project_name, version)
if version:
return version.get('policyStatus', None)
else:
return "{}:{} not found".format(project_name, version)

or something along those lines?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, well I believe I was looking at it this way because it is a more precise error. If I remember get_project_version_by_name will return a non 200 on both missing projects or versions. and I wanted to tell my users what was missing the project or the version or if the server was just miss-behaving

if (project is None):
print ("Project " + project_name + " not found")
return ("NO_PROJECT")

link = self.get_link(project, "versions")
if link:
response = self.execute_get(link)
if response.status_code == 200:
versions_list = json.loads(response.text)
for version_item in versions_list['items']:
print("Got version in file "+version_item['versionName'])
if version == 'empty':
version = version_item['versionName']
if version_item['versionName'] == version:
print("Found " + version)
return version_item['policyStatus']
else:
return ("VERSION_NOT_SCANNED")
else:
return ("SERVER_NOT_RETURNED_200")
else:
return {} # nada

def get_project_roles(self):
all_project_roles = self.get_roles(parameters={"filter":"scope:project"})
return all_project_roles['items']
Expand Down
4 changes: 2 additions & 2 deletions examples/get_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

project = hub.get_project_by_name(args.project_name)

print(json.dumps(project))
print(json.dumps(project, indent=4, sort_keys=True))

if args.link:
print(json.dumps(hub.get_project_info(args.project_name, args.link)))
print(json.dumps(hub.get_project_info(args.project_name, args.link), indent=4))
32 changes: 32 additions & 0 deletions examples/get_project_violations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
Created on Nov 14, 2018

@author: gsnyder

Print a project given its name

'''

from blackduck.HubRestApi import HubInstance

import argparse
import json

parser = argparse.ArgumentParser(description='Use this to check the status of a given version for a given project. If no version is given then it will return the status of the highest version.')

parser.add_argument("--limit")
parser.add_argument("project_name")
parser.add_argument("--version", default="empty", help="If provided, will result in if that version passed policy check")

args = parser.parse_args()

hub = HubInstance()

status=hub.get_project_violation_status(args.project_name, args.version)
print(status)
if(status == "IN_VIOLATION"):
exit(0)
elif(status == "NOT_IN_VIOLATION"):
exit(0)
else:
exit(0)
4 changes: 2 additions & 2 deletions examples/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
if len(versionlist) == 1:
continue
for index in range(len(versionlist) - 1):
print ("index is ".format(index))
print ("index is ", format(index))
va = versionlist[index]
components = hub.get_version_components(va)
codelocations = hub.get_version_codelocations(va)
# hub.execute_delete(va['_meta']['href'])
print ("version {} has {} codelocations".format(va['versionName'], codelocations['totalCount']))
if codelocations > 0:
if codelocations['totalCount'] > 0:
for codelocation in codelocations['items']:
print (codelocation['_meta']['href'])
locationid = codelocation['_meta']['href'].split("/")[5]
Expand Down