todos.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package gitlab
  2. import "time"
  3. import "fmt"
  4. // TodosService handles communication with the todos related methods of
  5. // the Gitlab API.
  6. //
  7. // GitLab API docs: https://docs.gitlab.com/ce/api/todos.html
  8. type TodosService struct {
  9. client *Client
  10. }
  11. // TodoAction represents the available actions that can be performed on a todo.
  12. //
  13. // GitLab API docs: https://docs.gitlab.com/ce/api/todos.html
  14. type TodoAction string
  15. // The available todo actions.
  16. const (
  17. TodoAssigned TodoAction = "assigned"
  18. TodoMentioned TodoAction = "mentioned"
  19. TodoBuildFailed TodoAction = "build_failed"
  20. TodoMarked TodoAction = "marked"
  21. TodoApprovalRequired TodoAction = "approval_required"
  22. TodoDirectlyAddressed TodoAction = "directly_addressed"
  23. )
  24. // TodoTarget represents a todo target of type Issue or MergeRequest
  25. type TodoTarget struct {
  26. // TODO: replace both Assignee and Author structs with v4 User struct
  27. Assignee struct {
  28. Name string `json:"name"`
  29. Username string `json:"username"`
  30. ID int `json:"id"`
  31. State string `json:"state"`
  32. AvatarURL string `json:"avatar_url"`
  33. WebURL string `json:"web_url"`
  34. } `json:"assignee"`
  35. Author struct {
  36. Name string `json:"name"`
  37. Username string `json:"username"`
  38. ID int `json:"id"`
  39. State string `json:"state"`
  40. AvatarURL string `json:"avatar_url"`
  41. WebURL string `json:"web_url"`
  42. } `json:"author"`
  43. CreatedAt *time.Time `json:"created_at"`
  44. Description string `json:"description"`
  45. Downvotes int `json:"downvotes"`
  46. ID int `json:"id"`
  47. IID int `json:"iid"`
  48. Labels []string `json:"labels"`
  49. Milestone Milestone `json:"milestone"`
  50. ProjectID int `json:"project_id"`
  51. State string `json:"state"`
  52. Subscribed bool `json:"subscribed"`
  53. Title string `json:"title"`
  54. UpdatedAt *time.Time `json:"updated_at"`
  55. Upvotes int `json:"upvotes"`
  56. UserNotesCount int `json:"user_notes_count"`
  57. WebURL string `json:"web_url"`
  58. // Only available for type Issue
  59. Confidential bool `json:"confidential"`
  60. DueDate string `json:"due_date"`
  61. Weight int `json:"weight"`
  62. // Only available for type MergeRequest
  63. ApprovalsBeforeMerge bool `json:"approvals_before_merge"`
  64. ForceRemoveSourceBranch bool `json:"force_remove_source_branch"`
  65. MergeCommitSha string `json:"merge_commit_sha"`
  66. MergeWhenPipelineSucceeds bool `json:"merge_when_pipeline_succeeds"`
  67. MergeStatus string `json:"merge_status"`
  68. Sha string `json:"sha"`
  69. ShouldRemoveSourceBranch bool `json:"should_remove_source_branch"`
  70. SourceBranch string `json:"source_branch"`
  71. SourceProjectID int `json:"source_project_id"`
  72. Squash bool `json:"squash"`
  73. TargetBranch string `json:"target_branch"`
  74. TargetProjectID int `json:"target_project_id"`
  75. WorkInProgress bool `json:"work_in_progress"`
  76. }
  77. // Todo represents a GitLab todo.
  78. //
  79. // GitLab API docs: https://docs.gitlab.com/ce/api/todos.html
  80. type Todo struct {
  81. ID int `json:"id"`
  82. Project struct {
  83. ID int `json:"id"`
  84. HTTPURLToRepo string `json:"http_url_to_repo"`
  85. WebURL string `json:"web_url"`
  86. Name string `json:"name"`
  87. NameWithNamespace string `json:"name_with_namespace"`
  88. Path string `json:"path"`
  89. PathWithNamespace string `json:"path_with_namespace"`
  90. } `json:"project"`
  91. Author struct {
  92. ID int `json:"id"`
  93. Name string `json:"name"`
  94. Username string `json:"username"`
  95. State string `json:"state"`
  96. AvatarURL string `json:"avatar_url"`
  97. WebURL string `json:"web_url"`
  98. } `json:"author"`
  99. ActionName TodoAction `json:"action_name"`
  100. TargetType string `json:"target_type"`
  101. Target TodoTarget `json:"target"`
  102. TargetURL string `json:"target_url"`
  103. Body string `json:"body"`
  104. State string `json:"state"`
  105. CreatedAt *time.Time `json:"created_at"`
  106. }
  107. func (t Todo) String() string {
  108. return Stringify(t)
  109. }
  110. // ListTodosOptions represents the available ListTodos() options.
  111. //
  112. // GitLab API docs: https://docs.gitlab.com/ce/api/todos.html#get-a-list-of-todos
  113. type ListTodosOptions struct {
  114. Action *TodoAction `url:"action,omitempty" json:"action,omitempty"`
  115. AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
  116. ProjectID *int `url:"project_id,omitempty" json:"project_id,omitempty"`
  117. State *string `url:"state,omitempty" json:"state,omitempty"`
  118. Type *string `url:"type,omitempty" json:"type,omitempty"`
  119. }
  120. // ListTodos lists all todos created by authenticated user.
  121. // When no filter is applied, it returns all pending todos for the current user.
  122. //
  123. // GitLab API docs:
  124. // https://docs.gitlab.com/ce/api/todos.html#get-a-list-of-todos
  125. func (s *TodosService) ListTodos(opt *ListTodosOptions, options ...OptionFunc) ([]*Todo, *Response, error) {
  126. req, err := s.client.NewRequest("GET", "todos", opt, options)
  127. if err != nil {
  128. return nil, nil, err
  129. }
  130. var t []*Todo
  131. resp, err := s.client.Do(req, &t)
  132. if err != nil {
  133. return nil, resp, err
  134. }
  135. return t, resp, err
  136. }
  137. // MarkTodoAsDone marks a single pending todo given by its ID for the current user as done.
  138. //
  139. // GitLab API docs: https://docs.gitlab.com/ce/api/todos.html#mark-a-todo-as-done
  140. func (s *TodosService) MarkTodoAsDone(id int, options ...OptionFunc) (*Response, error) {
  141. u := fmt.Sprintf("todos/%d/mark_as_done", id)
  142. req, err := s.client.NewRequest("POST", u, nil, options)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return s.client.Do(req, nil)
  147. }
  148. // MarkAllTodosAsDone marks all pending todos for the current user as done.
  149. //
  150. // GitLab API docs: https://docs.gitlab.com/ce/api/todos.html#mark-all-todos-as-done
  151. func (s *TodosService) MarkAllTodosAsDone(options ...OptionFunc) (*Response, error) {
  152. req, err := s.client.NewRequest("POST", "todos/mark_as_done", nil, options)
  153. if err != nil {
  154. return nil, err
  155. }
  156. return s.client.Do(req, nil)
  157. }