Skip to content

Commit 7cdab4c

Browse files
authored
Merge pull request #122 from garymsegal/gms/add-components
New example: add_components_to_project_version
2 parents 7d882c7 + facdd1d commit 7cdab4c

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
#
4+
# This script manually adds compnents to an existing project/version.
5+
# The use case is to transfer a BOM from one Black Duck to antoher. The BOM is read from the JSON
6+
# output of get_bom_components.py. To avoid changing .restconfig.json when swtiching between
7+
# Black Duck instances, this script takes the Black Duck credentials from the command line.
8+
#
9+
# Example Use case to tansfer a BOM from a source Black Duck to a destination Black Duck.
10+
# Retrieve BOM for for a project-version on the source Black Duck (.restconfig.json identifies source Black Duck)
11+
# get_bom_components.py -l <LIMIT> <PROJECT_NAME> <VERSION> > output.json
12+
#
13+
# If the project-version is not present on the destination Black Duck, create it.
14+
#
15+
# add_components_to_project_version.py <PROJECT_NAME> <VERSION> output.json <DESTINATION BASE URL> <API_TOKEN>
16+
# Note: BASE_URL includes https. For example, https:\\example.blackduck.synopsys.com
17+
#
18+
#
19+
20+
21+
import argparse
22+
import logging
23+
import sys
24+
import json
25+
26+
from blackduck.HubRestApi import HubInstance, object_id
27+
28+
parser = argparse.ArgumentParser("Add components from JSON output of get_bom_components to the selected project-version on the desired Black Duck host")
29+
parser.add_argument("project_name", help="Name of destination project ")
30+
parser.add_argument("version", help="Version of destination project")
31+
parser.add_argument("component_file",help="JSON file with component list from get_bom_components")
32+
parser.add_argument("url_base",help="Base URL of Black Duck host. https://blackduck-hostname")
33+
parser.add_argument("api_token", help="API token for Black Duck host")
34+
35+
36+
args = parser.parse_args()
37+
38+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
39+
logging.getLogger("requests").setLevel(logging.WARNING)
40+
logging.getLogger("urllib3").setLevel(logging.WARNING)
41+
42+
with open(args.component_file) as f:
43+
components = json.load(f)
44+
f.close()
45+
46+
url_base = args.url_base
47+
api_token = args.api_token
48+
49+
hub = HubInstance(url_base, api_token=api_token, insecure=True, write_config_flag=False)
50+
51+
project_version = hub.get_project_version_by_name(args.project_name, args.version)
52+
53+
components_api_url = hub.get_link(project_version, "components")
54+
55+
for component in components:
56+
# Check if the version is known for this component and get the component/version or component URL
57+
if 'componentVersion' in component:
58+
component_url = component['componentVersion']
59+
versionName = component['componentVersionName']
60+
else:
61+
component_url = component['component']
62+
versionName= "?"
63+
64+
post_data = {"component": component_url}
65+
66+
response = hub.execute_post(components_api_url, data=post_data)
67+
68+
if response.status_code == 200:
69+
logging.info(f"Successfully added {component['componentName']}, {versionName} to project {args.project_name}, {args.version}")
70+
else:
71+
logging.error(f"Failed to add {component['componentName']}, {versionName} to project {args.project_name}, {args.version}. Status code: {response.status_code}")

0 commit comments

Comments
 (0)