Skip to content

Commit fd4ab7b

Browse files
authored
✨ feat(scm): add environment infomation to suspect commit prs (#93025)
1 parent 5d66a4f commit fd4ab7b

File tree

5 files changed

+91
-28
lines changed

5 files changed

+91
-28
lines changed

src/sentry/integrations/github/integration.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,6 @@ def get_open_pr_comment_workflow(self) -> OpenPRCommentWorkflow:
384384
385385
<sub>Did you find this useful? React with a 👍 or 👎</sub>"""
386386

387-
MERGED_PR_SINGLE_ISSUE_TEMPLATE = "- ‼️ **{title}** `{subtitle}` [View Issue]({url})"
388-
389387

390388
class GitHubPRCommentWorkflow(PRCommentWorkflow):
391389
organization_option_key = "sentry:github_pr_bot"
@@ -405,10 +403,10 @@ def get_comment_body(self, issue_ids: list[int]) -> str:
405403

406404
issue_list = "\n".join(
407405
[
408-
MERGED_PR_SINGLE_ISSUE_TEMPLATE.format(
406+
self.get_merged_pr_single_issue_template(
409407
title=issue.title,
410-
subtitle=self.format_comment_subtitle(issue.culprit or "unknown culprit"),
411408
url=self.format_comment_url(issue.get_absolute_url(), self.referrer_id),
409+
environment=self.get_environment_info(issue),
412410
)
413411
for issue in issues
414412
]

src/sentry/integrations/gitlab/integration.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ def get_open_pr_comment_workflow(self) -> OpenPRCommentWorkflow:
230230
231231
{issue_list}"""
232232

233-
MERGED_PR_SINGLE_ISSUE_TEMPLATE = "- ‼️ **{title}** `{subtitle}` [View Issue]({url})"
234-
235233

236234
class GitlabPRCommentWorkflow(PRCommentWorkflow):
237235
organization_option_key = "sentry:gitlab_pr_bot"
@@ -253,10 +251,10 @@ def get_comment_body(self, issue_ids: list[int]) -> str:
253251

254252
issue_list = "\n".join(
255253
[
256-
MERGED_PR_SINGLE_ISSUE_TEMPLATE.format(
254+
self.get_merged_pr_single_issue_template(
257255
title=issue.title,
258-
subtitle=self.format_comment_subtitle(issue.culprit),
259256
url=self.format_comment_url(issue.get_absolute_url(), self.referrer_id),
257+
environment=self.get_environment_info(issue),
260258
)
261259
for issue in issues
262260
]

src/sentry/integrations/source_code_management/commit_context.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ class PullRequestFile:
139139
patch: str
140140

141141

142+
ISSUE_TITLE_MAX_LENGTH = 50
143+
MERGED_PR_SINGLE_ISSUE_TEMPLATE = "* ‼️ [**{title}**]({url}){environment}\n"
144+
145+
142146
class CommitContextIntegration(ABC):
143147
"""
144148
Base class for integrations that include commit context features: suspect commits, suspect PR comments
@@ -570,6 +574,37 @@ def get_top_5_issues_by_count(
570574
)
571575
return raw_snql_query(request, referrer=self.referrer.value)["data"]
572576

577+
@staticmethod
578+
def _truncate_title(title: str, max_length: int = ISSUE_TITLE_MAX_LENGTH) -> str:
579+
"""Truncate title if it's too long and add ellipsis."""
580+
if len(title) <= max_length:
581+
return title
582+
return title[:max_length].rstrip() + "..."
583+
584+
def get_environment_info(self, issue: Group) -> str:
585+
try:
586+
recommended_event = issue.get_recommended_event()
587+
if recommended_event:
588+
environment = recommended_event.get_environment()
589+
if environment and environment.name:
590+
return f" in `{environment.name}`"
591+
except Exception as e:
592+
# If anything goes wrong, just continue without environment info
593+
logger.info(
594+
"get_environment_info.no-environment",
595+
extra={"issue_id": issue.id, "error": e},
596+
)
597+
return ""
598+
599+
@staticmethod
600+
def get_merged_pr_single_issue_template(title: str, url: str, environment: str) -> str:
601+
truncated_title = PRCommentWorkflow._truncate_title(title)
602+
return MERGED_PR_SINGLE_ISSUE_TEMPLATE.format(
603+
title=truncated_title,
604+
url=url,
605+
environment=environment,
606+
)
607+
573608

574609
class OpenPRCommentWorkflow(ABC):
575610
def __init__(self, integration: CommitContextIntegration):

tests/sentry/integrations/github/tasks/test_pr_comment.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,12 @@ def test_do_not_ignore_other_issues(self):
338338
class TestGetCommentBody(GithubCommentTestCase):
339339
def test_simple(self):
340340
ev1 = self.store_event(
341-
data={"message": "issue 1", "culprit": "issue1", "fingerprint": ["group-1"]},
341+
data={
342+
"message": "issue 1",
343+
"culprit": "issue1",
344+
"fingerprint": ["group-1"],
345+
"environment": "dev",
346+
},
342347
project_id=self.project.id,
343348
)
344349
assert ev1.group is not None
@@ -348,7 +353,12 @@ def test_simple(self):
348353
)
349354
assert ev2.group is not None
350355
ev3 = self.store_event(
351-
data={"message": "issue 3", "culprit": "issue3", "fingerprint": ["group-3"]},
356+
data={
357+
"message": "issue 3",
358+
"culprit": "issue3",
359+
"fingerprint": ["group-3"],
360+
"environment": "prod",
361+
},
352362
project_id=self.project.id,
353363
)
354364
assert ev3.group is not None
@@ -359,9 +369,12 @@ def test_simple(self):
359369
expected_comment = f"""## Suspect Issues
360370
This pull request was deployed and Sentry observed the following issues:
361371
362-
- ‼️ **issue 1** `issue1` [View Issue](http://testserver/organizations/foo/issues/{ev1.group.id}/?referrer=github-pr-bot)
363-
- ‼️ **issue 2** `issue2` [View Issue](http://testserver/organizations/foo/issues/{ev2.group.id}/?referrer=github-pr-bot)
364-
- ‼️ **issue 3** `issue3` [View Issue](http://testserver/organizations/foo/issues/{ev3.group.id}/?referrer=github-pr-bot)
372+
* ‼️ [**issue 1**](http://testserver/organizations/{self.organization.slug}/issues/{ev1.group.id}/?referrer=github-pr-bot) in `dev`
373+
374+
* ‼️ [**issue 2**](http://testserver/organizations/{self.organization.slug}/issues/{ev2.group.id}/?referrer=github-pr-bot)
375+
376+
* ‼️ [**issue 3**](http://testserver/organizations/{self.organization.slug}/issues/{ev3.group.id}/?referrer=github-pr-bot) in `prod`
377+
365378
366379
<sub>Did you find this useful? React with a 👍 or 👎</sub>"""
367380
assert formatted_comment == expected_comment
@@ -384,7 +397,6 @@ def test_comment_workflow(self, mock_metrics, mock_issues):
384397
group_objs = Group.objects.order_by("id").all()
385398
groups = [g.id for g in group_objs]
386399
titles = [g.title for g in group_objs]
387-
culprits = [g.culprit for g in group_objs]
388400
mock_issues.return_value = [{"group_id": id, "event_count": 10} for id in groups]
389401

390402
responses.add(
@@ -397,7 +409,7 @@ def test_comment_workflow(self, mock_metrics, mock_issues):
397409
github_comment_workflow(self.pr.id, self.project.id)
398410

399411
assert (
400-
f'"body": "## Suspect Issues\\nThis pull request was deployed and Sentry observed the following issues:\\n\\n- \\u203c\\ufe0f **{titles[0]}** `{culprits[0]}` [View Issue](http://testserver/organizations/foo/issues/{groups[0]}/?referrer=github-pr-bot)\\n- \\u203c\\ufe0f **{titles[1]}** `{culprits[1]}` [View Issue](http://testserver/organizations/foobar/issues/{groups[1]}/?referrer=github-pr-bot)\\n\\n<sub>Did you find this useful? React with a \\ud83d\\udc4d or \\ud83d\\udc4e</sub>"'.encode()
412+
f'"body": "## Suspect Issues\\nThis pull request was deployed and Sentry observed the following issues:\\n\\n* \\u203c\\ufe0f [**{titles[0]}**](http://testserver/organizations/foo/issues/{groups[0]}/?referrer=github-pr-bot)\\n\\n* \\u203c\\ufe0f [**{titles[1]}**](http://testserver/organizations/foobar/issues/{groups[1]}/?referrer=github-pr-bot)\\n\\n\\n<sub>Did you find this useful? React with a \\ud83d\\udc4d or \\ud83d\\udc4e</sub>"'.encode()
401413
in responses.calls[0].request.body
402414
)
403415
pull_request_comment_query = PullRequestComment.objects.all()
@@ -413,7 +425,9 @@ def test_comment_workflow(self, mock_metrics, mock_issues):
413425
@responses.activate
414426
@freeze_time(datetime(2023, 6, 8, 0, 0, 0, tzinfo=UTC))
415427
def test_comment_workflow_updates_comment(self, mock_metrics, mock_issues):
416-
groups = [g.id for g in Group.objects.all()]
428+
group_objs = Group.objects.order_by("id").all()
429+
groups = [g.id for g in group_objs]
430+
titles = [g.title for g in group_objs]
417431
mock_issues.return_value = [{"group_id": id, "event_count": 10} for id in groups]
418432
pull_request_comment = PullRequestComment.objects.create(
419433
external_id=1,
@@ -443,7 +457,7 @@ def test_comment_workflow_updates_comment(self, mock_metrics, mock_issues):
443457
github_comment_workflow(self.pr.id, self.project.id)
444458

445459
assert (
446-
f'"body": "## Suspect Issues\\nThis pull request was deployed and Sentry observed the following issues:\\n\\n- \\u203c\\ufe0f **issue 1** `issue1` [View Issue](http://testserver/organizations/foo/issues/{groups[0]}/?referrer=github-pr-bot)\\n- \\u203c\\ufe0f **issue 2** `issue2` [View Issue](http://testserver/organizations/foobar/issues/{groups[1]}/?referrer=github-pr-bot)\\n\\n<sub>Did you find this useful? React with a \\ud83d\\udc4d or \\ud83d\\udc4e</sub>"'.encode()
460+
f'"body": "## Suspect Issues\\nThis pull request was deployed and Sentry observed the following issues:\\n\\n* \\u203c\\ufe0f [**{titles[0]}**](http://testserver/organizations/foo/issues/{groups[0]}/?referrer=github-pr-bot)\\n\\n* \\u203c\\ufe0f [**{titles[1]}**](http://testserver/organizations/foobar/issues/{groups[1]}/?referrer=github-pr-bot)\\n\\n\\n<sub>Did you find this useful? React with a \\ud83d\\udc4d or \\ud83d\\udc4e</sub>"'.encode()
447461
in responses.calls[0].request.body
448462
)
449463
pull_request_comment.refresh_from_db()

tests/sentry/integrations/gitlab/tasks/test_pr_comment.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,12 @@ def test_do_not_ignore_other_issues(self):
299299
class TestGetCommentBody(GitlabCommentTestCase):
300300
def test_simple(self):
301301
ev1 = self.store_event(
302-
data={"message": "issue 1", "culprit": "issue1", "fingerprint": ["group-1"]},
302+
data={
303+
"message": "issue 1",
304+
"culprit": "issue1",
305+
"fingerprint": ["group-1"],
306+
"environment": "dev",
307+
},
303308
project_id=self.project.id,
304309
)
305310
assert ev1.group is not None
@@ -309,7 +314,12 @@ def test_simple(self):
309314
)
310315
assert ev2.group is not None
311316
ev3 = self.store_event(
312-
data={"message": "issue 3", "culprit": "issue3", "fingerprint": ["group-3"]},
317+
data={
318+
"message": "issue 3",
319+
"culprit": "issue3",
320+
"fingerprint": ["group-3"],
321+
"environment": "prod",
322+
},
313323
project_id=self.project.id,
314324
)
315325
assert ev3.group is not None
@@ -320,9 +330,12 @@ def test_simple(self):
320330
expected_comment = f"""## Suspect Issues
321331
This merge request was deployed and Sentry observed the following issues:
322332
323-
- ‼️ **issue 1** `issue1` [View Issue](http://testserver/organizations/baz/issues/{ev1.group.id}/?referrer=gitlab-pr-bot)
324-
- ‼️ **issue 2** `issue2` [View Issue](http://testserver/organizations/baz/issues/{ev2.group.id}/?referrer=gitlab-pr-bot)
325-
- ‼️ **issue 3** `issue3` [View Issue](http://testserver/organizations/baz/issues/{ev3.group.id}/?referrer=gitlab-pr-bot)"""
333+
* ‼️ [**{ev1.group.title}**](http://testserver/organizations/{self.organization.slug}/issues/{ev1.group.id}/?referrer=gitlab-pr-bot) in `dev`
334+
335+
* ‼️ [**{ev2.group.title}**](http://testserver/organizations/{self.organization.slug}/issues/{ev2.group.id}/?referrer=gitlab-pr-bot)
336+
337+
* ‼️ [**{ev3.group.title}**](http://testserver/organizations/{self.organization.slug}/issues/{ev3.group.id}/?referrer=gitlab-pr-bot) in `prod`
338+
"""
326339
assert formatted_comment == expected_comment
327340

328341

@@ -343,7 +356,6 @@ def test_comment_workflow(self, mock_metrics, mock_issues):
343356
group_objs = Group.objects.order_by("id").all()
344357
groups = [g.id for g in group_objs]
345358
titles = [g.title for g in group_objs]
346-
culprits = [g.culprit for g in group_objs]
347359
mock_issues.return_value = [{"group_id": id, "event_count": 10} for id in groups]
348360

349361
responses.add(
@@ -360,8 +372,10 @@ def test_comment_workflow(self, mock_metrics, mock_issues):
360372
## Suspect Issues
361373
This merge request was deployed and Sentry observed the following issues:
362374
363-
- ‼️ **{titles[0]}** `{culprits[0]}` [View Issue](http://testserver/organizations/baz/issues/{groups[0]}/?referrer=gitlab-pr-bot)
364-
- ‼️ **{titles[1]}** `{culprits[1]}` [View Issue](http://testserver/organizations/foobar/issues/{groups[1]}/?referrer=gitlab-pr-bot)"""
375+
* ‼️ [**{titles[0]}**](http://testserver/organizations/{self.organization.slug}/issues/{groups[0]}/?referrer=gitlab-pr-bot)
376+
377+
* ‼️ [**{titles[1]}**](http://testserver/organizations/{self.another_organization.slug}/issues/{groups[1]}/?referrer=gitlab-pr-bot)
378+
"""
365379
}
366380

367381
pull_request_comment_query = PullRequestComment.objects.all()
@@ -377,7 +391,9 @@ def test_comment_workflow(self, mock_metrics, mock_issues):
377391
@responses.activate
378392
@freeze_time(datetime(2023, 6, 8, 0, 0, 0, tzinfo=UTC))
379393
def test_comment_workflow_updates_comment(self, mock_metrics, mock_issues):
380-
groups = [g.id for g in Group.objects.all()]
394+
group_objs = Group.objects.order_by("id").all()
395+
groups = [g.id for g in group_objs]
396+
titles = [g.title for g in group_objs]
381397
mock_issues.return_value = [{"group_id": id, "event_count": 10} for id in groups]
382398
pull_request_comment = PullRequestComment.objects.create(
383399
external_id=1,
@@ -411,8 +427,10 @@ def test_comment_workflow_updates_comment(self, mock_metrics, mock_issues):
411427
## Suspect Issues
412428
This merge request was deployed and Sentry observed the following issues:
413429
414-
- ‼️ **issue 1** `issue1` [View Issue](http://testserver/organizations/baz/issues/{groups[0]}/?referrer=gitlab-pr-bot)
415-
- ‼️ **issue 2** `issue2` [View Issue](http://testserver/organizations/foobar/issues/{groups[1]}/?referrer=gitlab-pr-bot)"""
430+
* ‼️ [**{titles[0]}**](http://testserver/organizations/{self.organization.slug}/issues/{groups[0]}/?referrer=gitlab-pr-bot)
431+
432+
* ‼️ [**{titles[1]}**](http://testserver/organizations/{self.another_organization.slug}/issues/{groups[1]}/?referrer=gitlab-pr-bot)
433+
"""
416434
}
417435

418436
pull_request_comment.refresh_from_db()

0 commit comments

Comments
 (0)