Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid printing parsed json response #1246

Merged
merged 4 commits into from
Nov 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 30 additions & 41 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def __init__(
"""
# force a copy of the tuple to be used in __del__() because
# sys.version_info could have already been deleted in __del__()
self.sys_version_info = tuple(i for i in sys.version_info)
self.sys_version_info = tuple(sys.version_info)

if options is None:
options = {}
Expand Down Expand Up @@ -537,7 +537,7 @@ def __init__(
auth_method = (
oauth or basic_auth or jwt or kerberos or auth or "anonymous"
)
raise JIRAError(f"Can not log in with {str(auth_method)}")
raise JIRAError(f"Can not log in with {auth_method}")

self.deploymentType = None
if get_server_info:
Expand Down Expand Up @@ -641,7 +641,6 @@ def _check_for_html_error(self, content: str):
if "<!-- SecurityTokenMissing -->" in content:
self.log.warning("Got SecurityTokenMissing")
raise JIRAError(f"SecurityTokenMissing: {content}")
return False
return True

def _get_sprint_field_id(self):
Expand Down Expand Up @@ -1435,13 +1434,13 @@ def create_issue(

p = data["fields"]["project"]

if isinstance(p, str) or isinstance(p, int):
if isinstance(p, (str, int)):
data["fields"]["project"] = {"id": self.project(str(p)).id}

p = data["fields"]["issuetype"]
if isinstance(p, int):
data["fields"]["issuetype"] = {"id": p}
if isinstance(p, str) or isinstance(p, int):
if isinstance(p, (str, int)):
data["fields"]["issuetype"] = {"id": self.issue_type_by_name(str(p)).id}

url = self._get_url("issue")
Expand Down Expand Up @@ -1478,7 +1477,7 @@ def create_issues(
issue_data: Dict[str, Any] = _field_worker(field_dict)
p = issue_data["fields"]["project"]

if isinstance(p, str) or isinstance(p, int):
if isinstance(p, (str, int)):
issue_data["fields"]["project"] = {"id": self.project(str(p)).id}

p = issue_data["fields"]["issuetype"]
Expand Down Expand Up @@ -1579,7 +1578,6 @@ def service_desks(self) -> List[ServiceDesk]:
url = self.server_url + "/rest/servicedeskapi/servicedesk"
headers = {"X-ExperimentalApi": "opt-in"}
r_json = json_loads(self._session.get(url, headers=headers))
print(r_json)
projects = [
ServiceDesk(self._options, self._session, raw_project_json)
for raw_project_json in r_json["values"]
Expand Down Expand Up @@ -1628,7 +1626,7 @@ def create_customer_request(
p = data["serviceDeskId"]
service_desk = None

if isinstance(p, str) or isinstance(p, int):
if isinstance(p, (str, int)):
service_desk = self.service_desk(p)
elif isinstance(p, ServiceDesk):
service_desk = p
Expand Down Expand Up @@ -1756,7 +1754,7 @@ def assign_issue(self, issue: Union[int, str], assignee: Optional[str]) -> bool:
Returns:
bool
"""
url = self._get_latest_url(f"issue/{str(issue)}/assignee")
url = self._get_latest_url(f"issue/{issue}/assignee")
user_id = self._get_user_id(assignee)
payload = {"accountId": user_id} if self._is_cloud else {"name": user_id}
r = self._session.put(url, data=json.dumps(payload))
Expand All @@ -1777,7 +1775,7 @@ def comments(self, issue: str, expand: Optional[str] = None) -> List[Comment]:
params = {}
if expand is not None:
params["expand"] = expand
r_json = self._get_json(f"issue/{str(issue)}/comment", params=params)
r_json = self._get_json(f"issue/{issue}/comment", params=params)

comments = [
Comment(self._options, self._session, raw_comment_json)
Expand Down Expand Up @@ -3608,15 +3606,14 @@ def _get_mime_type(self, buff: bytes) -> Optional[str]:
"""
if self._magic is not None:
return self._magic.id_buffer(buff)
else:
try:
return mimetypes.guess_type("f." + str(imghdr.what(0, buff)))[0]
except (OSError, TypeError):
self.log.warning(
"Couldn't detect content type of avatar image"
". Specify the 'contentType' parameter explicitly."
)
return None
try:
return mimetypes.guess_type("f." + str(imghdr.what(0, buff)))[0]
except (OSError, TypeError):
self.log.warning(
"Couldn't detect content type of avatar image"
". Specify the 'contentType' parameter explicitly."
)
return None

def rename_user(self, old_user: str, new_user: str):
"""Rename a Jira user.
Expand Down Expand Up @@ -3657,9 +3654,8 @@ def delete_user(self, username: str) -> bool:
r = self._session.delete(url)
if 200 <= r.status_code <= 299:
return True
else:
self.log.error(r.status_code)
return False
self.log.error(r.status_code)
return False

def deactivate_user(self, username: str) -> Union[str, int]:
"""Disable/deactivate the user.
Expand Down Expand Up @@ -3697,11 +3693,10 @@ def deactivate_user(self, username: str) -> Union[str, int]:
)
if r.status_code == 200:
return True
else:
self.log.warning(
f"Got response from deactivating {username}: {r.status_code}"
)
return r.status_code
self.log.warning(
f"Got response from deactivating {username}: {r.status_code}"
)
return r.status_code
except Exception as e:
self.log.error(f"Error Deactivating {username}: {e}")
raise JIRAError(f"Error Deactivating {username}: {e}")
Expand All @@ -3725,11 +3720,10 @@ def deactivate_user(self, username: str) -> Union[str, int]:
)
if r.status_code == 200:
return True
else:
self.log.warning(
f"Got response from deactivating {username}: {r.status_code}"
)
return r.status_code
self.log.warning(
f"Got response from deactivating {username}: {r.status_code}"
)
return r.status_code
except Exception as e:
self.log.error(f"Error Deactivating {username}: {e}")
raise JIRAError(f"Error Deactivating {username}: {e}")
Expand All @@ -3748,11 +3742,7 @@ def reindex(self, force: bool = False, background: bool = True) -> bool:
"""
# /secure/admin/IndexAdmin.jspa
# /secure/admin/jira/IndexProgress.jspa?taskId=1
if background:
indexingStrategy = "background"
else:
indexingStrategy = "stoptheworld"

indexingStrategy = "background" if background else "stoptheworld"
url = self.server_url + "/secure/admin/jira/IndexReIndex.jspa"

r = self._session.get(url, headers=self._options["headers"])
Expand All @@ -3762,7 +3752,7 @@ def reindex(self, force: bool = False, background: bool = True) -> bool:

if (
not r.text.find("To perform the re-index now, please go to the")
and force is False
and not force
):
return True

Expand Down Expand Up @@ -3796,9 +3786,8 @@ def backup(self, filename: str = "backup.zip", attachments: bool = False):
r = self._session.post(url, headers=self._options["headers"], data=payload)
if r.status_code == 200:
return True
else:
self.log.warning(f"Got {r.status_code} response from calling backup.")
return r.status_code
self.log.warning(f"Got {r.status_code} response from calling backup.")
return r.status_code
except Exception as e:
self.log.error("I see %s", e)

Expand Down