Skip to content

Commit 4fcf449

Browse files
author
Glenn Snyder
committed
adding samples showing how to read custom field value on a project version
1 parent 6d6950a commit 4fcf449

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
32+
parser = argparse.ArgumentParser("Get custom field value from a project version")
33+
parser.add_argument("bd_url")
34+
parser.add_argument("user_api_token")
35+
parser.add_argument("project")
36+
parser.add_argument("version")
37+
parser.add_argument("field_label")
38+
parser.add_argument("-v", "--verify", default=False, help="Set this to verify the SSL certificate. In production you should always use this. In dev/test the default (False) is ok.")
39+
args = parser.parse_args()
40+
41+
42+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
43+
logging.getLogger("requests").setLevel(logging.WARNING)
44+
logging.getLogger("urllib3").setLevel(logging.WARNING)
45+
logging.getLogger("blackduck").setLevel(logging.WARNING)
46+
47+
def get_project_version_by_name(project_name, version_name):
48+
params = {
49+
'q': [f"name:{project_name}"]
50+
}
51+
projects = [p for p in bd.get_resource('projects', params=params) if p['name'] == project_name]
52+
assert len(projects) == 1, f"There should be one, and only one project named {project_name}. We found {len(projects)}"
53+
project = projects[0]
54+
55+
params = {
56+
'q': [f"versionName:{version_name}"]
57+
}
58+
versions = [v for v in bd.get_resource('versions', project, params=params) if v['versionName'] == version_name]
59+
assert len(versions) == 1, f"There should be one, and only one version named {version_name}. We found {len(versions)}"
60+
version = versions[0]
61+
return project, version
62+
63+
64+
bd = Client(
65+
base_url=args.bd_url,
66+
token=args.user_api_token,
67+
verify=args.verify
68+
)
69+
70+
project, version = get_project_version_by_name(args.project, args.version)
71+
custom_fields = [cf for cf in bd.get_resource("custom-fields", version)]
72+
73+
cfs = list(filter(lambda cf: cf['label'] == args.field_label, custom_fields))
74+
assert len(cfs) == 1, f"We did not find the field labeled {args.field_label} or we found more than one and that shouldn't happen!"
75+
custom_field = cfs[0]
76+
print(f"Custom field {args.field_label} on project-version {args.project}-{args.version} has value(s) {custom_field['values']}")
77+
print(f"Refer to the BD REST API doc for more details on how to interact with the different custom field types, {bd.base_url}/api-doc/public.html#_reading_a_single_project_version_custom_field")
78+

examples/get_custom_field_options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
hub = HubInstance()
2323

24-
# delete all custom fields for the specified object type
24+
# get all custom fields for the specified object type
2525
custom_fields = hub.get_custom_fields(args.object).get('items', [])
2626
for custom_field in custom_fields:
2727
field_url = custom_field['_meta']['href']
2828
field_id = field_url.split("/")[-1]
29+
import pdb; pdb.set_trace()
2930
if field_id == args.field_id:
3031
field_obj = hub.execute_get(field_url).json()
3132

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.HubRestApi import HubInstance, object_id
31+
32+
parser = argparse.ArgumentParser("Get custom field value from a project version")
33+
parser.add_argument("project")
34+
parser.add_argument("version")
35+
parser.add_argument("field_label")
36+
args = parser.parse_args()
37+
38+
39+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
40+
logging.getLogger("requests").setLevel(logging.WARNING)
41+
logging.getLogger("urllib3").setLevel(logging.WARNING)
42+
logging.getLogger("blackduck").setLevel(logging.WARNING)
43+
44+
hub = HubInstance()
45+
46+
project_version = hub.get_project_version_by_name(args.project, args.version)
47+
custom_fields_url = hub.get_link(project_version, "custom-fields")
48+
custom_fields = hub.execute_get(custom_fields_url).json().get('items', [])
49+
50+
cfs = list(filter(lambda cf: cf['label'] == args.field_label, custom_fields))
51+
assert len(cfs) == 1, f"We did not find the field labeled {args.field_label} or we found more than one and that shouldn't happen!"
52+
custom_field = cfs[0]
53+
print(f"Custom field {args.field_label} on project-version {args.project}-{args.version} has value(s) {custom_field['values']}")
54+
print(f"Refer to the BD REST API doc for more details on how to interact with the different custom field types, {hub.get_urlbase()}/api-doc/public.html#_reading_a_single_project_version_custom_field")

examples/update_cf_value.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
cf_to_modify = cf
3030
break
3131

32+
import pdb; pdb.set_trace()
33+
3234
if cf_to_modify:
3335
logging.debug("Updating custom field {} with value {}".format(cf_to_modify, args.new_value))
3436
cf_to_modify['values'] = [args.new_value]

0 commit comments

Comments
 (0)