From 53f6ec236b49a3d9ae699826b5d087f4c60ac6a4 Mon Sep 17 00:00:00 2001 From: krrrr38 Date: Thu, 12 Aug 2021 19:49:17 +0900 Subject: [PATCH] Split Gitlab Server comments if over max length --- server/events/vcs/gitlab_client.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/server/events/vcs/gitlab_client.go b/server/events/vcs/gitlab_client.go index d3f32373c1..07e26df88d 100644 --- a/server/events/vcs/gitlab_client.go +++ b/server/events/vcs/gitlab_client.go @@ -34,6 +34,10 @@ import ( gitlab "github.com/xanzy/go-gitlab" ) +// gitlabMaxCommentLength is the maximum number of chars allowed by Gitlab in a +// single comment. +const gitlabMaxCommentLength = 1000000 + type GitlabClient struct { Client *gitlab.Client // Version is set to the server version. @@ -146,8 +150,17 @@ func (g *GitlabClient) GetModifiedFiles(repo models.Repo, pull models.PullReques // CreateComment creates a comment on the merge request. func (g *GitlabClient) CreateComment(repo models.Repo, pullNum int, comment string, command string) error { - _, _, err := g.Client.Notes.CreateMergeRequestNote(repo.FullName, pullNum, &gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(comment)}) - return err + sepEnd := "\n```\n" + + "\n
\n\n**Warning**: Output length greater than max comment size. Continued in next comment." + sepStart := "Continued from previous comment.\n
Show Output\n\n" + + "```diff\n" + comments := common.SplitComment(comment, gitlabMaxCommentLength, sepEnd, sepStart) + for _, c := range comments { + if _, _, err := g.Client.Notes.CreateMergeRequestNote(repo.FullName, pullNum, &gitlab.CreateMergeRequestNoteOptions{Body: gitlab.String(c)}); err != nil { + return err + } + } + return nil } func (g *GitlabClient) HidePrevCommandComments(repo models.Repo, pullNum int, command string) error {