-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.go
485 lines (424 loc) · 15.6 KB
/
tasks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright (c) 2022, John Moore
// All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package clickup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
type Status struct {
ID string `json:"id"`
Status string `json:"status"`
Color string `json:"color"`
Orderindex int `json:"-"`
Type string `json:"type"`
}
type SingleTask struct {
ID string `json:"id"`
CustomID string `json:"custom_id"`
Name string `json:"name"`
TextContent string `json:"text_content"`
Description string `json:"description"`
Status Status `json:"status"`
Orderindex string `json:"-"`
DateCreated string `json:"date_created"`
DateUpdated string `json:"date_updated"`
DateClosed string `json:"date_closed"`
Archived bool `json:"archived"`
Creator TeamUser `json:"creator"`
Assignees []TeamUser `json:"assignees"`
Watchers []TeamUser `json:"watchers"`
Checklists []struct {
ID string `json:"id"`
TaskID string `json:"task_id"`
Name string `json:"name"`
DateCreated string `json:"date_created"`
Orderindex int `json:"-"`
Creator int `json:"creator"`
Resolved int `json:"resolved"`
Unresolved int `json:"unresolved"`
Items []struct {
ID string `json:"id"`
Name string `json:"name"`
Orderindex int `json:"-"`
Assignee struct {
ID int `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Color string `json:"color"`
Initials string `json:"initials"`
ProfilePicture string `json:"profilePicture"`
} `json:"assignee"`
Resolved bool `json:"resolved"`
DateCreated string `json:"date_created"`
} `json:"items"`
} `json:"checklists"`
Tags []Tag `json:"tags"`
Parent string `json:"parent"`
Priority struct {
ID string `json:"id"`
Priority string `json:"priority"`
Color string `json:"color"`
Orderindex string `json:"-"`
} `json:"priority"`
DueDate string `json:"due_date"`
StartDate string `json:"start_date"`
Points int `json:"points"`
TimeEstimate int `json:"time_estimate"`
TimeSpent int `json:"time_spent"`
CustomFields []struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
TypeConfig struct {
Simple bool `json:"simple"`
Default int `json:"default"`
Placeholder string `json:"placeholder"`
NewDropDown bool `json:"new_drop_down"`
SingleUser bool `json:"single_user"`
IncludeGroups bool `json:"include_groups"`
IncludeGuests bool `json:"include_guests"`
IncludeTeamMembers bool `json:"include_team_members"`
Formula string `json:"formula"`
CompleteOn int `json:"complete_on"`
SubtaskRollup bool `json:"subtask_rollup"`
Options []struct {
ID string `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
Orderindex int `json:"-"`
} `json:"options"`
Fields []interface{} `json:"fields"`
Tracking struct {
Subtasks bool `json:"subtasks"`
Checklists bool `json:"checklists"`
} `json:"tracking"`
} `json:"type_config"`
DateCreated string `json:"date_created"`
HideFromGuests bool `json:"hide_from_guests"`
Required bool `json:"required"`
Value interface{} `json:"value"`
} `json:"custom_fields"`
Dependencies []struct {
TaskID string `json:"task_id"`
DependsOn string `json:"depends_on"`
Type int `json:"type"`
DateCreated string `json:"date_created"`
Userid string `json:"userid"`
} `json:"dependencies"`
LinkedTasks []struct {
TaskID string `json:"task_id"`
LinkID string `json:"link_id"`
DateCreated string `json:"date_created"`
Userid string `json:"userid"`
} `json:"linked_tasks"`
TeamID string `json:"team_id"`
URL string `json:"url"`
PermissionLevel string `json:"permission_level"`
List struct {
ID string `json:"id"`
Name string `json:"name"`
Access bool `json:"access"`
} `json:"list"`
Project struct {
ID string `json:"id"`
Name string `json:"name"`
Hidden bool `json:"hidden"`
Access bool `json:"access"`
} `json:"project"`
Folder struct {
ID string `json:"id"`
Name string `json:"name"`
Hidden bool `json:"hidden"`
Access bool `json:"access"`
} `json:"folder"`
Space struct {
ID string `json:"id"`
} `json:"space"`
Subtasks []SingleTask `json:"subtasks"`
Attachments []struct{} `json:"attachments"`
}
type GetTasksResponse struct {
Tasks []SingleTask `json:"tasks"`
}
const (
MaxPageSize = 100
)
type TaskTimeInStatusResponse struct {
CurrentStatus struct {
Status string `json:"status"`
Color string `json:"color"`
TotalTime struct {
ByMinute int `json:"by_minute"`
Since string `json:"since"`
} `json:"total_time"`
} `json:"current_status"`
StatusHistory []struct {
Status string `json:"status"`
Color string `json:"color"`
Type string `json:"type"`
TotalTime struct {
ByMinute int `json:"by_minute"`
Since string `json:"since"`
} `json:"total_time"`
Orderindex int `json:"-"`
} `json:"status_history"`
}
type OrderByVal string
const (
OrderByID OrderByVal = "id"
OrderByCreated OrderByVal = "created"
OrderByUpdated OrderByVal = "updated"
OrderByDueDate OrderByVal = "due_date"
)
type TaskQueryOptions struct {
IncludeArchived bool
Page int
OrderBy OrderByVal
Reverse bool
IncludeSubtasks bool
Statuses []string // statuses to query
IncludeClosed bool
Assignees []string
DueDateGreaterThan int
DueDateLessThan int
DateCreatedGreaterThan int
DateCreatedLessThan int
DateUpdatedGreaterThan int
DateUpdatedLessThan int
// CustomFields map[string]interface{}
}
func queryParamsFor(opts *TaskQueryOptions) *url.Values {
urlValues := &url.Values{}
urlValues.Add("page", strconv.Itoa(opts.Page))
if opts.IncludeArchived {
urlValues.Add("archived", "true")
}
if opts.IncludeSubtasks {
urlValues.Add("subtasks", "true")
}
if opts.IncludeClosed {
urlValues.Add("include_closed", "true")
}
if opts.Reverse {
urlValues.Add("reverse", "true")
}
if len(opts.Statuses) > 0 {
for _, v := range opts.Statuses {
urlValues.Add("statuses%5B%5D", v)
}
}
if opts.DueDateGreaterThan > 0 {
urlValues.Add("due_date_gt", strconv.Itoa(opts.DueDateGreaterThan))
}
if opts.DueDateLessThan > 0 {
urlValues.Add("due_date_lt", strconv.Itoa(opts.DueDateLessThan))
}
if opts.DateCreatedGreaterThan > 0 {
urlValues.Add("date_created_gt", strconv.Itoa(opts.DateCreatedGreaterThan))
}
if opts.DateCreatedLessThan > 0 {
urlValues.Add("date_created_lt", strconv.Itoa(opts.DateCreatedLessThan))
}
if opts.DateUpdatedGreaterThan > 0 {
urlValues.Add("date_updated_gt", strconv.Itoa(opts.DateUpdatedGreaterThan))
}
if opts.DateUpdatedLessThan > 0 {
urlValues.Add("date_updated_lt", strconv.Itoa(opts.DateUpdatedLessThan))
}
switch opts.OrderBy {
case OrderByID:
urlValues.Add("order_by", string(OrderByID))
case OrderByDueDate:
urlValues.Add("order_by", string(OrderByDueDate))
case OrderByCreated:
urlValues.Add("order_by", string(OrderByCreated))
case OrderByUpdated:
urlValues.Add("order_by", string(OrderByUpdated))
default:
}
return urlValues
}
// TaskTimeInStatus returns status history for taskID. useCustomTaskIDs should be true if querying with a custom ID.
func (c *Client) TaskTimeInStatus(ctx context.Context, taskID, workspaceID string, useCustomTaskIDs bool) (*TaskTimeInStatusResponse, error) {
if useCustomTaskIDs && workspaceID == "" {
return nil, fmt.Errorf("workspaceID must be provided if querying by custom task id: %w", ErrValidation)
}
urlValues := url.Values{}
urlValues.Set("task_id", taskID)
urlValues.Add("custom_task_ids", strconv.FormatBool(useCustomTaskIDs))
urlValues.Add("team_id", workspaceID)
endpoint := fmt.Sprintf("/task/%s/time_in_status/?%s", taskID, urlValues.Encode())
var taskTimeInStatus TaskTimeInStatusResponse
if err := c.call(ctx, http.MethodGet, endpoint, nil, &taskTimeInStatus); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &taskTimeInStatus, nil
}
// BulkTaskTimeInStatus returns task status history data for the provided taskIDs. Must provide
// >= 2 and <= 100 task IDs at a time.
func (c *Client) BulkTaskTimeInStatus(ctx context.Context, taskIDs []string, workspaceID string, useCustomTaskIDs bool) (map[string]TaskTimeInStatusResponse, error) {
if useCustomTaskIDs && workspaceID == "" {
return nil, fmt.Errorf("workspaceID must be provided if querying by custom task id: %w", ErrValidation)
}
if len(taskIDs) < 2 || len(taskIDs) > 100 {
return nil, fmt.Errorf("must provide between 2 and 100 tasks to retrieve bulk tasks time in status: %w", ErrValidation)
}
urlValues := url.Values{}
urlValues.Set("custom_task_ids", strconv.FormatBool(useCustomTaskIDs))
urlValues.Add("team_id", workspaceID)
for _, v := range taskIDs {
urlValues.Add("task_ids", v)
}
endpoint := fmt.Sprintf("/task/bulk_time_in_status/task_ids/?%s", urlValues.Encode())
var bulkTaskTimeInStatus map[string]TaskTimeInStatusResponse
if err := c.call(ctx, http.MethodGet, endpoint, nil, &bulkTaskTimeInStatus); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return bulkTaskTimeInStatus, nil
}
// TasksForList returns a listing of tasks that belong to the specified listID and fall withing the constraints of queryOpts.
// Clickup has some rather informal paging, so the caller is responsible for inspecting the count of tasks returned, and incrementing
// the Page in queryOpts if the number of tasks is 100.
// ie. if the current page returns 100 tasks (the maximum page size), then another query should be made to get the next page.
func (c *Client) TasksForList(ctx context.Context, listID string, queryOpts *TaskQueryOptions) (*GetTasksResponse, error) {
endpoint := fmt.Sprintf("/list/%s/task/?%s", listID, queryParamsFor(queryOpts).Encode())
var tasks GetTasksResponse
if err := c.call(ctx, http.MethodGet, endpoint, nil, &tasks); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &tasks, nil
}
// TaskByID queries a single task.
func (c *Client) TaskByID(ctx context.Context, taskID, workspaceID string, useCustomTaskIDs, includeSubtasks bool) (*SingleTask, error) {
if useCustomTaskIDs && workspaceID == "" {
return nil, fmt.Errorf("workspaceID must be provided if querying by custom task id: %w", ErrValidation)
}
urlValues := url.Values{}
urlValues.Set("custom_task_ids", strconv.FormatBool(useCustomTaskIDs))
urlValues.Add("include_subtasks", strconv.FormatBool(includeSubtasks))
urlValues.Add("team_id", workspaceID)
endpoint := fmt.Sprintf("/task/%s/?%s", taskID, urlValues.Encode())
var task SingleTask
if err := c.call(ctx, http.MethodGet, endpoint, nil, &task); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &task, nil
}
type TaskRequest struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
Status string `json:"status,omitempty"`
DueDate int `json:"due_date,omitempty"`
DueDateTime bool `json:"due_date_time,omitempty"`
StartDate int `json:"start_date,omitempty"`
StartDateTime bool `json:"start_date_time,omitempty"`
}
// CreateTask inserts a new task into the specified list.
func (c *Client) CreateTask(ctx context.Context, listID string, task TaskRequest) (*SingleTask, error) {
if listID == "" {
return nil, fmt.Errorf("must provide a list id to create a task: %w", ErrValidation)
}
if task.Name == "" {
return nil, fmt.Errorf("must provide a name for a new task: %w", ErrValidation)
}
b, err := json.Marshal(task)
if err != nil {
return nil, fmt.Errorf("unable to serialize new task: %w", err)
}
buf := bytes.NewBuffer(b)
endpoint := fmt.Sprintf("/list/%s/task", listID)
var newTask SingleTask
if err := c.call(ctx, http.MethodPost, endpoint, buf, &newTask); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &newTask, nil
}
type TaskUpdateRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
Status string `json:"status,omitempty"`
DueDate int `json:"due_date,omitempty"`
DueDateTime bool `json:"due_date_time,omitempty"`
StartDate int `json:"start_date,omitempty"`
StartDateTime bool `json:"start_date_time,omitempty"`
}
// UpdateTask changes an existing task.
func (c *Client) UpdateTask(ctx context.Context, task *TaskUpdateRequest, workspaceID string, useCustomTaskIDs bool) (*SingleTask, error) {
if useCustomTaskIDs && workspaceID == "" {
return nil, fmt.Errorf("workspaceID must be provided if updating by custom task id: %w", ErrValidation)
}
if task.ID == "" {
return nil, fmt.Errorf("task to update must have an id provided: %w", ErrValidation)
}
b, err := json.Marshal(task)
if err != nil {
return nil, fmt.Errorf("unable to serialize new task: %w", err)
}
buf := bytes.NewBuffer(b)
urlValues := url.Values{}
urlValues.Set("custom_task_ids", strconv.FormatBool(useCustomTaskIDs))
urlValues.Add("team_id", workspaceID)
endpoint := fmt.Sprintf("/task/%s/?%s", task.ID, urlValues.Encode())
var updatedTask SingleTask
if err := c.call(ctx, http.MethodPut, endpoint, buf, &updatedTask); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &updatedTask, nil
}
// DeleteTask removes an existing task.
func (c *Client) DeleteTask(ctx context.Context, taskID, workspaceID string, useCustomTaskIDs bool) error {
if useCustomTaskIDs && workspaceID == "" {
return fmt.Errorf("workspaceID must be provided if deleting by custom task id: %w", ErrValidation)
}
urlValues := url.Values{}
urlValues.Set("custom_task_ids", strconv.FormatBool(useCustomTaskIDs))
urlValues.Add("team_id", workspaceID)
endpoint := fmt.Sprintf("/task/%s/?%s", taskID, urlValues.Encode())
return c.call(ctx, http.MethodDelete, endpoint, nil, &struct{}{})
}
type CustomFieldInfo struct {
val interface{}
typ string
}
// CustomFieldVal finds the value from a list of arbitrary custome fields and types
// from the task t. fieldName is used as the target field to extract.
// Custom fields that are of type "date" will be returned in the CustomFieldInfo
// as a string of unix milliseconds.
// CustomFieldInfo is an interface{} and should be handled accordingly.
// The consumer of this library can do any of this themselves with the SingleTask
// model. This is simply a utility function.
func (t *SingleTask) CustomFieldVal(fieldName string) *CustomFieldInfo {
var cf CustomFieldInfo
for _, field := range t.CustomFields {
if field.Name == fieldName {
if field.Type == "date" {
if field.Value == nil {
cf.val = ""
return &cf
}
cf.val = field.Value.(string)
cf.typ = field.Type
return &cf
}
for _, v := range field.TypeConfig.Options {
if float64(v.Orderindex) == field.Value.(float64) {
cf.val = v.Name
cf.typ = field.Type
break
}
}
break
}
}
return &cf
}