From 2dc2ee64d8f018a671679406becfd6ac178a9804 Mon Sep 17 00:00:00 2001 From: Joseph Lombrozo Date: Fri, 15 Dec 2023 15:42:37 -0500 Subject: [PATCH] trim comments when necessary (#91) --- pkg/github_client/message.go | 12 ++++++++++++ pkg/gitlab_client/message.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pkg/github_client/message.go b/pkg/github_client/message.go index 73e817ce..5f25346c 100644 --- a/pkg/github_client/message.go +++ b/pkg/github_client/message.go @@ -16,10 +16,17 @@ import ( "github.com/zapier/kubechecks/telemetry" ) +const MaxCommentLength = 64 * 1024 + func (c *Client) PostMessage(ctx context.Context, repo *repo.Repo, prID int, msg string) *pkg.Message { _, span := otel.Tracer("Kubechecks").Start(ctx, "PostMessageToMergeRequest") defer span.End() + if len(msg) > MaxCommentLength { + log.Warn().Int("original_length", len(msg)).Msg("trimming the comment size") + msg = msg[:MaxCommentLength] + } + log.Debug().Msgf("Posting message to PR %d in repo %s", prID, repo.FullName) comment, _, err := c.Issues.CreateComment( ctx, @@ -41,6 +48,11 @@ func (c *Client) UpdateMessage(ctx context.Context, m *pkg.Message, msg string) _, span := otel.Tracer("Kubechecks").Start(ctx, "UpdateMessage") defer span.End() + if len(msg) > MaxCommentLength { + log.Warn().Int("original_length", len(msg)).Msg("trimming the comment size") + msg = msg[:MaxCommentLength] + } + log.Info().Msgf("Updating message for PR %d in repo %s", m.CheckID, m.Name) repoNameComponents := strings.Split(m.Name, "/") diff --git a/pkg/gitlab_client/message.go b/pkg/gitlab_client/message.go index ec3696f2..3519d1e4 100644 --- a/pkg/gitlab_client/message.go +++ b/pkg/gitlab_client/message.go @@ -15,10 +15,17 @@ import ( "github.com/zapier/kubechecks/telemetry" ) +const MaxCommentLength = 1_000_000 + func (c *Client) PostMessage(ctx context.Context, repo *repo.Repo, mergeRequestID int, msg string) *pkg.Message { _, span := otel.Tracer("Kubechecks").Start(ctx, "PostMessageToMergeRequest") defer span.End() + if len(msg) > MaxCommentLength { + log.Warn().Int("original_length", len(msg)).Msg("trimming the comment size") + msg = msg[:MaxCommentLength] + } + n, _, err := c.Notes.CreateMergeRequestNote( repo.FullName, mergeRequestID, &gitlab.CreateMergeRequestNoteOptions{ @@ -57,6 +64,11 @@ func (c *Client) hideOutdatedMessages(ctx context.Context, projectName string, m `, note.Body) + if len(newBody) > MaxCommentLength { + log.Warn().Int("original_length", len(newBody)).Msg("trimming the comment size") + newBody = newBody[:MaxCommentLength] + } + log.Debug().Str("projectName", projectName).Int("mr", mergeRequestID).Msgf("Updating comment %d as outdated", note.ID) _, _, err := c.Notes.UpdateMergeRequestNote(projectName, mergeRequestID, note.ID, &gitlab.UpdateMergeRequestNoteOptions{ @@ -75,6 +87,11 @@ func (c *Client) hideOutdatedMessages(ctx context.Context, projectName string, m func (c *Client) UpdateMessage(ctx context.Context, m *pkg.Message, msg string) error { log.Debug().Msgf("Updating message %d for %s", m.NoteID, m.Name) + if len(msg) > MaxCommentLength { + log.Warn().Int("original_length", len(msg)).Msg("trimming the comment size") + msg = msg[:MaxCommentLength] + } + n, _, err := c.Notes.UpdateMergeRequestNote(m.Name, m.CheckID, m.NoteID, &gitlab.UpdateMergeRequestNoteOptions{ Body: pkg.Pointer(msg), })