issue_comment.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package gitote
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. )
  8. // Comment represents a comment in commit and issue page.
  9. type Comment struct {
  10. ID int64 `json:"id"`
  11. HTMLURL string `json:"html_url"`
  12. Poster *User `json:"user"`
  13. Body string `json:"body"`
  14. Created time.Time `json:"created_at"`
  15. Updated time.Time `json:"updated_at"`
  16. }
  17. // ListIssueComments list comments on an issue.
  18. func (c *Client) ListIssueComments(owner, repo string, index int64) ([]*Comment, error) {
  19. comments := make([]*Comment, 0, 10)
  20. return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), nil, nil, &comments)
  21. }
  22. // ListRepoIssueComments list comments for a given repo.
  23. func (c *Client) ListRepoIssueComments(owner, repo string) ([]*Comment, error) {
  24. comments := make([]*Comment, 0, 10)
  25. return comments, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments", owner, repo), nil, nil, &comments)
  26. }
  27. // CreateIssueCommentOption is option when creating an issue comment.
  28. type CreateIssueCommentOption struct {
  29. Body string `json:"body" binding:"Required"`
  30. }
  31. // CreateIssueComment create comment on an issue.
  32. func (c *Client) CreateIssueComment(owner, repo string, index int64, opt CreateIssueCommentOption) (*Comment, error) {
  33. body, err := json.Marshal(&opt)
  34. if err != nil {
  35. return nil, err
  36. }
  37. comment := new(Comment)
  38. return comment, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues/%d/comments", owner, repo, index), jsonHeader, bytes.NewReader(body), comment)
  39. }
  40. // EditIssueCommentOption is option when editing an issue comment.
  41. type EditIssueCommentOption struct {
  42. Body string `json:"body" binding:"Required"`
  43. }
  44. // EditIssueComment edits an issue comment.
  45. func (c *Client) EditIssueComment(owner, repo string, index, commentID int64, opt EditIssueCommentOption) (*Comment, error) {
  46. body, err := json.Marshal(&opt)
  47. if err != nil {
  48. return nil, err
  49. }
  50. comment := new(Comment)
  51. return comment, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d/comments/%d", owner, repo, index, commentID), jsonHeader, bytes.NewReader(body), comment)
  52. }
  53. // DeleteIssueComment deletes an issue comment.
  54. func (c *Client) DeleteIssueComment(owner, repo string, index, commentID int64) error {
  55. _, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/comments/%d", owner, repo, index, commentID), nil, nil)
  56. return err
  57. }