-
Notifications
You must be signed in to change notification settings - Fork 4
/
spaces.go
242 lines (202 loc) · 6.87 KB
/
spaces.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
// 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 SingleSpace struct {
ID string `json:"id"`
Name string `json:"name"`
Private bool `json:"private"`
Statuses []struct {
ID string `json:"id"`
Status string `json:"status"`
Type string `json:"type"`
Orderindex int `json:"-"`
Color string `json:"color"`
} `json:"statuses"`
MultipleAssignees bool `json:"multiple_assignees"`
Features struct {
DueDates struct {
Enabled bool `json:"enabled"`
StartDate bool `json:"start_date"`
RemapDueDates bool `json:"remap_due_dates"`
RemapClosedDueDate bool `json:"remap_closed_due_date"`
} `json:"due_dates"`
Sprints struct {
Enabled bool `json:"enabled"`
} `json:"sprints"`
Points struct {
Enabled bool `json:"enabled"`
} `json:"points"`
CustomItems struct {
Enabled bool `json:"enabled"`
} `json:"custom_items"`
Priorities struct {
Enabled bool `json:"enabled"`
Priorities []struct {
ID string `json:"id"`
Priority string `json:"priority"`
Color string `json:"color"`
Orderindex string `json:"-"`
} `json:"priorities"`
} `json:"priorities"`
Tags struct {
Enabled bool `json:"enabled"`
} `json:"tags"`
CheckUnresolved struct {
Enabled bool `json:"enabled"`
Subtasks struct {
} `json:"subtasks"`
Checklists struct {
} `json:"checklists"`
Comments struct {
} `json:"comments"`
} `json:"check_unresolved"`
Zoom struct {
Enabled bool `json:"enabled"`
} `json:"zoom"`
Milestones struct {
Enabled bool `json:"enabled"`
} `json:"milestones"`
CustomFields struct {
Enabled bool `json:"enabled"`
} `json:"custom_fields"`
DependencyWarning struct {
Enabled bool `json:"enabled"`
} `json:"dependency_warning"`
} `json:"features"`
Archived bool `json:"archived"`
Members []struct {
User struct {
ID int `json:"id"`
Username string `json:"username"`
Color string `json:"color"`
ProfilePicture string `json:"profilePicture"`
Initials string `json:"initials"`
} `json:"user"`
} `json:"members"`
}
type SpacesResponse struct {
Spaces []SingleSpace `json:"spaces"`
}
func (c *Client) SpacesForWorkspace(ctx context.Context, teamID string, includeArchived bool) (*SpacesResponse, error) {
urlValues := url.Values{}
urlValues.Set("archived", strconv.FormatBool(includeArchived))
endpoint := fmt.Sprintf("/team/%s/space/?%s", teamID, urlValues.Encode())
var spacesResponse SpacesResponse
if err := c.call(ctx, http.MethodGet, endpoint, nil, &spacesResponse); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &spacesResponse, nil
}
func (c *Client) SpaceByID(ctx context.Context, spaceID string) (*SingleSpace, error) {
endpoint := fmt.Sprintf("/space/%s", spaceID)
var spaceResponse SingleSpace
if err := c.call(ctx, http.MethodGet, endpoint, nil, &spaceResponse); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &spaceResponse, nil
}
type TimeTracking struct {
Enabled bool `json:"enabled"`
}
type DueDates struct {
Enabled bool `json:"enabled"`
StartDate bool `json:"start_date"`
RemapDueDates bool `json:"remap_due_dates"`
RemapClosedDueDate bool `json:"remap_closed_due_date"`
}
type Tags struct {
Enabled bool `json:"enabled,omitempty"`
}
type TimeEstimates struct {
Enabled bool `json:"enabled,omitempty"`
}
type Checklists struct {
Enabled bool `json:"enabled,omitempty"`
}
type CustomFields struct {
Enabled bool `json:"enabled,omitempty"`
}
type RemapDependencies struct {
Enabled bool `json:"enabled,omitempty"`
}
type DependencyWarning struct {
Enabled bool `json:"enabled,omitempty"`
}
type Portfolios struct {
Enabled bool `json:"enabled,omitempty"`
}
type Features struct {
DueDates *DueDates `json:"due_dates,omitempty"`
TimeTracking *TimeTracking `json:"time_tracking,omitempty"`
Tags *Tags `json:"tags,omitempty"`
TimeEstimates *TimeEstimates `json:"time_estimates,omitempty"`
Checklists *Checklists `json:"checklists,omitempty"`
CustomFields *CustomFields `json:"custom_fields,omitempty"`
RemapDependencies *RemapDependencies `json:"remap_dependencies,omitempty"`
DependencyWarning *DependencyWarning `json:"dependency_warning,omitempty"`
Portfolios *Portfolios `json:"portfolios,omitempty"`
}
type CreateSpaceRequest struct {
WorkspaceID string
Name string `json:"name"`
MultipleAssignees bool `json:"multiple_assignees"`
Features *Features `json:"features,omitempty"`
}
// CreateSpaceForWorkspace uses the parameters from space to create a new space in the specified workspace.
func (c *Client) CreateSpaceForWorkspace(ctx context.Context, space CreateSpaceRequest) (*SingleSpace, error) {
if space.WorkspaceID == "" {
return nil, fmt.Errorf("must provide a workspace id: %w", ErrValidation)
}
b, err := json.Marshal(space)
if err != nil {
return nil, fmt.Errorf("unable to serialize new space: %w", err)
}
buf := bytes.NewBuffer(b)
endpoint := fmt.Sprintf("/team/%s/space", space.WorkspaceID)
var newSpace SingleSpace
if err := c.call(ctx, http.MethodPost, endpoint, buf, &newSpace); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &newSpace, nil
}
type UpdateSpaceRequest struct {
ID string
Name string `json:"name,omitempty"`
MultipleAssignees bool `json:"multiple_assignees,omitempty"`
Features *Features `json:"features,omitempty"`
}
// UpdateSpaceForWorkspace makes changes to an existing space using parameters specified in space.
func (c *Client) UpdateSpaceForWorkspace(ctx context.Context, space UpdateSpaceRequest) (*SingleSpace, error) {
if space.ID == "" {
return nil, fmt.Errorf("must provide a workspace id: %w", ErrValidation)
}
b, err := json.Marshal(space)
if err != nil {
return nil, fmt.Errorf("unable to serialize new space: %w", err)
}
buf := bytes.NewBuffer(b)
endpoint := fmt.Sprintf("/space/%s", space.ID)
var updatedSpace SingleSpace
if err := c.call(ctx, http.MethodPut, endpoint, buf, &updatedSpace); err != nil {
return nil, fmt.Errorf("failed to make clickup request: %w", err)
}
return &updatedSpace, nil
}
// DeleteSpace removes an existing space using spaceID.
func (c *Client) DeleteSpace(ctx context.Context, spaceID string) error {
if spaceID == "" {
return fmt.Errorf("must provide a space id to delete: %w", ErrValidation)
}
return c.call(ctx, http.MethodGet, fmt.Sprintf("/space/%s", spaceID), nil, &struct{}{})
}