forked from collective/collective.developermanual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-issues-import.py
executable file
·181 lines (139 loc) · 5.41 KB
/
gh-issues-import.py
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
#!/usr/bin/python
"""
Import Github issues from one repo to another.
The orignal work: https://github.com/mkorenkov/tools/blob/master/gh-issues-import/gh-issues-import.py
(assuming public domain license).
Used to migrate Github's Plone Conference 2012 temporary repository
to collective.developermanual issues.
NOTE: Comments are not imported! See TODO.
"""
import os
import urllib2
import json
from StringIO import StringIO
import base64
#==== configurations =======
username = "miohtama"
password = os.environ.get("GITHUB_PASSWORD", None)
if not password:
raise RuntimeError("Set environment variable GITHUB_PASSWORD")
src_repo = "miohtama/Documentation-f-ck-yeah"
dst_repo = "collective/collective.developermanual"
#==== 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 "comments" in issue \
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")
try:
res = urllib2.urlopen(req)
except urllib2.HTTPError:
# urllib2.HTTPError: HTTP Error 422: Unprocessable Entity
print "Could not process the issue %s" % dest
continue
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()