Skip to content

Commit bb04608

Browse files
author
Glenn Snyder
committed
fixing header issue and adding sample code showing how to work with custom fields and custom field options
1 parent 8758242 commit bb04608

File tree

6 files changed

+166
-2
lines changed

6 files changed

+166
-2
lines changed

blackduck/HubRestApi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def get_headers(self):
181181
return {
182182
'X-CSRF-TOKEN': self.csrf_token,
183183
'Authorization': 'Bearer {}'.format(self.token),
184+
'Accept': 'application/json',
184185
'Content-Type': 'application/json'}
185186
else:
186187
if self.bd_major_version == "3":

examples/add_custom_field_options.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#!/usr/bin/env python
3+
4+
import argparse
5+
import json
6+
import logging
7+
import sys
8+
9+
from blackduck.HubRestApi import HubInstance
10+
11+
parser = argparse.ArgumentParser("Modify a custom field")
12+
parser.add_argument("object", choices=["BOM Component", "Component", "Component Version", "Project", "Project Version"], help="The object that the custom field should be attached to")
13+
parser.add_argument("field_id", help="The ID of the custom field to modify")
14+
parser.add_argument("-o", "--options", action='append', nargs=2, metavar=('label', 'position'), help="The options to add. To add more than one option repeat the -o option, supply a label and position for each possible selection. Used for DROPDOWN, MULTISELECT, and RADIO field types.")
15+
args = parser.parse_args()
16+
17+
18+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
19+
logging.getLogger("requests").setLevel(logging.DEBUG)
20+
logging.getLogger("urllib3").setLevel(logging.WARNING)
21+
22+
options = [{"label": io[0], "position": io[1]} for io in args.options]
23+
24+
hub = HubInstance()
25+
26+
# delete all custom fields for the specified object type
27+
custom_fields = hub.get_custom_fields(args.object).get('items', [])
28+
for custom_field in custom_fields:
29+
url = custom_field['_meta']['href']
30+
field_id = url.split("/")[-1]
31+
if field_id == args.field_id:
32+
field_obj = hub.execute_get(url).json()
33+
34+
options_url = hub.get_link(field_obj, "custom-field-option-list")
35+
for option in options:
36+
response = hub.execute_post(options_url, data=option)
37+
if response.status_code == 201:
38+
print("Successfully added option {} to custom field {}".format(option, url))
39+
else:
40+
print("Failed to add option {} for custom field {}, status code: {}".format(
41+
option, url, response.status_code))
42+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
#!/usr/bin/env python
3+
4+
import argparse
5+
import json
6+
import logging
7+
import sys
8+
9+
from blackduck.HubRestApi import HubInstance
10+
11+
parser = argparse.ArgumentParser("Modify a custom field option")
12+
parser.add_argument("object", choices=["BOM Component", "Component", "Component Version", "Project", "Project Version"], help="The object that the custom field should be attached to")
13+
parser.add_argument("field_id", help="The ID of the custom field to modify")
14+
parser.add_argument("option_id", help="The ID of the custom field option to modify")
15+
args = parser.parse_args()
16+
17+
18+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
19+
logging.getLogger("requests").setLevel(logging.DEBUG)
20+
logging.getLogger("urllib3").setLevel(logging.WARNING)
21+
22+
hub = HubInstance()
23+
24+
# delete all custom fields for the specified object type
25+
custom_fields = hub.get_custom_fields(args.object).get('items', [])
26+
for custom_field in custom_fields:
27+
field_url = custom_field['_meta']['href']
28+
field_id = field_url.split("/")[-1]
29+
if field_id == args.field_id:
30+
field_obj = hub.execute_get(field_url).json()
31+
32+
options_url = hub.get_link(field_obj, "custom-field-option-list")
33+
options = hub.execute_get(options_url).json().get('items', [])
34+
for option in options:
35+
option_url = option['_meta']['href']
36+
option_id = option_url.split("/")[-1]
37+
if option_id == args.option_id:
38+
response = hub.execute_delete(option_url)
39+
if response.status_code == 204:
40+
print("Deleted option {}".format(option_url))
41+
else:
42+
print("Failed to delete option {}, status code: {}".format(option_url, response.status_code))

examples/get_custom_field_options.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#!/usr/bin/env python
3+
4+
import argparse
5+
import json
6+
import logging
7+
from pprint import pprint
8+
import sys
9+
10+
from blackduck.HubRestApi import HubInstance
11+
12+
parser = argparse.ArgumentParser("Get custom field options")
13+
parser.add_argument("object", choices=["BOM Component", "Component", "Component Version", "Project", "Project Version"], help="The object that the custom field should be attached to")
14+
parser.add_argument("field_id", help="The ID of the custom field to modify")
15+
args = parser.parse_args()
16+
17+
18+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
19+
logging.getLogger("requests").setLevel(logging.DEBUG)
20+
logging.getLogger("urllib3").setLevel(logging.WARNING)
21+
22+
hub = HubInstance()
23+
24+
# delete all custom fields for the specified object type
25+
custom_fields = hub.get_custom_fields(args.object).get('items', [])
26+
for custom_field in custom_fields:
27+
field_url = custom_field['_meta']['href']
28+
field_id = field_url.split("/")[-1]
29+
if field_id == args.field_id:
30+
field_obj = hub.execute_get(field_url).json()
31+
32+
options_url = hub.get_link(field_obj, "custom-field-option-list")
33+
options = hub.execute_get(options_url).json().get('items', [])
34+
options_info = [{'option_url': o['_meta']['href'], 'label': o['label'], 'position': o['position']} for o in options]
35+
pprint(options_info)

examples/get_policy.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
logging.getLogger("urllib3").setLevel(logging.WARNING)
2121

2222
hub = HubInstance()
23-
policies = hub.get_policies()
24-
for policy in policies.get('items', []):
23+
policies_url = hub.get_apibase() + "/policy-rules"
24+
# policies = hub.get_policies()
25+
policies = hub.execute_get(policies_url).json().get('items', [])
26+
27+
for policy in policies:
2528
if policy['name'] == args.name:
2629
if args.prep:
2730
for attribute in ['createdAt', 'createdBy', 'createdByUser', 'updatedAt', 'updatedBy', 'updatedByUser', '_meta']:

examples/modify_custom_field.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#!/usr/bin/env python
3+
4+
import argparse
5+
import json
6+
import logging
7+
import sys
8+
9+
from blackduck.HubRestApi import HubInstance
10+
11+
parser = argparse.ArgumentParser("Modify a custom field")
12+
parser.add_argument("object", choices=["BOM Component", "Component", "Component Version", "Project", "Project Version"], help="The object that the custom field should be attached to")
13+
parser.add_argument("field_id", help="The ID of the custom field to modify")
14+
parser.add_argument("-l", "--label", help="The new label to apply")
15+
parser.add_argument("-d", "--description", help="The new description to apply")
16+
args = parser.parse_args()
17+
18+
19+
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG)
20+
logging.getLogger("requests").setLevel(logging.DEBUG)
21+
logging.getLogger("urllib3").setLevel(logging.WARNING)
22+
23+
24+
hub = HubInstance()
25+
26+
# delete all custom fields for the specified object type
27+
custom_fields = hub.get_custom_fields(args.object).get('items', [])
28+
for custom_field in custom_fields:
29+
url = custom_field['_meta']['href']
30+
field_id = url.split("/")[-1]
31+
if field_id == args.field_id:
32+
field_obj = hub.execute_get(url).json()
33+
if args.label:
34+
field_obj['label'] = args.label
35+
if args.description:
36+
field_obj['description'] = args.description
37+
response = hub.execute_put(url, data=field_obj)
38+
if response.status_code == 200:
39+
print("Successfully updated field {}".format(url))
40+
else:
41+
print("Failed to update field {}, status code: {}".format(response.status_code))

0 commit comments

Comments
 (0)