Skip to content

Commit

Permalink
github-pr-review: add simple test for summary text
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Sep 29, 2019
1 parent ebce3fb commit eba86ca
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
7 changes: 5 additions & 2 deletions service/github/github.go
Expand Up @@ -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("<details>\n")
sb.WriteString(fmt.Sprintf("<summary>%s</summary>\n", tool))
sb.WriteString("\n")
Expand Down
63 changes: 63 additions & 0 deletions service/github/github_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit eba86ca

Please sign in to comment.