/
linker.py
249 lines (231 loc) · 7.55 KB
/
linker.py
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
import sys
import yaml
import requests
import logging
#disabling insecureplatformwarnings for cleaner output
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
from git import Git
from trello import Trello
logging.basicConfig()
GIT = "Github Issue"
BUG = "Bug"
ENH = "Enhancement"
QA = "Quality"
Q = "Question"
def sync(git, trello, issue, card, newer, config):
#first update comments both ways. No comment deletion yet.
issue_comments_list = []
if len(issue["body"]) > 0:
created = issue["created_at"]
created = datetime.strptime(created,"%Y-%m-%dT%H:%M:%SZ")
issue_comments_list.append([issue["body"], created])
issue_comments = git.getIssueComments("Brewmaster", str(issue["number"]))
issue_events = git.getIssueEvents("Brewmaster", str(issue["number"]))
for event in issue_events:
time = event["created_at"]
time = datetime.strptime(time,"%Y-%m-%dT%H:%M:%SZ")
text = ""
if event["event"] == "referenced":
commit = event["commit_id"]
text += "referenced in "+commit[:6]+"... on "+str(time.date())+"\n"
commit = git.getCommit("Brewmaster", commit)
text += commit["commit"]["message"]+" - "+commit["commit"]["committer"]["name"]
issue_comments_list.append([text,time])
elif event["event"] == "assigned":
assignee = event["assignee"]["login"]
name = None
for item in config["git_to_trello"]:
if assignee in item.keys():
name = item[assignee]
text = ""
text += "Assign to "+name+" on "+str(time.date())
issue_comments_list.append([text,time])
elif event["event"] == "unassigned":
assignee = event["assignee"]["login"]
name = None
for item in config["git_to_trello"]:
if assignee in item.keys():
name = item[assignee]
text = ""
text += "Unassign to "+name+" on "+str(time.date())
issue_comments_list.append([text,time])
for comment in issue_comments:
text = comment["body"]
time = comment["updated_at"]
time = datetime.strptime(time,"%Y-%m-%dT%H:%M:%SZ")
issue_comments_list.append([text,date])
issue_comments_list.sort(key = lambda row: row[1])
card_comments_list = []
card_comments = trello.getCardComments(card["id"])
for comment in card_comments:
text = comment["data"]["text"]
time = comment["date"]
time = datetime.strptime(time,"%Y-%m-%dT%H:%M:%S.%fZ")
card_comments_list.append([text,time])
card_comments_list.sort(key = lambda row: row[1])
for item in issue_comments_list:
if len(card_comments_list) > 0:
new_list = []
for comment in card_comments_list:
new_list.append(comment[0])
if not item[0] in new_list:
trello.comment(card["id"], item[0])
else:
trello.comment(card["id"], item[0])
for item in card_comments_list:
if len(issue_comments_list) > 0:
new_list = []
for comment in issue_comments_list:
new_list.append(comment[0])
if not item[0] in new_list:
response = git.comment("Brewmaster", str(issue["number"]), item[0])
else:
git.comment("Brewmaster", str(issue["number"]), item[0])
lists = trello.getLists(config["trello_board"])
labels = trello.getLabels(config["trello_board"])
if newer == "issue":
list_id = None
status = issue["state"]
if status == "open":
target_list = None
if len(issue_comments)+len(issue_events) > 0:
target_list = "To Do"
else:
target_list = "Doing"
for list in lists:
if list["name"] == target_list:
list_id = list["id"]
if status == "closed":
target_list = "closed"
for list in lists:
if list["name"] == target_list:
list_id = list["id"]
trello.moveCard(card["id"], list_id)
issue_labels = issue["labels"]
issue_names = []
for label in issue_labels:
issue_names.append(label["name"])
for label in labels:
if label["name"].lower() in issue_names:
trello.addLabel(card["id"], label["id"])
if label["name"] == GIT:
trello.addLabel(card["id"], label["id"])
elif newer == "card":
new_state = None
status = card["idList"]
for list_id in lists:
if list_id["id"] == status:
status = list_id["name"]
if status == "Done":
new_state = "closed"
else:
new_state = "open"
label_names = []
for label in card["labels"]:
label_names.append(label["name"])
git.updateIssue("Brewmaster", str(issue["number"]), labels=label_names,
state=new_state)
def link():
config_yaml = open("config.yaml", "r")
config = yaml.load(config_yaml)
config_yaml.close()
trello_board = config["trello_board"]
git = Git(config["git_username"], config["git_email"], config["git_password"])
trello = Trello(config["trello_key"], config["trello_token"])
#first get all issues from the github repo
shared_topics = {}
issues = git.getIssues("Brewmaster")
issue_titles = []
for issue in issues:
issue_titles.append(issue["title"])
#get all cards from trello board
card_names = []
cards = trello.getCards(trello_board)
for card in cards:
card_names.append(card["name"])
for label in card["labels"]:
if GIT == label["name"]:
if not card["name"] in issue_titles:
label_list = []
for label in card["labels"]:
if not label["name"] == GIT:
label_list.append(label["name"])
title = card["name"]
body = card["desc"]
response = git.raiseIssue("Brewmaster", title, body,
labels=label_list)
comments = trello.getCardComments(card["id"])
for comment in comments:
body = comment["data"]["text"]
git.comment("Brewmaster", response["number"],
body)
else:
shared_topics[str(card["name"])] = [card]
lists = trello.getLists(config["trello_board"])
labels = trello.getLabels(config["trello_board"])
for issue in issues:
if not issue["title"] in card_names:
#add issue as a trello card
#label it
name = issue["title"]
desc = issue["body"]
label_list = []
for label in issue["labels"]:
label_list.append(label["name"])
label_ids = []
for label in labels:
if label["name"].lower() in label_list:
label_ids.append(label["id"])
if label["name"] == GIT:
label_ids.append(label["id"])
list_name = None
if issue["state"] == "open":
if issue["comments"] > 0:
list_name = "Doing"
else:
list_name = "To Do"
else:
list_name = "Done"
list_id = None
for list in lists:
if list["name"] == list_name:
list_id = list["id"]
response = trello.addCard(name, desc, list_id, label_ids)
#add all comments as trello card comments
comments = git.getIssueComments("Brewmaster", str(issue["number"]))
for comment in comments:
body = comment["body"]
trello.comment(response["id"], body)
else:
shared_topics[issue["title"]].append(issue)
""" next check if each issue/card in the dictionary are synced
if not, then adjust the older version to match the most recently
updated version
Check update times:
card["dateLastActivity"], issue["updated_at"]
Formatted:
2015-09-27T00:12:32.556Z 2015-09-26T21:52:01Z """
for key in shared_topics.keys():
#both on Zulu time
issue = shared_topics[key][1]
card = shared_topics[key][0]
issue_time = issue["updated_at"]
card_time = card["dateLastActivity"]
issue_time = datetime.strptime(issue_time,"%Y-%m-%dT%H:%M:%SZ")
card_time = datetime.strptime(card_time,"%Y-%m-%dT%H:%M:%S.%fZ")
if issue_time > card_time:
sync(git, trello, issue, card, "issue", config)
elif card_time > issue_time:
sync(git, trello, issue, card, "card", config)
def main(argv):
if len(argv) > 1:
#initialize some variables
pass
scheduler = BlockingScheduler()
scheduler.add_job(link, "interval", hours=1, id="link_job")
scheduler.start()
if __name__ == "__main__":
main(sys.argv[1:])