...

Source file src/github.com/google/go-github/v55/github/pulls_comments.go

Documentation: github.com/google/go-github/v55/github

     1  // Copyright 2013 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"context"
    10  	"fmt"
    11  	"strings"
    12  	"time"
    13  )
    14  
    15  // PullRequestComment represents a comment left on a pull request.
    16  type PullRequestComment struct {
    17  	ID                  *int64     `json:"id,omitempty"`
    18  	NodeID              *string    `json:"node_id,omitempty"`
    19  	InReplyTo           *int64     `json:"in_reply_to_id,omitempty"`
    20  	Body                *string    `json:"body,omitempty"`
    21  	Path                *string    `json:"path,omitempty"`
    22  	DiffHunk            *string    `json:"diff_hunk,omitempty"`
    23  	PullRequestReviewID *int64     `json:"pull_request_review_id,omitempty"`
    24  	Position            *int       `json:"position,omitempty"`
    25  	OriginalPosition    *int       `json:"original_position,omitempty"`
    26  	StartLine           *int       `json:"start_line,omitempty"`
    27  	Line                *int       `json:"line,omitempty"`
    28  	OriginalLine        *int       `json:"original_line,omitempty"`
    29  	OriginalStartLine   *int       `json:"original_start_line,omitempty"`
    30  	Side                *string    `json:"side,omitempty"`
    31  	StartSide           *string    `json:"start_side,omitempty"`
    32  	CommitID            *string    `json:"commit_id,omitempty"`
    33  	OriginalCommitID    *string    `json:"original_commit_id,omitempty"`
    34  	User                *User      `json:"user,omitempty"`
    35  	Reactions           *Reactions `json:"reactions,omitempty"`
    36  	CreatedAt           *Timestamp `json:"created_at,omitempty"`
    37  	UpdatedAt           *Timestamp `json:"updated_at,omitempty"`
    38  	// AuthorAssociation is the comment author's relationship to the pull request's repository.
    39  	// Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE".
    40  	AuthorAssociation *string `json:"author_association,omitempty"`
    41  	URL               *string `json:"url,omitempty"`
    42  	HTMLURL           *string `json:"html_url,omitempty"`
    43  	PullRequestURL    *string `json:"pull_request_url,omitempty"`
    44  	// Can be one of: LINE, FILE from https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28#create-a-review-comment-for-a-pull-request
    45  	SubjectType *string `json:"subject_type,omitempty"`
    46  }
    47  
    48  func (p PullRequestComment) String() string {
    49  	return Stringify(p)
    50  }
    51  
    52  // PullRequestListCommentsOptions specifies the optional parameters to the
    53  // PullRequestsService.ListComments method.
    54  type PullRequestListCommentsOptions struct {
    55  	// Sort specifies how to sort comments. Possible values are: created, updated.
    56  	Sort string `url:"sort,omitempty"`
    57  
    58  	// Direction in which to sort comments. Possible values are: asc, desc.
    59  	Direction string `url:"direction,omitempty"`
    60  
    61  	// Since filters comments by time.
    62  	Since time.Time `url:"since,omitempty"`
    63  
    64  	ListOptions
    65  }
    66  
    67  // ListComments lists all comments on the specified pull request. Specifying a
    68  // pull request number of 0 will return all comments on all pull requests for
    69  // the repository.
    70  //
    71  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#list-review-comments-on-a-pull-request
    72  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#list-review-comments-in-a-repository
    73  func (s *PullRequestsService) ListComments(ctx context.Context, owner, repo string, number int, opts *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
    74  	var u string
    75  	if number == 0 {
    76  		u = fmt.Sprintf("repos/%v/%v/pulls/comments", owner, repo)
    77  	} else {
    78  		u = fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
    79  	}
    80  	u, err := addOptions(u, opts)
    81  	if err != nil {
    82  		return nil, nil, err
    83  	}
    84  
    85  	req, err := s.client.NewRequest("GET", u, nil)
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  
    90  	// TODO: remove custom Accept header when this API fully launches.
    91  	acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
    92  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
    93  
    94  	var comments []*PullRequestComment
    95  	resp, err := s.client.Do(ctx, req, &comments)
    96  	if err != nil {
    97  		return nil, resp, err
    98  	}
    99  
   100  	return comments, resp, nil
   101  }
   102  
   103  // GetComment fetches the specified pull request comment.
   104  //
   105  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#get-a-review-comment-for-a-pull-request
   106  func (s *PullRequestsService) GetComment(ctx context.Context, owner, repo string, commentID int64) (*PullRequestComment, *Response, error) {
   107  	u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
   108  	req, err := s.client.NewRequest("GET", u, nil)
   109  	if err != nil {
   110  		return nil, nil, err
   111  	}
   112  
   113  	// TODO: remove custom Accept header when this API fully launches.
   114  	acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
   115  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   116  
   117  	comment := new(PullRequestComment)
   118  	resp, err := s.client.Do(ctx, req, comment)
   119  	if err != nil {
   120  		return nil, resp, err
   121  	}
   122  
   123  	return comment, resp, nil
   124  }
   125  
   126  // CreateComment creates a new comment on the specified pull request.
   127  //
   128  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#create-a-review-comment-for-a-pull-request
   129  func (s *PullRequestsService) CreateComment(ctx context.Context, owner, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
   130  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
   131  	req, err := s.client.NewRequest("POST", u, comment)
   132  	if err != nil {
   133  		return nil, nil, err
   134  	}
   135  	// TODO: remove custom Accept headers when their respective API fully launches.
   136  	acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
   137  	req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
   138  
   139  	c := new(PullRequestComment)
   140  	resp, err := s.client.Do(ctx, req, c)
   141  	if err != nil {
   142  		return nil, resp, err
   143  	}
   144  
   145  	return c, resp, nil
   146  }
   147  
   148  // CreateCommentInReplyTo creates a new comment as a reply to an existing pull request comment.
   149  //
   150  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#create-a-review-comment-for-a-pull-request
   151  func (s *PullRequestsService) CreateCommentInReplyTo(ctx context.Context, owner, repo string, number int, body string, commentID int64) (*PullRequestComment, *Response, error) {
   152  	comment := &struct {
   153  		Body      string `json:"body,omitempty"`
   154  		InReplyTo int64  `json:"in_reply_to,omitempty"`
   155  	}{
   156  		Body:      body,
   157  		InReplyTo: commentID,
   158  	}
   159  	u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
   160  	req, err := s.client.NewRequest("POST", u, comment)
   161  	if err != nil {
   162  		return nil, nil, err
   163  	}
   164  
   165  	c := new(PullRequestComment)
   166  	resp, err := s.client.Do(ctx, req, c)
   167  	if err != nil {
   168  		return nil, resp, err
   169  	}
   170  
   171  	return c, resp, nil
   172  }
   173  
   174  // EditComment updates a pull request comment.
   175  // A non-nil comment.Body must be provided. Other comment fields should be left nil.
   176  //
   177  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#update-a-review-comment-for-a-pull-request
   178  func (s *PullRequestsService) EditComment(ctx context.Context, owner, repo string, commentID int64, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
   179  	u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
   180  	req, err := s.client.NewRequest("PATCH", u, comment)
   181  	if err != nil {
   182  		return nil, nil, err
   183  	}
   184  
   185  	c := new(PullRequestComment)
   186  	resp, err := s.client.Do(ctx, req, c)
   187  	if err != nil {
   188  		return nil, resp, err
   189  	}
   190  
   191  	return c, resp, nil
   192  }
   193  
   194  // DeleteComment deletes a pull request comment.
   195  //
   196  // GitHub API docs: https://docs.github.com/en/rest/pulls/comments#delete-a-review-comment-for-a-pull-request
   197  func (s *PullRequestsService) DeleteComment(ctx context.Context, owner, repo string, commentID int64) (*Response, error) {
   198  	u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
   199  	req, err := s.client.NewRequest("DELETE", u, nil)
   200  	if err != nil {
   201  		return nil, err
   202  	}
   203  	return s.client.Do(ctx, req, nil)
   204  }
   205  

View as plain text