From f146a9b173179a4bb12f5b93792dee1c5f9e7c7a Mon Sep 17 00:00:00 2001 From: Gibs Date: Tue, 3 Sep 2024 01:13:47 -0700 Subject: [PATCH] [Jira] Fix get method data retrieval failures when advanced mode is on (#1449) * [Rest Client] Add helper method to get content _get_response_content is to be used for chaining together response retrieval. This is useful in the case where you are querying the API and then immediately extracting some data from the response. This method is needed to account for the case where advanced mode is on and the initial get call returns a requests.Response rather than a dictionary. * [Jira] Update retrieval method calls For methods that query the API and then immediately retrieve some data from the response, replace the get call with the new _get_response_content method to account for advanced mode usage. * [Jira] Fix get_issue_labels data retrieval --- atlassian/jira.py | 22 ++++++++++++---------- atlassian/rest_client.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/atlassian/jira.py b/atlassian/jira.py index 6a283f3e7..e3a89b0a4 100644 --- a/atlassian/jira.py +++ b/atlassian/jira.py @@ -1232,7 +1232,7 @@ def get_issue_changelog(self, issue_key, start=None, limit=None): return self.get(url, params=params) else: url = "{base_url}/{issue_key}?expand=changelog".format(base_url=base_url, issue_key=issue_key) - return (self.get(url) or {}).get("changelog", params) + return self._get_response_content(url, fields=[("changelog", params)]) def issue_add_json_worklog(self, key, worklog): """ @@ -1378,7 +1378,7 @@ def get_issue_labels(self, issue_key): url = "{base_url}/{issue_key}?fields=labels".format(base_url=base_url, issue_key=issue_key) if self.advanced_mode: return self.get(url) - return (self.get(url) or {}).get("fields").get("labels") + return self._get_response_content(url, fields=[("fields",), ("labels",)]) def update_issue(self, issue_key, update): """ @@ -1919,12 +1919,14 @@ def set_issue_status_by_transition_id(self, issue_key, transition_id): def get_issue_status(self, issue_key): base_url = self.resource_url("issue") url = "{base_url}/{issue_key}?fields=status".format(base_url=base_url, issue_key=issue_key) - return (((self.get(url) or {}).get("fields") or {}).get("status") or {}).get("name") or {} + fields = [("fields",), ("status",), ("name",)] + return self._get_response_content(url, fields=fields) or {} def get_issue_status_id(self, issue_key): base_url = self.resource_url("issue") url = "{base_url}/{issue_key}?fields=status".format(base_url=base_url, issue_key=issue_key) - return (self.get(url) or {}).get("fields").get("status").get("id") + fields = [("fields",), ("status",), ("id",)] + return self._get_response_content(url, fields=fields) def get_issue_transitions_full(self, issue_key, transition_id=None, expand=None): """ @@ -2721,7 +2723,7 @@ def get_project_actors_for_role_project(self, project_key, role_id): """ base_url = self.resource_url("project") url = "{base_url}/{projectIdOrKey}/role/{id}".format(base_url=base_url, projectIdOrKey=project_key, id=role_id) - return (self.get(url) or {}).get("actors") + return self._get_response_content(url, fields=[("actors",)]) def delete_project_actors(self, project_key, role_id, actor, actor_type=None): """ @@ -3080,7 +3082,7 @@ def get_assignable_users_for_issue(self, issue_key, username=None, start=0, limi def get_status_id_from_name(self, status_name): base_url = self.resource_url("status") url = "{base_url}/{name}".format(base_url=base_url, name=status_name) - return int((self.get(url) or {}).get("id")) + return int(self._get_response_content(url, fields=[("id",)])) def get_status_for_project(self, project_key): base_url = self.resource_url("project") @@ -3181,7 +3183,7 @@ def get_issue_link_types(self): a name and a label for the outward and inward link relationship. """ url = self.resource_url("issueLinkType") - return (self.get(url) or {}).get("issueLinkTypes") + return self._get_response_content(url, fields=[("issueLinkTypes",)]) def get_issue_link_types_names(self): """ @@ -3712,7 +3714,7 @@ def get_all_permissionschemes(self, expand=None): params = {} if expand: params["expand"] = expand - return (self.get(url, params=params) or {}).get("permissionSchemes") + return self._get_response_content(url, params=params, fields=[("permissionSchemes",)]) def get_permissionscheme(self, permission_id, expand=None): """ @@ -3768,7 +3770,7 @@ def get_issue_security_schemes(self): :return: list """ url = self.resource_url("issuesecurityschemes") - return self.get(url).get("issueSecuritySchemes") + return self._get_response_content(url, fields=[("issueSecuritySchemes",)]) def get_issue_security_scheme(self, scheme_id, only_levels=False): """ @@ -3785,7 +3787,7 @@ def get_issue_security_scheme(self, scheme_id, only_levels=False): url = "{base_url}/{scheme_id}".format(base_url=base_url, scheme_id=scheme_id) if only_levels is True: - return self.get(url).get("levels") + return self._get_response_content(url, fields=[("levels",)]) else: return self.get(url) diff --git a/atlassian/rest_client.py b/atlassian/rest_client.py index 677a99313..afe7caa4e 100644 --- a/atlassian/rest_client.py +++ b/atlassian/rest_client.py @@ -362,6 +362,34 @@ def get( log.error(e) return response.text + def _get_response_content( + self, + *args, + fields, + **kwargs, + ): + """ + :param fields: list of tuples in the form (field_name, default value (optional)). + Used for chaining dictionary value accession. + E.g. [("field1", "default1"), ("field2", "default2"), ("field3", )] + """ + response = self.get(*args, **kwargs) + if "advanced_mode" in kwargs: + advanced_mode = kwargs["advanced_mode"] + else: + advanced_mode = self.advanced_mode + + if not advanced_mode: # dict + for field in fields: + response = response.get(*field) + else: # requests.Response + first_field = fields[0] + response = response.json().get(*first_field) + for field in fields[1:]: + response = response.get(*field) + + return response + def post( self, path,