Skip to content

Commit 722ecf5

Browse files
author
Glenn Snyder
committed
adding example showing how to update a custom field value
1 parent 1e97855 commit 722ecf5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

examples/update_cf_value.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import argparse
2+
import json
3+
import logging
4+
import sys
5+
6+
from blackduck.HubRestApi import HubInstance
7+
8+
parser = argparse.ArgumentParser("Work with custom fields")
9+
parser.add_argument("object_url", help="The URL to the specific object (e.g. project, project-version, component, BOM component) whose custom field you want to update")
10+
parser.add_argument("field_label", help="The label of the custom field to update on the object")
11+
parser.add_argument("new_value", help="The new value to assign to the custom field. Note: In some cases the new value is a URL, e.g. to a custom field selection value")
12+
args = parser.parse_args()
13+
14+
15+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
16+
logging.getLogger("requests").setLevel(logging.DEBUG)
17+
logging.getLogger("urllib3").setLevel(logging.WARNING)
18+
19+
20+
hub = HubInstance()
21+
22+
obj = hub.execute_get(args.object_url).json()
23+
24+
custom_fields = hub.get_cf_values(obj).get('items', [])
25+
26+
cf_to_modify = None
27+
for cf in custom_fields:
28+
if cf['label'].lower() == args.field_label.lower():
29+
cf_to_modify = cf
30+
break
31+
32+
if cf_to_modify:
33+
logging.debug("Updating custom field {} with value {}".format(cf_to_modify, args.new_value))
34+
cf_to_modify['values'] = [args.new_value]
35+
url = cf_to_modify['_meta']['href']
36+
response = hub.put_cf_value(url, cf_to_modify)
37+
if response.status_code == 200:
38+
logging.info("succeeded updating custom field {} at {} with new value {}".format(args.field_label, args.object_url, args.new_value))
39+
else:
40+
logging.error("succeeded updating custom field {} at {} with new value {}. status code returned was: {}".format(
41+
args.field_label, args.object_url, args.new_value, response.status_code))
42+
else:
43+
logging.error("Failed to find a custom field with label={} at {}".format(args.field_label, args.object_url))

0 commit comments

Comments
 (0)