Skip to content

Commit

Permalink
Merge pull request #793 from reviewdog/github-pr-review-suggest-with-…
Browse files Browse the repository at this point in the history
…single-loc

github-pr-review: [suggestion] support start only location with ranged suggestion
  • Loading branch information
haya14busa committed Oct 20, 2020
2 parents 59f48c0 + 0b3a5b1 commit f1aa5e0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
14 changes: 12 additions & 2 deletions service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions service/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f1aa5e0

Please sign in to comment.