Skip to content

Commit 9ff0964

Browse files
authored
Merge pull request #86 from suyogpotnis/sp_blackduck
"Write Component source paths to custom field"
2 parents 1ebcf49 + 94cff05 commit 9ff0964

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Date: 04/07/2020
5+
Requirements:
6+
1. Define a Custom Field at the BOM Component Level from the Black Duck UI under Custom Fields Management.
7+
2. The field type needs to be "Text Area"
8+
9+
Script: Executing the script will write source paths of each component from a Project-Version to the defined Custom Field.
10+
Executing the script again will simply overwrite the existing values with new ones. This is particularly useful if an end-user
11+
is using REST APIs and would like to pull file paths.
12+
13+
"""
14+
15+
import argparse
16+
import logging
17+
18+
from blackduck.HubRestApi import HubInstance
19+
20+
21+
parser = argparse.ArgumentParser("Write the complete file path to a custom field for BOM component file matches for the given project and version")
22+
parser.add_argument("project_name")
23+
parser.add_argument("version")
24+
25+
args = parser.parse_args()
26+
27+
# Set the Custom Field Id
28+
custom_field_id = ""
29+
30+
31+
custom_headers = {'Accept': 'application/vnd.blackducksoftware.bill-of-materials-6+json'}
32+
33+
hub = HubInstance()
34+
35+
36+
project = hub.get_project_by_name(args.project_name)
37+
version = hub.get_version_by_name(project, args.version)
38+
39+
# Custom Field Id Check
40+
if custom_field_id == "":
41+
print("Set the custom_field_id variable above with the correct Id and try executing the script again.")
42+
exit()
43+
44+
# Get all BOM Components
45+
bom_components = hub.get_version_components(version)
46+
47+
# Total Components for given project and Version
48+
print("Total Components: ", len(bom_components['items']))
49+
50+
# Iterate through each Bom Component
51+
for bomComponent in bom_components['items']:
52+
53+
# Parse the JSON to build the custom field URL
54+
customFieldURL = bomComponent['_meta']['href'] + "/custom-fields/"+custom_field_id
55+
56+
# Check no of Origins per component
57+
if len(bomComponent['origins']) == 0:
58+
59+
pass
60+
61+
else:
62+
63+
for origin in bomComponent['origins']:
64+
65+
matched_files_url = origin['_meta']['links'][1]['href']
66+
matched_files_json = hub.execute_get(matched_files_url).json()
67+
68+
# Iterate through each components matched files to build a comma separated string of source paths
69+
70+
if matched_files_json['totalCount'] > 0:
71+
72+
component_source_paths = ''
73+
74+
for file_path in range(0, len(matched_files_json['items'])):
75+
path = matched_files_json['items'][file_path]['filePath']['path']
76+
code_location = matched_files_json['items'][file_path]['_meta']['links'][0]['href']
77+
archive_Context = (matched_files_json['items'][file_path]['filePath']['archiveContext']).replace("!", '')
78+
code_location_json = hub.execute_get(code_location).json()
79+
code_location_name = code_location_json['name']
80+
component_source_paths = component_source_paths + ',' + code_location_name+"/"+(archive_Context)[:-1]+path
81+
82+
# Wrapping component_source_paths in double quotes
83+
component_source_paths = '"'+component_source_paths[1::]+'"'
84+
85+
# Creating a dictionary to save component_source_paths to key "values"
86+
data = {"values": [component_source_paths]}
87+
88+
# Execute PUT call
89+
response = hub.execute_put(customFieldURL, data, custom_headers)
90+
91+
# Logging
92+
if response.status_code != 200:
93+
print(bomComponent['componentName'] + ' | ' + bomComponent['componentVersionName'] + ' | '+ 'No of Origins: '+str(len(bomComponent['origins'])) + ' failed to write to the custom field \n' )
94+
else:
95+
logging.info("Successfully updated custom field for bom component {} , version {}".format(
96+
bomComponent['componentName'], bomComponent['componentVersionName']))

0 commit comments

Comments
 (0)