From 4568213356d7315ba36c6c091715fc24d4087001 Mon Sep 17 00:00:00 2001 From: Chris Alexander Date: Thu, 9 Jul 2020 17:18:08 -0500 Subject: [PATCH] Reversed if statement for comment splitting --- server/events/vcs/github_client.go | 4 +- server/events/vcs/github_client_test.go | 68 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/server/events/vcs/github_client.go b/server/events/vcs/github_client.go index fc1b8c43d9..43e0deb1f7 100644 --- a/server/events/vcs/github_client.go +++ b/server/events/vcs/github_client.go @@ -141,10 +141,10 @@ func (g *GithubClient) CreateComment(repo models.Repo, pullNum int, comment stri "\n
\n\n**Warning**: Output length greater than max comment size. Continued in next comment." if command != "" { - sepStart = "Continued from previous comment.\n
Show Output\n\n" + + sepStart = fmt.Sprintf("Continued %s output from previous comment.\n
Show Output\n\n", command) + "```diff\n" } else { - sepStart = fmt.Sprintf("Continued %s output from previous comment.\n
Show Output\n\n", command) + + sepStart = "Continued from previous comment.\n
Show Output\n\n" + "```diff\n" } diff --git a/server/events/vcs/github_client_test.go b/server/events/vcs/github_client_test.go index abff8fb215..6c46fe07ea 100644 --- a/server/events/vcs/github_client_test.go +++ b/server/events/vcs/github_client_test.go @@ -784,3 +784,71 @@ func disableSSLVerification() func() { http.DefaultTransport.(*http.Transport).TLSClientConfig = orig } } + +func TestGithubClient_SplitComments(t *testing.T) { + type githubComment struct { + Body string `json:"body"` + } + githubComments := make([]githubComment, 0, 1) + + testServer := httptest.NewTLSServer( + http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + switch r.Method + " " + r.RequestURI { + case "POST /api/v3/repos/runatlantis/atlantis/issues/1/comments": + defer r.Body.Close() // nolint: errcheck + body, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("read body error: %v", err) + http.Error(w, "server error", http.StatusInternalServerError) + return + } + requestBody := githubComment{} + err = json.Unmarshal(body, &requestBody) + if err != nil { + t.Errorf("parse body error: %v", err) + http.Error(w, "server error", http.StatusInternalServerError) + return + } + githubComments = append(githubComments, requestBody) + return + default: + t.Errorf("got unexpected request at %q", r.RequestURI) + http.Error(w, "not found", http.StatusNotFound) + return + } + })) + + testServerURL, err := url.Parse(testServer.URL) + Ok(t, err) + client, err := vcs.NewGithubClient(testServerURL.Host, &vcs.GithubUserCredentials{"user", "pass"}, nil) + Ok(t, err) + defer disableSSLVerification()() + pull := models.PullRequest{Num: 1} + repo := models.Repo{ + FullName: "runatlantis/atlantis", + Owner: "runatlantis", + Name: "atlantis", + CloneURL: "", + SanitizedCloneURL: "", + VCSHost: models.VCSHost{ + Type: models.Github, + Hostname: "github.com", + }, + } + // create an extra long string + comment := strings.Repeat("a", 65537) + err = client.CreateComment(repo, pull.Num, comment, models.PlanCommand.String()) + Ok(t, err) + err = client.CreateComment(repo, pull.Num, comment, "") + Ok(t, err) + + body := strings.Split(githubComments[1].Body, "\n") + firstSplit := strings.ToLower(body[0]) + body = strings.Split(githubComments[3].Body, "\n") + secondSplit := strings.ToLower(body[0]) + + Equals(t, 4, len(githubComments)) + Assert(t, strings.Contains(firstSplit, models.PlanCommand.String()), fmt.Sprintf("comment should contain the command name but was %q", firstSplit)) + Assert(t, strings.Contains(secondSplit, "continued from previous comment"), fmt.Sprintf("comment should contain no reference to the command name but was %q", secondSplit)) +}