issue.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package gitote
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. )
  8. type StateType string
  9. const (
  10. STATE_OPEN StateType = "open"
  11. STATE_CLOSED StateType = "closed"
  12. )
  13. type PullRequestMeta struct {
  14. HasMerged bool `json:"merged"`
  15. Merged *time.Time `json:"merged_at"`
  16. }
  17. type Issue struct {
  18. ID int64 `json:"id"`
  19. Index int64 `json:"number"`
  20. Poster *User `json:"user"`
  21. Title string `json:"title"`
  22. Body string `json:"body"`
  23. Labels []*Label `json:"labels"`
  24. Milestone *Milestone `json:"milestone"`
  25. Assignee *User `json:"assignee"`
  26. State StateType `json:"state"`
  27. Comments int `json:"comments"`
  28. Created time.Time `json:"created_at"`
  29. Updated time.Time `json:"updated_at"`
  30. PullRequest *PullRequestMeta `json:"pull_request"`
  31. }
  32. type ListIssueOption struct {
  33. Page int
  34. State string
  35. }
  36. func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) {
  37. issues := make([]*Issue, 0, 10)
  38. return issues, c.getParsedResponse("GET", fmt.Sprintf("/issues?page=%d", opt.Page), nil, nil, &issues)
  39. }
  40. func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) {
  41. issues := make([]*Issue, 0, 10)
  42. return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?page=%d", opt.Page), nil, nil, &issues)
  43. }
  44. func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) {
  45. issues := make([]*Issue, 0, 10)
  46. return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues)
  47. }
  48. func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) {
  49. issue := new(Issue)
  50. return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
  51. }
  52. type CreateIssueOption struct {
  53. Title string `json:"title" binding:"Required"`
  54. Body string `json:"body"`
  55. Assignee string `json:"assignee"`
  56. Milestone int64 `json:"milestone"`
  57. Labels []int64 `json:"labels"`
  58. Closed bool `json:"closed"`
  59. }
  60. func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) {
  61. body, err := json.Marshal(&opt)
  62. if err != nil {
  63. return nil, err
  64. }
  65. issue := new(Issue)
  66. return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
  67. jsonHeader, bytes.NewReader(body), issue)
  68. }
  69. type EditIssueOption struct {
  70. Title string `json:"title"`
  71. Body *string `json:"body"`
  72. Assignee *string `json:"assignee"`
  73. Milestone *int64 `json:"milestone"`
  74. State *string `json:"state"`
  75. }
  76. func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) {
  77. body, err := json.Marshal(&opt)
  78. if err != nil {
  79. return nil, err
  80. }
  81. issue := new(Issue)
  82. return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
  83. jsonHeader, bytes.NewReader(body), issue)
  84. }