From ceb3b65648c08fbe1557f2aa220ec95c31248737 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Wed, 27 Mar 2024 12:48:42 +0000 Subject: [PATCH] Fix maxComment comment on github Fixes #935 --- docs/changelog.md | 1 + go.mod | 2 +- go.sum | 4 ++-- internal/reporter/github.go | 18 ++++++++++++------ internal/reporter/github_test.go | 2 +- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 0d11873c..c0f7bd4f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,7 @@ - Fixed compatibility with older git releases. - Fixed `maxComments` defaulting to zero when using GitHub reporter. +- Fixed `maxComments` comment creation on GitHub - #935. ## v0.57.2 diff --git a/go.mod b/go.mod index 2c232299..27759814 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/fatih/color v1.16.0 github.com/gkampitakis/go-snaps v0.5.2 github.com/google/go-cmp v0.6.0 - github.com/google/go-github/v57 v57.0.0 + github.com/google/go-github/v60 v60.0.0 github.com/hashicorp/hcl/v2 v2.20.0 github.com/klauspost/compress v1.17.7 github.com/neilotoole/slogt v1.1.0 diff --git a/go.sum b/go.sum index ceed6ae8..bc67e2f3 100644 --- a/go.sum +++ b/go.sum @@ -214,8 +214,8 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/go-github/v57 v57.0.0 h1:L+Y3UPTY8ALM8x+TV0lg+IEBI+upibemtBD8Q9u7zHs= -github.com/google/go-github/v57 v57.0.0/go.mod h1:s0omdnye0hvK/ecLvpsGfJMiRt85PimQh4oygmLIxHw= +github.com/google/go-github/v60 v60.0.0 h1:oLG98PsLauFvvu4D/YPxq374jhSxFYdzQGNCyONLfn8= +github.com/google/go-github/v60 v60.0.0/go.mod h1:ByhX2dP9XT9o/ll2yXAu2VD8l5eNVg8hD4Cr0S/LmQk= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= diff --git a/internal/reporter/github.go b/internal/reporter/github.go index e3b13baa..666ebb9d 100644 --- a/internal/reporter/github.go +++ b/internal/reporter/github.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/google/go-github/v57/github" + "github.com/google/go-github/v60/github" "golang.org/x/oauth2" "github.com/cloudflare/pint/internal/checks" @@ -173,7 +173,7 @@ func (gr GithubReporter) addReviewComments(headCommit string, summary Summary) e added++ if added >= gr.maxComments { - return gr.tooManyComments(headCommit, len(summary.Reports())) + return gr.tooManyComments(len(summary.Reports())) } } @@ -336,11 +336,17 @@ func reportToGitHubComment(headCommit string, rep Report) *github.PullRequestCom return &c } -func (gr GithubReporter) tooManyComments(headCommit string, nrComments int) error { - comment := github.PullRequestComment{ - CommitID: github.String(headCommit), +func (gr GithubReporter) tooManyComments(nrComments int) error { + comment := github.IssueComment{ Body: github.String(fmt.Sprintf(`This pint run would create %d comment(s), which is more than %d limit configured for pint. %d comments were skipped and won't be visibile on this PR.`, nrComments, gr.maxComments, nrComments-gr.maxComments)), } - return gr.createComment(&comment) + + slog.Debug("Creating PR comment", slog.String("body", comment.GetBody())) + + ctx, cancel := context.WithTimeout(context.Background(), gr.timeout) + defer cancel() + + _, _, err := gr.client.Issues.CreateComment(ctx, gr.owner, gr.repo, gr.prNum, &comment) + return err } diff --git a/internal/reporter/github_test.go b/internal/reporter/github_test.go index 4f47bcf1..ccff8130 100644 --- a/internal/reporter/github_test.go +++ b/internal/reporter/github_test.go @@ -355,7 +355,7 @@ filename %s switch b { case `{"body":":stop_sign: [mock1](https://cloudflare.github.io/pint/checks/mock1.html): syntax error1\n\nsyntax details1","path":"","line":2,"side":"RIGHT","commit_id":"fake-commit-id"}`: case `{"body":":stop_sign: [mock2](https://cloudflare.github.io/pint/checks/mock2.html): syntax error2\n\nsyntax details2","path":"","line":2,"side":"RIGHT","commit_id":"fake-commit-id"}`: - case `{"body":"This pint run would create 4 comment(s), which is more than 2 limit configured for pint.\n2 comments were skipped and won't be visibile on this PR.","commit_id":"fake-commit-id"}`: + case `{"body":"This pint run would create 4 comment(s), which is more than 2 limit configured for pint.\n2 comments were skipped and won't be visibile on this PR."}`: default: t.Errorf("Unexpected comment: %s", b) }