forked from thouis/numpy-trac-migration
/
issue.py
269 lines (237 loc) · 10.1 KB
/
issue.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
import util
from time import strftime, gmtime
import re
import ghissues
def _t(secs):
return strftime("%Y-%m-%d", gmtime(secs))
def indent(str, level):
if not isinstance(str, basestring):
return str
if '\n' not in str:
return str
prefix = "\n" + ('\t' * level)
return prefix + (prefix).join(str.split('\n'))
class base(object):
pass
class issue(object):
"A trac-to-github issue"
def __init__(self, **kwargs):
# minimal requirements
assert "id" in kwargs
assert "owner" in kwargs
assert "summary" in kwargs
assert "description" in kwargs
self.trac = base()
self.trac.__dict__.update(kwargs)
self.github = base()
self.existing_issues = None
def __unicode__(self):
return "Ticket\n\tTrac\n" + \
"\n".join("\t\t%s: %s" % (k, indent(v, 3)) for k, v in \
self.trac.__dict__.items()) + "\n" + \
"\tGithub\n" + \
"\n".join("\t\t%s: %s" % (k, indent(v, 3)) for k, v in \
self.github.__dict__.items())
def __str__(self):
return unicode(self).encode('utf-8')
def githubify(self):
self.gh_lookup() # sets id
self.gh_set_title()
self.gh_set_body()
self.gh_set_comments()
self.gh_set_user()
self.gh_set_assignee()
self.gh_set_state()
self.gh_set_labels()
self.gh_set_milestone()
self.gh_set_closed_atby()
def gh_lookup(self):
pass
def gh_set_title(self):
self.github.title = "%s (Trac #%d)" % (self.trac.summary, self.trac.id)
def gh_set_body(self):
self.github.body = self.body_header() + t2g_markup(self.trac.description)
def body_header(self):
return "\n".join(["_Original ticket %s " % self.trac_url() +
"on %s by %s, assigned to %s._" % \
(_t(self.trac.time),
util.mention_trac_user(self.trac.reporter),
util.mention_trac_user(self.trac.owner)),
"",
""])
def trac_url(self):
return "http://projects.scipy.org/numpy/ticket/" + str(self.trac.id)
def gh_set_comments(self):
self.github.comments = [t2g_markup(c) for c in self.get_trac_comments()]
def get_trac_comments(self):
sorted_events = sorted(c for c in self.trac._changes_and_attachments)
for event in sorted_events:
if len(event) == 5:
time, author, field, oldvalue, newvalue = event
if field == 'comment' and newvalue not in ['', '<change title>']:
header = "_%s wrote on %s_" % (util.mention_trac_user(author), _t(time))
body = t2g_markup(newvalue)
yield "\n".join([header, "", body])
elif field == 'milestone':
try:
new_milestone = ghissues.find_milestone(newvalue).title
except:
new_milestone = "Unscheduled"
yield "Milestone changed to `%s` by %s on %s" % (new_milestone, util.mention_trac_user(author), _t(time))
elif field == 'summary':
yield "Title changed from `%s` to `%s` by %s on %s" % (oldvalue, newvalue, util.mention_trac_user(author), _t(time))
elif len(event) == 4:
time, author, description, filename = event
url = "http://projects.scipy.org/numpy/attachment/ticket/%d/%s" % (self.trac.id, filename)
body = "Attachment added by %s on %s: [%s](%s)" % \
(util.mention_trac_user(author),
_t(time),
filename,
url)
yield body
# XXX - other changes
def gh_set_user(self):
pass # handled by self.body_header()
def gh_set_assignee(self):
self.github.assignee = util.trac_user_to_github(self.trac.owner)
def gh_set_state(self):
self.github.state = 'closed' if self.trac.status == 'closed' else 'open'
def gh_set_labels(self):
labels = ["priority: " + self.trac.priority,
self.trac.type.title(), # Upcase first letters
]
if self.trac.component != 'component1': # Trac default
labels.append('component: ' + self.trac.component)
self.github.labels = [ghissues.find_label(l) for l in labels]
def gh_set_milestone(self):
self.github.milestone = ghissues.find_milestone(self.trac.milestone)
def gh_set_closed_atby(self):
if self.github.state == 'closed':
self.github.comments += [c for c in self.closed_atby_comments()]
def closed_atby_comments(self):
sorted_events = sorted(c for c in self.trac._changes_and_attachments)
for event in sorted_events:
if len(event) == 5:
time, author, field, oldvalue, newvalue = event
if field == 'resolution':
yield "Resolved in Trac by %s, %s: %s" % \
(util.mention_trac_user(author),
_t(time),
newvalue)
def in_github(self, existing_issues={}):
if len(existing_issues) == 0:
repo = ghissues.gh_repo()
for i in repo.get_issues(state='open'):
existing_issues[i.title] = i.number
for i in repo.get_issues(state='closed'):
existing_issues[i.title] = i.number
print len(existing_issues), "existing issues"
return existing_issues.get(self.github.title, False)
def push(self):
repo = ghissues.gh_repo()
github_issue = repo.create_issue(title=self.github.title,
body=self.github.body,
milestone=self.github.milestone,
labels=self.github.labels)
try:
for comment in self.github.comments:
github_issue.create_comment(comment)
except:
print("!!! Error in comments for ticket %s" % self.trac.id)
finally:
if self.github.state == "closed":
github_issue.edit(state='closed')
pass
def repush_body(self):
repo = ghissues.gh_repo()
existing_id = self.in_github()
print "Replacing body in", existing_id
github_issue = repo.get_issue(self.in_github())
github_issue.edit(body=self.github.body)
def repush_comments(self):
repo = ghissues.gh_repo()
existing_id = self.in_github()
print "Replacing comments in", existing_id
github_issue = repo.get_issue(self.in_github())
for comment in github_issue.get_comments():
comment.delete()
try:
for comment in self.github.comments:
github_issue.create_comment(comment)
except:
print("!!! Error in comments for ticket %s" % self.trac.id)
finally:
if self.github.state == "closed":
github_issue.edit(state='closed')
pass
def check_crossrefs(self):
def fix_ref(old_ref):
num = int(old_ref.group(0)[1:])
if num > 10: # try to avoid common non-issues
new_num = trac_id_to_github_number(num)
if new_num:
return "#%d" % new_num
return old_ref.group(0)
def fix_line(l):
if l.startswith(' '):
return l # ignore code blocks
if l.startswith('Ticket #'):
return l # ignore unquoted numpy.test() output lines
new_line = re.sub('(?<!Python .\..\.. \()#[0-9]+(?!(( 0x00)|( PyEval)|(-Ubuntu)))', fix_ref, l)
if l != new_line:
print "OLD", l
print "NEW", new_line
print ""
return new_line
def fix_refs(block):
return "\n".join(fix_line(l)
for l in block.split('\n'))
old_body = self.github.body
old_comments = [c for c in self.github.comments]
self.github.body = fix_refs(self.github.body)
self.github.comments = [fix_refs(c) for c in self.github.comments]
if self.github.body != old_body:
self.repush_body()
if self.github.comments != old_comments:
self.repush_comments()
def t2g_inline_code(s):
# single line code is `code`
s = re.sub('{{{(.*?)}}}', '`\\1`', s)
# bold is __text__
s = re.sub("'''(.*?)'''", '__\\1__', s)
# italic is _text_
s = re.sub("''(.*?)''", '_\\1_', s)
# not all git hashes are recognized as such without markup
s = re.sub("commit:([a-f0-9]*)", '[\\1](http://github.com/numpy/numpy/commit/\\1)', s)
return s
def t2g_markup(s):
# First replace all inline code blocks with `code`
s = ''.join(t2g_inline_code(line) for line in s.split('\r'))
# Then replace any multi-line blocks with indentation.
# Note that Trac only does multiline blocks if {{{ and }}} are on lines by themselves
in_block = False
new_s = []
for line in s.split('\n'):
if not in_block and (line.strip() == '{{{'):
in_block = True
new_s.append("\n")
elif in_block and (line.strip() == '}}}'):
in_block = False
new_s.append("\n")
else:
new_s.append((' ' + line) if in_block else line)
return '\n'.join(new_s) # .replace('@', 'atmention:') # avoid mentioning, REMOVE before full run
def trac_id_to_github_number(tracid, issuemap={}):
if len(issuemap) == 0:
repo = ghissues.gh_repo()
for i in repo.get_issues(state='open'):
if 'Trac #' in i.title:
tracid = int(i.title.split('#')[-1][:-1])
issuemap[tracid] = i.number
for i in repo.get_issues(state='closed'):
if 'Trac #' in i.title:
tracid = int(i.title.split('#')[-1][:-1])
issuemap[tracid] = i.number
print len(issuemap), "existing issues"
return issuemap.get(tracid, False)