issue.go 3.0 KB

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