github.com/google/go-github/v33@v33.0.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 // Since filters comments by time. 39 Since *time.Time `url:"since,omitempty"` 40 41 ListOptions 42 } 43 44 // ListComments lists all comments on the specified issue. Specifying an issue 45 // number of 0 will return all comments on all issues for the repository. 46 // 47 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/issues/#list-issue-comments 48 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/issues/#list-issue-comments-for-a-repository 49 func (s *IssuesService) ListComments(ctx context.Context, owner string, repo string, number int, opts *IssueListCommentsOptions) ([]*IssueComment, *Response, error) { 50 var u string 51 if number == 0 { 52 u = fmt.Sprintf("repos/%v/%v/issues/comments", owner, repo) 53 } else { 54 u = fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number) 55 } 56 u, err := addOptions(u, opts) 57 if err != nil { 58 return nil, nil, err 59 } 60 61 req, err := s.client.NewRequest("GET", u, nil) 62 if err != nil { 63 return nil, nil, err 64 } 65 66 // TODO: remove custom Accept header when this API fully launches. 67 req.Header.Set("Accept", mediaTypeReactionsPreview) 68 69 var comments []*IssueComment 70 resp, err := s.client.Do(ctx, req, &comments) 71 if err != nil { 72 return nil, resp, err 73 } 74 75 return comments, resp, nil 76 } 77 78 // GetComment fetches the specified issue comment. 79 // 80 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/issues/#get-an-issue-comment 81 func (s *IssuesService) GetComment(ctx context.Context, owner string, repo string, commentID int64) (*IssueComment, *Response, error) { 82 u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID) 83 84 req, err := s.client.NewRequest("GET", u, nil) 85 if err != nil { 86 return nil, nil, err 87 } 88 89 // TODO: remove custom Accept header when this API fully launches. 90 req.Header.Set("Accept", mediaTypeReactionsPreview) 91 92 comment := new(IssueComment) 93 resp, err := s.client.Do(ctx, req, comment) 94 if err != nil { 95 return nil, resp, err 96 } 97 98 return comment, resp, nil 99 } 100 101 // CreateComment creates a new comment on the specified issue. 102 // 103 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/issues/#create-an-issue-comment 104 func (s *IssuesService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *IssueComment) (*IssueComment, *Response, error) { 105 u := fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number) 106 req, err := s.client.NewRequest("POST", u, comment) 107 if err != nil { 108 return nil, nil, err 109 } 110 c := new(IssueComment) 111 resp, err := s.client.Do(ctx, req, c) 112 if err != nil { 113 return nil, resp, err 114 } 115 116 return c, resp, nil 117 } 118 119 // EditComment updates an issue comment. 120 // A non-nil comment.Body must be provided. Other comment fields should be left nil. 121 // 122 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/issues/#update-an-issue-comment 123 func (s *IssuesService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *IssueComment) (*IssueComment, *Response, error) { 124 u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID) 125 req, err := s.client.NewRequest("PATCH", u, comment) 126 if err != nil { 127 return nil, nil, err 128 } 129 c := new(IssueComment) 130 resp, err := s.client.Do(ctx, req, c) 131 if err != nil { 132 return nil, resp, err 133 } 134 135 return c, resp, nil 136 } 137 138 // DeleteComment deletes an issue comment. 139 // 140 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/issues/#delete-an-issue-comment 141 func (s *IssuesService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) { 142 u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID) 143 req, err := s.client.NewRequest("DELETE", u, nil) 144 if err != nil { 145 return nil, err 146 } 147 return s.client.Do(ctx, req, nil) 148 }