/
org-trello-api.el
286 lines (247 loc) · 11.5 KB
/
org-trello-api.el
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
;;; org-trello-api.el --- Interface functions to Trello API
;;; Commentary:
;;; Code:
(require 'dash)
(require 'org-trello-setup)
(require 'org-trello-log)
(require 'org-trello-hash)
(require 'org-trello-data)
(defun orgtrello-api-make-query (method uri &optional params)
"Make a query hash map from a METHOD, URI and optional parameter PARAMS."
(let ((h (orgtrello-hash-empty-hash)))
(->> (if params (orgtrello-data-put-entity-params params h) h)
(orgtrello-data-put-entity-method method)
(orgtrello-data-put-entity-uri uri))))
(defun orgtrello-api--deal-with-optional-value (optional-entry value entries)
"Add the optional value in entries depending on optional-entry.
If OPTIONAL-ENTRY is non nil, cons the VALUE to ENTRIES and return it.
Otherwise,return ENTRIES."
(if optional-entry (cons value entries) entries))
(defun orgtrello-api--deal-with-optional-values (optional-entries-values entries)
"Add the optional entry/value OPTIONAL-ENTRIES-VALUES in ENTRIES.
Return entries updated with value if entry, entries untouched otherwise."
(--reduce-from (orgtrello-api--deal-with-optional-value (car it) (cdr it) acc)
entries
optional-entries-values))
(defun orgtrello-api-add-board (name &optional description)
"Create a board query from NAME and optional DESCRIPTION."
(orgtrello-api-make-query "POST" "/boards"
(orgtrello-api--deal-with-optional-value
description
`("desc" . ,description)
`(("name" . ,name)))))
(defun orgtrello-api-get-boards (&optional filter)
"Retrieve the current boards of the user.
If FILTER is specified, this will filter on this."
(orgtrello-api-make-query "GET" "/members/me/boards"
(orgtrello-api--deal-with-optional-values
`((,filter . ("filter" . ,filter)))
`(("lists" . "open")))))
(defun orgtrello-api-get-board (id)
"Create a retrieve board with board ID query."
(orgtrello-api-make-query
"GET"
(format "/boards/%s" id)
'(("memberships" . "active")
("memberships_member" . "true")
("lists" . "open")
("fields" . "name,memberships,closed")
("labels" . "all")
("label_fields" . "name,color"))))
(defun orgtrello-api-do (api-uri id &optional undo-flag)
"Compute the query to do/undo thing using API-URI and ID.
When UNDO-FLAG is set, trigger the undo computation."
(orgtrello-api-make-query "PUT" (format api-uri id)
`(("value" . ,(if undo-flag "false" "true")))))
(defun orgtrello-api-close-board (board-id)
"Close a board with id BOARD-ID."
(orgtrello-api-do "/boards/%s/closed" board-id))
(defun orgtrello-api-open-board (board-id)
"Open a board with id BOARD-ID."
(orgtrello-api-do "/boards/%s/closed" board-id 'open))
(defun orgtrello-api-get-members (board-id)
"Retrieve the memberships from a BOARD-ID."
(orgtrello-api-make-query "GET" (format "/boards/%s/members" board-id)))
(defun orgtrello-api-get-cards (board-id)
"Create a cards retrieval from the board with BOARD-ID query."
(orgtrello-api-make-query
"GET"
(format "/boards/%s/cards" board-id)
'(("actions" . "commentCard")
("fields" .
"closed,desc,due,idBoard,idChecklists,idList,idMembers,name,pos"))))
(defun orgtrello-api-get-full-cards (board-id)
"Create a cards retrieval from the board with BOARD-ID query."
(orgtrello-api-make-query
"GET"
(format "/boards/%s/cards" board-id)
'(("actions" . "commentCard")
("checklists" . "all")
;;("checkItemStates" . "true")
("filter" . "open")
("fields" .
"closed,desc,due,idBoard,idList,idMembers,labels,name,pos"))))
(defun orgtrello-api-get-archived-cards (board-id)
"Create a cards retrieval from the board with BOARD-ID query."
(orgtrello-api-make-query
"GET"
(format "/boards/%s/cards" board-id)
'(("filter" . "closed")
("fields" .
"closed,desc,due,idBoard,idList,idMembers,labels,name,pos"))))
(defun orgtrello-api-get-card (card-id)
"Create a get-card with CARD-ID query."
(orgtrello-api-make-query
"GET"
(format "/cards/%s" card-id)
'(("actions" . "commentCard")
("action_fields" . "data")
("action_memberCreator_fields" . "username")
("fields" .
"closed,dateLastActivity,desc,due,idChecklists,idList,idMembers,labels,name,pos"))))
(defun orgtrello-api-get-full-card (card-id)
"Create a get-card with details query with CARD-ID query."
(orgtrello-api-make-query
"GET"
(format "/cards/%s" card-id)
'(("actions" . "commentCard")
("action_fields" . "data,date")
("checklists" . "all")
("action_memberCreator_fields" . "username")
("fields" . "closed,dateLastActivity,desc,due,idList,idMembers,labels,name,pos"))))
(defun orgtrello-api-delete-card (card-id)
"Create a delete card with id CARD-ID query."
(orgtrello-api-make-query "DELETE" (format "/cards/%s" card-id)))
(defun orgtrello-api-archive-card (card-id)
"Archive a card with id CARD-ID."
(orgtrello-api-do "/cards/%s/closed" card-id))
(defun orgtrello-api-unarchive-card (card-id)
"Unarchive a card with id CARD-ID."
(orgtrello-api-do "/cards/%s/closed" card-id 'unarchive))
(defun orgtrello-api-get-lists (board-id)
"Create a get-lists of the board with BOARD-ID."
(orgtrello-api-make-query "GET" (format "/boards/%s/lists" board-id)))
(defun orgtrello-api-close-list (list-id)
"Create a close list with id LIST-ID query."
(orgtrello-api-do "/lists/%s/closed" list-id))
(defun orgtrello-api-open-list (list-id)
"Create a close list with id LIST-ID query."
(orgtrello-api-do "/lists/%s/closed" list-id 'open))
(defun orgtrello-api-add-list (name idBoard &optional pos)
"Create an add a list with NAME, IDBOARD and optional POS."
(orgtrello-api-make-query "POST" "/lists/"
(orgtrello-api--deal-with-optional-value
pos
`("pos" . ,pos)
`(("name" . ,name) ("idBoard" . ,idBoard)))))
(defun orgtrello-api-add-card (name idList
&optional due id-members desc labels pos)
"Create an add a card with NAME to the list IDLIST.
Optional fields DUE, ID-MEMBERS, DESC, LABELS, POS query."
(orgtrello-api-make-query
"POST"
"/cards/"
(orgtrello-api--deal-with-optional-values
`((,id-members . ("idMembers" . ,id-members))
(,due . ("due" . ,due))
(,desc . ("desc" . ,desc))
(,labels . ("labels" . ,labels))
(,pos . ("pos" . ,pos)))
`(("name" . ,name)
("idList" . ,idList)))))
(defun orgtrello-api-move-card (card-id idList
&optional name due id-members desc
labels pos)
"Create an update a card CARD-ID to IDLIST.
Optional NAME, DUE date, ID-MEMBERS, DESC, LABELS, POS query."
(let ((due (if due due "")))
(->> (orgtrello-api--deal-with-optional-values
`((,name . ("name" . ,name))
(,id-members . ("idMembers" . ,id-members))
(,due . ("due" . ,due))
(,desc . ("desc" . ,desc))
(,labels . ("labels" . ,labels))
(,pos . ("pos" . ,pos)))
`(("idList" . ,idList)))
(orgtrello-api-make-query "PUT" (format "/cards/%s" card-id)))))
(defun orgtrello-api-add-checklist (card-id name pos)
"Create an add a checklist to a card CARD-ID, checklist with NAME, POS query."
(orgtrello-api-make-query "POST"
(format "/cards/%s/checklists" card-id)
`(("name" . ,name)
("pos" . ,pos))))
(defun orgtrello-api-update-checklist (checklist-id name pos)
"Create an update the checklist CHECKLIST-ID with NAME, POS query."
(orgtrello-api-make-query "PUT"
(format "/checklists/%s" checklist-id)
`(("name" . ,name)
("pos" . ,pos))))
(defun orgtrello-api-get-checklist (checklist-id &optional without-items)
"Create a retrieve a checklist CHECKLIST-ID.
Optional fields WITHOUT-ITEMS flag query."
(let ((default-params '(("fields" . "name,pos,idCard")
("checkItem_fields" . "name,pos,state"))))
(orgtrello-api-make-query "GET"
(format "/checklists/%s" checklist-id)
(if without-items
(cons '("checkItems" . "none")
default-params)
default-params))))
(defun orgtrello-api-delete-checklist (checklist-id)
"Create a delete a checklist CHECKLIST-ID."
(orgtrello-api-make-query "DELETE" (format "/checklists/%s" checklist-id)))
(defun orgtrello-api-add-items (checklist-id name &optional checked pos)
"Create an add items to a checklist CHECKLIST-ID with NAME.
Optional fields CHECKED state query and POS."
(->> (orgtrello-api--deal-with-optional-values
`((,checked . ("checked" . ,checked))
(,pos . ("pos" . ,pos)))
`(("name" . ,name)))
(orgtrello-api-make-query
"POST"
(format "/checklists/%s/checkItems" checklist-id))))
(defun orgtrello-api-update-item (card-id checklist-id item-id name
&optional state pos)
"Create an update item from the CARD-ID, CHECKLIST-ID and ITEM-ID with NAME.
Optional fields STATE query, POS."
(->> (orgtrello-api--deal-with-optional-values `((,state . ("state" . ,state))
(,pos . ("pos" . ,pos)))
`(("name" . ,name)))
(orgtrello-api-make-query
"PUT"
(format "/cards/%s/checklist/%s/checkItem/%s"
card-id checklist-id item-id))))
(defun orgtrello-api-get-item (checklist-id item-id)
"Create a get item from the checklist CHECKLIST-ID and item ITEM-ID query."
(orgtrello-api-make-query
"GET"
(format "/checklists/%s/checkItems/%s" checklist-id item-id)
'(("fields" . "name,pos,state"))))
(defun orgtrello-api-delete-item (checklist-id item-id)
"Create a delete from the checklist CHECKLIST-ID the item ITEM-ID query."
(orgtrello-api-make-query
"DELETE"
(format "/checklists/%s/checkItems/%s" checklist-id item-id)))
(defun orgtrello-api-get-me ()
"Retrieve the current user's member informations query."
(orgtrello-api-make-query "GET" "/members/me"))
(defun orgtrello-api-add-card-comment (card-id comment-text)
"Create a add a card comment to the card with id CARD-ID with the COMMENT-TEXT."
(orgtrello-api-make-query "POST"
(format "/cards/%s/actions/comments" card-id)
`(("text" . ,comment-text))))
(defun orgtrello-api-update-card-comment (card-id comment-id comment-text)
"Update the card CARD-ID comment COMMENT-ID with COMMENT-TEXT.
https://trello.com/docs/api/card/index.html#put-1-cards-card-id-or-shortlink-actions-idaction-comments"
(orgtrello-api-make-query
"PUT"
(format "/cards/%s/actions/%s/comments" card-id comment-id)
`(("text" . ,comment-text ))))
(defun orgtrello-api-delete-card-comment (card-id comment-id)
"Delete the card (CARD-ID)'s comment with id COMMENT-ID.
https://trello.com/docs/api/card/index.html#delete-1-cards-card-id-or-shortlink-actions-idaction-comments"
(orgtrello-api-make-query
"DELETE" (format "/cards/%s/actions/%s/comments" card-id comment-id)))
(orgtrello-log-msg orgtrello-log-debug "orgtrello-api loaded!")
(provide 'org-trello-api)
;;; org-trello-api.el ends here