Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi-line comments. #1566

Merged
merged 3 commits into from Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions github/github-stringify_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions github/pulls_reviews.go
Expand Up @@ -7,10 +7,13 @@ package github

import (
"context"
"errors"
"fmt"
"time"
)

var ErrMixedCommentStyles = errors.New("cannot use both position and side/line form comments")

// PullRequestReview represents a review of a pull request.
type PullRequestReview struct {
ID *int64 `json:"id,omitempty"`
Expand All @@ -36,6 +39,12 @@ type DraftReviewComment struct {
Path *string `json:"path,omitempty"`
Position *int `json:"position,omitempty"`
Body *string `json:"body,omitempty"`

// The new comfort-fade-preview fields
StartSide *string `json:"start_side,omitempty"`
Side *string `json:"side,omitempty"`
StartLine *int `json:"start_line,omitempty"`
Line *int `json:"line,omitempty"`
}

func (c DraftReviewComment) String() string {
Expand All @@ -55,6 +64,32 @@ func (r PullRequestReviewRequest) String() string {
return Stringify(r)
}

func (r PullRequestReviewRequest) isComfortFadePreview() (bool, error) {
var isCF *bool
for _, comment := range r.Comments {
if comment == nil {
continue
}
hasPos := comment.Position != nil
hasComfortFade := (comment.StartSide != nil) || (comment.Side != nil) ||
(comment.StartLine != nil) || (comment.Line != nil)

switch {
case hasPos && hasComfortFade:
return false, ErrMixedCommentStyles
case hasPos && isCF != nil && *isCF:
return false, ErrMixedCommentStyles
case hasComfortFade && isCF != nil && !*isCF:
return false, ErrMixedCommentStyles
}
isCF = &hasComfortFade
}
if isCF != nil {
return *isCF, nil
}
return false, nil
}

// PullRequestReviewDismissalRequest represents a request to dismiss a review.
type PullRequestReviewDismissalRequest struct {
Message *string `json:"message,omitempty"`
Expand Down Expand Up @@ -183,6 +218,15 @@ func (s *PullRequestsService) CreateReview(ctx context.Context, owner, repo stri
return nil, nil, err
}

// Detect which style of review comment is being used.
if isCF, err := review.isComfortFadePreview(); err != nil {
return nil, nil, err
} else if isCF {
// If the review comments are using the comfort fade preview fields,
// then pass the comfort fade header.
req.Header.Set("Accept", "application/vnd.github.comfort-fade-preview+json")
mattmoor marked this conversation as resolved.
Show resolved Hide resolved
}

r := new(PullRequestReview)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
Expand Down
117 changes: 116 additions & 1 deletion github/pulls_reviews_test.go
Expand Up @@ -73,7 +73,7 @@ func TestReviewers_marshall(t *testing.T) {
"created_at": ` + referenceTimeStr + `,
"url": "u"
}
],
],
"teams" : [
{
"id": 1,
Expand Down Expand Up @@ -228,6 +228,121 @@ func TestPullRequestsService_ListReviewComments_withOptions(t *testing.T) {
}
}

func TestPullRequestReviewRequest_isComfortFadePreview(t *testing.T) {
path := "path/to/file.go"
body := "this is a comment body"
left, right := "LEFT", "RIGHT"
pos1, pos2, pos3 := 1, 2, 3
line1, line2, line3 := 11, 22, 33

tests := []struct {
name string
review *PullRequestReviewRequest
wantErr error
wantBool bool
}{{
name: "empty review",
review: &PullRequestReviewRequest{},
wantBool: false,
}, {
name: "old-style review",
review: &PullRequestReviewRequest{
Comments: []*DraftReviewComment{{
Path: &path,
Body: &body,
Position: &pos1,
}, {
Path: &path,
Body: &body,
Position: &pos2,
}, {
Path: &path,
Body: &body,
Position: &pos3,
}},
},
wantBool: false,
}, {
name: "new-style review",
review: &PullRequestReviewRequest{
Comments: []*DraftReviewComment{{
Path: &path,
Body: &body,
Side: &right,
Line: &line1,
}, {
Path: &path,
Body: &body,
Side: &left,
Line: &line2,
}, {
Path: &path,
Body: &body,
Side: &right,
Line: &line3,
}},
},
wantBool: true,
}, {
name: "blended comment",
review: &PullRequestReviewRequest{
Comments: []*DraftReviewComment{{
Path: &path,
Body: &body,
Position: &pos1, // can't have both styles.
Side: &right,
Line: &line1,
}},
},
wantErr: ErrMixedCommentStyles,
}, {
name: "position then line",
review: &PullRequestReviewRequest{
Comments: []*DraftReviewComment{{
Path: &path,
Body: &body,
Position: &pos1,
}, {
Path: &path,
Body: &body,
Side: &right,
Line: &line1,
}},
},
wantErr: ErrMixedCommentStyles,
}, {
name: "line then position",
review: &PullRequestReviewRequest{
Comments: []*DraftReviewComment{{
Path: &path,
Body: &body,
Side: &right,
Line: &line1,
}, {
Path: &path,
Body: &body,
Position: &pos1,
}},
},
wantErr: ErrMixedCommentStyles,
}}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotBool, gotErr := tc.review.isComfortFadePreview()
if tc.wantErr != nil {
if gotErr != tc.wantErr {
t.Errorf("isComfortFadePreview() = %v, wanted %v", gotErr, tc.wantErr)
}
} else {
if gotBool != tc.wantBool {
t.Errorf("isComfortFadePreview() = %v, wanted %v", gotBool, tc.wantBool)
}
}
})
}
}

func TestPullRequestsService_ListReviewComments_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
Expand Down