Repository: reviewdog/reviewdog
Pull request #793 (2020-10-20 15:27:35)

github-pr-review: [suggestion] support start only location with ranged suggestion

- [x] Updated Unreleased section in [CHANGELOG](https://github.com/reviewdog/reviewdog/blob/master/CHANGELOG.md) or it's not notable changes.




Patch:
diff --git a/go.mod b/go.mod
index 5c92097f..477dea9b 100644
--- a/go.mod
+++ b/go.mod
@@ -21,7 +21,7 @@ require (
 	github.com/xanzy/go-gitlab v0.38.2
 	go.opencensus.io v0.22.5
 	golang.org/x/build v0.0.0-20200616162219-07bebbe343e9
-	golang.org/x/net v0.0.0-20201020065357-d65d470038a5
+	golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0
 	golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
 	golang.org/x/sync v0.0.0-20201008141435-b3e1573b7520
 	google.golang.org/protobuf v1.25.0
diff --git a/go.sum b/go.sum
index d419cc85..9e6e457c 100644
--- a/go.sum
+++ b/go.sum
@@ -322,8 +322,8 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up
 golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
 golang.org/x/net v0.0.0-20201010224723-4f7140c49acb h1:mUVeFHoDKis5nxCAzoAi7E8Ghb86EXh/RK6wtvJIqRY=
 golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
-golang.org/x/net v0.0.0-20201020065357-d65d470038a5 h1:KrxvpY64uUzANd9wKWr6ZAsufiii93XnvXaeikyCJ2g=
-golang.org/x/net v0.0.0-20201020065357-d65d470038a5/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0 h1:5kGOVHlq0euqwzgTC9Vu15p6fV1Wi0ArVi8da2urnVg=
+golang.org/x/net v0.0.0-20201016165138-7b1cca2348c0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
 golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
 golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
diff --git a/service/github/github.go b/service/github/github.go
index 19b113b3..53568749 100644
--- a/service/github/github.go
+++ b/service/github/github.go
@@ -278,11 +278,21 @@ func buildSuggestions(c *reviewdog.Comment) string {
 func buildSingleSuggestion(c *reviewdog.Comment, s *rdf.Suggestion) (string, error) {
 	start := s.GetRange().GetStart()
 	end := s.GetRange().GetEnd()
+	endLine := end.GetLine()
+	if endLine == 0 {
+		endLine = start.GetLine()
+	}
 	drange := c.Result.Diagnostic.GetLocation().GetRange()
+	dendLine := drange.GetEnd().GetLine()
+	if dendLine == 0 {
+		// As long as suggestion has valid range, it's ok even if diagnostic
+		// location only contains start.
+		dendLine = drange.GetStart().GetLine()
+	}
 	if start.GetLine() != drange.GetStart().GetLine() ||
-		end.GetLine() != drange.GetEnd().GetLine() {
+		(endLine != dendLine) {
 		return "", fmt.Errorf("the Diagnostic's lines and Suggestion lines must be the same. L%d-L%d v.s. L%d-L%d",
-			drange.GetStart().GetLine(), drange.GetEnd().GetLine(), start.GetLine(), end.GetLine())
+			drange.GetStart().GetLine(), dendLine, start.GetLine(), endLine)
 	}
 	if start.GetColumn() > 0 || end.GetColumn() > 0 {
 		return buildNonLineBasedSuggestion(c, s)


Test patch:
diff --git a/service/github/github_test.go b/service/github/github_test.go
index 30fef898..2c0bc4c5 100644
--- a/service/github/github_test.go
+++ b/service/github/github_test.go
@@ -392,6 +392,17 @@ func TestGitHubPullRequest_Post_Flush_review_api(t *testing.T) {
 					"```",
 				}, "\n") + "\n"),
 			},
+			{
+				Path: github.String("reviewdog.go"),
+				Side: github.String("RIGHT"),
+				Line: github.Int(15),
+				Body: github.String(commentutil.BodyPrefix + strings.Join([]string{
+					"range suggestion with start only location",
+					"```suggestion",
+					"haya14busa",
+					"```",
+				}, "\n") + "\n"),
+			},
 		}
 		if diff := pretty.Compare(want, req.Comments); diff != "" {
 			t.Errorf("req.Comments diff: (-got +want)\n%s", diff)
@@ -797,6 +808,30 @@ func TestGitHubPullRequest_Post_Flush_review_api(t *testing.T) {
 				InDiffContext: true,
 			},
 		},
+		{
+			Result: &filter.FilteredDiagnostic{
+				SourceLines: []string{"haya15busa"},
+				Diagnostic: &rdf.Diagnostic{
+					Location: &rdf.Location{
+						Path: "reviewdog.go",
+						Range: &rdf.Range{
+							Start: &rdf.Position{Line: 15, Column: 5},
+						},
+					},
+					Suggestions: []*rdf.Suggestion{
+						{
+							Range: &rdf.Range{
+								Start: &rdf.Position{Line: 15, Column: 5},
+								End:   &rdf.Position{Line: 15, Column: 7},
+							},
+							Text: "14",
+						},
+					},
+					Message: "range suggestion with start only location",
+				},
+				InDiffContext: true,
+			},
+		},
 	}
 	for _, c := range comments {
 		if err := g.Post(context.Background(), c); err != nil {