-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrello_integration.py
More file actions
326 lines (272 loc) · 8.59 KB
/
trello_integration.py
File metadata and controls
326 lines (272 loc) · 8.59 KB
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
# -*- coding: utf-8 -*-
# std
import copy
from datetime import datetime
import dateutil.parser
# 3p
from trello import TrelloApi
from github_integration import SingleIssue as GithubSingleIssue
from github_integration import AssociatedIssue as GithubAssociatedIssue
from github_integration import Milestone as GithubMilestone
class Trello(TrelloApi):
"""
Trello wrapper
"""
APP_NAME = 'Partoo'
TRELLO_MILESTONE_BOARD_NAME = "TECH - Milestones"
# TRELLO_MILESTONE_BOARD_NAME = "TEST_MILESTONES"
TRELLO_SINGLE_ISSUE_BOARD_NAME = "TECH - Issues"
# TRELLO_SINGLE_ISSUE_BOARD_NAME = "TEST_ISSUES"
def __init__(self, app_key, token, username=None, user=None):
super(Trello, self).__init__(app_key, token)
self.username = username
self.user = user
@property
def milestone_board(self):
boards = [board for board in self.members.get_board(self.username)
if board['name'] == self.TRELLO_MILESTONE_BOARD_NAME]
return MilestoneBoard(self, **boards[0])
@property
def single_issue_board(self):
boards = [board for board in self.members.get_board(self.username)
if board['name'] == self.TRELLO_SINGLE_ISSUE_BOARD_NAME]
return SingleIssueBoard(self, **boards[0])
def update(self, issue):
"""
Update relevant board from issue given
:param issue:
:return:
"""
if isinstance(issue, GithubSingleIssue):
card = Card.from_github(self, issue)
existing_card = self.single_issue_board.get_card(card.name)
if existing_card:
self.single_issue_board.update_card(existing_card, card.to_json_dict())
else:
self.single_issue_board.add_card(card)
elif isinstance(issue, GithubAssociatedIssue):
checklist_item = CheckListItem.from_github(self, issue)
existing_checklist = self.milestone_board.get_checklist(issue.milestone.title)
if existing_checklist:
# existing_checklist_item = self.milestone_board.get_checklist_item(checklist_item.name, existing_checklist)
# if existing_checklist_item:
# pass # TODO: should update
# else:
# self.milestone_board.add_checklist_item(checklist_item.to_json_dict(), existing_checklist)
pass
else:
# Should create card ?
card = self.milestone_board.add_card(Card.from_github(self, issue.milestone))
checklist = self.milestone_board.add_checklist(CheckList.from_github(self, issue.milestone), card)
# self.milestone_board.add_checklist_item(checklist_item.to_json_dict(), checklist)
else: raise Exception
def refresh_token(self):
return self.get_token_url(self.APP_NAME, expires='never', write_access=True)
class Board(object):
"""
Load raw data from Trello
"""
LIST_NAMES = [
"TODO",
"DOING",
"DONE",
]
def __init__(self, trello_api, **kwargs):
self.trello_api = trello_api
self.id = kwargs.get("id")
self.name = kwargs.get("name")
@property
def cards(self):
return [Card(**card) for card in self.trello_api.boards.get_card_filter("open", self.id)]
@property
def lists(self):
return [List(**list) for list in self.trello_api.boards.get_list(self.id)] # init lists from Trello
@property
def checklists(self):
return [CheckList(**checklist) for checklist in self.trello_api.boards.get_checklist(self.id)] # init checklists from Trello
def get_card(self, card_name):
for card in self.cards:
if card_name == card.name: return card
return None
def add_card(self, card):
resp = self.trello_api.cards.new(
card.name,
card.idList,
desc=card.desc
)
added_card = Card(**resp)
self.cards.append(added_card)
return added_card
def update_card(self, card, json_dict):
"""
Update card in Trello with data from a json_dict
:param card:
:param json_dict:
:return:
"""
self.trello_api.cards.update(
card.id,
**json_dict
)
# TODO: implement reload
def get_checklist(self, checklist_name):
for checklist in self.checklists:
if checklist_name == checklist.name: return checklist
return None
def add_checklist(self, checklist, card):
resp = self.trello_api.checklists.new(
checklist.name,
card.id
)
return CheckList(**resp)
def get_checklist_item(self, checklist_item_name, checklist):
checklist_item = self.trello_api.checklists.get_checkItem(
checklist.id,
name=checklist_item_name
)
if checklist_item:
return CheckListItem(**checklist_item)
return None
def add_checklist_item(self, checklist_item, checklist):
# TODO implement
return checklist_item
def update_checklist_item(self, checklist_item, json_dict):
pass
def get_list(self, element):
return self.lists[self.get_list_index(element)]
@staticmethod
def get_list_index(element):
# Implemented by child
pass
class MilestoneBoard(Board):
LIST_NAMES = [
"Product Backlog",
"Sprint Backlog",
"Staging",
"Prod",
]
@staticmethod
def get_list_index(element):
if element.state == "closed": return MilestoneBoard.LIST_NAMES.index("Prod")
elif element.issues: return MilestoneBoard.LIST_NAMES.index("Sprint Backlog")
elif element.deadline: return MilestoneBoard.LIST_NAMES.index("Sprint Backlog")
return MilestoneBoard.LIST_NAMES.index("Product Backlog")
class SingleIssueBoard(Board):
LIST_NAMES = [
"TOUPGRADE",
"TODO",
"DOING",
"DONE",
"ARCHIVE"
]
@staticmethod
def get_list_index(element):
if element.state == "closed":
if datetime.now().strftime("%W") == dateutil.parser.parse(element.closed_at).strftime("%W"):
return SingleIssueBoard.LIST_NAMES.index("DONE")
else:
return SingleIssueBoard.LIST_NAMES.index("ARCHIVE")
return SingleIssueBoard.LIST_NAMES.index("TODO")
class List(object):
"""
Load raw data from Trello
"""
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.name = kwargs.get("name")
class Card(object):
"""
Load raw data from json dict
"""
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.idList = kwargs.get("idList")
self.name = kwargs.get("name")
self.desc = kwargs.get("desc")
self.labels = kwargs.get("labels", [])
self.due = kwargs.get("due", None)
self.closed = kwargs.get("closed", None)
@classmethod
def from_github(cls, trello_api, element):
if isinstance(element, GithubMilestone):
input_dict = {
"id": element.id,
"idList": trello_api.milestone_board.get_list(element).id,
"name": element.title,
"desc": element.description,
"due": element.deadline,
# "closed": element.state == "closed"
"closed": False # do not close them so far
}
elif isinstance(element, GithubSingleIssue):
input_dict = {
"id": element.id,
"idList": trello_api.single_issue_board.get_list(element).id,
"name": element.title,
"desc": element.description,
# "closed": element.state == "closed"
"closed": False # do not close them so far
}
return cls(**input_dict)
def to_json_dict(self, action="update"):
if action == "update":
attrs = ["name", "desc", "closed", "idList", "due"]
for attr in copy.deepcopy(self).__dict__:
if attr not in attrs:
delattr(self, attr)
elif type(getattr(self, attr)) == bool: # Convert bool to str
setattr(self, attr, str(getattr(self, attr)).lower())
return self.__dict__
class CheckList(object):
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.name = kwargs.get("name")
@classmethod
def from_github(cls, trello_api, element):
if isinstance(element, GithubMilestone):
input_dict = {
"id": element.id,
# "idList": trello_api.single_issue_board.get_list(element).id,
"name": element.title,
# "desc": element.description,
# "closed": element.state == "closed"
# "closed": False # do not close them so far
}
return cls(**input_dict)
def to_json_dict(self, action="update"):
return self.__dict__
class CheckListItem(object):
def __init__(self, **kwargs):
self.id = kwargs.get("id")
self.name = kwargs.get("name")
@classmethod
def from_github(cls, trello_api, element):
if isinstance(element, GithubAssociatedIssue):
input_dict = {
"id": element.id,
# "idList": trello_api.single_issue_board.get_list(element).id,
"name": element.title,
# "desc": element.description,
# "closed": element.state == "closed"
# "closed": False # do not close them so far
}
return cls(**input_dict)
def to_json_dict(self, action="update"):
return self.__dict__
#
#
# TRELLO_USER = os.environ["TRELLO_USER"]
# TRELLO_APP_KEY = os.environ["TRELLO_APP_KEY"]
# TRELLO_TOKEN = os.environ["TRELLO_TOKEN"]
# t = Trello(TRELLO_APP_KEY, TRELLO_TOKEN, username=TRELLO_USER)
#
# milestone = GithubMilestone(**{
#
# })
# t.update(GithubAssociatedIssue(**{
# "title": "HELLO",
# "state": "closed",
# "description": "OK GO",
# "closed_at": u'2017-01-26T11:53:05Z',
# "milestone": {"id": "fake_id", "title": "OK OK"}
# }))