Skip to content

Instantly share code, notes, and snippets.

@Pretz
Forked from dittos/trac-tickets-to-gh.py
Created September 21, 2010 03:03
Show Gist options
  • Save Pretz/589126 to your computer and use it in GitHub Desktop.
Save Pretz/589126 to your computer and use it in GitHub Desktop.
import trac tickets to github from a mysql trac install
import sys; reload(sys); sys.setdefaultencoding('utf-8')
import MySQLdb
import urllib2
import urllib
import simplejson
login = 'your github login'
token = 'your github API key'
repo = 'username/repo'
dbhost = 'your mysql hostname'
dbname = 'your mysql database name'
dbuser = 'your mysql login'
dbpass = 'your mysql password'
def urlopen(url, data):
try:
return urllib2.urlopen(url, urllib.urlencode(data))
except urllib2.HTTPError, e:
return e
conn = MySQLdb.connect(host=dbhost, user=dbuser, passwd=dbpass, db=dbname, use_unicode=True, charset="utf8")
c = conn.cursor()
c.execute('SELECT id, summary, status, description FROM ticket')
for row in c:
id, summary, status, description = row
print 'Ticket #%d' % id
ticket = simplejson.load(urlopen('https://github.com/api/v2/json/issues/open/%s' % repo, {'login': login, 'token': token, 'title': summary, 'body': description}))
c2 = conn.cursor()
c2.execute('SELECT author, newvalue AS body FROM ticket_change WHERE field="comment" AND ticket=%d' % id)
for comment in c2:
print '- comment'
author, body = comment
urlopen('https://github.com/api/v2/json/issues/comment/%s/%d' % (repo, ticket['issue']['number']), {'login': login, 'token': token, 'comment': body + '\n\n--' + author})
if status == 'closed':
print '- close'
urlopen('https://github.com/api/v2/json/issues/close/%s/%d' % (repo, ticket['issue']['number']), {'login': login, 'token': token})
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment