Skip to content

Commit

Permalink
Don't try to report problems on unmodified files
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Aug 22, 2024
1 parent c934cc3 commit 4f6d49e
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/pint/tests/0032_ci_github.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0033_ci_github_multi.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0083_github_action.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/foo/bar/pulls/123/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/foo/bar/pulls/123/reviews 200 []
http method github POST /api/v3/repos/foo/bar/pulls/123/reviews 200 {}
http method github GET /api/v3/repos/foo/bar/pulls/123/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0084_github_action_override.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0085_github_no_envs.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/123/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/123/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0098_rule_file_symlink_gh.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0118_ci_dir_move.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0119_ci_fail_on_warning.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/files 200 [{"filename":"rules.yml"}]
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 []
http method github POST /api/v3/repos/cloudflare/pint/pulls/1/reviews 200 {}
http method github GET /api/v3/repos/cloudflare/pint/pulls/1/comments 200 []
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

- If there is a pint config file present then pint will now always add it to the `parser` block `exclude` list.
This is to avoid trying to parse it as a rule file if it's included in the same folder as rules.
- Don't try to report problem on unmodified files when using GitHub reporter.

## v0.64.1

Expand Down
38 changes: 35 additions & 3 deletions internal/reporter/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"log/slog"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -37,6 +38,10 @@ type ghCommentMeta struct {
id int64
}

type ghPR struct {
paths []string
}

// NewGithubReporter creates a new GitHub reporter that reports
// problems via comments on a given pull request number (integer).
func NewGithubReporter(version, baseURL, uploadURL string, timeout time.Duration, token, owner, repo string, prNum, maxComments int, headCommit string) (_ GithubReporter, err error) {
Expand Down Expand Up @@ -84,8 +89,10 @@ func (gr GithubReporter) Describe() string {
return "GitHub"
}

func (gr GithubReporter) Destinations(context.Context) ([]any, error) {
return []any{gr.prNum}, nil
func (gr GithubReporter) Destinations(ctx context.Context) (_ []any, err error) {
pr := ghPR{}
pr.paths, err = gr.listPRFiles(ctx)
return []any{pr}, err
}

func (gr GithubReporter) Summary(ctx context.Context, _ any, s Summary, errs []error) error {
Expand Down Expand Up @@ -144,7 +151,16 @@ func (gr GithubReporter) List(ctx context.Context, _ any) ([]ExistingComment, er
return comments, nil
}

func (gr GithubReporter) Create(ctx context.Context, _ any, p PendingComment) error {
func (gr GithubReporter) Create(ctx context.Context, dst any, p PendingComment) error {
pr := dst.(ghPR)

if !slices.Contains(pr.paths, p.path) {
slog.Debug("Skipping report for path with no changes",
slog.String("path", p.path),
)
return nil
}

var side string
if p.anchor == checks.AnchorBefore {
side = "LEFT"
Expand Down Expand Up @@ -261,6 +277,22 @@ func (gr GithubReporter) createReview(ctx context.Context, summary Summary) erro
return nil
}

func (gr GithubReporter) listPRFiles(ctx context.Context) ([]string, error) {
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

slog.Debug("Getting the list of modified files", slog.Int("pr", gr.prNum))
files, _, err := gr.client.PullRequests.ListFiles(reqCtx, gr.owner, gr.repo, gr.prNum, nil)
if err != nil {
return nil, fmt.Errorf("failed to list pull request files: %w", err)
}
paths := []string{}
for _, file := range files {
paths = append(paths, file.GetFilename())
}
return paths, nil
}

func formatGHReviewBody(version string, summary Summary) string {
var b strings.Builder

Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestGithubReporter(t *testing.T) {
}),
timeout: 100 * time.Millisecond,
error: func(_ string) string {
return "failed to list pull request reviews: context deadline exceeded"
return "failed to list pull request files: context deadline exceeded"
},
reports: []reporter.Report{
{
Expand Down

0 comments on commit 4f6d49e

Please sign in to comment.