This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
/
connection.go
319 lines (255 loc) · 6.85 KB
/
connection.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
log "github.com/smashwilson/sprint-closer/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
// Connection performs authenticated actions against the Trello API.
type Connection struct {
profile Profile
}
// Org contains a little information about a Trello organization.
type Org struct {
ID string
MemberIDs []string
}
// List captures information about a Trello List.
type List struct {
ID string `json:"id"`
Name string `json:"name"`
Position float64 `json:"pos"`
}
func (c Connection) url(parts []string, query map[string]string) string {
pathParts := []string{"1"}
pathParts = append(pathParts, parts...)
queryValues := url.Values{
"key": []string{c.profile.Key},
"token": []string{c.profile.Token},
}
for key, value := range query {
queryValues[key] = []string{value}
}
u := url.URL{
Scheme: "https",
Host: "trello.com",
Path: strings.Join(pathParts, "/"),
RawQuery: queryValues.Encode(),
}
return u.String()
}
func (c Connection) extract(resp *http.Response, response interface{}) error {
defer resp.Body.Close()
if resp.StatusCode != 200 && resp.StatusCode != 201 {
rbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
rbody = []byte(err.Error())
}
return fmt.Errorf("Unexpected status code: %d\n%s", resp.StatusCode, rbody)
}
if response != nil {
return json.NewDecoder(resp.Body).Decode(response)
}
return nil
}
func (c Connection) get(url string, response interface{}) error {
resp, err := http.Get(url)
if err != nil {
return err
}
return c.extract(resp, response)
}
func (c Connection) post(url string, payload, response interface{}) error {
var body io.Reader
if payload != nil {
b, err := json.Marshal(payload)
if err != nil {
return err
}
body = bytes.NewBuffer(b)
}
resp, err := http.Post(url, "application/json", body)
if err != nil {
return err
}
return c.extract(resp, response)
}
func (c Connection) put(url string, payload, response interface{}) error {
var body io.Reader
if payload != nil {
b, err := json.Marshal(payload)
if err != nil {
return err
}
body = bytes.NewBuffer(b)
}
req, err := http.NewRequest("PUT", url, body)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
return c.extract(resp, response)
}
func (c Connection) delete(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
_, err = http.DefaultClient.Do(req)
return err
}
// CreateBoard creates a new Trello board.
func (c Connection) CreateBoard(name string) (string, error) {
u := c.url([]string{"boards"}, nil)
reqBody := map[string]string{
"name": name,
"idOrganization": c.profile.Organization,
"prefs_permissonLevel": "org",
}
var resp struct {
ID string `json:"id"`
}
err := c.post(u, reqBody, &resp)
return resp.ID, err
}
// FindMyUserID returns the user ID associated with the token we're using.
func (c Connection) FindMyUserID() (string, error) {
u := c.url([]string{"members", "me"}, nil)
var respBody struct {
ID string `json:"id"`
}
err := c.get(u, &respBody)
return respBody.ID, err
}
// FindOrg looks up the ID and members of the configured organization.
func (c Connection) FindOrg() (*Org, error) {
u := c.url([]string{"organizations", c.profile.Organization}, map[string]string{
"members": "all",
})
var respBody struct {
ID string `json:"id"`
Members []struct {
ID string `json:"id"`
Username string `json:"username"`
} `json:"members"`
}
err := c.get(u, &respBody)
if err != nil {
return nil, err
}
memberIDs := make([]string, 0, len(respBody.Members))
for _, member := range respBody.Members {
memberIDs = append(memberIDs, member.ID)
}
return &Org{
ID: respBody.ID,
MemberIDs: memberIDs,
}, err
}
// AddMember adds all members of the named organization to a board.
func (c Connection) AddMember(boardID string, memberID string) error {
u := c.url([]string{"boards", boardID, "members", memberID}, map[string]string{
"type": "normal",
})
return c.put(u, nil, nil)
}
// FindBoard discovers the ID of an existing board by name.
func (c Connection) FindBoard(name string) (string, error) {
u := c.url([]string{"organizations", c.profile.Organization, "boards"}, map[string]string{
"fields": "name",
})
var boardResults []struct {
ID string `json:"id"`
Name string `json:"name"`
}
err := c.get(u, &boardResults)
if err != nil {
return "", err
}
for _, board := range boardResults {
log.WithFields(log.Fields{
"name": board.Name,
"id": board.ID,
}).Debug("Board")
if board.Name == name {
return board.ID, nil
}
}
return "", fmt.Errorf("Unable to find a board with the name [%s].", name)
}
// GetListIDs returns an array of IDs of the lists on an existing board.
func (c Connection) GetListIDs(boardID string) ([]string, error) {
u := c.url([]string{"boards", boardID, "lists"}, nil)
var respBody []struct {
ID string `json:"id"`
}
err := c.get(u, &respBody)
if err != nil {
return nil, err
}
ids := make([]string, 0, len(respBody))
for _, each := range respBody {
ids = append(ids, each.ID)
}
return ids, nil
}
// DeleteList deletes a list, or as close to it as Trello lets you.
func (c Connection) DeleteList(listID string) error {
u := c.url([]string{"lists", listID, "closed"}, map[string]string{
"value": "true",
})
return c.put(u, nil, nil)
}
// FindList locates a list on a board by name.
func (c Connection) FindList(name string, boardID string) (*List, error) {
u := c.url([]string{"boards", boardID, "lists"}, nil)
var listResults []List
err := c.get(u, &listResults)
if err != nil {
return nil, err
}
for _, list := range listResults {
log.WithFields(log.Fields{
"name": list.Name,
"id": list.ID,
"position": list.Position,
}).Debug("List")
if list.Name == name {
return &list, nil
}
}
return nil, fmt.Errorf("Unable to find a list with the name [%s].", name)
}
// MoveList moves a list to a different board.
func (c Connection) MoveList(listID string, toBoardID string, position int) error {
u := c.url([]string{"lists", listID, "idBoard"}, nil)
var params struct {
Value string `json:"value"`
Position string `json:"pos"`
}
params.Value = toBoardID
if position != 0 {
params.Position = strconv.Itoa(position)
} else {
params.Position = "top"
}
return c.put(u, ¶ms, nil)
}
// AddList creates a new list on the specified board at the given position.
func (c Connection) AddList(name, boardID string, position float64) error {
u := c.url([]string{"boards", boardID, "lists"}, map[string]string{
"name": name,
"pos": strconv.FormatFloat(position, 'f', 1, 64),
})
return c.post(u, nil, nil)
}