|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | + |
| 6 | +from blackduck.HubRestApi import HubInstance |
| 7 | + |
| 8 | + |
| 9 | +parser = argparse.ArgumentParser("Write the complete file path to a custom field for BOM component file matches for the given project and version") |
| 10 | +parser.add_argument("project_name") |
| 11 | +parser.add_argument("version") |
| 12 | + |
| 13 | +args = parser.parse_args() |
| 14 | + |
| 15 | +custom_field_id = "4" |
| 16 | + |
| 17 | + |
| 18 | +custom_headers = {'Accept': 'application/vnd.blackducksoftware.bill-of-materials-6+json'} |
| 19 | + |
| 20 | +hub = HubInstance() |
| 21 | + |
| 22 | + |
| 23 | +project = hub.get_project_by_name(args.project_name) |
| 24 | +version = hub.get_version_by_name(project, args.version) |
| 25 | + |
| 26 | +# Get all BOM Components |
| 27 | +bom_components = hub.get_version_components(version) |
| 28 | + |
| 29 | +# Total Components for given project and Version |
| 30 | +print("Total Components: ", len(bom_components['items'])) |
| 31 | + |
| 32 | +# Iterate through each Bom Component |
| 33 | +for bomComponent in bom_components['items']: |
| 34 | + |
| 35 | + # Parse the JSON to build the custom field URL |
| 36 | + customFieldURL = bomComponent['_meta']['href'] + "/custom-fields/"+custom_field_id |
| 37 | + |
| 38 | + # Check no of Origins per component |
| 39 | + if len(bomComponent['origins']) == 0: |
| 40 | + |
| 41 | + pass |
| 42 | + |
| 43 | + else: |
| 44 | + |
| 45 | + for origin in bomComponent['origins']: |
| 46 | + |
| 47 | + matched_files_url = origin['_meta']['links'][1]['href'] |
| 48 | + matched_files_json = hub.execute_get(matched_files_url).json() |
| 49 | + |
| 50 | + # Iterate through each components matched files to build a comma separated string of source paths |
| 51 | + |
| 52 | + if matched_files_json['totalCount'] > 0: |
| 53 | + |
| 54 | + component_source_paths = '' |
| 55 | + |
| 56 | + for file_path in range(0, len(matched_files_json['items'])): |
| 57 | + path = matched_files_json['items'][file_path]['filePath']['path'] |
| 58 | + code_location = matched_files_json['items'][file_path]['_meta']['links'][0]['href'] |
| 59 | + archive_Context = (matched_files_json['items'][file_path]['filePath']['archiveContext']).replace("!", '') |
| 60 | + code_location_json = hub.execute_get(code_location).json() |
| 61 | + code_location_name = code_location_json['name'] |
| 62 | + component_source_paths = component_source_paths + ',' + code_location_name+"/"+(archive_Context)[:-1]+path |
| 63 | + |
| 64 | + # Wrapping component_source_paths in double quotes |
| 65 | + component_source_paths = '"'+component_source_paths[1::]+'"' |
| 66 | + |
| 67 | + # Creating a dictionary to save component_source_paths to key "values" |
| 68 | + data = {"values": [component_source_paths]} |
| 69 | + |
| 70 | + # Execute PUT call |
| 71 | + response = hub.execute_put(customFieldURL, data, custom_headers) |
| 72 | + |
| 73 | + # Logging |
| 74 | + if response.status_code != 200: |
| 75 | + print(bomComponent['componentName'] + ' | ' + bomComponent['componentVersionName'] + ' | '+ 'No of Origins: '+str(len(bomComponent['origins'])) + ' failed to write to the custom field \n' ) |
| 76 | + else: |
| 77 | + logging.info("Successfully updated custom field for bom component {} , version {}".format( |
| 78 | + bomComponent['componentName'], bomComponent['componentVersionName'])) |
0 commit comments