github.com/google/go-github/v52@v52.0.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/en/rest/commits/comments#list-commit-comments-for-a-repository
    39  func (s *RepositoriesService) ListComments(ctx context.Context, owner, repo string, opts *ListOptions) ([]*RepositoryComment, *Response, error) {
    40  	u := fmt.Sprintf("repos/%v/%v/comments", owner, repo)
    41  	u, err := addOptions(u, opts)
    42  	if err != nil {
    43  		return nil, nil, err
    44  	}
    45  
    46  	req, err := s.client.NewRequest("GET", u, nil)
    47  	if err != nil {
    48  		return nil, nil, err
    49  	}
    50  
    51  	// TODO: remove custom Accept header when this API fully launches.
    52  	req.Header.Set("Accept", mediaTypeReactionsPreview)
    53  
    54  	var comments []*RepositoryComment
    55  	resp, err := s.client.Do(ctx, req, &comments)
    56  	if err != nil {
    57  		return nil, resp, err
    58  	}
    59  
    60  	return comments, resp, nil
    61  }
    62  
    63  // ListCommitComments lists all the comments for a given commit SHA.
    64  //
    65  // GitHub API docs: https://docs.github.com/en/rest/commits/comments#list-commit-comments
    66  func (s *RepositoriesService) ListCommitComments(ctx context.Context, owner, repo, sha string, opts *ListOptions) ([]*RepositoryComment, *Response, error) {
    67  	u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
    68  	u, err := addOptions(u, opts)
    69  	if err != nil {
    70  		return nil, nil, err
    71  	}
    72  
    73  	req, err := s.client.NewRequest("GET", u, nil)
    74  	if err != nil {
    75  		return nil, nil, err
    76  	}
    77  
    78  	// TODO: remove custom Accept header when this API fully launches.
    79  	req.Header.Set("Accept", mediaTypeReactionsPreview)
    80  
    81  	var comments []*RepositoryComment
    82  	resp, err := s.client.Do(ctx, req, &comments)
    83  	if err != nil {
    84  		return nil, resp, err
    85  	}
    86  
    87  	return comments, resp, nil
    88  }
    89  
    90  // CreateComment creates a comment for the given commit.
    91  // Note: GitHub allows for comments to be created for non-existing files and positions.
    92  //
    93  // GitHub API docs: https://docs.github.com/en/rest/commits/comments#create-a-commit-comment
    94  func (s *RepositoriesService) CreateComment(ctx context.Context, owner, repo, sha string, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
    95  	u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
    96  	req, err := s.client.NewRequest("POST", u, comment)
    97  	if err != nil {
    98  		return nil, nil, err
    99  	}
   100  
   101  	c := new(RepositoryComment)
   102  	resp, err := s.client.Do(ctx, req, c)
   103  	if err != nil {
   104  		return nil, resp, err
   105  	}
   106  
   107  	return c, resp, nil
   108  }
   109  
   110  // GetComment gets a single comment from a repository.
   111  //
   112  // GitHub API docs: https://docs.github.com/en/rest/commits/comments#get-a-commit-comment
   113  func (s *RepositoriesService) GetComment(ctx context.Context, owner, repo string, id int64) (*RepositoryComment, *Response, error) {
   114  	u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
   115  	req, err := s.client.NewRequest("GET", u, nil)
   116  	if err != nil {
   117  		return nil, nil, err
   118  	}
   119  
   120  	// TODO: remove custom Accept header when this API fully launches.
   121  	req.Header.Set("Accept", mediaTypeReactionsPreview)
   122  
   123  	c := new(RepositoryComment)
   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  // UpdateComment updates the body of a single comment.
   133  //
   134  // GitHub API docs: https://docs.github.com/en/rest/commits/comments#update-a-commit-comment
   135  func (s *RepositoriesService) UpdateComment(ctx context.Context, owner, repo string, id int64, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
   136  	u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
   137  	req, err := s.client.NewRequest("PATCH", u, comment)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	c := new(RepositoryComment)
   143  	resp, err := s.client.Do(ctx, req, c)
   144  	if err != nil {
   145  		return nil, resp, err
   146  	}
   147  
   148  	return c, resp, nil
   149  }
   150  
   151  // DeleteComment deletes a single comment from a repository.
   152  //
   153  // GitHub API docs: https://docs.github.com/en/rest/commits/comments#delete-a-commit-comment
   154  func (s *RepositoriesService) DeleteComment(ctx context.Context, owner, repo string, id int64) (*Response, error) {
   155  	u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
   156  	req, err := s.client.NewRequest("DELETE", u, nil)
   157  	if err != nil {
   158  		return nil, err
   159  	}
   160  	return s.client.Do(ctx, req, nil)
   161  }