Skip to content

Commit

Permalink
Reversed if statement for comment splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
crainte committed Jul 10, 2020
1 parent 1944657 commit 1e34107
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
4 changes: 2 additions & 2 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ func (g *GithubClient) CreateComment(repo models.Repo, pullNum int, comment stri
"\n<br>\n\n**Warning**: Output length greater than max comment size. Continued in next comment."

if command != "" {
sepStart = "Continued from previous comment.\n<details><summary>Show Output</summary>\n\n" +
sepStart = fmt.Sprintf("Continued %s output from previous comment.\n<details><summary>Show Output</summary>\n\n", command) +
"```diff\n"
} else {
sepStart = fmt.Sprintf("Continued %s output from previous comment.\n<details><summary>Show Output</summary>\n\n", command) +
sepStart = "Continued from previous comment.\n<details><summary>Show Output</summary>\n\n" +
"```diff\n"
}

Expand Down
64 changes: 64 additions & 0 deletions server/events/vcs/github_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,3 +784,67 @@ 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)

// copied from github_client.HidePrevPlanComments to ensure proper string matching
body := strings.Split(githubComments[1].Body, "\n")
firstLine := strings.ToLower(body[0])

Equals(t, 2, len(githubComments))
Assert(t, strings.Contains(firstLine, models.PlanCommand.String()), fmt.Sprintf("comment should contain the command name but was %q", firstLine))
}

0 comments on commit 1e34107

Please sign in to comment.