diff --git a/CHANGELOG.md b/CHANGELOG.md index c472d51..054c27d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Version 1.1.4](https://github.com/dataiku/dss-plugin-jira/releases/tag/v1.1.4) - Bug Fix release - 2025-10-29 - Implement SSL check ignore on post requests (issue creation) +- Bubble up 4XX errors on agent tool ## [Version 1.1.3](https://github.com/dataiku/dss-plugin-jira/releases/tag/v1.1.3) - Bug Fix release - 2025-09-11 diff --git a/python-agent-tools/create-issue/tool.py b/python-agent-tools/create-issue/tool.py index 1e18d5f..982edf0 100644 --- a/python-agent-tools/create-issue/tool.py +++ b/python-agent-tools/create-issue/tool.py @@ -37,12 +37,7 @@ def get_descriptor(self, tool): } def create_jira_issue(self, summary: str, description: str, issue_type: str = "Task"): - try: - new_issue = self.client.create_issue(self.jira_project_key, summary, description, issue_type) - return new_issue - - except Exception as exception: - return f"Error creating issue: {str(exception)}" + return self.client.create_issue(self.jira_project_key, summary, description, issue_type) def invoke(self, input, trace): args = input.get("input", {}) @@ -58,7 +53,7 @@ def invoke(self, input, trace): trace.attributes["config"] = { "jira_instance_url": jira_instance_url, "jira_project_key": self.jira_project_key - } + } created_issue = self.create_jira_issue(summary, description) @@ -68,10 +63,16 @@ def invoke(self, input, trace): ) else: output_text = f"Issue created: {created_issue.get('key')} available at {jira_instance_url}browse/{created_issue.get('key')}" if isinstance(created_issue, dict) else created_issue - + # Log outputs to trace trace.outputs["output"] = output_text return { "output": output_text - } \ No newline at end of file + } + + def load_sample_query(self, tool): + return { + "summary": "Computer not working", + "description": "User cannot log in his computer. Please create an account" + } diff --git a/python-lib/jira_client.py b/python-lib/jira_client.py index 58a09b2..27d1d8d 100644 --- a/python-lib/jira_client.py +++ b/python-lib/jira_client.py @@ -245,6 +245,7 @@ def create_issue(self, jira_project_key, summary, description, issue_type): # This data form does not work on v3 url = "/".join([self.get_site_url(), "rest/api/2/issue"]) response = self.post(url=url, json=json) + raise_on_response_error(response, context="Failure while creating issue") json_response = response.json() return json_response @@ -311,3 +312,47 @@ def arrayed(data): return data else: return [data] + + +def raise_on_response_error(response, context=""): + if not isinstance(response, requests.Response): + raise Exception("The response from server is not recognized") + status_code = response.status_code + if status_code > 299 or status_code < 200: + json_response = safe_json(response) + if json_response: + message = extract_message(json_response) + if not message: + message = response.text + else: + message = response.text + raise Exception( + "Error {}: {}: {}".format( + status_code, + context, + message + ) + ) + + +def extract_message(json_response): + paths = [ + ["errors", "summary"], + ["errors", "issuetype"], + ["errors", "description"] + ] + for path in paths: + next_token = json_response + for token in path: + next_token = next_token.get(token, {}) + if next_token: + return next_token + return None + + +def safe_json(response): + try: + reponse_content = response.json() + except Exception: + return None + return reponse_content