Skip to content

Commit

Permalink
trim comments when necessary (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus authored Dec 15, 2023
1 parent 1158af1 commit 2dc2ee6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/github_client/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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, "/")
Expand Down
17 changes: 17 additions & 0 deletions pkg/gitlab_client/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -57,6 +64,11 @@ func (c *Client) hideOutdatedMessages(ctx context.Context, projectName string, m
</details>
`, 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{
Expand All @@ -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),
})
Expand Down

0 comments on commit 2dc2ee6

Please sign in to comment.