From 61958acbe1aca0b846d205aa6a446fe892958971 Mon Sep 17 00:00:00 2001 From: Maciej Tonderski Date: Fri, 14 Jun 2024 10:30:34 +0200 Subject: [PATCH] added field validation for editmeta response action --- jira_connector.py | 36 +++++++++++++++++++++++++++++++++++- requirements.txt | 1 + 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/jira_connector.py b/jira_connector.py index 4b13194..b766746 100644 --- a/jira_connector.py +++ b/jira_connector.py @@ -22,6 +22,7 @@ import time from builtins import str from datetime import datetime +from urllib.parse import urlparse, urlunparse import dateutil import phantom.app as phantom @@ -72,7 +73,7 @@ def __init__(self): self._jira = None self._timezone = None - def initialize(self): + def _base_url(self): config = self.get_config() @@ -491,6 +492,35 @@ def _list_projects(self, param): return action_result.set_status(phantom.APP_SUCCESS) + def _get_base_url(self, url): + parsed_url = urlparse(url) + return f"{parsed_url.scheme}://{parsed_url.netloc}" + + def _update_base_url(self, url, new_base): + parsed_url = urlparse(url) + new_parsed_base = urlparse(new_base) + updated_url = urlunparse(( + new_parsed_base.scheme, new_parsed_base.netloc, + parsed_url.path, parsed_url.params, + parsed_url.query, parsed_url.fragment + )) + return updated_url + + def _validate_and_update_custom_fields_url(self, custom_fields): + try: + allowed_values = custom_fields['custom_field']['allowed_values'] + for value in allowed_values: + if 'self' in value: + self_url = value['self'] + base_url = self._get_base_url(self_url) + if base_url != self._base_url: + value['self'] = self._update_base_url(self_url, self._base_url) + return custom_fields, None + except KeyError as e: + return False, f"Key error: {e}" + except Exception as e: + return False, f"Error: {e}" + def _get_custom_fields_for_issue(self, issue_id, action_result): try: @@ -514,6 +544,10 @@ def _get_custom_fields_for_issue(self, issue_id, action_result): {}".format(error_message) return action_result.set_status(phantom.APP_ERROR, error_text), None, None + custom_fields, err = self._validate_and_update_custom_fields_url(custom_fields) + if err: + return action_result.set_status(phantom.APP_ERROR, err), None, None + return phantom.APP_SUCCESS, custom_fields, fields_meta def _replace_custom_id_with_name(self, input_fields, custom_id_to_name, action_result): diff --git a/requirements.txt b/requirements.txt index 880588e..73eb9a1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ python-dateutil==2.9.0.post0 pytz==2024.1 requests_oauthlib==2.0.0 requests_toolbelt==1.0.0 +urllib3==2.2.1