Skip to content

Commit 0808077

Browse files
author
Glenn Snyder
authored
Merge pull request #64 from blackducksoftware/gsnyder/matched-files-and-snippets
Gsnyder/matched files and snippets
2 parents 6b0ecbd + 4e0c111 commit 0808077

File tree

5 files changed

+11
-126
lines changed

5 files changed

+11
-126
lines changed

blackduck/HubRestApi.py

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -597,17 +597,6 @@ def _check_version_compatibility(self):
597597
if int(self.bd_major_version) < 2018:
598598
raise UnsupportedBDVersion("The BD major version {} is less than the minimum required major version {}".format(self.bd_major_version, 2018))
599599

600-
def get_file_bom_entries(self, hub_release_id, limit=100):
601-
self._check_version_compatibility()
602-
paramstring = self.get_limit_paramstring(limit)
603-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
604-
url = "{}/v1/releases/{}/file-bom-entries{}".format(self.get_apibase(), hub_release_id)
605-
url += paramstring
606-
logging.debug("GET {}".format(url))
607-
response = self.execute_get(url)
608-
jsondata = response.json()
609-
return jsondata
610-
611600
def get_file_matches_for_bom_component(self, bom_component, limit=1000):
612601
self._check_version_compatibility()
613602
url = self.get_link(bom_component, "matched-files")
@@ -617,116 +606,6 @@ def get_file_matches_for_bom_component(self, bom_component, limit=1000):
617606
jsondata = response.json()
618607
return jsondata
619608

620-
def get_snippet_bom_entries(self, project_id, version_id, reviewed=False, included=False, limit=100, offset=0):
621-
self._check_version_compatibility()
622-
paramstring = "?limit=" + str(limit) + "&offset=" + \
623-
str(offset) + "&filter=bomReviewStatus:" + str(reviewed).lower() + "&filter=bomInclusion:" + str(included).lower()
624-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
625-
path = "{}/internal/projects/{}/versions/{}/snippet-bom-entries".format(self.get_apibase(), project_id, version_id)
626-
url = path + paramstring
627-
response = self.execute_get(url)
628-
jsondata = response.json()
629-
return jsondata
630-
631-
def ignore_snippet_bom_entry(self, hub_version_id, snippet_bom_entry):
632-
self._check_version_compatibility()
633-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
634-
url = "{}/v1/releases/{}/snippet-bom-entries".format(self.get_apibase(), hub_version_id)
635-
body = self.get_ignore_snippet_json(snippet_bom_entry)
636-
response = self.execute_put(url, body)
637-
jsondata = response.json()
638-
return jsondata
639-
640-
def get_ignore_snippet_json(self, snippet_bom_entry):
641-
self._check_version_compatibility()
642-
for cur_fileSnippetBomComponents in snippet_bom_entry['fileSnippetBomComponents']:
643-
cur_fileSnippetBomComponents['ignored'] = True
644-
return [snippet_bom_entry]
645-
646-
def confirm_snippet_bom_entry(self, hub_version_id, snippet_bom_entry):
647-
self._check_version_compatibility()
648-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
649-
url = "{}/v1/releases/{}/snippet-bom-entries".format(self.get_apibase(), hub_version_id)
650-
body = self.get_confirm_snippet_json(snippet_bom_entry)
651-
response = self.execute_put(url, body)
652-
jsondata = response.json()
653-
return jsondata
654-
655-
def get_confirm_snippet_json(self, snippet_bom_entry):
656-
self._check_version_compatibility()
657-
for cur_fileSnippetBomComponents in snippet_bom_entry['fileSnippetBomComponents']:
658-
cur_fileSnippetBomComponents['reviewStatus'] = 'REVIEWED'
659-
cur_fileSnippetBomComponents['ignored'] = False
660-
return [snippet_bom_entry]
661-
662-
def edit_snippet_bom_entry(self, hub_version_id, snippet_bom_entry, new_kb_component):
663-
self._check_version_compatibility()
664-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
665-
url = "{}/v1/releases/{}/snippet-bom-entries".format(self.get_apibase(), hub_version_id)
666-
body = self.get_edit_snippet_json(snippet_bom_entry, new_kb_component)
667-
response = self.execute_put(url, body)
668-
jsondata = response.json()
669-
return jsondata
670-
671-
def get_edit_snippet_json(self, snippet_bom_entry, new_kb_component):
672-
self._check_version_compatibility()
673-
assert 'fileSnippetBomComponents' in snippet_bom_entry
674-
assert len(snippet_bom_entry['fileSnippetBomComponents']) == 1, "We can only edit the component info for one snippet match at a time"
675-
676-
# TODO: Handle case where either the component from snippet_bom_entry OR new_kb_component does not have a version?
677-
snippet_component_info = snippet_bom_entry['fileSnippetBomComponents'][0]
678-
snippet_component_info['project']['id'] = new_kb_component['component'].split("/")[-1]
679-
snippet_component_info['release']['id'] = new_kb_component['componentVersion'].split("/")[-1]
680-
return [snippet_bom_entry]
681-
682-
def get_alternate_matches_for_snippet(self, project_id, version_id, snippet_object):
683-
self._check_version_compatibility()
684-
version_bom_entry_id = snippet_object['fileSnippetBomComponents'][0]['versionBomEntryId']
685-
686-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
687-
url = "{}/internal/projects/{}/versions/{}/alternate-snippet-matches/{}".format(
688-
self.get_apibase(), project_id, version_id, version_bom_entry_id)
689-
response = self.execute_get(url)
690-
jsondata = response.json()
691-
alternate_matches = list()
692-
for snippet_bom_components_d in jsondata['snippetMatches']:
693-
for snippet_bom_component in snippet_bom_components_d['snippetBomComponents']:
694-
alternate_matches.append(snippet_bom_component)
695-
return alternate_matches
696-
697-
def find_matching_alternative_snippet_match(self, project_id, version_id, snippet_object, kb_component):
698-
# Given a KB component, find the matching alternative snippet match for a given snippet BOM entry
699-
# Returns None if no match was found
700-
kb_component_id = kb_component['component'].split("/")[-1]
701-
# TODO: handle cases where there is no version supplied?
702-
kb_component_version_id = kb_component['componentVersion'].split("/")[-1]
703-
for alternative_match in self.get_alternate_matches_for_snippet(project_id, version_id, snippet_object):
704-
alternative_match_component_id = alternative_match['project']['id']
705-
alternative_match_component_version_id = alternative_match['release']['id']
706-
if kb_component_id == alternative_match_component_id and kb_component_version_id == alternative_match_component_version_id:
707-
return alternative_match
708-
709-
def _generate_new_match_selection(self, original_snippet_match, new_component_match):
710-
# Merge the values from new_component_match into the origingal_snippet_match
711-
# Note: Must do the merge to preserver other key/value pairs in the original_snippet_match (e.g. ignored, reviewStatus, versionBomComponentId)
712-
# TODO: Can there ever be more than one item in fileSnippetBomComponents?
713-
for k in original_snippet_match['fileSnippetBomComponents'][0].keys():
714-
if k in new_component_match:
715-
original_snippet_match['fileSnippetBomComponents'][0][k] = new_component_match[k]
716-
return [original_snippet_match]
717-
718-
def update_snippet_match(self, version_id, current_snippet_match, new_snippet_match_component):
719-
# Update the (snippet) component selection for a given snippet match
720-
# Assumption: new_snippet_match_component is from one of the alternate matches listed for the file snippet match
721-
self._check_version_compatibility()
722-
headers = self.get_headers()
723-
headers['ContentType'] = "application/json"
724-
# Using internal API - see https://jira.dc1.lan/browse/HUB-18270: Make snippet API calls for ignoring, confirming snippet matches public
725-
url = "{}/v1/releases/{}/snippet-bom-entries".format(self.get_apibase(), version_id)
726-
body = self._generate_new_match_selection(current_snippet_match, new_snippet_match_component)
727-
response = self.execute_put(url, body)
728-
jsondata = response.json()
729-
return jsondata
730609

731610
##
732611
#

blackduck/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = (0, 0, 35)
1+
VERSION = (0, 0, 36)
22

33
__version__ = '.'.join(map(str, VERSION))

examples/assign_role_to_user_group.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424

2525
args = parser.parse_args()
2626

27-
user_groups = hub.get_user_groups(parameters={'q':args.group_name})
27+
user_groups = hub.get_user_groups(parameters={'q':'name:{}'.format(args.group_name)})
2828

2929
if user_groups['totalCount'] == 1:
3030
user_group = user_groups['items'][0]
31+
else:
32+
user_group = None
3133

3234
if user_group:
3335
if args.role == 'All':
@@ -42,3 +44,5 @@
4244
print("Failed to assign role {} to group {} due to status code 412. Has the role already been assigned?".format(role_to_assign, args.group_name))
4345
else:
4446
print("Failed to assign role {} to group {}. status code: {}".format(role_to_assign, args.group_name, response.status_code))
47+
else:
48+
print("Didn't find user group {}".format(args.group_name))

examples/create_custom_field.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030

3131
initial_options = [{"label": io[0], "position": io[1]} for io in args.initial_options]
3232

33-
import pdb; pdb.set_trace()
34-
3533
response = hub.create_cf(
3634
args.object,
3735
args.field_type,

examples/get_bom_component_vulnerability_info.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env python
22

3+
import http.client
4+
http.client._MAXHEADERS = 1000
5+
36
import argparse
47
import copy
58
from datetime import datetime
@@ -68,11 +71,12 @@
6871
response = hub.execute_get(vulnerable_components_url, custom_headers=custom_headers)
6972
vulnerable_bom_components = response.json().get('items', [])
7073

71-
for vuln in vulnerable_bom_components:
74+
for i, vuln in enumerate(vulnerable_bom_components):
7275
source = vuln['vulnerabilityWithRemediation']['source']
7376
vuln_name = vuln['vulnerabilityWithRemediation']['vulnerabilityName']
7477

7578
# Retrieve additional details about the vulnerability
79+
logging.debug("Retrieving additional details regarding vuln {}, i={}".format(vuln_name, i))
7680
vuln_url = hub.get_apibase() + "/vulnerabilities/{}".format(vuln_name)
7781
vuln_details_response = hub.execute_get(vuln_url, custom_headers={'Accept': 'application/json'})
7882
vuln_details = vuln_details_response.json()

0 commit comments

Comments
 (0)