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