Skip to content

Commit

Permalink
use fields_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtini committed Sep 2, 2024
1 parent 9b032ab commit 43ed02d
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,18 +1800,17 @@ def issue(

issue = Issue(self._options, self._session)

translation_map: dict[str, str] = {}
params = {}
if fields is not None:
fields_list, translation_map = self._translate_fields_name_to_id(fields)
fields_list = self._translate_fields_name_to_id(fields)
params["fields"] = ",".join(fields_list)
if expand is not None:
params["expand"] = expand
if properties is not None:
params["properties"] = properties

issue.find(id, params=params)
self._untranslate_issue_fields_id_to_fields_name([issue], translation_map)
self._untranslate_issues_fields_id_to_fields_name([issue])
return issue

def create_issue(
Expand Down Expand Up @@ -3525,7 +3524,7 @@ def search_issues(
elif fields is None:
fields = ["*all"]

fields, translation_map = self._translate_fields_name_to_id(fields)
fields = self._translate_fields_name_to_id(fields)

search_params = {
"jql": jql_str,
Expand Down Expand Up @@ -3561,12 +3560,12 @@ def search_issues(
use_post=use_post,
)

self._untranslate_issue_fields_id_to_fields_name(issues, translation_map)
self._untranslate_issues_fields_id_to_fields_name(issues)
return issues

def _translate_fields_name_to_id(
self, fields: list[str] | str
) -> tuple[list[str], dict[str, str]]:
) -> list[str]:
"""Translate fields names to JIRA's REST API field id.
Args:
Expand All @@ -3587,33 +3586,25 @@ def _translate_fields_name_to_id(
"Translating fields for <%s> type is unsupported", type(fields)
)

translation_map = {}
if self._fields_cache:
for i, field_name in enumerate(translated_fields):
if field_name in self._fields_cache:
translation_map[self._fields_cache[field_name]] = translated_fields[
i
]
translated_fields[i] = self._fields_cache[field_name]

return translated_fields, translation_map
return translated_fields

@staticmethod
def _untranslate_issue_fields_id_to_fields_name(
issues: list[Issue], translation_map: dict[str, str]
) -> None:
def _untranslate_issues_fields_id_to_fields_name(self, issues: list[Issue]) -> None:
"""Untranslate issues fields names from JIRA's REST API field id.
Args:
issues (list[Issue]): list of issues to be untranslated
translation_map (dict[str, str]): translation dict containing original field name and field id
"""
if not translation_map:
if not self._fields_cache:
return
for issue in issues:
if not issue.raw:
return
for field_id, field_name in translation_map.items():
for field_name, field_id in self._fields_cache.items():
if field_id in issue.raw.get("fields", {}):
issue.raw["fields"][field_name] = issue.raw["fields"][field_id]
setattr(issue.fields, field_name, issue.raw["fields"][field_id])
Expand Down

0 comments on commit 43ed02d

Please sign in to comment.