Skip to content

Commit

Permalink
[Jira] Fix get method data retrieval failures when advanced mode is on (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
gibsramen authored Sep 3, 2024
1 parent e9520ab commit f146a9b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
22 changes: 12 additions & 10 deletions atlassian/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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)

Expand Down
28 changes: 28 additions & 0 deletions atlassian/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

1 comment on commit f146a9b

@hasssr
Copy link

@hasssr hasssr commented on f146a9b Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ‎atlassian/rest_client.py line 368, the fields argument should be prefixed with ** (DOUBLESTAR)

Please sign in to comment.