[d5753a]: / ForgeImporters / forgeimporters / github / tracker.py  Maximize  Restore  History

Download this file

293 lines (262 with data), 11.5 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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import re
import logging
from datetime import datetime
from urllib.error import HTTPError
from io import BytesIO
from tg import (
expose,
flash,
redirect
)
from tg.decorators import (
with_trailing_slash,
without_trailing_slash
)
from allura import model as M
from allura.lib import helpers as h
from allura.lib import validators as v
from allura.lib.plugin import ImportIdConverter
from allura.lib.decorators import require_post
from ming.odm import session, ThreadLocalODMSession
from tg import tmpl_context as c
from tg import app_globals as g
from forgetracker import model as TM
from forgeimporters.base import (
ToolImporter,
ToolImportForm,
ToolImportController,
)
from forgeimporters.github import (
GitHubProjectExtractor,
GitHubOAuthMixin,
GitHubProjectNameValidator,
)
from forgeimporters.github.utils import GitHubMarkdownConverter
log = logging.getLogger(__name__)
class GitHubTrackerImportForm(ToolImportForm):
gh_project_name = GitHubProjectNameValidator()
gh_user_name = v.UnicodeString(not_empty=True)
class GitHubTrackerImportController(ToolImportController, GitHubOAuthMixin):
import_form = GitHubTrackerImportForm
@with_trailing_slash
@expose('jinja:forgeimporters.github:templates/tracker/index.html')
def index(self, **kw):
self.oauth_begin()
return dict(importer=self.importer,
target_app=self.target_app)
@without_trailing_slash
@expose()
@require_post()
def create(self, gh_project_name, gh_user_name, mount_point, mount_label, **kw):
if self.importer.enforce_limit(c.project):
self.importer.post(
project_name=gh_project_name,
user_name=gh_user_name,
mount_point=mount_point,
mount_label=mount_label)
flash('Ticket import has begun. Your new tracker will be available '
'when the import is complete.')
else:
flash(
'There are too many imports pending at this time. Please wait and try again.', 'error')
redirect(c.project.url() + 'admin/')
class GitHubTrackerImporter(ToolImporter):
source = 'GitHub'
target_app_ep_names = 'tickets'
controller = GitHubTrackerImportController
tool_label = 'Issues'
max_ticket_num = 0
open_milestones = set()
def import_tool(self, project, user, project_name, mount_point=None,
mount_label=None, **kw):
import_id_converter = ImportIdConverter.get()
project_name = '{}/{}'.format(kw['user_name'], project_name)
extractor = GitHubProjectExtractor(project_name, user=user)
if not extractor.has_tracker():
return
app = project.install_app('tickets', mount_point, mount_label,
EnableVoting=False,
open_status_names='open',
closed_status_names='closed',
import_id={
'source': self.source,
'project_name': project_name,
}
)
self.github_markdown_converter = GitHubMarkdownConverter(
kw['user_name'], project_name)
ThreadLocalODMSession.flush_all()
try:
M.session.artifact_orm_session._get().skip_mod_date = True
with h.push_config(c, user=M.User.anonymous(), app=app):
for ticket_num, issue in extractor.iter_issues():
self.max_ticket_num = max(ticket_num, self.max_ticket_num)
ticket = TM.Ticket(
app_config_id=app.config._id,
custom_fields=dict(),
ticket_num=ticket_num,
import_id=import_id_converter.expand(ticket_num, app)
)
self.process_fields(extractor, ticket, issue)
self.process_comments(extractor, ticket, issue)
self.process_events(extractor, ticket, issue)
self.process_milestones(ticket, issue)
session(ticket).flush(ticket)
session(ticket).expunge(ticket)
app.globals.custom_fields = self.postprocess_milestones()
app.globals.last_ticket_num = self.max_ticket_num
ThreadLocalODMSession.flush_all()
M.AuditLog.log(
'import tool {} from {} on {}'.format(
app.config.options.mount_point,
project_name, self.source),
project=project, user=user, url=app.url)
g.post_event('project_updated')
app.globals.invalidate_bin_counts()
return app
finally:
M.session.artifact_orm_session._get().skip_mod_date = False
def parse_datetime(self, datetime_string):
return datetime.strptime(datetime_string, '%Y-%m-%dT%H:%M:%SZ')
def get_user_link(self, user):
return '[{0}](https://github.com/{0})'.format(user)
def process_fields(self, extractor, ticket, issue):
ticket.summary = issue['title']
ticket.status = issue['state']
ticket.created_date = self.parse_datetime(issue['created_at'])
ticket.mod_date = self.parse_datetime(issue['updated_at'])
if issue['assignee']:
owner_line = '*Originally owned by:* {}\n'.format(
self.get_user_link(issue['assignee']['login']))
else:
owner_line = ''
# body processing happens here
body, attachments = self._get_attachments(extractor, issue['body'])
ticket.add_multiple_attachments(attachments)
ticket.description = (
'*Originally created by:* {creator}\n'
'{owner}'
'\n'
'{body}').format(
creator=self.get_user_link(issue['user']['login']),
owner=owner_line,
body=self.github_markdown_converter.convert(body),
)
ticket.labels = [label['name'] for label in issue['labels']]
def process_comments(self, extractor, ticket, issue):
for comment in extractor.iter_comments(issue):
body, attachments = self._get_attachments(
extractor, comment['body'])
if comment['user']:
posted_by = '*Originally posted by:* {}\n\n'.format(
self.get_user_link(comment['user']['login']))
body = posted_by + body
p = ticket.discussion_thread.add_post(
text=self.github_markdown_converter.convert(body),
ignore_security=True,
timestamp=self.parse_datetime(comment['created_at']),
)
p.add_multiple_attachments(attachments)
def process_events(self, extractor, ticket, issue):
for event in extractor.iter_events(issue):
prefix = text = ''
actor = event['actor']
if event['event'] in ('reopened', 'closed'):
prefix = '*Ticket changed by:* {}\n\n'.format(
self.get_user_link(actor['login'] if actor else 'ghost'))
if event['event'] == 'reopened':
text = '- **status**: closed --> open'
elif event['event'] == 'closed':
text = '- **status**: open --> closed'
elif event['event'] == 'assigned':
text = '- **assigned_to**: {}'.format(
self.get_user_link(actor['login'] if actor else 'ghost'))
text = prefix + text
if not text:
continue
ticket.discussion_thread.add_post(
text=text,
ignore_security=True,
timestamp=self.parse_datetime(event['created_at'])
)
def process_milestones(self, ticket, issue):
if issue['milestone']:
title = issue['milestone']['title']
due = None
if issue['milestone']['due_on']:
due = self.parse_datetime(issue['milestone']['due_on'])
ticket.custom_fields = {
'_milestone': title,
}
self.open_milestones.add((title, due,))
def postprocess_milestones(self):
global_milestones = {
'milestones': [],
'type': 'milestone',
'name': '_milestone',
'label': 'Milestone'
}
for milestone in self.open_milestones:
global_milestones['milestones'].append({
'name': milestone[0],
'due_date': str(milestone[1].date()) if milestone[1] else None,
'complete': False,
})
return [global_milestones]
def _get_attachments(self, extractor, body):
# at github, attachments are images only and are included into comment's body
# usual syntax is
# ![cdbpzjc5ex4](https://f.cloud.github.com/assets/979771/1027411/a393ab5e-0e70-11e3-8a38-b93a3df904cf.jpg)\r\n
REGEXP = r'!\[[\w]+?\]\(((?:https?:\/\/)?[\da-z\.-]+\.[a-z\.]{2,6}'\
'[\\/%\\w\\.-]*.(jpg|jpeg|png|gif))\\)[\r\n]*'
attachments = []
try:
found_matches = re.finditer(REGEXP, body, re.IGNORECASE)
except TypeError:
found_matches = re.finditer(REGEXP, str(body), re.IGNORECASE)
for i, match in enumerate(found_matches):
# removing attach text from comment
body = body.replace(match.group(0), '')
# stripping url and extension
attachments.append(Attachment(
extractor,
match.group(1), # url
f'attach{i + 1}.{match.group(2)}' # extension
))
return (body, attachments)
class Attachment:
def __init__(self, extractor, url, filename):
self.url = url
self.filename = filename
self.type = None
file = self.get_file(extractor)
if file:
# don't set unless valid (add_multiple_attachments uses hasattr)
self.file = file
def get_file(self, extractor):
try:
fp_ish = extractor.urlopen(self.url)
fp = BytesIO(fp_ish.read())
return fp
except HTTPError as e:
if e.code == 404:
log.error('Unable to load attachment: %s', self.url)
return None
else:
raise