Created
June 14, 2011 22:59
-
-
Save bavovna/1026140 to your computer and use it in GitHub Desktop.
GitHub repository issues import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib2 | |
import json | |
from StringIO import StringIO | |
import base64 | |
#==== configurations ======= | |
username = "[email protected]" | |
password = "naked_password" | |
src_repo = "octocat/Hello-World" | |
dst_repo = "helloworld-inc/Hello-World" | |
#==== end of configurations === | |
server = "api.github.com" | |
src_url = "https://%s/repos/%s" % (server, src_repo) | |
dst_url = "https://%s/repos/%s" % (server, dst_repo) | |
def get_milestones(url): | |
response = urllib2.urlopen("%s/milestones?state=open" % url) | |
result = response.read() | |
milestones = json.load(StringIO(result)) | |
return milestones | |
def get_labels(url): | |
response = urllib2.urlopen("%s/labels" % url) | |
result = response.read() | |
labels = json.load(StringIO(result)) | |
return labels | |
def get_issues(url): | |
response = urllib2.urlopen("%s/issues" % url) | |
result = response.read() | |
issues = json.load(StringIO(result)) | |
return issues | |
def get_comments_on_issue(issue): | |
if issue.has_key("comments") \ | |
and issue["comments"] is not None \ | |
and issue["comments"] != 0: | |
response = urllib2.urlopen("%s/comments" % issue["url"]) | |
result = response.read() | |
comments = json.load(StringIO(result)) | |
return comments | |
else : | |
return [] | |
def import_milestones(milestones): | |
for source in milestones: | |
dest = json.dumps({ | |
"title": source["title"], | |
"state": "open", | |
"description": source["description"], | |
"due_on": source["due_on"]}) | |
req = urllib2.Request("%s/milestones" % dst_url, dest) | |
req.add_header("Authorization", "Basic " + base64.urlsafe_b64encode("%s:%s" % (username, password))) | |
req.add_header("Content-Type", "application/json") | |
req.add_header("Accept", "application/json") | |
res = urllib2.urlopen(req) | |
data = res.read() | |
res_milestone = json.load(StringIO(data)) | |
print "Successfully created milestone %s" % res_milestone["title"] | |
def import_labels(labels): | |
for source in labels: | |
dest = json.dumps({ | |
"name": source["name"], | |
"color": source["color"] | |
}) | |
req = urllib2.Request("%s/labels" % dst_url, dest) | |
req.add_header("Authorization", "Basic " + base64.urlsafe_b64encode("%s:%s" % (username, password))) | |
req.add_header("Content-Type", "application/json") | |
req.add_header("Accept", "application/json") | |
res = urllib2.urlopen(req) | |
data = res.read() | |
res_label = json.load(StringIO(data)) | |
print "Successfully created label %s" % res_label["name"] | |
def import_issues(issues, dst_milestones, dst_labels): | |
for source in issues: | |
labels = [] | |
if source.has_key("labels"): | |
for src_label in source["labels"]: | |
name = src_label["name"] | |
for dst_label in dst_labels: | |
if dst_label["name"] == name: | |
labels.append(name) | |
break | |
milestone = None | |
if source.has_key("milestone") and source["milestone"] is not None: | |
title = source["milestone"]["title"] | |
for dst_milestone in dst_milestones: | |
if dst_milestone["title"] == title: | |
milestone = dst_milestone["number"] | |
break | |
assignee = None | |
if source.has_key("assignee") and source["assignee"] is not None: | |
assignee = source["assignee"]["login"] | |
body = None | |
if source.has_key("body") and source["body"] is not None: | |
body = source["body"] | |
dest = json.dumps({ | |
"title": source["title"], | |
"body": body, | |
"assignee": assignee, | |
"milestone": milestone, | |
"labels": labels | |
}) | |
comments = get_comments_on_issue(source) | |
#todo: insert logic on comments if needed | |
req = urllib2.Request("%s/issues" % dst_url, dest) | |
req.add_header("Authorization", "Basic " + base64.urlsafe_b64encode("%s:%s" % (username, password))) | |
req.add_header("Content-Type", "application/json") | |
req.add_header("Accept", "application/json") | |
res = urllib2.urlopen(req) | |
data = res.read() | |
res_issue = json.load(StringIO(data)) | |
print "Successfully created issue %s" % res_issue["title"] | |
def main(): | |
#get milestones and issues to import | |
milestones = get_milestones(src_url) | |
labels = get_labels(src_url) | |
#do import | |
import_milestones(milestones) | |
import_labels(labels) | |
#get imported milestones and labels | |
milestones = get_milestones(dst_url) | |
labels = get_labels(dst_url) | |
#process issues | |
issues = get_issues(src_url) | |
import_issues(issues, milestones, labels) | |
if __name__ == '__main__': | |
main() |
Yeah I do too, could it be that something in the Github API changed in the past 2 years and broke it?
Only works if the source repo doesn't require a password. See how line #18 doesn't use the password variable?
hey! sorry it took my years to respond. This script got moved to https://github.com/IQAndreas/github-issues-import and evolved
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, I think this is a great piece of code, but I cannot get it to work: I'm always getting a 404 Not Found