issue_comment.go 2.5 KB

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