Skip to content

Commit 9164908

Browse files
authored
chore(bitbucket): Truncate issue link title length (#95290)
Should reduce failures in SLOs From GCP logs: ``` event: "integrations.slo.failure" exception_summary: "IntegrationError('Error Communicating with Bitbucket (HTTP 400): title: Ensure this value has at most 255 characters (it has 288).')" ```
1 parent fad1fc6 commit 9164908

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/sentry/integrations/bitbucket/issues.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
from sentry.users.models.identity import Identity
1818
from sentry.users.models.user import User
1919
from sentry.users.services.user import RpcUser
20+
from sentry.utils.strings import truncatechars
2021

2122
# Generated based on the response from the Bitbucket API
2223
# Example: {"type": "error", "error": {"message": "Repository has no issue tracker."}}
2324
BITBUCKET_HALT_ERROR_CODES = ["Repository has no issue tracker.", "Resource not found"]
25+
BITBUCKET_MAX_TITLE_LENGTH = 255
2426

2527

2628
ISSUE_TYPES = (
@@ -72,6 +74,13 @@ def get_create_issue_config(
7274
"sentry-extensions-bitbucket-search", args=[org.slug, self.model.id]
7375
)
7476

77+
title_field = next((field for field in fields if field["name"] == "title"), None)
78+
if title_field:
79+
title_field["maxLength"] = BITBUCKET_MAX_TITLE_LENGTH
80+
title_field["default"] = truncatechars(
81+
title_field["default"], BITBUCKET_MAX_TITLE_LENGTH
82+
)
83+
7584
return [
7685
{
7786
"name": "repo",

tests/sentry/integrations/bitbucket/test_issues.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import orjson
22
import responses
33

4-
from sentry.integrations.bitbucket.issues import ISSUE_TYPES, PRIORITIES
4+
from sentry.integrations.bitbucket.issues import BITBUCKET_MAX_TITLE_LENGTH, ISSUE_TYPES, PRIORITIES
55
from sentry.integrations.models.external_issue import ExternalIssue
66
from sentry.integrations.services.integration import integration_service
77
from sentry.testutils.cases import APITestCase
88
from sentry.testutils.factories import EventType
99
from sentry.testutils.helpers.datetime import before_now
1010
from sentry.testutils.skips import requires_snuba
11+
from sentry.utils.strings import truncatechars
1112

1213
pytestmark = [requires_snuba]
1314

@@ -211,6 +212,7 @@ def test_get_create_issue_config(self):
211212
"default": "message",
212213
"type": "string",
213214
"required": True,
215+
"maxLength": BITBUCKET_MAX_TITLE_LENGTH,
214216
},
215217
{
216218
"name": "description",
@@ -237,6 +239,28 @@ def test_get_create_issue_config(self):
237239
},
238240
]
239241

242+
@responses.activate
243+
def test_get_create_issue_config_with_long_title(self):
244+
responses.add(
245+
responses.GET,
246+
"https://api.bitbucket.org/2.0/repositories/myaccount",
247+
json={"values": [{"full_name": "myaccount/repo1"}, {"full_name": "myaccount/repo2"}]},
248+
)
249+
installation = self.integration.get_installation(self.organization.id)
250+
event = self.store_event(
251+
data={
252+
"event_id": "a" * 32,
253+
"message": "b" * 5000,
254+
"timestamp": before_now(minutes=1).isoformat(),
255+
},
256+
project_id=self.project.id,
257+
)
258+
config = installation.get_create_issue_config(event.group, self.user)
259+
title_field = next(field for field in config if field["name"] == "title")
260+
assert title_field
261+
assert title_field["default"] == truncatechars(event.message, BITBUCKET_MAX_TITLE_LENGTH)
262+
assert title_field["maxLength"] == BITBUCKET_MAX_TITLE_LENGTH
263+
240264
@responses.activate
241265
def test_get_create_issue_config_without_group(self):
242266
responses.add(

0 commit comments

Comments
 (0)