From 834c19cb85d076f2a497832391d4cf2877d6ea5c Mon Sep 17 00:00:00 2001 From: emersonwood Date: Fri, 13 Jul 2018 15:24:25 +1200 Subject: [PATCH 1/2] Fix InReplyToID JSON tag for PullRequestComment --- github/github-accessors.go | 8 ++++---- github/pulls_comments.go | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index d9939c2043f..1e8e2e28b02 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -6364,12 +6364,12 @@ func (p *PullRequestComment) GetID() int64 { return *p.ID } -// GetInReplyTo returns the InReplyTo field if it's non-nil, zero value otherwise. -func (p *PullRequestComment) GetInReplyTo() int64 { - if p == nil || p.InReplyTo == nil { +// GetInReplyToID returns the InReplyToID field if it's non-nil, zero value otherwise. +func (p *PullRequestComment) GetInReplyToID() int64 { + if p == nil || p.InReplyToID == nil { return 0 } - return *p.InReplyTo + return *p.InReplyToID } // GetOriginalCommitID returns the OriginalCommitID field if it's non-nil, zero value otherwise. diff --git a/github/pulls_comments.go b/github/pulls_comments.go index a7f8ac30c3e..bb151f1aea6 100644 --- a/github/pulls_comments.go +++ b/github/pulls_comments.go @@ -14,7 +14,7 @@ import ( // PullRequestComment represents a comment left on a pull request. type PullRequestComment struct { ID *int64 `json:"id,omitempty"` - InReplyTo *int64 `json:"in_reply_to,omitempty"` + InReplyToID *int64 `json:"in_reply_to_id,omitempty"` Body *string `json:"body,omitempty"` Path *string `json:"path,omitempty"` DiffHunk *string `json:"diff_hunk,omitempty"` @@ -115,7 +115,37 @@ func (s *PullRequestsService) GetComment(ctx context.Context, owner string, repo // GitHub API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment func (s *PullRequestsService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) { u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number) - req, err := s.client.NewRequest("POST", u, comment) + + // pullRequestCreateComment is an alias to PullRequestComment that corrects the InReplyToID field tag from `in_reply_to_id` to `in_reply_to`. + type pullRequestCreateComment struct { + ID *int64 `json:"id,omitempty"` + InReplyToID *int64 `json:"in_reply_to,omitempty"` + Body *string `json:"body,omitempty"` + Path *string `json:"path,omitempty"` + DiffHunk *string `json:"diff_hunk,omitempty"` + PullRequestReviewID *int64 `json:"pull_request_review_id,omitempty"` + Position *int `json:"position,omitempty"` + OriginalPosition *int `json:"original_position,omitempty"` + CommitID *string `json:"commit_id,omitempty"` + OriginalCommitID *string `json:"original_commit_id,omitempty"` + User *User `json:"user,omitempty"` + Reactions *Reactions `json:"reactions,omitempty"` + CreatedAt *time.Time `json:"created_at,omitempty"` + UpdatedAt *time.Time `json:"updated_at,omitempty"` + // AuthorAssociation is the comment author's relationship to the pull request's repository. + // Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE". + AuthorAssociation *string `json:"author_association,omitempty"` + URL *string `json:"url,omitempty"` + HTMLURL *string `json:"html_url,omitempty"` + PullRequestURL *string `json:"pull_request_url,omitempty"` + } + var alias *pullRequestCreateComment + if comment != nil { + a := pullRequestCreateComment(*comment) + alias = &a + } + + req, err := s.client.NewRequest("POST", u, alias) if err != nil { return nil, nil, err } From 03410e2b2d2601360ed578aa95f8709bcd9f3647 Mon Sep 17 00:00:00 2001 From: emersonwood Date: Mon, 16 Jul 2018 19:43:29 +1200 Subject: [PATCH 2/2] Add CreateCommentInReplyTo helper function --- github/github-accessors.go | 8 +++--- github/pulls_comments.go | 54 ++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 1e8e2e28b02..d9939c2043f 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -6364,12 +6364,12 @@ func (p *PullRequestComment) GetID() int64 { return *p.ID } -// GetInReplyToID returns the InReplyToID field if it's non-nil, zero value otherwise. -func (p *PullRequestComment) GetInReplyToID() int64 { - if p == nil || p.InReplyToID == nil { +// GetInReplyTo returns the InReplyTo field if it's non-nil, zero value otherwise. +func (p *PullRequestComment) GetInReplyTo() int64 { + if p == nil || p.InReplyTo == nil { return 0 } - return *p.InReplyToID + return *p.InReplyTo } // GetOriginalCommitID returns the OriginalCommitID field if it's non-nil, zero value otherwise. diff --git a/github/pulls_comments.go b/github/pulls_comments.go index bb151f1aea6..f3067762566 100644 --- a/github/pulls_comments.go +++ b/github/pulls_comments.go @@ -14,7 +14,7 @@ import ( // PullRequestComment represents a comment left on a pull request. type PullRequestComment struct { ID *int64 `json:"id,omitempty"` - InReplyToID *int64 `json:"in_reply_to_id,omitempty"` + InReplyTo *int64 `json:"in_reply_to_id,omitempty"` Body *string `json:"body,omitempty"` Path *string `json:"path,omitempty"` DiffHunk *string `json:"diff_hunk,omitempty"` @@ -115,37 +115,33 @@ func (s *PullRequestsService) GetComment(ctx context.Context, owner string, repo // GitHub API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment func (s *PullRequestsService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) { u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number) - - // pullRequestCreateComment is an alias to PullRequestComment that corrects the InReplyToID field tag from `in_reply_to_id` to `in_reply_to`. - type pullRequestCreateComment struct { - ID *int64 `json:"id,omitempty"` - InReplyToID *int64 `json:"in_reply_to,omitempty"` - Body *string `json:"body,omitempty"` - Path *string `json:"path,omitempty"` - DiffHunk *string `json:"diff_hunk,omitempty"` - PullRequestReviewID *int64 `json:"pull_request_review_id,omitempty"` - Position *int `json:"position,omitempty"` - OriginalPosition *int `json:"original_position,omitempty"` - CommitID *string `json:"commit_id,omitempty"` - OriginalCommitID *string `json:"original_commit_id,omitempty"` - User *User `json:"user,omitempty"` - Reactions *Reactions `json:"reactions,omitempty"` - CreatedAt *time.Time `json:"created_at,omitempty"` - UpdatedAt *time.Time `json:"updated_at,omitempty"` - // AuthorAssociation is the comment author's relationship to the pull request's repository. - // Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE". - AuthorAssociation *string `json:"author_association,omitempty"` - URL *string `json:"url,omitempty"` - HTMLURL *string `json:"html_url,omitempty"` - PullRequestURL *string `json:"pull_request_url,omitempty"` + req, err := s.client.NewRequest("POST", u, comment) + if err != nil { + return nil, nil, err } - var alias *pullRequestCreateComment - if comment != nil { - a := pullRequestCreateComment(*comment) - alias = &a + + c := new(PullRequestComment) + resp, err := s.client.Do(ctx, req, c) + if err != nil { + return nil, resp, err } - req, err := s.client.NewRequest("POST", u, alias) + return c, resp, nil +} + +// CreateCommentInReplyTo creates a new comment as a reply to an existing pull request comment. +// +// GitHub API docs: https://developer.github.com/v3/pulls/comments/#alternative-input +func (s *PullRequestsService) CreateCommentInReplyTo(ctx context.Context, owner string, repo string, number int, body string, commentID int64) (*PullRequestComment, *Response, error) { + comment := &struct { + Body string `json:"body,omitempty"` + InReplyTo int64 `json:"in_reply_to,omitempty"` + }{ + Body: body, + InReplyTo: commentID, + } + u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number) + req, err := s.client.NewRequest("POST", u, comment) if err != nil { return nil, nil, err }