From eba86ca404f59bfd77893328436ed02421c81d99 Mon Sep 17 00:00:00 2001 From: haya14busa Date: Sun, 29 Sep 2019 11:46:58 +0000 Subject: [PATCH] github-pr-review: add simple test for summary text --- service/github/github.go | 7 ++-- service/github/github_test.go | 63 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/service/github/github.go b/service/github/github.go index 01b6d1413b..a9440429dc 100644 --- a/service/github/github.go +++ b/service/github/github.go @@ -121,14 +121,17 @@ func (g *GitHubPullRequest) postAsReviewComment(ctx context.Context) error { } func (g *GitHubPullRequest) remainingCommentsSummary(remaining []*reviewdog.Comment) string { + if len(remaining) == 0 { + return "" + } perTool := make(map[string][]*reviewdog.Comment) for _, c := range remaining { perTool[c.ToolName] = append(perTool[c.ToolName], c) } var sb strings.Builder + sb.WriteString("Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit\n") + sb.WriteString("\n") for tool, comments := range perTool { - sb.WriteString("Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit\n") - sb.WriteString("\n") sb.WriteString("
\n") sb.WriteString(fmt.Sprintf("%s\n", tool)) sb.WriteString("\n") diff --git a/service/github/github_test.go b/service/github/github_test.go index 7b098914ce..2ac685fc85 100644 --- a/service/github/github_test.go +++ b/service/github/github_test.go @@ -286,6 +286,69 @@ func TestGitHubPullRequest_Post_Flush_review_api(t *testing.T) { } } +func TestGitHubPullRequest_Post_toomany(t *testing.T) { + cwd, _ := os.Getwd() + defer os.Chdir(cwd) + + moveToRootDir() + + listCommentsAPICalled := 0 + postCommentsAPICalled := 0 + + mux := http.NewServeMux() + mux.HandleFunc("/repos/o/r/pulls/14/comments", func(w http.ResponseWriter, r *http.Request) { + listCommentsAPICalled++ + if err := json.NewEncoder(w).Encode([]*github.PullRequestComment{}); err != nil { + t.Fatal(err) + } + }) + mux.HandleFunc("/repos/o/r/pulls/14/reviews", func(w http.ResponseWriter, r *http.Request) { + postCommentsAPICalled++ + var req github.PullRequestReviewRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + t.Error(err) + } + if req.GetBody() == "" { + t.Errorf("PullRequestReviewRequest.Body is empty but want some summary text") + } + }) + ts := httptest.NewServer(mux) + defer ts.Close() + + cli := github.NewClient(nil) + cli.BaseURL, _ = url.Parse(ts.URL + "/") + g, err := NewGitHubPullRequest(cli, "o", "r", 14, "sha") + if err != nil { + t.Fatal(err) + } + comments := []*reviewdog.Comment{} + for i := 0; i < 100; i++ { + comments = append(comments, &reviewdog.Comment{ + CheckResult: &reviewdog.CheckResult{ + Path: "reviewdog.go", + Lnum: i, + }, + LnumDiff: i, + Body: "comment", + ToolName: "tool", + }) + } + for _, c := range comments { + if err := g.Post(context.Background(), c); err != nil { + t.Error(err) + } + } + if err := g.Flush(context.Background()); err != nil { + t.Error(err) + } + if want := 1; listCommentsAPICalled != want { + t.Errorf("GitHub List PullRequest comments API called %v times, want %d times", listCommentsAPICalled, want) + } + if want := 1; postCommentsAPICalled != want { + t.Errorf("GitHub post PullRequest comments API called %v times, want %d times", postCommentsAPICalled, want) + } +} + func TestGitHubPullRequest_workdir(t *testing.T) { cwd, _ := os.Getwd() defer os.Chdir(cwd)