|
| 1 | +import logging |
| 2 | +import requests |
| 3 | +import json |
| 4 | +from operator import itemgetter |
| 5 | +import urllib.parse |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | +def _get_cf_url(self): |
| 10 | + return self.get_apibase() + "/custom-fields/objects" |
| 11 | + |
| 12 | +def supported_cf_object_types(self): |
| 13 | + '''Get the types and cache them since they are static (on a per-release basis)''' |
| 14 | + if not hasattr(self, "_cf_object_types"): |
| 15 | + logger.debug("retrieving object types") |
| 16 | + self._cf_object_types = [cfo['name'] for cfo in self.get_cf_objects().get('items', [])] |
| 17 | + return self._cf_object_types |
| 18 | + |
| 19 | +def get_cf_objects(self): |
| 20 | + '''Get CF objects and cache them since these are static (on a per-release basis)''' |
| 21 | + url = self._get_cf_url() |
| 22 | + if not hasattr(self, "_cf_objects"): |
| 23 | + logger.debug("retrieving objects") |
| 24 | + response = self.execute_get(url) |
| 25 | + self._cf_objects = response.json() |
| 26 | + return self._cf_objects |
| 27 | + |
| 28 | +def _get_cf_object_url(self, object_name): |
| 29 | + for cf_object in self.get_cf_objects().get('items', []): |
| 30 | + if cf_object['name'].lower() == object_name.lower(): |
| 31 | + return cf_object['_meta']['href'] |
| 32 | + |
| 33 | +def get_cf_object(self, object_name): |
| 34 | + assert object_name in self.supported_cf_object_types(), "Object name {} not one of the supported types ({})".format(object_name, self.supported_cf_object_types()) |
| 35 | + |
| 36 | + object_url = self._get_cf_object_url(object_name) |
| 37 | + response = self.execute_get(object_url) |
| 38 | + return response.json() |
| 39 | + |
| 40 | +def _get_cf_obj_rel_path(self, object_name): |
| 41 | + return object_name.lower().replace(" ", "-") |
| 42 | + |
| 43 | +def create_cf(self, object_name, field_type, description, label, position, active=True, initial_options=[]): |
| 44 | + ''' |
| 45 | + Create a custom field for the given object type (e.g. "Project", "Project Version") using the field_type and other parameters. |
| 46 | +
|
| 47 | + Initial options are needed for field types like multi-select where the multiple values to choose from must also be provided. |
| 48 | +
|
| 49 | + initial_options = [{"label":"val1", "position":0}, {"label":"val2", "position":1}] |
| 50 | + ''' |
| 51 | + assert isinstance(position, int) and position >= 0, "position must be an integer that is greater than or equal to 0" |
| 52 | + assert field_type in ["BOOLEAN", "DATE", "DROPDOWN", "MULTISELECT", "RADIO", "TEXT", "TEXTAREA"] |
| 53 | + |
| 54 | + types_using_initial_options = ["DROPDOWN", "MULTISELECT", "RADIO"] |
| 55 | + |
| 56 | + post_url = self._get_cf_object_url(object_name) + "/fields" |
| 57 | + cf_object = self._get_cf_obj_rel_path(object_name) |
| 58 | + cf_request = { |
| 59 | + "active": active, |
| 60 | + "description": description, |
| 61 | + "label": label, |
| 62 | + "position": position, |
| 63 | + "type": field_type, |
| 64 | + } |
| 65 | + if field_type in types_using_initial_options and initial_options: |
| 66 | + cf_request.update({"initialOptions": initial_options}) |
| 67 | + response = self.execute_post(post_url, data=cf_request) |
| 68 | + return response |
| 69 | + |
| 70 | +def delete_cf(self, object_name, field_id): |
| 71 | + '''Delete a custom field from a given object type, e.g. Project, Project Version, Component, etc |
| 72 | +
|
| 73 | + WARNING: Deleting a custom field is irreversiable. Any data in the custom fields could be lost so use with caution. |
| 74 | + ''' |
| 75 | + assert object_name in self.supported_cf_object_types(), "You must supply a supported object name that is in {}".format(self.supported_cf_object_types()) |
| 76 | + |
| 77 | + delete_url = self._get_cf_object_url(object_name) + "/fields/{}".format(field_id) |
| 78 | + return self.execute_delete(delete_url) |
| 79 | + |
| 80 | +def get_custom_fields(self, object_name): |
| 81 | + '''Get the custom field (definition) for a given object type, e.g. Project, Project Version, Component, etc |
| 82 | + ''' |
| 83 | + assert object_name in self.supported_cf_object_types(), "You must supply a supported object name that is in {}".format(self.supported_cf_object_types()) |
| 84 | + |
| 85 | + url = self._get_cf_object_url(object_name) + "/fields" |
| 86 | + |
| 87 | + response = self.execute_get(url) |
| 88 | + return response.json() |
| 89 | + |
| 90 | +def get_cf_values(self, obj): |
| 91 | + '''Get all of the custom fields from an object such as a Project, Project Version, Component, etc |
| 92 | +
|
| 93 | + The obj is expected to be the JSON document for a project, project-version, component, etc |
| 94 | + ''' |
| 95 | + url = self.get_link(obj, "custom-fields") |
| 96 | + response = self.execute_get(url) |
| 97 | + return response.json() |
| 98 | + |
| 99 | +def get_cf_value(self, obj, field_id): |
| 100 | + '''Get a custom field value from an object such as a Project, Project Version, Component, etc |
| 101 | +
|
| 102 | + The obj is expected to be the JSON document for a project, project-version, component, etc |
| 103 | + ''' |
| 104 | + url = self.get_link(obj, "custom-fields") + "/{}".format(field_id) |
| 105 | + response = self.execute_get(url) |
| 106 | + return response.json() |
| 107 | + |
| 108 | +def put_cf_value(self, cf_url, new_cf_obj): |
| 109 | + '''new_cf_obj is expected to be a modified custom field value object with the values updated accordingly, e.g. |
| 110 | + call get_cf_value, modify the object, and then call put_cf_value |
| 111 | + ''' |
| 112 | + return self.execute_put(cf_url, new_cf_obj) |
0 commit comments