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 e97d6a5 commit 5fef79c
Show file tree
Hide file tree
Showing 10 changed files with 44 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
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 5fef79c

Please sign in to comment.