diff --git a/CHANGELOG.md b/CHANGELOG.md index 743f143b..55c4e4a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,15 +9,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### :sparkles: Release Note ### :rocket: Enhancements -- ... +- [#1845](https://github.com/reviewdog/reviewdog/pull/1845) Normalize file path in `related_locations` too. ### :bug: Fixes - [#1846](https://github.com/reviewdog/reviewdog/issues/1846) Unexpected error exit code when using `-reporter=github-[pr-]check` with `-fail-on-error=true`. See the below breaking changes section for more details. -### :rotating_light: (Actually No) Breaking changes -- [#1854](https://github.com/reviewdog/reviewdog/pull/1854) `-fail-on-error` +### :rotating_light: Breaking changes +- [#1858](https://github.com/reviewdog/reviewdog/pull/1858) Remove original_output from rdjson/rdjsonl reporters' output + +### :rotating_light: Deprecation Warnings +- [#1854](https://github.com/reviewdog/reviewdog/pull/1854) (Actually No Breaking changes) `-fail-on-error` flag is deprecated. Use `-fail-level=[none,any,error,warning,info]` flag instead. You can reproduce the same behavior with `-fail-level=any` for most reporters (reviewdog will exit with 1 if it find any severity level). @@ -26,7 +29,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `-fail-on-error` is just deprecated and it's not removed yet. Please update the flag while it's working. - ## [v0.20.1] - 2024-07-10 diff --git a/comment_iowriter.go b/comment_iowriter.go index e69330cf..77c1ea57 100644 --- a/comment_iowriter.go +++ b/comment_iowriter.go @@ -79,6 +79,8 @@ func (cw *RDJSONLCommentWriter) Post(_ context.Context, c *Comment) error { Name: c.ToolName, } } + // Remove OriginalOutput. It's used internally and we shouldn't expose it as output. + c.Result.Diagnostic.OriginalOutput = "" b, err := protojson.MarshalOptions{ UseProtoNames: true, UseEnumNumbers: false, @@ -124,6 +126,8 @@ func (cw *RDJSONCommentWriter) Flush(_ context.Context) error { Diagnostics: make([]*rdf.Diagnostic, 0, len(cw.comments)), } for _, c := range cw.comments { + // Remove OriginalOutput. It's used internally and we shouldn't expose it as output. + c.Result.Diagnostic.OriginalOutput = "" result.Diagnostics = append(result.Diagnostics, c.Result.Diagnostic) } b, err := protojson.MarshalOptions{ diff --git a/comment_iowriter_test.go b/comment_iowriter_test.go index 8aeaf502..ac75f43b 100644 --- a/comment_iowriter_test.go +++ b/comment_iowriter_test.go @@ -104,8 +104,9 @@ func TestRDJSONLCommentWriter_Post(t *testing.T) { in: &Comment{ Result: &filter.FilteredDiagnostic{ Diagnostic: &rdf.Diagnostic{ - Location: &rdf.Location{Path: "/path/to/file"}, - Message: "message", + Location: &rdf.Location{Path: "/path/to/file"}, + Message: "message", + OriginalOutput: "original output", }, }, ToolName: "tool name", @@ -167,8 +168,9 @@ func TestRDJSONCommentWriter_Post(t *testing.T) { { Result: &filter.FilteredDiagnostic{ Diagnostic: &rdf.Diagnostic{ - Location: &rdf.Location{Path: "/path/to/file"}, - Message: "message", + Location: &rdf.Location{Path: "/path/to/file"}, + Message: "message", + OriginalOutput: "original output", }, }, ToolName: "tool name", diff --git a/filter/filter.go b/filter/filter.go index eef96a94..3e9d9297 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -41,7 +41,6 @@ func FilterCheck(results []*rdf.Diagnostic, diff []*diff.FileDiff, strip int, for _, result := range results { check := &FilteredDiagnostic{Diagnostic: result, SourceLines: make(map[int]string)} loc := result.GetLocation() - loc.Path = pathutil.NormalizePath(loc.GetPath(), cwd, "") startLine := int(loc.GetRange().GetStart().GetLine()) endLine := int(loc.GetRange().GetEnd().GetLine()) if endLine == 0 { diff --git a/go.mod b/go.mod index db063de9..ee33f9a4 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.21.7 require ( cloud.google.com/go/compute/metadata v0.5.0 cloud.google.com/go/datastore v1.17.1 - code.gitea.io/sdk/gitea v0.18.0 + code.gitea.io/sdk/gitea v0.19.0 contrib.go.opencensus.io/exporter/stackdriver v0.13.12 github.com/bradleyfalzon/ghinstallation/v2 v2.11.0 github.com/google/go-cmp v0.6.0 @@ -22,9 +22,9 @@ require ( github.com/reviewdog/go-bitbucket v0.0.0-20201024094602-708c3f6a7de0 github.com/stretchr/testify v1.9.0 github.com/vvakame/sdlog v1.2.0 - github.com/xanzy/go-gitlab v0.106.0 + github.com/xanzy/go-gitlab v0.107.0 go.opencensus.io v0.24.0 - golang.org/x/build v0.0.0-20240712162709-0b82a206aadf + golang.org/x/build v0.0.0-20240731015247-ede91cf91ea8 golang.org/x/oauth2 v0.21.0 golang.org/x/sync v0.7.0 google.golang.org/protobuf v1.34.2 diff --git a/go.sum b/go.sum index ad57e8e4..67ae880a 100644 --- a/go.sum +++ b/go.sum @@ -58,8 +58,8 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9 cloud.google.com/go/trace v1.0.0/go.mod h1:4iErSByzxkyHWzzlAj63/Gmjz0NH1ASqhJguHpGcr6A= cloud.google.com/go/trace v1.10.7 h1:gK8z2BIJQ3KIYGddw9RJLne5Fx0FEXkrEQzPaeEYVvk= cloud.google.com/go/trace v1.10.7/go.mod h1:qk3eiKmZX0ar2dzIJN/3QhY2PIFh1eqcIdaN5uEjQPM= -code.gitea.io/sdk/gitea v0.18.0 h1:+zZrwVmujIrgobt6wVBWCqITz6bn1aBjnCUHmpZrerI= -code.gitea.io/sdk/gitea v0.18.0/go.mod h1:IG9xZJoltDNeDSW0qiF2Vqx5orMWa7OhVWrjvrd5NpI= +code.gitea.io/sdk/gitea v0.19.0 h1:8I6s1s4RHgzxiPHhOQdgim1RWIRcr0LVMbHBjBFXq4Y= +code.gitea.io/sdk/gitea v0.19.0/go.mod h1:IG9xZJoltDNeDSW0qiF2Vqx5orMWa7OhVWrjvrd5NpI= contrib.go.opencensus.io/exporter/stackdriver v0.13.12 h1:bjBKzIf7/TAkxd7L2utGaLM78bmUWlCval5K9UeElbY= contrib.go.opencensus.io/exporter/stackdriver v0.13.12/go.mod h1:mmxnWlrvrFdpiOHOhxBaVi1rkc0WOqhgfknj4Yg0SeQ= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= @@ -279,8 +279,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/vvakame/sdlog v1.2.0 h1:gwZRXZ0EmhJQJN/Do/+PTQigcmFiSqZ07aDjxqGOLT8= github.com/vvakame/sdlog v1.2.0/go.mod h1:gFYv2g/iR3pJSxkJz0YnkNmhNbXT5R3PzWsfZKGQADY= -github.com/xanzy/go-gitlab v0.106.0 h1:EDfD03K74cIlQo2EducfiupVrip+Oj02bq9ofw5F8sA= -github.com/xanzy/go-gitlab v0.106.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI= +github.com/xanzy/go-gitlab v0.107.0 h1:P2CT9Uy9yN9lJo3FLxpMZ4xj6uWcpnigXsjvqJ6nd2Y= +github.com/xanzy/go-gitlab v0.107.0/go.mod h1:wKNKh3GkYDMOsGmnfuX+ITCmDuSDWFO0G+C4AygL9RY= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -308,8 +308,8 @@ go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35 go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI= -golang.org/x/build v0.0.0-20240712162709-0b82a206aadf h1:yxXHOnHUeXT9mop6DOYhyBNeVl20sSyKYR6+5YAwLnk= -golang.org/x/build v0.0.0-20240712162709-0b82a206aadf/go.mod h1:YsGhg4JUVUWLzdqU2wCrtpRrOveOql6w56FLDHq/CJ4= +golang.org/x/build v0.0.0-20240731015247-ede91cf91ea8 h1:RnTh6wT7sv6wjo49xAgWQBozAvNnPfNoXJZGUNbR4x0= +golang.org/x/build v0.0.0-20240731015247-ede91cf91ea8/go.mod h1:C12yKxWgJeykUO2lfQINDq2U/JrauqR2Rh/tQeFnU5I= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go index a714660a..0e8dc929 100644 --- a/pathutil/pathutil.go +++ b/pathutil/pathutil.go @@ -3,6 +3,8 @@ package pathutil import ( "path/filepath" "strings" + + "github.com/reviewdog/reviewdog/proto/rdf" ) // NormalizePath return normalized path with workdir and relative path to @@ -42,6 +44,22 @@ func NormalizeDiffPath(diffpath string, strip int) string { return filepath.ToSlash(filepath.Clean(path)) } +// NormalizePathInResults normalize file path in RDFormat results. +func NormalizePathInResults(results []*rdf.Diagnostic, cwd string) { + for _, result := range results { + normalizeLocation(result.GetLocation(), cwd) + for _, rel := range result.GetRelatedLocations() { + normalizeLocation(rel.GetLocation(), cwd) + } + } +} + +func normalizeLocation(loc *rdf.Location, cwd string) { + if loc != nil { + loc.Path = NormalizePath(loc.GetPath(), cwd, "") + } +} + func contains(path, base string) bool { ps := splitPathList(path) bs := splitPathList(base) diff --git a/pathutil/pathutil_test.go b/pathutil/pathutil_test.go new file mode 100644 index 00000000..7ff63333 --- /dev/null +++ b/pathutil/pathutil_test.go @@ -0,0 +1,49 @@ +package pathutil + +import ( + "strings" + "testing" + + "github.com/reviewdog/reviewdog/proto/rdf" +) + +func TestNormalizePathInResults(t *testing.T) { + cwd := "/path/to/cwd" + results := []*rdf.Diagnostic{ + { + Location: &rdf.Location{ + Path: cwd + "/" + "sample_1_abs.txt", + }, + }, + { + Location: &rdf.Location{ + Path: "sample_2_rel.txt", + }, + }, + { + RelatedLocations: []*rdf.RelatedLocation{ + { + Location: &rdf.Location{ + Path: cwd + "/" + "sample_related_1_abs.txt", + }, + }, + { + Location: &rdf.Location{ + Path: "sample_related_2_rel.txt", + }, + }, + }, + }, + } + NormalizePathInResults(results, cwd) + for _, result := range results { + if strings.HasPrefix(result.GetLocation().GetPath(), cwd) { + t.Errorf("path unexpectedly contain prefix: %s", result.GetLocation().GetPath()) + } + for _, rel := range result.GetRelatedLocations() { + if strings.HasPrefix(rel.GetLocation().GetPath(), cwd) { + t.Errorf("related locations path unexpectedly contain prefix: %s", rel.GetLocation().GetPath()) + } + } + } +} diff --git a/reviewdog.go b/reviewdog.go index 9bc0b78f..18c1c85a 100644 --- a/reviewdog.go +++ b/reviewdog.go @@ -10,6 +10,7 @@ import ( "github.com/reviewdog/reviewdog/diff" "github.com/reviewdog/reviewdog/filter" "github.com/reviewdog/reviewdog/parser" + "github.com/reviewdog/reviewdog/pathutil" "github.com/reviewdog/reviewdog/proto/rdf" ) @@ -73,6 +74,8 @@ func (w *Reviewdog) runFromResult(ctx context.Context, results []*rdf.Diagnostic return err } + pathutil.NormalizePathInResults(results, wd) + checks := filter.FilterCheck(results, filediffs, strip, wd, w.filterMode) shouldFail := false diff --git a/service/github/diff.go b/service/github/diff.go index ecb66db9..0bbd04ea 100644 --- a/service/github/diff.go +++ b/service/github/diff.go @@ -35,7 +35,7 @@ func (p *PullRequestDiffService) Diff(ctx context.Context) ([]byte, error) { d, resp, err := p.Cli.PullRequests.GetRaw(ctx, p.Owner, p.Repo, p.PR, opt) if err != nil { if resp != nil && p.FallBackToGitCLI && resp.StatusCode == http.StatusNotAcceptable { - log.Print("reviwedog: fallback to use git command") + log.Print("reviewdog: fallback to use git command") return p.diffUsingGitCommand(ctx) }