Skip to content

Commit c715d83

Browse files
authored
cicd chunk up comments and check runs (#1750)
1 parent 16bf9fc commit c715d83

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

sqlmesh/integrations/github/cicd/controller.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,13 @@ def update_sqlmesh_comment_info(
502502
# If we find a match against the regex then we just return since the comment has already been posted
503503
if seq_get(re.findall(dedup_regex, comment.body), 0):
504504
return comment
505-
comment.edit(body=f"{comment.body}\n{value}")
505+
full_comment = f"{comment.body}\n{value}"
506+
body, *truncated = self._chunk_up_api_message(f"{full_comment}")
507+
if truncated:
508+
logger.warning(
509+
f"Comment body was too long so we truncated it. Full text: {full_comment}"
510+
)
511+
comment.edit(body=body)
506512
return comment
507513

508514
def update_pr_environment(self) -> None:
@@ -579,15 +585,10 @@ def _update_check(
579585
kwargs["completed_at"] = current_time
580586
if conclusion:
581587
kwargs["conclusion"] = conclusion.value
582-
full_summary_bytes = (full_summary or title).encode("utf-8")
583-
summary, text, *truncated = [
584-
full_summary_bytes[i : i + self.MAX_BYTE_LENGTH].decode("utf-8", "ignore")
585-
for i in range(0, len(full_summary_bytes), self.MAX_BYTE_LENGTH)
586-
] + [None]
588+
full_summary = full_summary or title
589+
summary, text, *truncated = self._chunk_up_api_message(full_summary) + [None]
587590
if truncated and truncated[0] is not None:
588-
logger.warning(
589-
f'Summary was too long so we truncated it. Full text: {full_summary_bytes.decode("utf-8", "ignore")}'
590-
)
591+
logger.warning(f"Summary was too long so we truncated it. Full text: {full_summary}")
591592
kwargs["output"] = {"title": title, "summary": summary}
592593
if text:
593594
kwargs["output"]["text"] = text
@@ -924,3 +925,13 @@ def get_command_from_comment(self) -> BotCommand:
924925
return BotCommand.from_comment_body(
925926
self._event.pull_request_comment_body, self.bot_config.command_namespace
926927
)
928+
929+
def _chunk_up_api_message(self, message: str) -> t.List[str]:
930+
"""
931+
Chunks up the message into `MAX_BYTE_LENGTH` byte chunks
932+
"""
933+
message_encoded = message.encode("utf-8")
934+
return [
935+
message_encoded[i : i + self.MAX_BYTE_LENGTH].decode("utf-8", "ignore")
936+
for i in range(0, len(message_encoded), self.MAX_BYTE_LENGTH)
937+
]

tests/integrations/github/cicd/test_github_controller.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ def test_run_tests(github_client, make_controller):
345345
"**SQLMesh Bot Info**\ntest2",
346346
None,
347347
),
348+
(
349+
"Ensure comments are truncated if they are too long",
350+
[
351+
MockIssueComment(body="**SQLMesh Bot Info**\ntest1"),
352+
],
353+
# Making sure that although we will be under the character limit of `65535` we will still truncate
354+
# because the byte size of this character is 3 and therefore we will be over the limit since it is based
355+
# on bytes on not characters (despite what the error message may say)
356+
"桜" * 65000,
357+
None,
358+
# ((Max Byte Length) - (Length of "**SQLMesh Bot Info**\ntest1\n")) / (Length of "桜")
359+
"**SQLMesh Bot Info**\ntest1\n" + ("桜" * int((65535 - 27) / 3)),
360+
None,
361+
),
348362
]
349363

350364

0 commit comments

Comments
 (0)