You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 gitea
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "time"
  10. )
  11. // StateType issue state type
  12. type StateType string
  13. const (
  14. // StateOpen pr is opend
  15. StateOpen StateType = "open"
  16. // StateClosed pr is closed
  17. StateClosed StateType = "closed"
  18. )
  19. // PullRequestMeta PR info if an issue is a PR
  20. type PullRequestMeta struct {
  21. HasMerged bool `json:"merged"`
  22. Merged *time.Time `json:"merged_at"`
  23. }
  24. // Issue represents an issue in a repository
  25. // swagger:model
  26. type Issue struct {
  27. ID int64 `json:"id"`
  28. URL string `json:"url"`
  29. Index int64 `json:"number"`
  30. Poster *User `json:"user"`
  31. Title string `json:"title"`
  32. Body string `json:"body"`
  33. Labels []*Label `json:"labels"`
  34. Milestone *Milestone `json:"milestone"`
  35. Assignee *User `json:"assignee"`
  36. Assignees []*User `json:"assignees"`
  37. // Whether the issue is open or closed
  38. //
  39. // type: string
  40. // enum: open,closed
  41. State StateType `json:"state"`
  42. Comments int `json:"comments"`
  43. // swagger:strfmt date-time
  44. Created time.Time `json:"created_at"`
  45. // swagger:strfmt date-time
  46. Updated time.Time `json:"updated_at"`
  47. // swagger:strfmt date-time
  48. Closed *time.Time `json:"closed_at"`
  49. // swagger:strfmt date-time
  50. Deadline *time.Time `json:"due_date"`
  51. PullRequest *PullRequestMeta `json:"pull_request"`
  52. }
  53. // ListIssueOption list issue options
  54. type ListIssueOption struct {
  55. Page int
  56. State string
  57. }
  58. // ListIssues returns all issues assigned the authenticated user
  59. func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) {
  60. issues := make([]*Issue, 0, 10)
  61. return issues, c.getParsedResponse("GET", fmt.Sprintf("/issues?page=%d", opt.Page), nil, nil, &issues)
  62. }
  63. // ListUserIssues returns all issues assigned to the authenticated user
  64. func (c *Client) ListUserIssues(opt ListIssueOption) ([]*Issue, error) {
  65. issues := make([]*Issue, 0, 10)
  66. return issues, c.getParsedResponse("GET", fmt.Sprintf("/user/issues?page=%d", opt.Page), nil, nil, &issues)
  67. }
  68. // ListRepoIssues returns all issues for a given repository
  69. func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) {
  70. issues := make([]*Issue, 0, 10)
  71. return issues, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues?page=%d", owner, repo, opt.Page), nil, nil, &issues)
  72. }
  73. // GetIssue returns a single issue for a given repository
  74. func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) {
  75. issue := new(Issue)
  76. return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
  77. }
  78. // CreateIssueOption options to create one issue
  79. type CreateIssueOption struct {
  80. // required:true
  81. Title string `json:"title" binding:"Required"`
  82. Body string `json:"body"`
  83. // username of assignee
  84. Assignee string `json:"assignee"`
  85. Assignees []string `json:"assignees"`
  86. // swagger:strfmt date-time
  87. Deadline *time.Time `json:"due_date"`
  88. // milestone id
  89. Milestone int64 `json:"milestone"`
  90. // list of label ids
  91. Labels []int64 `json:"labels"`
  92. Closed bool `json:"closed"`
  93. }
  94. // CreateIssue create a new issue for a given repository
  95. func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) {
  96. body, err := json.Marshal(&opt)
  97. if err != nil {
  98. return nil, err
  99. }
  100. issue := new(Issue)
  101. return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
  102. jsonHeader, bytes.NewReader(body), issue)
  103. }
  104. // EditIssueOption options for editing an issue
  105. type EditIssueOption struct {
  106. Title string `json:"title"`
  107. Body *string `json:"body"`
  108. Assignee *string `json:"assignee"`
  109. Assignees []string `json:"assignees"`
  110. Milestone *int64 `json:"milestone"`
  111. State *string `json:"state"`
  112. // swagger:strfmt date-time
  113. Deadline *time.Time `json:"due_date"`
  114. }
  115. // EditIssue modify an existing issue for a given repository
  116. func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) {
  117. body, err := json.Marshal(&opt)
  118. if err != nil {
  119. return nil, err
  120. }
  121. issue := new(Issue)
  122. return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
  123. jsonHeader, bytes.NewReader(body), issue)
  124. }
  125. // EditDeadlineOption options for creating a deadline
  126. type EditDeadlineOption struct {
  127. // required:true
  128. // swagger:strfmt date-time
  129. Deadline *time.Time `json:"due_date"`
  130. }
  131. // IssueDeadline represents an issue deadline
  132. // swagger:model
  133. type IssueDeadline struct {
  134. // swagger:strfmt date-time
  135. Deadline *time.Time `json:"due_date"`
  136. }
  137. // EditPriorityOption options for updating priority
  138. type EditPriorityOption struct {
  139. // required:true
  140. Priority int `json:"priority"`
  141. }