Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding check for project-version-violations and report back #55

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -697,6 +697,7 @@ def get_projects(self, limit=100, parameters={}):
headers['Accept'] = 'application/vnd.blackducksoftware.project-detail-4+json'
logger.debug(f"Retrieving projects using url {url}")
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 @@ -1255,6 +1256,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):
logging.debug("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']:
logging.debug("Got version in file "+version_item['versionName'])
if version == 'empty':
version = version_item['versionName']
if version_item['versionName'] == version:
logging.debug("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)
10 changes: 5 additions & 5 deletions examples/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
print ("total projects found: %s" % projects['totalCount'])

for project in projects['items']:
print (project['name'])
print(project['name'])
versions = hub.get_project_versions(project)
print ("\t versions found %s" % versions['totalCount'])
print("\t versions found %s" % versions['totalCount'])
versionlist = versions['items']
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:
print("version {} has {} codelocations".format(va['versionName'], codelocations['totalCount']))
if codelocations['totalCount'] > 0:
jmvanryn marked this conversation as resolved.
Show resolved Hide resolved
for codelocation in codelocations['items']:
print (codelocation['_meta']['href'])
locationid = codelocation['_meta']['href'].split("/")[5]
Expand Down