forked from VojtechVitek/go-trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
394 lines (354 loc) · 12 KB
/
board.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
/*
Copyright 2014 go-trello authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package trello
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
// Board Type for Trello Board
type Board struct {
client *Client
ID string `json:"id"`
Name string `json:"name"`
Desc string `json:"desc"`
DescData struct {
Emoji struct{} `json:"emoji"`
} `json:"descData"`
Closed bool `json:"closed"`
IDMemberCreator string `json:"idMemberCreator"`
IDOrganization string `json:"idOrganization"`
Members []*Member `json:"members"`
Memberships []*Membership `json:"memberships"`
Pinned bool `json:"pinned"`
URL string `json:"url"`
ShortURL string `json:"shortUrl"`
Prefs struct {
PermissionLevel string `json:"permissionLevel"`
Voting string `json:"voting"`
Comments string `json:"comments"`
Invitations string `json:"invitations"`
SelfJoin bool `json:"selfjoin"`
CardCovers bool `json:"cardCovers"`
CardAging string `json:"cardAging"`
CalendarFeedEnabled bool `json:"calendarFeedEnabled"`
Background string `json:"background"`
BackgroundColor string `json:"backgroundColor"`
BackgroundImage string `json:"backgroundImage"`
BackgroundImageScaled []BoardBackground `json:"backgroundImageScaled"`
BackgroundTile bool `json:"backgroundTile"`
BackgroundBrightness string `json:"backgroundBrightness"`
CanBePublic bool `json:"canBePublic"`
CanBeOrg bool `json:"canBeOrg"`
CanBePrivate bool `json:"canBePrivate"`
CanInvite bool `json:"canInvite"`
} `json:"prefs"`
LabelNames struct {
Red string `json:"red"`
Orange string `json:"orange"`
Yellow string `json:"yellow"`
Green string `json:"green"`
Blue string `json:"blue"`
Purple string `json:"purple"`
} `json:"labelNames"`
}
// BoardBackground Type
type BoardBackground struct {
Width int `json:"width"`
Height int `json:"height"`
URL string `json:"url"`
}
// Board - Get board by boardID
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-get
func (c *Client) Board(boardID string) (board *Board, err error) {
body, err := c.Get("/boards/" + boardID)
if err == nil {
board = &Board{}
board.parseBoard(body, c)
}
return
}
// CreateBoard - Create Board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-post
func (c *Client) CreateBoard(name string) (board *Board, err error) {
payload := url.Values{}
payload.Set("name", name)
body, err := c.Post("/boards", payload)
if err == nil {
board = &Board{}
err = board.parseBoard(body, c)
}
return
}
// Duplicate - Duplicate (Copy) Board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-post
func (b *Board) Duplicate(name string, keepCards bool) (board *Board, err error) {
keepFromSource := "none"
if keepCards {
keepFromSource = "cards"
}
payload := url.Values{}
payload.Set("idBoardSource", b.ID)
payload.Set("keepFromSource", keepFromSource)
payload.Set("name", name)
body, err := b.client.Post("/boards", payload)
if err == nil {
board = &Board{}
err = board.parseBoard(body, b.client)
}
return
}
// SetBackground - Sets background on board
// background can be a color or a background id
func (b *Board) SetBackground(background string) (err error) {
return b.Update("prefs/background", background)
}
// SetDescription - Sets background on board
func (b *Board) SetDescription(description string) (err error) {
return b.Update("desc", description)
}
// Update - Update a Board (path and value, see API docs for details)
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-put
func (b *Board) Update(path, value string) (err error) {
payload := url.Values{}
payload.Set("value", value)
body, err := b.client.Put("/boards/"+b.ID+"/"+path, payload)
if err == nil {
err = b.parseBoard(body, b.client)
}
return
}
// Delete - Update a Board (path and value, see API docs for details)
// *WARNING* - No Confirmation Dialog!
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-delete
func (b *Board) Delete() (err error) {
_, err = b.client.Delete("/boards/" + b.ID)
return
}
// Lists - Get lists on a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-lists-get
func (b *Board) Lists() (lists []List, err error) {
body, err := b.client.Get("/boards/" + b.ID + "/lists")
if err == nil {
lists, err = parseListLists(body, b.client)
}
return
}
// GetMembers - Get the members of a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-members-get
func (b *Board) GetMembers() (members []*Member, err error) {
if len(b.Members) == 0 {
body, err := b.client.Get("/boards/" + b.ID + "/members")
if err == nil {
members, err = parseListMembers(body, b.client)
}
} else {
members = b.Members
}
return
}
// AddMember - Add a Member to a board
// memberType can be admin, normal or observer (if left blank will default to normal)
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-members-idmember-put
func (b *Board) AddMember(member *Member, memberType string) (err error) {
if memberType == "" {
memberType = "normal" // default to "normal"
}
payload := url.Values{}
payload.Set("type", memberType)
body, err := b.client.Put("/boards/"+b.ID+"/members/"+member.ID, payload)
if err == nil {
err = b.parseBoard(body, b.client)
}
return
}
// RemoveMember - Remove a Member from a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-members-idmember-put
func (b *Board) RemoveMember(member *Member) (err error) {
body, err := b.client.Delete("/boards/" + b.ID + "/members/" + member.ID)
if err == nil {
err = b.parseBoard(body, b.client)
}
return
}
// GetMembership - Get a Membership of a board by ID
// Get information about the memberships users have to the board.
// filter: Valid Values: admins, all, none, normal (default: all)
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-memberships-get
func (b *Board) GetMembership(id string) (membership *Membership, err error) {
body, err := b.client.Get("/boards/" + b.ID + "/memberships/" + id)
if err == nil {
membership = &Membership{}
err = parseMembership(body, membership, b)
}
return
}
// GetMemberships - Get Memberships of a board
// Get information about the memberships users have to the board.
// filter: Valid Values: admins, all, none, normal (default: all)
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-memberships-get
func (b *Board) GetMemberships() (memberships []*Membership, err error) {
if len(b.Memberships) == 0 {
body, err := b.client.Get("/boards/" + b.ID + "/memberships")
if err == nil {
memberships, err = parseListMemberships(body, b)
if err == nil {
b.Memberships = memberships
}
}
} else {
memberships = b.Memberships
}
return
}
// GetMembershipForMember - Return a Membership that matches a specific Member
func (b *Board) GetMembershipForMember(member *Member) (membership *Membership, err error) {
memberships, err := b.GetMemberships()
if err == nil {
for _, ms := range memberships {
if ms.IDMember == member.ID {
membership = ms
}
}
if membership == nil {
err = fmt.Errorf("ERROR: No membership found for member: " + member.ID)
}
}
return
}
// IsAdmin - Check to see if a member is an admin
func (b *Board) IsAdmin(member *Member) (isAdmin bool) {
ms, err := b.GetMembershipForMember(member)
if err == nil {
return ms.MemberType == "admin"
}
return false
}
// Cards - Get cards on a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-cards-get
func (b *Board) Cards() (cards []Card, err error) {
body, err := b.client.Get("/boards/" + b.ID + "/cards")
if err == nil {
cards, err = parseListCards(body, b.client)
}
return
}
// Card - Get a card on a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-cards-idcard-get
func (b *Board) Card(IDCard string) (card *Card, err error) {
card = &Card{}
body, err := b.client.Get("/boards/" + b.ID + "/cards/" + IDCard)
if err == nil {
err = parseCard(body, card, b.client)
}
return
}
// Checklists - Get checklists on a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-checklists-get
func (b *Board) Checklists() (checklists []Checklist, err error) {
body, err := b.client.Get("/boards/" + b.ID + "/checklists")
if err == nil {
checklists, err = parseListChecklists(body, b.client)
}
return
}
// MemberCards - Get cards for a member ID (string) on a board?
// - URL Link?
func (b *Board) MemberCards(IDMember string) (cards []Card, err error) {
body, err := b.client.Get("/boards/" + b.ID + "/members/" + IDMember + "/cards")
if err == nil {
cards, err = parseListCards(body, b.client)
}
return
}
// Actions - Get Actions for a Board
// - URL LINK?
func (b *Board) Actions(arg ...*Argument) (actions []Action, err error) {
ep := "/boards/" + b.ID + "/actions"
if query := EncodeArgs(arg); query != "" {
ep += "?" + query
}
body, err := b.client.Get(ep)
if err == nil {
actions, err = parseListActions(body, b.client)
}
return
}
// AddList - Add a List to a Board
func (b *Board) AddList(opts List) (list *List, err error) {
list = &List{}
opts.IDBoard = b.ID
payload := url.Values{}
payload.Set("name", opts.Name)
payload.Set("idBoard", opts.IDBoard)
payload.Set("pos", strconv.FormatFloat(float64(opts.Pos), 'g', -1, 32))
body, err := b.client.Post("/lists", payload)
if err == nil {
err = parseList(body, list, b.client)
}
return
}
// Labels - Get Labels on a Board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-labels-get
func (b *Board) Labels() (labels []Label, err error) {
body, err := b.client.Get("/boards/" + b.ID + "/labels")
if err == nil {
labels, err = parseListLabels(body, b.client)
}
return
}
// AddLabel - Create a Label on a board
// - https://developer.atlassian.com/cloud/trello/rest/api-group-boards/#api-boards-id-labels-post
// NOTE: Color can be an empty string
func (b *Board) AddLabel(name, color string) (label *Label, err error) {
label = &Label{}
payload := url.Values{}
payload.Set("name", name)
payload.Set("color", color)
body, err := b.client.Post("/boards/"+b.ID+"/labels", payload)
if err == nil {
err = parseLabel(body, label, b.client)
}
return
}
func (b *Board) parseBoard(body []byte, client *Client) (err error) {
err = json.Unmarshal(body, &b)
if err == nil {
b.client = client
for i := range b.Members {
b.Members[i].client = client
}
for i := range b.Memberships {
b.Memberships[i].client = client
b.Memberships[i].Board = b
}
}
return
}
func parseListBoards(body []byte, client *Client) (boards []*Board, err error) {
err = json.Unmarshal(body, &boards)
if err == nil {
for i := range boards {
boards[i].client = client
// List of boards will not have "Members"
// for i := range board.Members {
// board.Members[i].client = client
// }
for j := range boards[i].Memberships {
boards[i].Memberships[j].client = client
}
}
}
return
}