github.com/google/go-github/v74@v74.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 *Timestamp `json:"created_at,omitempty"`
    22  	UpdatedAt *Timestamp `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/rest/issues/comments#list-issue-comments
    54  // GitHub API docs: https://docs.github.com/rest/issues/comments#list-issue-comments-for-a-repository
    55  //
    56  //meta:operation GET /repos/{owner}/{repo}/issues/comments
    57  //meta:operation GET /repos/{owner}/{repo}/issues/{issue_number}/comments
    58  func (s *IssuesService) ListComments(ctx context.Context, owner string, repo string, number int, opts *IssueListCommentsOptions) ([]*IssueComment, *Response, error) {
    59  	var u string
    60  	if number == 0 {
    61  		u = fmt.Sprintf("repos/%v/%v/issues/comments", owner, repo)
    62  	} else {
    63  		u = fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number)
    64  	}
    65  	u, err := addOptions(u, opts)
    66  	if err != nil {
    67  		return nil, nil, err
    68  	}
    69  
    70  	req, err := s.client.NewRequest("GET", u, nil)
    71  	if err != nil {
    72  		return nil, nil, err
    73  	}
    74  
    75  	// TODO: remove custom Accept header when this API fully launches.
    76  	req.Header.Set("Accept", mediaTypeReactionsPreview)
    77  
    78  	var comments []*IssueComment
    79  	resp, err := s.client.Do(ctx, req, &comments)
    80  	if err != nil {
    81  		return nil, resp, err
    82  	}
    83  
    84  	return comments, resp, nil
    85  }
    86  
    87  // GetComment fetches the specified issue comment.
    88  //
    89  // GitHub API docs: https://docs.github.com/rest/issues/comments#get-an-issue-comment
    90  //
    91  //meta:operation GET /repos/{owner}/{repo}/issues/comments/{comment_id}
    92  func (s *IssuesService) GetComment(ctx context.Context, owner string, repo string, commentID int64) (*IssueComment, *Response, error) {
    93  	u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID)
    94  
    95  	req, err := s.client.NewRequest("GET", u, nil)
    96  	if err != nil {
    97  		return nil, nil, err
    98  	}
    99  
   100  	// TODO: remove custom Accept header when this API fully launches.
   101  	req.Header.Set("Accept", mediaTypeReactionsPreview)
   102  
   103  	comment := new(IssueComment)
   104  	resp, err := s.client.Do(ctx, req, comment)
   105  	if err != nil {
   106  		return nil, resp, err
   107  	}
   108  
   109  	return comment, resp, nil
   110  }
   111  
   112  // CreateComment creates a new comment on the specified issue.
   113  //
   114  // GitHub API docs: https://docs.github.com/rest/issues/comments#create-an-issue-comment
   115  //
   116  //meta:operation POST /repos/{owner}/{repo}/issues/{issue_number}/comments
   117  func (s *IssuesService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *IssueComment) (*IssueComment, *Response, error) {
   118  	u := fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number)
   119  	req, err := s.client.NewRequest("POST", u, comment)
   120  	if err != nil {
   121  		return nil, nil, err
   122  	}
   123  	c := new(IssueComment)
   124  	resp, err := s.client.Do(ctx, req, c)
   125  	if err != nil {
   126  		return nil, resp, err
   127  	}
   128  
   129  	return c, resp, nil
   130  }
   131  
   132  // EditComment updates an issue comment.
   133  // A non-nil comment.Body must be provided. Other comment fields should be left nil.
   134  //
   135  // GitHub API docs: https://docs.github.com/rest/issues/comments#update-an-issue-comment
   136  //
   137  //meta:operation PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}
   138  func (s *IssuesService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *IssueComment) (*IssueComment, *Response, error) {
   139  	u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID)
   140  	req, err := s.client.NewRequest("PATCH", u, comment)
   141  	if err != nil {
   142  		return nil, nil, err
   143  	}
   144  	c := new(IssueComment)
   145  	resp, err := s.client.Do(ctx, req, c)
   146  	if err != nil {
   147  		return nil, resp, err
   148  	}
   149  
   150  	return c, resp, nil
   151  }
   152  
   153  // DeleteComment deletes an issue comment.
   154  //
   155  // GitHub API docs: https://docs.github.com/rest/issues/comments#delete-an-issue-comment
   156  //
   157  //meta:operation DELETE /repos/{owner}/{repo}/issues/comments/{comment_id}
   158  func (s *IssuesService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) {
   159  	u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID)
   160  	req, err := s.client.NewRequest("DELETE", u, nil)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  	return s.client.Do(ctx, req, nil)
   165  }