Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions action/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@

from action import cli, context, inputs, model, render

#: Prefixes of report bodies written by earlier versions, kept so the first run
#: after upgrading adopts the existing comment instead of posting a second one.
#: Drop these once a release has been out long enough.
LEGACY_TITLES = ("# Commit Check", "# Commit-Check")

#: The colour a label gets when the Action has to create it.
AI_LABEL_COLOR = "8250df"

Expand Down Expand Up @@ -142,18 +137,13 @@ def _find_own_comments(comments: list[Any]) -> tuple[Any | None, list[Any]]:

Returns ``(target, stale)``. Only comments a bot posted with
``COMMENT_MARKER`` in them are ever edited or deleted: a person can paste
the marker too (quoting a report, say), and their comment is theirs. A comment from an earlier
version has no marker, so it is adopted (edited, which adds the marker)
when there is no marked comment yet, and only if a bot posted it: the
legacy signal is a title prefix, which a person can type by accident, and
editing someone's comment out from under them is not recoverable.
the marker too (quoting a report, say), and their comment is theirs.
A report from before v2.13.0 has no marker and is left as it is.
"""
marked = [c for c in comments if render.COMMENT_MARKER in c.body and _is_bot(c)]
if marked:
return marked[-1], marked[:-1]

legacy = [c for c in comments if c.body.startswith(LEGACY_TITLES) and _is_bot(c)]
return (legacy[-1], []) if legacy else (None, [])
return None, []


def add_pr_comments(results: list[model.ScopeResult]) -> int:
Expand Down
16 changes: 9 additions & 7 deletions tests/publish_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ def test_creates_comment_with_rendered_body(self):
self.assertIn("```diff\n@@ Commit 1/1 @@\n- bad message\n", body)

def test_updates_existing_comment_when_changed(self):
# A report from an earlier version: no marker, and posted by the bot,
# which is what makes it safe to adopt.
old_comment = MagicMock(body="# Commit-Check ❌ 0 failures")
# The report an earlier run posted: the marker, and posted by the bot,
# which is what makes it safe to edit.
old_comment = MagicMock(body=f"{render.COMMENT_MARKER}\nold report")
old_comment.user.type = "Bot"
mock_pull_request = MagicMock()
mock_pull_request.get_comments.return_value = [old_comment]
Expand Down Expand Up @@ -664,10 +664,12 @@ def test_human_comment_with_the_old_title_is_not_adopted(self):
self.assertIsNone(target)
self.assertEqual(stale, [])

def test_bot_comment_from_an_older_version_is_adopted(self):
def test_bot_comment_without_the_marker_is_left_alone(self):
"""A report from before v2.13.0 has no marker: it is neither edited
nor deleted, and the run posts a new comment."""
legacy = self._comment("# Commit-Check\nold report")
target, stale = publish._find_own_comments([legacy])
self.assertIs(target, legacy)
self.assertIsNone(target)
self.assertEqual(stale, [])

def test_human_comment_with_the_marker_is_never_touched(self):
Expand All @@ -685,8 +687,8 @@ def test_no_comments_yields_nothing_to_update(self):

def test_a_comment_without_an_author_is_not_a_bot(self):
"""A deleted account can leave ``user`` empty; that is not a crash,
and not a licence to adopt the comment either."""
orphan = self._comment("# Commit Check\nold report")
and not a licence to edit the comment either."""
orphan = self._comment(f"{render.COMMENT_MARKER}\nold report")
orphan.user = None
self.assertFalse(publish._is_bot(orphan))
self.assertEqual(publish._find_own_comments([orphan]), (None, []))
Expand Down
Loading