From e801b092f3cd28f8ed1a530e435ca986f8c71881 Mon Sep 17 00:00:00 2001 From: Cameron Brown Date: Tue, 26 Nov 2024 00:44:29 -0500 Subject: [PATCH] src/webhooks.py: Limit push commit listing to 2000 chars --- src/webhooks.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/webhooks.py b/src/webhooks.py index 0ca9477..3952f0e 100644 --- a/src/webhooks.py +++ b/src/webhooks.py @@ -178,13 +178,30 @@ async def push(self, payload: ClientPayload): ) # ignore webhooks with zero commits elif commit_count > 1: - formatted_commits = [ - f"* [`{commit['id'][:7]}`]({self.url(commit)}): \"{self.natural_wrap(commit['message'])[:100]}\"" - for commit in gh["commits"] - ] + preamble = f"{name} {pushed} {commit_count} commits to {branch} in {repo} ({compare}):\n" + ellipsis = f"* ... _and {commit_count - 1} more commits_" + formatted_commits = [] + for commit in gh["commits"]: + message = f"* [`{commit['id'][:7]}`]({self.url(commit)}): \"{self.natural_wrap(commit['message'])[:100]}\"" + if ( + sum(len(line) + 1 for line in formatted_commits) + + len(message) + + len(preamble) + + len(ellipsis) + < 2000 + ): + formatted_commits.append(message) + else: + break + ellipsis = ( + f"* ... _and {commit_count - len(formatted_commits) - 1} more commits_" + if len(formatted_commits) < commit_count - 1 + else "" + ) + formatted_commits.append(ellipsis) formatted_commits_str = "\n".join(formatted_commits) await updates_channel.send( - f"{name} {pushed} {commit_count} commits to {branch} in {repo} ({compare}):\n{formatted_commits_str}", + f"{preamble}{formatted_commits_str}", ) @Server.route()