Mercurial > trac2bitbucket
changeset 5:fbf8cfcd575d default tip
Added some additional markdown-formatted information:
* Convert trac block comments and links to markdown
* Add reported-by user using @ syntax
| author | Max Harper <maxharp3r@gmail.com> |
|---|---|
| date | Mon, 04 Feb 2013 10:27:38 -0600 |
| parents | 38324ef9b854 |
| children | |
| files | trac2bb.py |
| diffstat | 1 files changed, 47 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/trac2bb.py Sun Oct 07 12:39:10 2012 +0000 +++ b/trac2bb.py Mon Feb 04 10:27:38 2013 -0600 @@ -1,8 +1,11 @@ +#!/usr/bin/env python + import csv import requests import json import sqlite3 import datetime +import re BASE_URL = 'https://api.bitbucket.org/1.0' @@ -13,11 +16,14 @@ 'enhancement': 'enhancement', 'discussion': 'proposal', 'note': 'proposal', + 'proposal': 'proposal', 'task': 'task', } STATUSES = { 'new': 'new', 'accepted': 'open', + 'assigned': 'open', + 'pending': 'open', 'reopened': 'open', 'closed': 'resolved', @@ -33,11 +39,15 @@ PRIORITIES = { None: None, '--': None, - 'High': 'major', - 'Normal': 'minor', - 'Low': 'trivial', + 'critical': 'critical', + 'high': 'major', + 'normal': 'minor', + 'low': 'trivial', + 'wish': 'trivial', } - +USERS = { + #'tracUser': 'bitbucketUser', +} def create_issue(**kwargs): url = '/'.join([BASE_URL, 'repositories', REPO, 'issues']) @@ -58,13 +68,34 @@ response = requests.post(url, auth=AUTH, data={'name': name}) +def trac_to_markdown(text): + # convert links + url_regex = re.compile(r"\[\[(.*?)\|(.*?)\]\]") + text = url_regex.sub(r"[\2](\1)", text) + + # convert code blocks and some trac-specific text + indent = 0 + out = [] + for line in text.split('\n'): + if line.startswith("#!CommitTicketReference"): + pass + elif line.startswith("{{{") and indent == 0: + indent += 4 + out.append("") + elif line.startswith("}}}") and indent > 0: + indent -= 4 + else: + out.append("%s%s" % (" " * indent, line)) + return "\n".join(out) + + def create_comment(issue_id, text): url = '/'.join([BASE_URL, 'repositories', REPO, 'issues', str(issue_id), 'comments']) - response = requests.post(url, auth=AUTH, data={'content': text}) + response = requests.post(url, auth=AUTH, data={'content': trac_to_markdown(text)}) print response.content -def import_issue(summary, status, type_, priority, milestone, component, version, resolution, description, cmv): +def import_issue(summary, status, type_, priority, milestone, component, version, resolution, description, owner, cmv): if component not in cmv['components']: create_cmv('components', component) cmv['components'].add(component) @@ -78,12 +109,12 @@ 'status': STATUSES[resolution] or STATUSES[status], 'priority': PRIORITIES[priority], 'title': summary, - 'content': description, + 'content': trac_to_markdown(description), 'kind': TYPES[type_], 'component': component, 'milestone': milestone, 'version': version, - 'responsible': None, + 'responsible': USERS.get(owner), } return create_issue(**issue) @@ -109,18 +140,23 @@ cmv[what] = set(get_cmv(what)) db = sqlite3.connect(filename) for (id_, summary, status, type_, priority, milestone, component, - version, resolution, description) in db.execute( + version, resolution, description, reporter, owner, time_) in db.execute( "SELECT id, summary, status, type, priority, milestone, " - "component, version, resolution, description FROM ticket"): + "component, version, resolution, description, reporter, owner, time FROM ticket"): + when = datetime.datetime.utcfromtimestamp(time_/1000000) + reporter = "@%s" % USERS[reporter] if USERS.get(reporter) else reporter + description = u'(originally reported in Trac by %s on %s)\n\n%s' % (reporter, when, description) + issue_id = import_issue(summary, status, type_, priority, milestone, component, version, resolution, - description, cmv) + description, owner, cmv) for comment, author, time_ in db.execute( "SELECT newvalue, author, time FROM ticket_change WHERE " "ticket=? AND field='comment' ORDER BY oldvalue", (id_,)): if not comment: continue when = datetime.datetime.utcfromtimestamp(time_/1000000) + author = "@%s" % USERS[author] if USERS.get(author) else author text = u'%s on %s said:\n\n%s' % (author, when, comment) create_comment(issue_id, text)
