Skip to content

Commit 5086da4

Browse files
committed
Preserve alert order when truncating PR comments
1 parent 9949dca commit 5086da4

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎socketsecurity/core/messages.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,7 @@ def comment_alert_groups(alerts: list) -> list:
12281228

12291229
@staticmethod
12301230
def comment_alert_rows(groups: list, diff: Diff, config, budget: int) -> tuple[str, int]:
1231+
"""Keep the row order when the size budget cuts a table short."""
12311232
show_ignore = not (config and getattr(config, "disable_ignore", False))
12321233
rows = []
12331234
shown = 0
@@ -1239,10 +1240,11 @@ def comment_alert_rows(groups: list, diff: Diff, config, budget: int) -> tuple[s
12391240
else:
12401241
row = Messages.security_alert_row(group[0], diff, config, show_ignore)
12411242
row_size = len(row.encode())
1242-
if row_size <= budget:
1243-
rows.append(row)
1244-
budget -= row_size
1245-
shown += len(group)
1243+
if row_size > budget:
1244+
break
1245+
rows.append(row)
1246+
budget -= row_size
1247+
shown += len(group)
12461248
return "".join(rows), shown
12471249

12481250
@staticmethod

‎tests/core/test_existing_alerts.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,25 @@ def test_pr_comment_stays_within_the_size_budget(monkeypatch):
588588
assert "Existing alerts (20)" in comment
589589

590590

591+
def test_existing_comment_truncation_keeps_the_blocking_first_order(monkeypatch):
592+
monkeypatch.setattr(Messages, "SECURITY_COMMENT_SIZE_BUDGET", 6000)
593+
large_blocker = _issue("large-blocker")
594+
large_blocker.description = "é" * 3000
595+
diff = _comment_diff(existing=[
596+
_issue("later-warning", error=False, warn=True),
597+
_issue("first-blocker"),
598+
large_blocker,
599+
])
600+
601+
comment = Messages.security_comment_template(diff, _config())
602+
603+
assert "first-blocker@1.0.0" in comment
604+
assert "large-blocker@1.0.0" not in comment
605+
assert "later-warning@1.0.0" not in comment
606+
assert "2 more existing alert(s) are not shown" in comment
607+
assert "[View full report](https://socket.dev/diff?action=error%2Cwarn)" in comment
608+
609+
591610
def test_omitted_existing_blocker_still_fails_strict_blocking(monkeypatch):
592611
monkeypatch.setattr(Messages, "SECURITY_COMMENT_SIZE_BUDGET", 6000)
593612
new = [_issue(f"new-{index}", error=False, warn=True) for index in range(20)]

0 commit comments

Comments
 (0)