Uh oh!
There was an error while loading. Please reload this page.
forked from berestovskyy/bugzilla2github
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub2github.py
More file actions
Latest commit
executable file
·250 lines (207 loc) · 7.46 KB
/
Copy pathgithub2github.py
File metadata and controls
executable file
·250 lines (207 loc) · 7.46 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copy GitHub Issues between repos/owners
# by Andriy Berestovskyy <berestovskyy@gmail.com>
#
# To generate a GitHub access token:
# - on GitHub select "Settings"
# - select "Personal access tokens"
# - click "Generate new token"
# - type a token description, i.e. "bugzilla2issues"
# - select "public_repo" to access just public repositories
# - save the generated token into the migration script
#
importjson
importgetopt
importos
importpprint
importre
importrequests
importsys
importtime
ifsys.version[0] =='2':
reload(sys)
sys.setdefaultencoding('utf-8')
force_update=False
github_url="https://api.github.com"
src_github_owner=""
src_github_repo=""
src_github_token=""
dst_github_owner=""
dst_github_repo=""
dst_github_token=""
defusage():
print("Copy GitHub Issues between repos/owners")
print(("Usage: %s [-h] [-f]\n"
"\t[-O <src GitHub owner>] [-R <src repo>] [-T <src access token>]\n"
"\t[-o <dst GitHub owner>] [-r <dst repo>] [-t <dst access token>]\n"
%os.path.basename(__file__)))
print("Example:")
print(("\t%s -h"%os.path.basename(__file__)))
print(("\t%s -O src_login -R src_repo -T src_token \\\n"
"\t\t\t\t-o dst_login -r dst_repo -t dst_token"%os.path.basename(__file__)))
exit(1)
defsrc_github_get(url, avs={}):
globalgithub_url, src_github_owner, src_github_repo, src_github_token
ifurl[0] =="/":
u="%s%s"% (github_url, url)
elifurl.startswith("https://"):
u=url
elifurl.startswith("http://"):
u=url
else:
u="%s/repos/%s/%s/%s"% (github_url,
src_github_owner, src_github_repo, url)
# TODO: debug
# print "SRC GET: " + u
avs["access_token"] =src_github_token
returnrequests.get(u, params=avs)
defdst_github_get(url, avs={}):
globalgithub_url, dst_github_owner, dst_github_repo, dst_github_token
ifurl[0] =="/":
u="%s%s"% (github_url, url)
elifurl.startswith("https://"):
u=url
elifurl.startswith("http://"):
u=url
else:
u="%s/repos/%s/%s/%s"% (github_url,
dst_github_owner, dst_github_repo, url)
# TODO: debug
# print "DST GET: " + u
avs["access_token"] =dst_github_token
returnrequests.get(u, params=avs)
defdst_github_post(url, avs={}, fields=[]):
globalforce_update
globalgithub_url, dst_github_owner, dst_github_repo, dst_github_token
ifurl[0] =="/":
u="%s%s"% (github_url, url)
else:
u="%s/repos/%s/%s/%s"% (github_url,
dst_github_owner, dst_github_repo, url)
d= {}
# Copy fields into the data
forfieldinfields:
iffieldnotinavs:
print(("Error posting filed %s to %s"% (field, url)))
exit(1)
d[field] =avs[field]
# TODO: debug
# print "DST POST: " + u
# print "DST DATA: " + json.dumps(d)
ifforce_update:
returnrequests.post(u, params={"access_token": dst_github_token},
data=json.dumps(d))
else:
ifnotdst_github_post.warn:
print("Skipping POST... (use -f to force updates)")
dst_github_post.warn=True
returnTrue
dst_github_post.warn=False
defdst_github_issue_exist(number):
ifdst_github_get("issues/%d"%number):
returnTrue
else:
returnFalse
defdst_github_issue_update(issue):
id=issue["number"]
print(("\tupdating issue #%d on GitHub..."%id))
r=dst_github_post("issues/%d"%id, issue,
["title", "body", "state", "labels", "assignees"])
ifnotr:
print(("Error updating issue #%d on GitHub:\n%s"% (id, r.headers)))
exit(1)
defdst_github_issue_append(issue):
print("\tappending a new issue on GitHub...")
r=dst_github_post(
"issues", issue, ["title", "body", "labels", "assignees"])
ifnotr:
print(("Error appending an issue on GitHub:\n%s"%r.headers))
exit(1)
returnr
defgithub_issues_copy():
id=0
whileTrue:
id+=1
print(("Copying issue #%d..."%id))
issue=src_github_get("issues/%d"%id)
ifissue:
issue=issue.json()
ifdst_github_issue_exist(id):
ifforce_update:
dst_github_issue_update(issue)
else:
print(("\tupdating issue #%d..."%id))
else:
ifforce_update:
# Make sure the previous issue already exist
ifid>1andnotdst_github_issue_exist(id-1):
print(("Error adding issue #%d: previous issue does not exists"
%id))
exit(1)
req=dst_github_issue_append(issue)
new_issue=dst_github_get(req.headers["location"]).json()
ifnew_issue["number"] !=id:
print(("Error adding issue #%d: assigned unexpected issue id #%d"
% (id, new_issue["number"])))
exit(1)
# Update issue state
ifissue["state"] !="open":
dst_github_issue_update(issue)
else:
print(("\tadding new issue #%d..."%id))
else:
print("Done.")
break
defargs_parse(argv):
globalforce_update
globalsrc_github_owner, src_github_repo, src_github_token
globaldst_github_owner, dst_github_repo, dst_github_token
try:
opts, args=getopt.getopt(argv, "hfo:r:t:O:R:T:")
exceptgetopt.GetoptError:
usage()
foropt, arginopts:
ifopt=='-h':
usage()
elifopt=="-f":
print("WARNING: the repo will be UPDATED! No backups, no undos!")
print("Press Ctrl+C within next 5 secons to cancel the update:")
time.sleep(5)
force_update=True
elifopt=="-o":
dst_github_owner=arg
elifopt=="-r":
dst_github_repo=arg
elifopt=="-t":
dst_github_token=arg
ifnotsrc_github_token:
src_github_token=arg
elifopt=="-O":
src_github_owner=arg
elifopt=="-R":
src_github_repo=arg
elifopt=="-T":
src_github_token=arg
ifnotdst_github_token:
dst_github_token=arg
# Check the arguments
if (notsrc_github_ownerornotsrc_github_repoornotsrc_github_token
ornotdst_github_ownerornotdst_github_repoornotdst_github_token):
print("Error parsing arguments: "
"please specify source and destination GitHub owner, repo and token")
usage()
defmain(argv):
globalsrc_github_owner, src_github_repo
globaldst_github_owner, dst_github_repo
# Parse command line arguments
args_parse(argv)
print("===> Copying GitHub Issues between repos/owners...")
print(("\tsource GitHub owner: %s"%src_github_owner))
print(("\tsource GitHub repo: %s"%src_github_repo))
print(("\tdest. GitHub owner: %s"%dst_github_owner))
print(("\tdest. GitHub repo: %s"%dst_github_repo))
github_issues_copy()
if__name__=="__main__":
main(sys.argv[1:])