github.com/google/go-github/v49@v49.1.0/github/issues_comments.go (about) 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 "time" 12 ) 13 14 // IssueComment represents a comment left on an issue. 15 type IssueComment struct { 16 ID *int64 `json:"id,omitempty"` 17 NodeID *string `json:"node_id,omitempty"` 18 Body *string `json:"body,omitempty"` 19 User *User `json:"user,omitempty"` 20 Reactions *Reactions `json:"reactions,omitempty"` 21 CreatedAt *time.Time `json:"created_at,omitempty"` 22 UpdatedAt *time.Time `json:"updated_at,omitempty"` 23 // AuthorAssociation is the comment author's relationship to the issue's repository. 24 // Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE". 25 AuthorAssociation *string `json:"author_association,omitempty"` 26 URL *string `json:"url,omitempty"` 27 HTMLURL *string `json:"html_url,omitempty"` 28 IssueURL *string `json:"issue_url,omitempty"` 29 } 30 31 func (i IssueComment) String() string { 32 return Stringify(i) 33 } 34 35 // IssueListCommentsOptions specifies the optional parameters to the 36 // IssuesService.ListComments method. 37 type IssueListCommentsOptions struct { 38 // Sort specifies how to sort comments. Possible values are: created, updated. 39 Sort *string `url:"sort,omitempty"` 40 41 // Direction in which to sort comments. Possible values are: asc, desc. 42 Direction *string `url:"direction,omitempty"` 43 44 // Since filters comments by time. 45 Since *time.Time `url:"since,omitempty"` 46 47 ListOptions 48 } 49 50 // ListComments lists all comments on the specified issue. Specifying an issue 51 // number of 0 will return all comments on all issues for the repository. 52 // 53 // GitHub API docs: https://docs.github.com/en/rest/issues/comments#list-issue-comments 54 // GitHub API docs: https://docs.github.com/en/rest/issues/comments#list-issue-comments-for-a-repository 55 func (s *IssuesService) ListComments(ctx context.Context, owner string, repo string, number int, opts *IssueListCommentsOptions) ([]*IssueComment, *Response, error) { 56 var u string 57 if number == 0 { 58 u = fmt.Sprintf("repos/%v/%v/issues/comments", owner, repo) 59 } else { 60 u = fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number) 61 } 62 u, err := addOptions(u, opts) 63 if err != nil { 64 return nil, nil, err 65 } 66 67 req, err := s.client.NewRequest("GET", u, nil) 68 if err != nil { 69 return nil, nil, err 70 } 71 72 // TODO: remove custom Accept header when this API fully launches. 73 req.Header.Set("Accept", mediaTypeReactionsPreview) 74 75 var comments []*IssueComment 76 resp, err := s.client.Do(ctx, req, &comments) 77 if err != nil { 78 return nil, resp, err 79 } 80 81 return comments, resp, nil 82 } 83 84 // GetComment fetches the specified issue comment. 85 // 86 // GitHub API docs: https://docs.github.com/en/rest/issues/comments#get-an-issue-comment 87 func (s *IssuesService) GetComment(ctx context.Context, owner string, repo string, commentID int64) (*IssueComment, *Response, error) { 88 u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID) 89 90 req, err := s.client.NewRequest("GET", u, nil) 91 if err != nil { 92 return nil, nil, err 93 } 94 95 // TODO: remove custom Accept header when this API fully launches. 96 req.Header.Set("Accept", mediaTypeReactionsPreview) 97 98 comment := new(IssueComment) 99 resp, err := s.client.Do(ctx, req, comment) 100 if err != nil { 101 return nil, resp, err 102 } 103 104 return comment, resp, nil 105 } 106 107 // CreateComment creates a new comment on the specified issue. 108 // 109 // GitHub API docs: https://docs.github.com/en/rest/issues/comments#create-an-issue-comment 110 func (s *IssuesService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *IssueComment) (*IssueComment, *Response, error) { 111 u := fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number) 112 req, err := s.client.NewRequest("POST", u, comment) 113 if err != nil { 114 return nil, nil, err 115 } 116 c := new(IssueComment) 117 resp, err := s.client.Do(ctx, req, c) 118 if err != nil { 119 return nil, resp, err 120 } 121 122 return c, resp, nil 123 } 124 125 // EditComment updates an issue comment. 126 // A non-nil comment.Body must be provided. Other comment fields should be left nil. 127 // 128 // GitHub API docs: https://docs.github.com/en/rest/issues/comments#update-an-issue-comment 129 func (s *IssuesService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *IssueComment) (*IssueComment, *Response, error) { 130 u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID) 131 req, err := s.client.NewRequest("PATCH", u, comment) 132 if err != nil { 133 return nil, nil, err 134 } 135 c := new(IssueComment) 136 resp, err := s.client.Do(ctx, req, c) 137 if err != nil { 138 return nil, resp, err 139 } 140 141 return c, resp, nil 142 } 143 144 // DeleteComment deletes an issue comment. 145 // 146 // GitHub API docs: https://docs.github.com/en/rest/issues/comments#delete-an-issue-comment 147 func (s *IssuesService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) { 148 u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID) 149 req, err := s.client.NewRequest("DELETE", u, nil) 150 if err != nil { 151 return nil, err 152 } 153 return s.client.Do(ctx, req, nil) 154 }