Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(error-tracking): validate Jira issue responses (#78840)

Hugues Pouillotacc8d6e31a44
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index 588653a78fe2..49d93f401d1d 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -3201,6 +3201,25 @@ class JiraIntegration:
except Exception:
logger.warning("JiraIntegration: token refresh pre-check failed", exc_info=True)
+ def _raise_create_issue_error(self, response: requests.Response, response_body: Any) -> NoReturn:
+ properties: dict[str, Any] = {
+ "jira_status_code": response.status_code,
+ "jira_response_content_type": response.headers.get("Content-Type"),
+ "integration_id": self.integration.id,
+ "team_id": self.integration.team_id,
+ }
+ if isinstance(response_body, dict):
+ properties.update(
+ {
+ "jira_error_messages": response_body.get("errorMessages"),
+ "jira_field_errors": response_body.get("errors"),
+ "jira_response_keys": list(response_body.keys()),
+ }
+ )
+
+ capture_exception(Exception("Jira issue creation failed"), additional_properties=properties)
+ raise ValidationError("Could not create the Jira issue. Check the project's issue settings and try again.")
+
def list_projects(self) -> list[dict]:
"""List all Jira projects accessible to the user"""
cloud_id = self.cloud_id()
@@ -3263,8 +3282,18 @@ class JiraIntegration:
timeout=10,
)
- issue = response.json()
- return {"key": issue.get("key", ""), "id": issue.get("id", "")}
+ try:
+ issue = response.json()
+ except ValueError:
+ issue = None
+
+ if response.status_code != 201:
+ self._raise_create_issue_error(response, issue)
+
+ if not isinstance(issue, dict) or not issue.get("key"):
+ self._raise_create_issue_error(response, issue)
+
+ return {"key": issue["key"], "id": issue.get("id", "")}
# Default branches change rarely; a multi-hour TTL is plenty to avoid hitting
diff --git a/posthog/models/test/test_integration_model.py b/posthog/models/test/test_integration_model.py
index 8566d1ffae51..7207f09112bd 100644
--- a/posthog/models/test/test_integration_model.py
+++ b/posthog/models/test/test_integration_model.py
@@ -49,6 +49,7 @@ from posthog.models.integration import (
GoogleCloudIntegration,
GoogleCloudServiceAccountIntegration,
Integration,
+ JiraIntegration,
LinearIntegration,
OauthIntegration,
PostgreSQLIntegration,
@@ -233,6 +234,70 @@ class TestLinearIntegrationModel(BaseTest):
assert attachment_variables["url"].endswith(f'/project/{self.team.id}/error_tracking/issue-id" }} mutation {{')
+class TestJiraIntegrationModel:
+ @staticmethod
+ def integration() -> MagicMock:
+ return MagicMock(
+ id=123,
+ team_id=456,
+ kind=Integration.IntegrationKind.JIRA,
+ config={"cloud_id": "cloud-id"},
+ sensitive_config={"access_token": "access-token"},
+ )
+
+ @patch("posthog.models.integration.capture_exception")
+ @patch("posthog.models.integration.requests.post")
+ def test_create_issue_captures_structured_error_details(self, mock_post, mock_capture_exception):
+ mock_post.return_value.status_code = 400
+ mock_post.return_value.headers = {"Content-Type": "application/json"}
+ mock_post.return_value.json.return_value = {
+ "errorMessages": ["Issue type is not available"],
+ "errors": {"summary": "Summary is required"},
+ }
+
+ with pytest.raises(ValidationError) as error:
+ JiraIntegration(self.integration()).create_issue(
+ {"project_key": "ENG", "title": "Checkout failed", "description": "Details"}
+ )
+
+ assert error.value.args[0] == (
+ "Could not create the Jira issue. Check the project's issue settings and try again."
+ )
+ captured_error = mock_capture_exception.call_args.args[0]
+ assert str(captured_error) == "Jira issue creation failed"
+ assert mock_capture_exception.call_args.kwargs["additional_properties"] == {
+ "jira_status_code": 400,
+ "jira_response_content_type": "application/json",
+ "integration_id": 123,
+ "team_id": 456,
+ "jira_error_messages": ["Issue type is not available"],
+ "jira_field_errors": {"summary": "Summary is required"},
+ "jira_response_keys": ["errorMessages", "errors"],
+ }
+
+ @patch("posthog.models.integration.capture_exception")
+ @patch("posthog.models.integration.requests.post")
+ def test_create_issue_captures_non_json_response_metadata(self, mock_post, mock_capture_exception):
+ mock_post.return_value.status_code = 502
+ mock_post.return_value.headers = {"Content-Type": "text/html"}
+ mock_post.return_value.json.side_effect = ValueError
+
+ with pytest.raises(ValidationError) as error:
+ JiraIntegration(self.integration()).create_issue(
+ {"project_key": "ENG", "title": "Checkout failed", "description": "Details"}
+ )
+
+ assert error.value.args[0] == (
+ "Could not create the Jira issue. Check the project's issue settings and try again."
+ )
+ assert mock_capture_exception.call_args.kwargs["additional_properties"] == {
+ "jira_status_code": 502,
+ "jira_response_content_type": "text/html",
+ "integration_id": 123,
+ "team_id": 456,
+ }
+
+
class TestOauthIntegrationModel(BaseTest):
mock_settings = {
"SALESFORCE_CONSUMER_KEY": "salesforce-client-id",