-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjohill
executable file
·287 lines (251 loc) · 9.86 KB
/
johill
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
#!/usr/bin/env python3
"""Mangle the JSON data from JIRA to something Phabricator's Phill may like.
Usage:
johill <jira.json> [--base-url=url]
Options:
--base-url=url The default base URL for projects and tasks
-h --help Show this screen.
--version Show version.
"""
from docopt import docopt
from datetime import datetime
from itertools import chain
import json
import sys
import re
class Converter:
def __init__(self):
self.bytype = {}
self.projects = []
self.tasks = []
self.base_url = 'https://jira.example.org/browse'
def set_base_url(self, url):
self.base_url = url
def url(self, i):
url = i.get('url') or (self.base_url.rstrip('/') + '/' + i['key'])
return url
def load(self, fd):
self.bytype = json.load(fd)
def convert(self):
self.projects_convert()
self.issues_convert()
r = {
'projects': self.projects,
'tasks': self.tasks
}
return r
def date_to_iso(self, datestr):
iso = datetime.strptime(datestr, '%Y-%m-%d %H:%M:%S.%f').isoformat()
return iso
def user_email(self, key):
return self.bytype['users'][key]['emailAddress']
def markup_parse_quoted_blocks(self, markup):
quoting = False
for line in markup.split('\n'):
if (line.strip() + " ").startswith('bq. '):
quoting = True
if not line.strip():
quoting = False
if quoting:
line = '> ' + line
yield line
def markup_parse_quoted_sections(self, markup):
quoting = False
for line in markup.split('\n'):
if line.strip() == '{quote}':
quoting = not quoting
yield ''
continue
if quoting:
line = '> ' + line
yield line
def markup_parse(self, markup):
if not markup:
return None
# turn [text|link] to [[link|text]]
markup = re.sub(r'\[(.*)\|(.*)\]', r'[[\2|\1]]', markup)
# turn {{code}} to ##code##
markup = re.sub(r'{{(.*?)}}', r'##\1##', markup)
markup = markup.replace('{code}', '```')
# turn {quote}-delimited sections and 'bq.' blocks to '>'-prefixed ones
markup = '\n'.join(self.markup_parse_quoted_blocks(markup))
markup = '\n'.join(self.markup_parse_quoted_sections(markup))
return markup
def projects_convert(self):
for p in self.bytype['Project'].values():
self.project_convert(p)
def project_convert(self, p):
ret = {
'id': p['key'],
'name': p['name'],
'description': self.markup_parse(p.get('description')),
'tracker': 'JIRA',
'url': self.url(p),
'creator': self.user_email(p['lead']),
'creationDate': datetime.now().isoformat(),
}
# take the creation date and author from the first issue
for i in self.bytype['Issue'].values():
if i['project'] != p['id']: continue
date = self.date_to_iso(i['created'])
if date < ret.get('creationDate', datetime.max.isoformat()):
ret['creationDate'] = date
ret['creator'] = self.user_email(i['creator'])
members = []
for actor in chain(*p['roles'].values()):
if actor['roletype'] != 'atlassian-user-role-actor': continue
members.append(self.user_email(actor['roletypeparameter']))
ret['members'] = list(set(members))
self.projects.append(ret)
def transaction_create(self, actor, date, kind, value=None, comment=None):
ret = {
'actor': actor,
'date': date,
'type': kind,
}
if value:
ret['value'] = value
if comment:
ret['comment'] = comment
return ret;
def issues_convert(self):
for i in self.bytype['Issue'].values():
self.issue_convert(i)
def transactions_parse_action(self, i, a):
transactions = []
if a['type'] == 'comment':
comment = self.markup_parse(a['body'])
txn = self.transaction_create(self.user_email(a['author']), a['created'], 'comment', comment=comment)
transactions.append(txn)
return transactions
def priority_parse(self, status):
m = {
"Blocker": 100,
"Critical": 80,
"Major": 50,
"Minor": 25,
"Trivial": 0,
}
return m.get(status)
def status_parse(self, status):
m = {
"Open": "open",
"Reopened": "open",
"In Progress": "wip",
"Fixed": "resolved",
"Done": "resolved",
"Incomplete": "rejected",
"Cannot Reproduce": "rejected",
"Won't Fix": "rejected",
"Duplicate": "duplicate",
}
return m.get(status)
def status_resolution_fold(self, status, resolution):
return None
def transactions_preprocess_change(self, c):
resolution = next((i for i in c['items'] if i['field'] == 'resolution'), None)
status = next((i for i in c['items'] if i['field'] == 'status'), None)
if resolution:
if not status:
# treat resolution changes as status changes
resolution['field'] = 'status'
elif 'newstring' in resolution:
# take the resolution value as the status value
status['newstring'] = resolution['newstring']
def transactions_parse_change(self, i, c):
self.transactions_preprocess_change(c)
transactions = []
for item in c['items']:
value = item.get('newstring')
kind = item['field'].lower()
if kind == 'assignee':
kind = 'owner'
value = self.user_email(item['newvalue'])
elif kind == 'summary':
kind = 'title'
elif kind == 'attachment':
if 'newvalue' not in item: continue # ignore attachments being dropped
if 'attachments' not in i or item['newvalue'] not in i['attachments']: continue # ignore missing attachments
a = i['attachments'][item['newvalue']]
value = {
'author': self.user_email(a['author']),
'data': a['data'],
'name': a['filename'],
'mimetype': a['mimetype'],
}
elif kind == 'status':
value = self.status_parse(value)
if not value: continue
elif kind == 'priority':
value = self.priority_parse(value)
elif kind == 'description':
value = self.markup_parse(value)
elif kind == 'epic child':
kind = 'depends'
value = {}
if 'newstring' in item:
value['+'] = [ item['newstring'] ]
if 'oldstring' in item:
value['-'] = [ item['oldstring'] ]
elif kind == 'link' and ('is blocked by' in item.get('newstring', '') or 'is blocked by' in item.get('oldstring', '')):
kind = 'depends'
value = {}
if 'newvalue' in item:
value['+'] = [ item['newvalue'] ]
if 'oldvalue' in item:
value['-'] = [ item['oldvalue'] ]
else:
continue
txn = self.transaction_create(self.user_email(c['author']), c['created'], kind, value)
transactions.append(txn)
return transactions
def issue_convert(self, i):
reporter = self.user_email(i['reporter'])
date = self.date_to_iso(i['created'])
project = self.bytype['Project'][i['project']]['key']
ret = {
'id': i['key'],
'url': self.url(i),
'title': i['summary'],
'creationDate': date,
'creator': reporter,
'description': self.markup_parse(i.get('description')),
'assignee': self.user_email(i['assignee']),
'transactions': [
self.transaction_create(reporter, date, 'projects', { '=': [project] })
]
}
# handle attachments added on issue creation for which no explict transaction exist
explict_attach = set(change['items'][0]['id'] for change in i.get('changes', []) if change['items'][0]['field'].lower() == 'attachment')
implicit_attachments = (a for a in i.get('attachments', {}).values() if a['id'] not in explict_attach)
for attachment in implicit_attachments:
change = {
'author': i['creator'],
'created': i['created'],
'items': [
{
'field': 'Attachment',
'fieldtype': 'jira',
'newstring': attachment['filename'],
'newvalue': attachment['id']
}
]
}
i.setdefault('changes', []).insert(0, change)
actions = [self.transactions_parse_action(i, a) for a in i.get('actions', [])]
changes = [self.transactions_parse_change(i, c) for c in i.get('changes', [])]
ret['transactions'] += sorted(chain(*(actions + changes)), key=lambda t: t['date'])
self.tasks.append(ret)
def main(arguments):
fd = sys.stdin
if arguments['<jira.json>'] != '-':
fd = open(arguments['<jira.json>'])
converter = Converter()
if '--base-url' in arguments:
converter.set_base_url(arguments['--base-url'])
converter.load(fd)
data = converter.convert()
print(json.dumps(data, sort_keys=True, indent=4))
if __name__ == '__main__':
arguments = docopt(__doc__, version='Johill 0.1')
main(arguments)