|
| 1 | +# coding=utf-8 |
| 2 | +import logging |
| 3 | + |
| 4 | +from .rest_client import AtlassianRestAPI |
| 5 | + |
| 6 | +log = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +class Insight(AtlassianRestAPI): |
| 10 | + """Insight for Jira API wrapper.""" |
| 11 | + |
| 12 | + # https://insight-javadoc.riada.io/insight-javadoc-8.6/insight-rest/ |
| 13 | + |
| 14 | + def __init__(self, *args, **kwargs): |
| 15 | + kwargs["api_root"] = "rest/insight/1.0" |
| 16 | + # If cloud is set to true, trigger private __cloud__init method |
| 17 | + if kwargs.get("cloud"): |
| 18 | + args, kwargs = self.__cloud_init(*args, **kwargs) |
| 19 | + super(Insight, self).__init__(*args, **kwargs) |
| 20 | + |
| 21 | + def __cloud_init(self, *args, **kwargs): |
| 22 | + # trigger a normal init and avoid looping |
| 23 | + del kwargs["cloud"] |
| 24 | + temp = Insight(*args, **kwargs) |
| 25 | + # retrieve cloud workspace id and generate the api_root |
| 26 | + kwargs["api_root"] = "/jsm/insight/workspace/{}/v1/".format(temp.__get_workspace_id()) |
| 27 | + # insight cloud uses the atlassian base url, not custom instnace urls |
| 28 | + kwargs["url"] = "https://api.atlassian.com" |
| 29 | + # set cloud back to true and return |
| 30 | + kwargs["cloud"] = True |
| 31 | + # Insight cloud is particular about its headers.. |
| 32 | + self.default_headers = {"Accept": "application/json"} |
| 33 | + return args, kwargs |
| 34 | + |
| 35 | + def __get_workspace_id(self): |
| 36 | + return self.get("rest/servicedeskapi/insight/workspace", headers=self.default_headers,)["values"][ |
| 37 | + 0 |
| 38 | + ]["workspaceId"] |
| 39 | + |
| 40 | + # Attachments |
| 41 | + def get_attachments_of_objects(self, object_id): |
| 42 | + """ |
| 43 | + Get Attachment info |
| 44 | + Example output: |
| 45 | + [ |
| 46 | + { |
| 47 | + "id": 1, |
| 48 | + "author": "admin", |
| 49 | + "mimeType": "image/png", |
| 50 | + "filename": "astronaut.png", |
| 51 | + "filesize": "490 B", |
| 52 | + "created": "2019-11-27T11:42:22.283Z", |
| 53 | + "comment": "", |
| 54 | + "commentOutput": "", |
| 55 | + "url": "http://jira/rest/insight/1.0/attachments/1" |
| 56 | + } |
| 57 | + ] |
| 58 | +
|
| 59 | + :param object_id Object ID |
| 60 | + :return list of object |
| 61 | + id: required(string) |
| 62 | + author: (string) |
| 63 | + mimeType: (string) |
| 64 | + filename: required(string) |
| 65 | + filesize: (string) |
| 66 | + created: required(datetime) |
| 67 | + comment: (string) |
| 68 | + commentOutput: (string) |
| 69 | + url: required(string) |
| 70 | + """ |
| 71 | + if self.cloud: |
| 72 | + raise NotImplementedError |
| 73 | + url = self.url_joiner(self.api_root, "attachments/object/{objectId}".format(objectId=object_id)) |
| 74 | + return self.get(url) |
| 75 | + |
| 76 | + def upload_attachment_to_object(self, object_id, filename): |
| 77 | + """ |
| 78 | + Add attachment to Object |
| 79 | + :param object_id: int |
| 80 | + :param filename: str, name, if file in current directory or full path to file |
| 81 | + """ |
| 82 | + if self.cloud: |
| 83 | + raise NotImplementedError |
| 84 | + log.warning("Adding attachment...") |
| 85 | + url = "rest/insight/1.0/attachments/object/{objectId}".format(objectId=object_id) |
| 86 | + with open(filename, "rb") as attachment: |
| 87 | + files = {"file": attachment} |
| 88 | + return self.post(url, headers=self.no_check_headers, files=files) |
| 89 | + |
| 90 | + def delete_attachment(self, attachment_id): |
| 91 | + """ |
| 92 | + Add attachment to Object |
| 93 | + :param attachment_id: int |
| 94 | + """ |
| 95 | + if self.cloud: |
| 96 | + raise NotImplementedError |
| 97 | + log.warning("Adding attachment...") |
| 98 | + url = "rest/insight/1.0/attachments/{attachmentId}".format(attachmentId=attachment_id) |
| 99 | + return self.delete(url) |
| 100 | + |
| 101 | + # Comments |
| 102 | + # Handle comments on objets |
| 103 | + def add_comment_to_object(self, comment, object_id, role): |
| 104 | + """ |
| 105 | + Add comment to Object |
| 106 | +
|
| 107 | + :param comment: str |
| 108 | + :param object_id: int |
| 109 | + :param role: int |
| 110 | + 0 Insight Users |
| 111 | + 1 Insight Managers |
| 112 | + 2 Insight Administrators |
| 113 | + 3 Insight Developers |
| 114 | + :return: |
| 115 | + { |
| 116 | + "created": "2019-11-27T12:37:41.492Z", |
| 117 | + "updated": "2019-11-27T12:37:41.492Z", |
| 118 | + "id": 1, |
| 119 | + "actor": { |
| 120 | + "avatarUrl": "https://www.gravatar.com/avatar/64e1b8d34f425d19e1ee2ea7236d3028?d=mm&s=48", |
| 121 | + "displayName": "admin", |
| 122 | + "name": "admin", |
| 123 | + "key": "admin", |
| 124 | + "renderedLink": "<a href=\"/jira/secure/ViewProfile.jspa?name=admin\">admin</a>", |
| 125 | + "isDeleted": false |
| 126 | + }, |
| 127 | + "role": 0, |
| 128 | + "comment": "A comment to be added", |
| 129 | + "commentOutput": "A comment to be added", |
| 130 | + "objectId": 1, |
| 131 | + "canEdit": true, |
| 132 | + "canDelete": true |
| 133 | + } |
| 134 | + """ |
| 135 | + if self.cloud: |
| 136 | + raise NotImplementedError |
| 137 | + params = {"comment": comment, "objectId": object_id, "role": role} |
| 138 | + url = "rest/insight/1.0/comment/create" |
| 139 | + return self.post(url, params=params) |
| 140 | + |
| 141 | + def get_comment_of_object(self, object_id): |
| 142 | + """ |
| 143 | + The object id to fetch comments from |
| 144 | + :param object_id: |
| 145 | + :return: |
| 146 | + """ |
| 147 | + if self.cloud: |
| 148 | + raise NotImplementedError |
| 149 | + url = "rest/insight/1.0/comment/object/{objectId}".format(objectId=object_id) |
| 150 | + return self.get(url) |
| 151 | + |
| 152 | + # Icon |
| 153 | + # Resources dedicated to load and find icons |
| 154 | + def get_icon_by_id(self, id): |
| 155 | + """ |
| 156 | + Load a single icon by id |
| 157 | + :param id: |
| 158 | + :return: |
| 159 | + { |
| 160 | + "id": 1, |
| 161 | + "name": "Delete", |
| 162 | + "url16": "http://jira/rest/insight/1.0/icon/1/icon.png?size=16", |
| 163 | + "url48": "http://jira/rest/insight/1.0/icon/1/icon.png?size=48" |
| 164 | + } |
| 165 | + """ |
| 166 | + url = self.url_joiner(self.api_root, "icon/{id}".format(id=id)) |
| 167 | + return self.get(url) |
| 168 | + |
| 169 | + def get_all_global_icons(self): |
| 170 | + """ |
| 171 | + All existing global icons |
| 172 | + :return: |
| 173 | + """ |
| 174 | + url = self.url_joiner(self.api_root, "icon/global") |
| 175 | + return self.get(url) |
| 176 | + |
| 177 | + # Import |
| 178 | + # Start configured imports. To see an ongoing import see the Progress resource |
| 179 | + def start_import_configuration(self, id): |
| 180 | + """ |
| 181 | + The id of the import configuration that should be started |
| 182 | + :param id: |
| 183 | + :return: |
| 184 | + """ |
| 185 | + url = self.url_joiner(self.api_root, "import/start/{id}") |
| 186 | + return self.post(url) |
| 187 | + |
| 188 | + # Index |
| 189 | + # Handle the indexing of Insight |
| 190 | + def reindex_insight(self): |
| 191 | + """ |
| 192 | + Should the reindex clean the index before doing the reindex |
| 193 | + :return: |
| 194 | + """ |
| 195 | + if self.cloud: |
| 196 | + raise NotImplementedError |
| 197 | + url = self.url_joiner(self.api_root, "index/reindex/start") |
| 198 | + return self.post(url) |
| 199 | + |
| 200 | + def reindex_current_node_insight(self): |
| 201 | + """ |
| 202 | + Should the reindex clean the index before doing the reindex |
| 203 | + :return: |
| 204 | + """ |
| 205 | + if self.cloud: |
| 206 | + raise NotImplementedError |
| 207 | + url = self.url_joiner(self.api_root, "index/reindex/currentnode") |
| 208 | + return self.post(url) |
| 209 | + |
| 210 | + # IQL |
| 211 | + # Resource dedicated to finding objects based on the Insight Query Language (IQL) |
| 212 | + def iql( |
| 213 | + self, |
| 214 | + iql, |
| 215 | + object_schema_id, |
| 216 | + page=1, |
| 217 | + order_by_attribute_id=None, |
| 218 | + order_asc=True, |
| 219 | + result_per_page=25, |
| 220 | + include_attributes=True, |
| 221 | + include_attributes_deep=1, |
| 222 | + include_type_attributes=False, |
| 223 | + include_extended_info=False, |
| 224 | + extended=None, |
| 225 | + ): |
| 226 | + """ |
| 227 | +
|
| 228 | + :param iql: |
| 229 | + :param object_schema_id: |
| 230 | + :param page: |
| 231 | + :param order_by_attribute_id: |
| 232 | + :param order_asc: |
| 233 | + :param result_per_page: |
| 234 | + :param include_attributes: |
| 235 | + :param include_attributes_deep: |
| 236 | + :param include_type_attributes: |
| 237 | + :param include_extended_info: |
| 238 | + :param extended: |
| 239 | + :return: |
| 240 | + """ |
| 241 | + params = {"iql": iql, "objectSchemaId": object_schema_id, "page": page} |
| 242 | + if order_by_attribute_id: |
| 243 | + params["orderByAttributeId"] = order_by_attribute_id |
| 244 | + params["orderAsc"] = order_asc |
| 245 | + params["resultPerPage"] = result_per_page |
| 246 | + params["includeAttributes"] = include_attributes |
| 247 | + params["includeAttributesDeep"] = include_attributes_deep |
| 248 | + params["includeTypeAttributes"] = include_type_attributes |
| 249 | + params["includeExtendedInfo"] = include_extended_info |
| 250 | + if extended: |
| 251 | + params["extended"] = extended |
| 252 | + url = self.url_joiner(self.api_root, "iql/objects") |
| 253 | + return self.get(url, params=params) |
0 commit comments