github.com/google/go-github/v69@v69.2.0/github/repos_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  )
    12  
    13  // RepositoryComment represents a comment for a commit, file, or line in a repository.
    14  type RepositoryComment struct {
    15  	HTMLURL   *string    `json:"html_url,omitempty"`
    16  	URL       *string    `json:"url,omitempty"`
    17  	ID        *int64     `json:"id,omitempty"`
    18  	NodeID    *string    `json:"node_id,omitempty"`
    19  	CommitID  *string    `json:"commit_id,omitempty"`
    20  	User      *User      `json:"user,omitempty"`
    21  	Reactions *Reactions `json:"reactions,omitempty"`
    22  	CreatedAt *Timestamp `json:"created_at,omitempty"`
    23  	UpdatedAt *Timestamp `json:"updated_at,omitempty"`
    24  
    25  	// User-mutable fields
    26  	Body *string `json:"body"`
    27  	// User-initialized fields
    28  	Path     *string `json:"path,omitempty"`
    29  	Position *int    `json:"position,omitempty"`
    30  }
    31  
    32  func (r RepositoryComment) String() string {
    33  	return Stringify(r)
    34  }
    35  
    36  // ListComments lists all the comments for the repository.
    37  //
    38  // GitHub API docs: https://docs.github.com/rest/commits/comments#list-commit-comments-for-a-repository
    39  //
    40  //meta:operation GET /repos/{owner}/{repo}/comments
    41  func (s *RepositoriesService) ListComments(ctx context.Context, owner, repo string, opts *ListOptions) ([]*RepositoryComment, *Response, error) {
    42  	u := fmt.Sprintf("repos/%v/%v/comments", owner, repo)
    43  	u, err := addOptions(u, opts)
    44  	if err != nil {
    45  		return nil, nil, err
    46  	}
    47  
    48  	req, err := s.client.NewRequest("GET", u, nil)
    49  	if err != nil {
    50  		return nil, nil, err
    51  	}
    52  
    53  	// TODO: remove custom Accept header when this API fully launches.
    54  	req.Header.Set("Accept", mediaTypeReactionsPreview)
    55  
    56  	var comments []*RepositoryComment
    57  	resp, err := s.client.Do(ctx, req, &comments)
    58  	if err != nil {
    59  		return nil, resp, err
    60  	}
    61  
    62  	return comments, resp, nil
    63  }
    64  
    65  // ListCommitComments lists all the comments for a given commit SHA.
    66  //
    67  // GitHub API docs: https://docs.github.com/rest/commits/comments#list-commit-comments
    68  //
    69  //meta:operation GET /repos/{owner}/{repo}/commits/{commit_sha}/comments
    70  func (s *RepositoriesService) ListCommitComments(ctx context.Context, owner, repo, sha string, opts *ListOptions) ([]*RepositoryComment, *Response, error) {
    71  	u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
    72  	u, err := addOptions(u, opts)
    73  	if err != nil {
    74  		return nil, nil, err
    75  	}
    76  
    77  	req, err := s.client.NewRequest("GET", u, nil)
    78  	if err != nil {
    79  		return nil, nil, err
    80  	}
    81  
    82  	// TODO: remove custom Accept header when this API fully launches.
    83  	req.Header.Set("Accept", mediaTypeReactionsPreview)
    84  
    85  	var comments []*RepositoryComment
    86  	resp, err := s.client.Do(ctx, req, &comments)
    87  	if err != nil {
    88  		return nil, resp, err
    89  	}
    90  
    91  	return comments, resp, nil
    92  }
    93  
    94  // CreateComment creates a comment for the given commit.
    95  // Note: GitHub allows for comments to be created for non-existing files and positions.
    96  //
    97  // GitHub API docs: https://docs.github.com/rest/commits/comments#create-a-commit-comment
    98  //
    99  //meta:operation POST /repos/{owner}/{repo}/commits/{commit_sha}/comments
   100  func (s *RepositoriesService) CreateComment(ctx context.Context, owner, repo, sha string, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
   101  	u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
   102  	req, err := s.client.NewRequest("POST", u, comment)
   103  	if err != nil {
   104  		return nil, nil, err
   105  	}
   106  
   107  	c := new(RepositoryComment)
   108  	resp, err := s.client.Do(ctx, req, c)
   109  	if err != nil {
   110  		return nil, resp, err
   111  	}
   112  
   113  	return c, resp, nil
   114  }
   115  
   116  // GetComment gets a single comment from a repository.
   117  //
   118  // GitHub API docs: https://docs.github.com/rest/commits/comments#get-a-commit-comment
   119  //
   120  //meta:operation GET /repos/{owner}/{repo}/comments/{comment_id}
   121  func (s *RepositoriesService) GetComment(ctx context.Context, owner, repo string, id int64) (*RepositoryComment, *Response, error) {
   122  	u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
   123  	req, err := s.client.NewRequest("GET", u, nil)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  
   128  	// TODO: remove custom Accept header when this API fully launches.
   129  	req.Header.Set("Accept", mediaTypeReactionsPreview)
   130  
   131  	c := new(RepositoryComment)
   132  	resp, err := s.client.Do(ctx, req, c)
   133  	if err != nil {
   134  		return nil, resp, err
   135  	}
   136  
   137  	return c, resp, nil
   138  }
   139  
   140  // UpdateComment updates the body of a single comment.
   141  //
   142  // GitHub API docs: https://docs.github.com/rest/commits/comments#update-a-commit-comment
   143  //
   144  //meta:operation PATCH /repos/{owner}/{repo}/comments/{comment_id}
   145  func (s *RepositoriesService) UpdateComment(ctx context.Context, owner, repo string, id int64, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
   146  	u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
   147  	req, err := s.client.NewRequest("PATCH", u, comment)
   148  	if err != nil {
   149  		return nil, nil, err
   150  	}
   151  
   152  	c := new(RepositoryComment)
   153  	resp, err := s.client.Do(ctx, req, c)
   154  	if err != nil {
   155  		return nil, resp, err
   156  	}
   157  
   158  	return c, resp, nil
   159  }
   160  
   161  // DeleteComment deletes a single comment from a repository.
   162  //
   163  // GitHub API docs: https://docs.github.com/rest/commits/comments#delete-a-commit-comment
   164  //
   165  //meta:operation DELETE /repos/{owner}/{repo}/comments/{comment_id}
   166  func (s *RepositoriesService) DeleteComment(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   167  	u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
   168  	req, err := s.client.NewRequest("DELETE", u, nil)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	return s.client.Do(ctx, req, nil)
   173  }