Skip to content

Commit

Permalink
Refactoring setting jira error
Browse files Browse the repository at this point in the history
  • Loading branch information
bbielinski-splunk committed Feb 12, 2024
1 parent a0b15d3 commit 1d4d6db
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
102 changes: 55 additions & 47 deletions jira_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,67 +335,75 @@ def _set_jira_error(self, result_object, message, e):
# e.text. Instead, these messages are included in e.response,
# which is a Response object from requests package.
if hasattr(e, "text") and e.text:
error_text = e.text
# Try to parse the HTML content of the error in majority situations and if it fails to parse
# the error response as HTML, then, return the raw error text to ensure that the error text
# is not getting dropped from this point
self.debug_print(
"Jira error details available in exception, parsing it with "
"BeautifulSoup."
)
try:
soup = BeautifulSoup(error_text, "html.parser")
error_text = soup.text
split_lines = error_text.split('\n')
split_lines = [x.strip() for x in split_lines if x.strip()]
error_text = '\n'.join(split_lines)
except Exception as parsing_exception:
try:
error_text = (
f"Cannot parse error details. Unparsed error: "
f"{error_text}. Parsing exception: {parsing_exception}"
)
except Exception as ex:
error_text = (
f"Unable to parse the details of the error received "
f"in the output response. Parsing exception: "
f"{parsing_exception}. Formatting to str exception: "
f"{ex}."
)
error_text: str = self._extract_err_msg_from_jira_exc_text(e)
else:
self.debug_print(
"Jira error details missing in exception. Details will be "
"fetched from HTTP Response."
)
try:
response_content: dict = e.response.json()
jira_error_messages: list[str] = response_content.get(JIRA_RESPONSE_ERROR_MESSAGES_KEY, [])
jira_errors: dict[str, str] = response_content.get(JIRA_RESPONSE_ERRORS_KEY, {})
all_jira_error_messages: list[str] = jira_error_messages + [
f"{field_name}: {error_details}"
for field_name, error_details in jira_errors.items()
]
error_text = (
"\n".join(all_jira_error_messages)
if all_jira_error_messages
else "Unable to parse the details of the error received "
"in the output response"
)

except Exception as e:
error_text = self._get_error_message_from_exception(e)
error_text = self._extract_err_msg_from_jira_exc_response(e)

if "Epic Name is required" in error_text:
error_text = "{}. {}".format(error_text,
"Please create a custom field for Epic Name and provide it in the fields parameter as { \"customfield_Id\" : \"epic_name\" }")

return result_object.set_status(phantom.APP_ERROR, "{0}. Message: {1}".format(message, error_text))

def _extract_err_msg_from_jira_exc_text(self, jira_exc) -> str:
error_text = jira_exc.text
# Try to parse the HTML content of the error in majority situations and if it fails to parse
# the error response as HTML, then, return the raw error text to ensure that the error text
# is not getting dropped from this point
self.debug_print(
"Jira error details available in exception, parsing it with "
"BeautifulSoup."
)
try:
soup = BeautifulSoup(error_text, "html.parser")
error_text = soup.text
split_lines = error_text.split('\n')
split_lines = [x.strip() for x in split_lines if x.strip()]
error_text = '\n'.join(split_lines)
except Exception as parsing_exception:
try:
error_text = (
f"Cannot parse error details. Unparsed error: "
f"{error_text}. Parsing exception: {parsing_exception}"
)
except Exception as ex:
error_text = (
f"Unable to parse the details of the error received "
f"in the output response. Parsing exception: "
f"{parsing_exception}. Formatting to str exception: "
f"{ex}."
)
return error_text

def _extract_err_msg_from_jira_exc_response(self, jira_exc) -> str:
try:
response_content: dict = jira_exc.response.json()
jira_error_messages: list[str] = response_content.get(JIRA_RESPONSE_ERROR_MESSAGES_KEY, [])
jira_errors: dict[str, str] = response_content.get(JIRA_RESPONSE_ERRORS_KEY, {})
all_jira_error_messages: list[str] = jira_error_messages + [
f"{field_name}: {error_details}"
for field_name, error_details in jira_errors.items()
]
error_text = (
"\n".join(all_jira_error_messages)
if all_jira_error_messages
else "Unable to parse the details of the error received "
"in the output response"
)

except Exception as e:
error_text = self._get_error_message_from_exception(e)

return error_text

def _create_jira_object(self, action_result):

if not self._verify_cert:
if 'REQUESTS_CA_BUNDLE' in os.environ:
del os.environ['REQUESTS_CA_BUNDLE']
if not self._verify_cert and 'REQUESTS_CA_BUNDLE' in os.environ:
del os.environ['REQUESTS_CA_BUNDLE']

# create the options dictionary
options = {'server': self._base_url, 'verify': self._verify_cert}
Expand Down
14 changes: 6 additions & 8 deletions jira_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# either express or implied. See the License for the specific language governing permissions
# and limitations under the License.
def get_ctx_result(provides, result):
"""Function that parses data.
"""
Extracts ctx data from action's result.
:param result: result
:param result: action's result
:param provides: action name
:return: response data
"""
Expand All @@ -32,16 +33,13 @@ def get_ctx_result(provides, result):

ctx_result["action"] = provides

if not data:
ctx_result["data"] = []
return ctx_result
ctx_result["data"] = data

ctx_result["data"] = data if data else []
return ctx_result


def display_view(provides, all_app_runs, context):
"""Function that displays view.
"""
Returns a view of the results of jira connector actions.
:param provides: action name
:param context: context
Expand Down

0 comments on commit 1d4d6db

Please sign in to comment.