From e32a57b6f3a483da3eb80998291e00d343e8300a Mon Sep 17 00:00:00 2001 From: Tobias Bielohlawek Date: Sun, 28 Oct 2018 22:36:12 +0000 Subject: [PATCH] Fix broken format for continued comments on GitHub Adds close and (re)open
tag in between the split portions to ensure the markup is correct across multiple comments. This isn't the prettiest, but a close result, under the assumption continued-comments are in 99% within a terraform output anyway... --- server/events/vcs/github_client.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/server/events/vcs/github_client.go b/server/events/vcs/github_client.go index 3e1e6a82c..171a1f52c 100644 --- a/server/events/vcs/github_client.go +++ b/server/events/vcs/github_client.go @@ -25,9 +25,15 @@ import ( "github.com/runatlantis/atlantis/server/events/models" ) +const detailsClose = "```\n
" + + "continued...\n" +const detailsOpen = "
Show Output\n\n" + + "```diff\n" + // maxCommentBodySize is derived from the error message when you go over // this limit. -const maxCommentBodySize = 65536 +// We deduct some characters for appending details close/open tag +const maxCommentBodySize = 65536 - len(detailsClose) - len(detailsOpen) // GithubClient is used to perform GitHub actions. type GithubClient struct { @@ -91,7 +97,7 @@ func (g *GithubClient) GetModifiedFiles(repo models.Repo, pull models.PullReques // If comment length is greater than the max comment length we split into // multiple comments. func (g *GithubClient) CreateComment(repo models.Repo, pullNum int, comment string) error { - comments := g.splitAtMaxChars(comment, maxCommentBodySize, "\ncontinued...\n") + comments := g.splitAtMaxChars(comment, maxCommentBodySize) for _, c := range comments { _, _, err := g.client.Issues.CreateComment(g.ctx, repo.Owner, repo.Name, pullNum, &github.IssueComment{Body: &c}) if err != nil { @@ -144,28 +150,23 @@ func (g *GithubClient) UpdateStatus(repo models.Repo, pull models.PullRequest, s // splitAtMaxChars splits comment into a slice with string up to max // len separated by join which gets appended to the ends of the middle strings. -// If max <= len(join) we return an empty slice since this is an edge case we -// don't want to handle. // nolint: unparam -func (g *GithubClient) splitAtMaxChars(comment string, max int, join string) []string { +func (g *GithubClient) splitAtMaxChars(comment string, maxSize int) []string { // If we're under the limit then no need to split. - if len(comment) <= max { + if len(comment) <= maxSize { return []string{comment} } - // If we can't fit the joining string in then this doesn't make sense. - if max <= len(join) { - return nil - } - var comments []string - maxSize := max - len(join) numComments := int(math.Ceil(float64(len(comment)) / float64(maxSize))) for i := 0; i < numComments; i++ { upTo := g.min(len(comment), (i+1)*maxSize) portion := comment[i*maxSize : upTo] if i < numComments-1 { - portion += join + portion += detailsClose + } + if i > 0 { + portion = detailsOpen + portion } comments = append(comments, portion) }