Skip to content

Commit ad230b0

Browse files
author
Glenn Snyder
committed
adding sample for updating version settings using the new Client interface
1 parent baaa427 commit ad230b0

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

examples/client/template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
from blackduck import Client
3131

3232
parser = argparse.ArgumentParser("Program description")
33-
parser.add_argument("--base-url", required=True, help="Hub server URL e.g. https://your.blackduck.url")
34-
parser.add_argument("--token-file", dest='token_file', required=True, help="containing access token")
33+
parser.add_argument("base_url", help="Hub server URL e.g. https://your.blackduck.url")
34+
parser.add_argument("token_file", help="containing access token")
3535
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
3636
parser.add_argument("arg1")
3737
args = parser.parse_args()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Copyright (C) 2021 Synopsys, Inc.
5+
http://www.blackducksoftware.com/
6+
7+
Licensed to the Apache Software Foundation (ASF) under one
8+
or more contributor license agreements. See the NOTICE file
9+
distributed with this work for additional information
10+
regarding copyright ownership. The ASF licenses this file
11+
to you under the Apache License, Version 2.0 (the
12+
"License"); you may not use this file except in compliance
13+
with the License. You may obtain a copy of the License at
14+
15+
http://www.apache.org/licenses/LICENSE-2.0
16+
17+
Unless required by applicable law or agreed to in writing,
18+
software distributed under the License is distributed on an
19+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20+
KIND, either express or implied. See the License for the
21+
specific language governing permissions and limitations
22+
under the License.
23+
24+
'''
25+
import argparse
26+
import json
27+
import logging
28+
import sys
29+
30+
from blackduck import Client
31+
from blackduck.constants import PROJECT_VERSION_SETTINGS, VERSION_DISTRIBUTION, VERSION_PHASES
32+
33+
class InvalidVersionSetting(Exception):
34+
pass
35+
36+
37+
def validate_settings(settings):
38+
# convert to a dict
39+
settings_d = {s[0]:s[1] for s in settings}
40+
invalid_settings = [s for s in settings_d.keys() if s not in PROJECT_VERSION_SETTINGS]
41+
if invalid_settings:
42+
raise InvalidVersionSetting(f"The settings - {invalid_settings} - are not in the list of valid settings = {PROJECT_VERSION_SETTINGS}")
43+
44+
if 'phase' in settings_d and settings_d['phase'] not in VERSION_PHASES:
45+
raise InvalidVersionSetting(f"The phase {settings_d['phase']} is not in the list of valid phases = {VERSION_PHASES}")
46+
if 'distribution' in settings_d and settings_d['distribution'] not in VERSION_DISTRIBUTION:
47+
raise InvalidVersionSetting(f"The distribution {settings_d['distribution']} is not in the list of valid distribution types = {VERSION_DISTRIBUTION}")
48+
return settings_d
49+
50+
parser = argparse.ArgumentParser("Update one or more of the settings on a given project-version in Black Duck")
51+
parser.add_argument("base_url", help="Hub server URL e.g. https://your.blackduck.url")
52+
parser.add_argument("token_file", help="containing access token")
53+
parser.add_argument("project_name")
54+
parser.add_argument("version_name")
55+
parser.add_argument("-s", "--settings", required=True, action="append", nargs=2,metavar=('setting', 'value'),
56+
help=f"Settings you can change are {PROJECT_VERSION_SETTINGS}. Possible phases are {VERSION_PHASES}. Possible distribution values are {VERSION_DISTRIBUTION}")
57+
parser.add_argument("--no-verify", dest='verify', action='store_false', help="disable TLS certificate verification")
58+
args = parser.parse_args()
59+
60+
settings_d = validate_settings(args.settings)
61+
62+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stdout, level=logging.DEBUG)
63+
logging.getLogger("requests").setLevel(logging.WARNING)
64+
logging.getLogger("urllib3").setLevel(logging.WARNING)
65+
logging.getLogger("blackduck").setLevel(logging.WARNING)
66+
67+
with open(args.token_file, 'r') as tf:
68+
access_token = tf.readline().strip()
69+
70+
bd = Client(
71+
base_url=args.base_url,
72+
token=access_token,
73+
verify=args.verify
74+
)
75+
76+
params = {
77+
'q': [f"name:{args.project_name}"]
78+
}
79+
projects = [p for p in bd.get_resource('projects', params=params) if p['name'] == args.project_name]
80+
assert len(projects) == 1, f"There should be one, and only one project named {args.project_name}. We found {len(projects)}"
81+
project = projects[0]
82+
83+
params = {
84+
'q': [f"versionName:{args.version_name}"]
85+
}
86+
versions = [v for v in bd.get_resource('versions', project, params=params) if v['versionName'] == args.version_name]
87+
assert len(versions) == 1, f"There should be one, and only one version named {args.version_name}. We found {len(versions)}"
88+
version = versions[0]
89+
90+
logging.debug(f"Found {project['name']}:{version['versionName']}")
91+
92+
for setting, new_value in settings_d.items():
93+
version[setting] = new_value
94+
95+
logging.debug(f"Updating {project['name']}:{version['versionName']} settings to {settings_d}")
96+
try:
97+
r = bd.session.put(version['_meta']['href'], json=version)
98+
r.raise_for_status()
99+
logging.debug(f"updated version settings to the new values ({settings_d})")
100+
except requests.HTTPError as err:
101+
bd.http_error_handler(err)
102+
103+
104+

0 commit comments

Comments
 (0)