-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.go
105 lines (82 loc) · 2.72 KB
/
task.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
package clickup
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
// Task holds information for a ClickUp Task.
type Task struct {
ID string
Name string
Status TaskStatus
URL string
}
// TaskStatus holds a Task's Status information.
type TaskStatus struct {
Status Status
}
// GetTask fetches and returns a Task from ClickUp.
func (c Client) GetTask(taskID string) (Task, error) {
httpClient := &http.Client{}
url := c.url + "/task/" + taskID
request, _ := http.NewRequest(http.MethodGet, url, nil)
request.Header.Add("Authorization", c.key)
request.Header.Add("Content-Type", "application/json")
var task Task
response, err := httpClient.Do(request)
if err != nil {
return task, errors.Wrap(err, "Could not send request to the ClickUp API")
}
if response.StatusCode != http.StatusOK {
return task, errors.New(fmt.Sprint("ClickUp returned status ", response.StatusCode))
}
if err := json.NewDecoder(response.Body).Decode(&task); err != nil {
return task, errors.Wrap(err, "Could not parse Task body")
}
return task, nil
}
// UpdateTaskRequest holds the payload for UpdateTask.
type UpdateTaskRequest struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Status Status `json:"status,omitempty"`
Priority int `json:"priority,omitempty"`
TimeEstimate int `json:"time_estimate,omitempty"`
Assignees Assignees `json:"assignees,omitempty"`
Archived bool `json:"archived,omitempty"`
}
// Assignees holds lists of users to be added or removed from the Task
type Assignees struct {
Add []int `json:"add,omitempty"`
Rem []int `json:"rem,omitempty"`
}
// UpdateTask makes changes to a Task on ClickUp.
// Only pass information that is changed in the task parameter.
func (c Client) UpdateTask(taskID string, task UpdateTaskRequest) error {
httpClient := &http.Client{}
url := c.url + "/task/" + taskID
body, err := json.Marshal(task)
if err != nil {
return errors.Wrap(err, "Could not create UpdateTask body")
}
request, _ := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(body))
request.Header.Add("Authorization", c.key)
request.Header.Add("Content-Type", "application/json")
response, err := httpClient.Do(request)
if err != nil {
return errors.Wrap(err, "Could not send request to the ClickUp API")
}
if response.StatusCode != http.StatusOK {
return errors.New(fmt.Sprint("ClickUp returned status ", response.StatusCode))
}
var updatedTask Task
if err := json.NewDecoder(response.Body).Decode(&updatedTask); err != nil {
return errors.Wrap(err, "Could not parse Task body")
}
if updatedTask.Status.Status != task.Status {
return ErrStatusNotUpdated
}
return nil
}