Skip to content

Instantly share code, notes, and snippets.

@jef-n
Created June 24, 2016 10:18
Show Gist options
  • Save jef-n/1f67b80f3df24abd49eeb63de0b43f51 to your computer and use it in GitHub Desktop.
Save jef-n/1f67b80f3df24abd49eeb63de0b43f51 to your computer and use it in GitHub Desktop.
from github import Github
from github.NamedUser import NamedUser
from github.GithubObject import NotSet
import sqlite3
import sys
import subprocess
REPONAME = "rm2gh"
PROJECT_ID = 17
print "Login"
github = Github("XXXX")
user = github.get_user()
print "User:", user.login
"""
try:
repo = user.get_repo(REPONAME)
print "Removing existing repo {} [{}]".format( repo.name, repo.url )
repo.delete()
except:
print "No repository {}".format( REPONAME)
# create repo
print "Creating repo",
repo = user.create_repo("rm2gh")
"""
repo = user.get_repo(REPONAME)
conn = sqlite3.connect('redmine.db')
"""
CREATE TABLE "issue_categories" (
"id" int(11) NOT NULL ,
"project_id" int(11) NOT NULL DEFAULT '0',
"name" varchar(30) NOT NULL DEFAULT '',
"assigned_to_id" int(11) DEFAULT NULL,
PRIMARY KEY ("id")
);
"""
categorylabels = {}
def category(id):
if id in categorylabels:
return categorylabels[id]
cc = conn.cursor()
cc.execute( "SELECT name FROM issue_categories WHERE project_id=%d AND id=%d" % (PROJECT_ID, id) )
r = cc.fetchone()
if not r:
raise BaseException( "category {} not found".format(id) )
try:
categorylabels[id] = repo.get_label( "C:" + r[0] )
except:
categorylabels[id] = repo.create_label( "C:" + r[0], "ff0000" )
return categorylabels[id]
"""
CREATE TABLE "issue_statuses" (
"id" int(11) NOT NULL ,
"name" varchar(30) NOT NULL DEFAULT '',
"is_closed" tinyint(1) NOT NULL DEFAULT '0',
"is_default" tinyint(1) NOT NULL DEFAULT '0',
"position" int(11) DEFAULT '1',
"default_done_ratio" int(11) DEFAULT NULL,
PRIMARY KEY ("id")
);
"""
statuslabels = {}
def status(id):
if id in statuslabels:
return statuslabels[id]
cc = conn.cursor()
cc.execute( "SELECT name FROM issue_statuses WHERE id=%d" % id )
r = cc.fetchone()
if not r:
raise BaseException( "status {} not found".format(id) )
try:
statuslabels[id] = repo.get_label( "S:" + r[0] )
except:
statuslabels[id] = repo.create_label( "S:" + r[0], "ff0000" )
return statuslabels[id]
"""
CREATE TABLE "enumerations" (
"id" int(11) NOT NULL ,
"name" varchar(30) NOT NULL DEFAULT '',
"position" int(11) DEFAULT '1',
"is_default" tinyint(1) NOT NULL DEFAULT '0',
"type" varchar(255) DEFAULT NULL, # IssuePriority
"active" tinyint(1) NOT NULL DEFAULT '1',
"project_id" int(11) DEFAULT NULL,
"parent_id" int(11) DEFAULT NULL,
PRIMARY KEY ("id")
);
"""
prioritylabels = {}
def priority(id):
if id in prioritylabels:
return prioritylabels[id]
cc = conn.cursor()
cc.execute( "SELECT name FROM enumerations WHERE id=%d AND type='IssuePriority'" % id )
r = cc.fetchone()
if not r:
raise BaseException("priority {} not found.".format(id))
try:
prioritylabels[id] = repo.get_label( "P:" + r[0] )
except:
prioritylabels[id] = repo.create_label( "P:" + r[0], "ff0000" )
return prioritylabels[id]
"""
CREATE TABLE "versions" (
"id" int(11) NOT NULL ,
"project_id" int(11) NOT NULL DEFAULT '0',
"name" varchar(255) NOT NULL DEFAULT '',
"description" varchar(255) DEFAULT '',
"effective_date" date DEFAULT NULL,
"created_on" datetime DEFAULT NULL,
"updated_on" datetime DEFAULT NULL,
"wiki_page_title" varchar(255) DEFAULT NULL,
"status" varchar(255) DEFAULT 'open',
"sharing" varchar(255) NOT NULL DEFAULT 'none',
PRIMARY KEY ("id")
);
"""
fixedinlabels = {}
def fixedin(id):
if id in fixedinlabels:
return fixedinlabels[id]
cc = conn.cursor()
cc.execute( "SELECT name FROM versions WHERE project_id=%d AND id=%d" % (PROJECT_ID, id) )
r = cc.fetchone()
if not r:
raise BaseException("version {} not found.".format(id))
fixedinlabels[id] = repo.create_label( "V:" + r[0], "00ff00" )
return fixedinlabels[id]
"""
CREATE TABLE "users" (
"id" int(11) NOT NULL ,
"login" varchar(30) NOT NULL DEFAULT '',
"hashed_password" varchar(40) NOT NULL DEFAULT '',
"firstname" varchar(30) NOT NULL DEFAULT '',
"lastname" varchar(30) NOT NULL DEFAULT '',
"mail" varchar(60) NOT NULL DEFAULT '',
"mail_notification" tinyint(1) NOT NULL DEFAULT '1',
"admin" tinyint(1) NOT NULL DEFAULT '0',
"status" int(11) NOT NULL DEFAULT '1',
"last_login_on" datetime DEFAULT NULL,
"language" varchar(5) DEFAULT '',
"auth_source_id" int(11) DEFAULT NULL,
"created_on" datetime DEFAULT NULL,
"updated_on" datetime DEFAULT NULL,
"type" varchar(255) DEFAULT NULL,
"identity_url" varchar(255) DEFAULT NULL,
PRIMARY KEY ("id")
);
"""
users = {}
def user(id):
cc = conn.cursor()
cc.execute( "SELECT mail FROM users WHERE id=%d" % id )
r = cc.fetchone()
if not r:
raise BaseException( "user {} not found".format(id) )
for u in github.search_users("{} in:email".format(r[0]) ):
if u.email == r[0]:
return u
print "User {}:{} not found".format(id, r[0])
return r[0]
def convert(text):
# print "---"
# print "IN:", text
proc = subprocess.Popen('pandoc -f textile -t markdown_github',
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
)
text, _ = proc.communicate(text.encode('utf-8'))
# print "OUT:", text
# print
return text
"""
CREATE TABLE "issues" (
"id" int(11) NOT NULL ,
"tracker_id" int(11) NOT NULL DEFAULT '0',
"project_id" int(11) NOT NULL DEFAULT '0',
"subject" varchar(255) NOT NULL DEFAULT '',
"description" text,
"due_date" date DEFAULT NULL,
"category_id" int(11) DEFAULT NULL,
"status_id" int(11) NOT NULL DEFAULT '0',
"assigned_to_id" int(11) DEFAULT NULL,
"priority_id" int(11) NOT NULL DEFAULT '0',
"fixed_version_id" int(11) DEFAULT NULL,
"author_id" int(11) NOT NULL DEFAULT '0',
"lock_version" int(11) NOT NULL DEFAULT '0',
"created_on" datetime DEFAULT NULL,
"updated_on" datetime DEFAULT NULL,
"start_date" date DEFAULT NULL,
"done_ratio" int(11) NOT NULL DEFAULT '0',
"estimated_hours" float DEFAULT NULL,
"parent_id" int(11) DEFAULT NULL,
"root_id" int(11) DEFAULT NULL,
"lft" int(11) DEFAULT NULL,
"rgt" int(11) DEFAULT NULL,
PRIMARY KEY ("id")
);
"""
skipped=[]
gaps=[]
c = conn.cursor()
for r in c.execute("SELECT id,tracker_id,subject,description,category_id,status_id,assigned_to_id,priority_id,fixed_version_id,author_id,created_on,updated_on,parent_id,root_id,lft,rgt FROM issues WHERE project_id=%d ORDER BY id" % PROJECT_ID):
id,tracker_id,subject,description,category_id,status_id,assigned_to_id,priority_id,fixed_version_id,author_id,created_on,updated_on,parent_id,root_id,lft,rgt = r
print "ISSUE #{}".format(id)
try:
issue = repo.get_issue(id)
print "Issue %d already exists" % id
skipped.append(id)
continue
except:
pass
labels = []
if category_id:
labels.append( category(category_id) )
if status_id and status_id<>5:
labels.append( status(status_id) )
if priority_id:
labels.append( priority(priority_id) )
if fixed_version_id:
labels.append( fixedin(fixed_version_id) )
author=user(author_id)
if isinstance(author,NamedUser):
authorname=author.login
else:
authorname=author
description = u"""
Redmine: Author: {}, created on {}, last update on {}
{}
""".format( authorname, created_on, updated_on, description )
while True:
issue = repo.create_issue(subject, body=convert(description), labels=labels)
if issue.number==id:
break
print "next issue {}: change to {} to placeholder".format(id,issue.number)
issue.edit("placeholder", body="", state="closed")
gaps.append(issue.number)
if assigned_to_id:
assignee=user(assigned_to_id)
if isinstance(assignee,NamedUser):
issue.edit(assignee=assignee)
else:
print "Assignee {} not found.".format(assignee)
if status_id==5: # Closed
issue.edit(state="closed")
"""
CREATE TABLE "journals" (
"id" int(11) NOT NULL ,
"journalized_id" int(11) NOT NULL DEFAULT '0',
"journalized_type" varchar(30) NOT NULL DEFAULT '',
"user_id" int(11) NOT NULL DEFAULT '0',
"notes" text,
"created_on" datetime NOT NULL,
PRIMARY KEY ("id")
);
"""
cj = conn.cursor()
for rj in cj.execute( "SELECT id,user_id,notes,created_on FROM journals WHERE journalized_id=%d" % id):
jid,user_id,notes,created_on = rj
commenter=user(user_id)
"""
CREATE TABLE "journal_details" (
"id" int(11) NOT NULL ,
"journal_id" int(11) NOT NULL DEFAULT '0',
"property" varchar(30) NOT NULL DEFAULT '',
"prop_key" varchar(30) NOT NULL DEFAULT '',
"old_value" varchar(255) DEFAULT NULL,
"value" varchar(255) DEFAULT NULL,
PRIMARY KEY ("id")
);
"""
cd = conn.cursor()
for d in cd.execute( "SELECT
notes = convert(notes)
if id==20:
print "END"
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment