forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2github.py
713 lines (657 loc) · 23.1 KB
/
json2github.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
#!/usr/bin/env python3
"""
Alternative lp2gh JSON Importer for Mixxx issue migration
You can install this like this:
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install PyGithub
To use it, you first need to obtain a bugs and milestone json file from lp2gh:
https://github.com/Swiftb0y/lp2gh/blob/mixxx-import-fork/docs/moving_issues.rst#exporting-your-bugs
https://github.com/Swiftb0y/lp2gh/blob/mixxx-import-fork/docs/moving_issues.rst#exporting-your-milestones
Its important you are using my Fork with the `mixxx-import-fork` branch as I
made some slight modifications to add features.
You can use it like this:
$ python json2github.py --repo Holzhaus/mixxx-gh-issue-migration \
--token mytoken --output-file=mixxx_bugs.json \
mixxx_bugs.json mixxx_milestones.json
"""
import argparse
import binascii
import datetime
import json
import logging
import random
import re
import textwrap
import time
import github
import requests
LAUNCHPAD_STATUS_MAP = {
"Confirmed": ["confirmed"],
"Fix Committed": [],
"Fix Released": [],
"Incomplete": ["incomplete"],
"In Progress": [],
"Invalid": ["invalid"],
"New": [],
"Triaged": ["confirmed"],
"Won't Fix": ["wontfix"],
"Opinion": ["opinion"],
"Expired": ["expired"],
}
LAUNCHPAD_IMPORTANCE_MAP = {
"Critical": ["party stopper", "bug"],
"High": ["bug"],
"Low": ["bug"],
"Medium": ["bug"],
"Undecided": [],
"Wishlist": ["feature"],
}
LAUNCHPAD_USER_MAP = {
"aart": "amvanbaren",
"alex-jercaianu": "jercaianu",
"alex.barker": "kwhat",
# (not sure, email on launchpad matches
# with domain linked on github profile)
"another-rob": "borfo",
"arbeit-u": "dg3nec",
"arli0715": "dodler",
"badescunicu": "badescunicu",
"be.ing": "Be-ing",
"bencoder": "bencoder",
"bkgood": "bkgood",
"bruno-buccolo": "buccolo",
"buzz-dee": "BuZZ-dEE",
"cardinot": "cardinot",
"chloe-avrillon": "DJChloe",
"crislacerda": "crislacerda",
"daniel-64studio": "danielhjames",
"daschuer": "daschuer",
"default-kramer": "default-kramer",
"dj-kaza": "kazakore",
"eradkoff12": "radkoff",
"ewanuno": "ewanuno", # (not sure)
"federicobriata": "federicobriata",
"ferranpujol": "ferranpujolcamins",
"foss4": "foss-",
"frank-breitling": "fkbreitl",
"gamegod": "asantoni",
"gmsoft": "gmsoft-tuxicoman",
"goddisignz": "goddisignz",
"hacksdump": "hacksdump",
"hile": "hile",
"holthuis-jan": "Holzhaus",
"htown202": "htown101",
"iamcodemaker": "iamcodemaker",
"ironstorm-gmail": "deftdawg",
"jatwill": "idcmp",
"jean-claveau-g": "jclaveau",
"jessboerner": "doodlebro",
"jmigual": "jmigual",
"joerg-ubuntu": "JoergAtGithub",
"johan-lasperas": "johanLsp",
"josepma": "JosepMaJAZ",
"juha-pitkanen": "JuhaPit",
"jus": "esbrandt",
"kabelfrickler": "snue",
"kek001": "kek001",
"ketanlambat": "ketan-lambat",
"kevin-m-wern": "kevinwern",
"khyew": "khyew",
"kousu": "kousu", # not sure, but first name seems to match
"kshitij98": "kshitij98",
"launchpad-net-poelzi": "poelzi",
"lbot": "leematos",
"leigh123linux-j": "leigh123linux",
"lindybalboa": "LindyBalboa",
"marczis": "marczis",
"markusb": "markusb",
"max-linke": "kain88-de",
"mdizer": "mdizer",
"melgrubb": "MelGrubb",
"metastableb": "metastableB",
"mevsme": "mevsme",
"mhaulo": "mhaulo",
"michael-z-freeman": "Michael-Z-Freeman",
"mxmilkiib": "mxmilkiib",
"nachtigall": "jenszo",
"naught101": "naught101",
"nik-martin": "nikmartin",
"nimitbhardwaj": "nimitbhardwaj",
"ninomp": "ninomp",
"nm2107": "nm2107",
"nopeppermint": "nopeppermint",
"nschloe": "nschloe",
"pasanen-tuukka": "illuusio",
"pegasus-renegadetech": "Pegasus-RPG",
"pestrela": "pestrela",
"poelzi": "poelzi",
"pwhelan": "pwhelan",
"quentinfaidide": "QuentinFAIDIDE",
"raulbehl": "raulbehl",
"rawrr": "rawrr",
"ronso0": "ronso0",
"rryan": "rryan",
"sblaisot": "sblaisot",
"smashuu": "smashuu",
"stephane-guillou": "stragu",
"swiftb0y": "Swiftb0y",
"toszlanyi": "toszlanyi",
"troyane3": "troyane",
"uklotzde": "uklotzde",
"ulatekh": "ulatekh",
"vlada-dudr": "vlada-dudr",
"vrince": "vrince",
"wzssyqa": "wzssyqa",
"xerus2000": "xeruf",
"xor29a": "xorik",
"ywwg": "ywwg",
"zezic": "zezic",
}
# Github has restrictions on assignees
# Since this script was made to import to a private repo and transfer over
# afterwards, most assignees would be invalid and so we won't use them.
GITHUB_ALLOWED_ASSIGNEES = {
# "Holzhaus",
# "asantoni",
# "Be-ing",
# "daschuer",
# "esbrandt",
# "Pegasus-RPG",
# "ronso0",
# "rryan",
# "sblaisot",
# "Swiftb0y",
# "uklotzde",
# "ywwg",
}
LABELS = {
"aac": None,
"accessibility": None,
"analyzer": None,
"autodj": None,
"auxiliary": None,
"beatgrid": None,
"bpm": None,
"broadcast": None,
"browse": None,
"build": None,
"cloud": None,
"cmake": None,
"controllers": None,
"coverart": None,
"crash": None,
"cue": None,
"effects": None,
"engine": None,
"eq": None,
"follower": None,
"gui": None,
"hid": None,
"i18n": None,
"installer": None,
"itunes": None,
"jack": None,
"key": None,
"keyboard": None,
"library": None,
"looping": None,
"lyrics": None,
"m4a": None,
"manual": None,
"metadata": None,
"microphone": None,
"midi": None,
"mp3": None,
"overview": None,
"packaging": None,
"passthrough": None,
"performance": None,
"playlist": None,
"polish": None,
"portaudio": None,
"preferences": None,
"qt5": None,
"quantize": None,
"recording": None,
"rekordbox": None,
"sampler": None,
"scanner": None,
"serato": None,
"skin": None,
"slip": None,
"soundio": None,
"soundsource": None,
"standards": None,
"sync": None,
"tooltip": None,
"touchscreen": None,
"transport": None,
"usability": None,
"vinylcontrol": None,
"waveform": None,
# Importance (red)
"bug": "ff0000",
"security": "ff4400",
"regression": "ef233c",
"party stopper": "7b0d1e",
"feature": "bd2d87",
# status (yellow-ish)
"incomplete": "fff45c",
"invalid": "f9e900",
"confirmed": "faa916",
"wontfix": "ccbe00",
"duplicate": "8f8500",
"opinion": "9e9517",
"expired": "b5bf00",
# target system (blue-ish)
"windows": "1929b3",
"linux": "3b4be3",
"macos": "121d7d",
"raspberry": "5e6ce8",
# size (light green)
"easy": "68fa61",
"weekend": "8efb88",
"hackathon": "c7fdc4",
}
EXP_BACKUP_EXPONENT = 2
def format_text(text):
parts = text.split("\n\n")
for i, part in enumerate(parts):
preformatted = True
if "\n" in part.strip("\n") and all(
line.startswith(">") for line in part.strip("\n").splitlines()[1:]
):
preformatted = False
if (
"\n" not in part.strip()
and part.strip().lower().startswith("on ")
and part.strip().lower().endswith("wrote:")
):
preformatted = False
if not any(
c in part
for c in (
"<",
"\\",
"$",
";",
"|",
"{",
"}",
" ?? ",
"]:",
"(gdb) bt",
)
):
preformatted = False
if preformatted:
parts[i] = textwrap.indent(part, prefix=" ")
else:
# Prevent unintended GH issue links
parts[i] = re.sub(r"#(\d+)", r"#⁠\1", part)
return "\n\n".join(parts)
class LaunchpadImporter:
def __init__(self, token, repo, milestonedata, mention):
self.logger = logging.getLogger(__name__)
self.gh = github.Github(login_or_token=token)
self.repo = self.gh.get_repo(repo)
self.gh_labels = {
label.name: label for label in self.repo.get_labels()
}
self.gh_milestones = {
milestone.title: milestone
for milestone in self.repo.get_milestones(state="all")
}
self.lp_milestones = {x["name"]: x for x in milestonedata}
self.mention = mention
def get_user(self, username):
try:
username = LAUNCHPAD_USER_MAP[username]
if self.mention:
username = "@" + username
else:
# link to profile instead of mention
# to avoid notification/email spam
username = f"[{username}](https://github.com/{username})"
except KeyError:
username = f"[{username}](https://launchpad.net/~{username})"
return username
def get_label(self, name):
try:
label = self.gh_labels[name]
except KeyError:
label = self.import_label(name)
return label
def format_body(self, issuedata):
header = [
f"Reported by: **{self.get_user(issuedata['owner'])}**",
f"Date: {issuedata['date_created']}",
f"Status: {issuedata['status']}",
f"Importance: {issuedata['importance']}",
f"Launchpad Issue: [lp{issuedata['id']}]({issuedata['lp_url']})",
]
if issuedata["tags"]:
header.append("Tags: %s" % ", ".join(issuedata["tags"]))
if issuedata["attachments"]:
url_strings = [
f"[{data['title']}]({data['url']})"
for data in issuedata["attachments"]
]
header.append("Attachments: %s" % ", ".join(url_strings))
formatted_text = format_text(issuedata["description"])
if len(formatted_text.strip()) > 0:
formatted_text = "\n\n---\n\n" + formatted_text
return "\n".join(header) + formatted_text
def format_comment(self, commentdata):
header = [
f"Commented by: **{self.get_user(commentdata['owner'])}**",
f"Date: {commentdata['date_created']}",
]
if commentdata["attachments"]:
url_strings = [
f"[{data['title']}]({data['url']})"
for data in commentdata["attachments"]
]
header.append("Attachments: %s" % ", ".join(url_strings))
formatted_text = format_text(commentdata["content"])
if len(formatted_text.strip()) > 0:
formatted_text = "\n\n---\n\n" + formatted_text
return "\n".join(header) + formatted_text
def name_to_milestone(self, milestone_name):
try:
milestone = self.gh_milestones[milestone_name]
except KeyError:
milestonedata = self.lp_milestones.get(
milestone_name,
{
"active": True,
"date_targeted": None,
"name": milestone_name,
"summary": "",
},
)
milestone = self.import_milestone(milestonedata)
return milestone
def handle_ratelimit(self, func):
abuse_timeout = 30
def sleep_exp_backoff():
nonlocal abuse_timeout
time.sleep(abuse_timeout)
abuse_timeout *= EXP_BACKUP_EXPONENT
while True:
try:
return func()
except github.RateLimitExceededException:
rate_limit_resettime = datetime.datetime.utcfromtimestamp(
self.gh.rate_limiting_resettime
)
self.logger.warning(
"Rate limit exceeded (%d left/%d total), waiting until %s",
*self.gh.rate_limiting,
rate_limit_resettime.isoformat(),
)
time_to_wait = (
rate_limit_resettime - datetime.datetime.utcnow()
)
if time_to_wait.total_seconds() <= 0:
self.logger.warning(
"Failed to detect wait time, assuming 10 seconds..."
)
time_to_wait = datetime.timedelta(seconds=10)
self.logger.warning(f"Sleeping for {time_to_wait}")
time.sleep(time_to_wait.total_seconds())
except github.GithubException as e:
if e.status == 403 and "abuse" in e.data.get("message", ""):
self.logger.warning(
"Triggered abuse detection, sleeping %d seconds...",
abuse_timeout,
)
sleep_exp_backoff()
elif e.status == 403 and "secondary rate limit" in e.data.get(
"message", ""
):
abuse_timeout = e.headers.get("Retry-After", abuse_timeout)
self.logger.warning(
"Triggered secondary rate limit, "
"sleeping %d seconds...",
abuse_timeout,
)
sleep_exp_backoff()
elif e.status in range(500, 600):
self.logger.warning(
f"Internal server Error, sleeping {abuse_timeout} "
f"seconds and retrying"
)
self.logger.warning(e)
sleep_exp_backoff()
else:
raise
except requests.exceptions.ConnectionError as e:
self.logger.warning(e)
self.logger.warning(
"encountered connection error, retrying..."
)
sleep_exp_backoff()
else:
break
def import_milestone(self, milestonedata):
state = "open" if milestonedata["active"] else "closed"
due_on = github.GithubObject.NotSet
if milestonedata["date_targeted"]:
due_on = datetime.datetime.strptime(
milestonedata["date_targeted"], "%Y-%m-%dT%H:%M:%SZ"
)
description = github.GithubObject.NotSet
if milestonedata["summary"]:
description = milestonedata["summary"]
milestone = self.handle_ratelimit(
lambda: self.repo.create_milestone(
milestonedata["name"], state, description, due_on
)
)
self.logger.info("Created milestone: %r", milestone)
self.gh_milestones[milestone.title] = milestone
return milestone
def status_to_label(self, status):
for label_name in LAUNCHPAD_STATUS_MAP.get(status, []):
yield self.get_label(label_name)
def importance_to_label(self, importance):
for label_name in LAUNCHPAD_IMPORTANCE_MAP.get(importance, []):
yield self.get_label(label_name)
def import_label(self, label_name):
color = LABELS.get(label_name, None)
if not color:
color = "{c}{c}{c}".format(
c=binascii.hexlify(random.randbytes(1)).decode()
)
label = self.handle_ratelimit(
lambda: self.repo.create_label(label_name, color)
)
self.logger.info("Created label: %r", label)
self.gh_labels[label_name] = label
return label
def name_to_assignee(self, name):
username = LAUNCHPAD_USER_MAP.get(name)
if username and username in GITHUB_ALLOWED_ASSIGNEES:
return username
else:
return github.GithubObject.NotSet
def import_issue(self, issuedata):
milestone = github.GithubObject.NotSet
if issuedata["milestone"]:
milestone = self.name_to_milestone(issuedata["milestone"])
labels = []
labels.extend(self.status_to_label(issuedata["status"]))
labels.extend(self.importance_to_label(issuedata["importance"]))
if issuedata["duplicate_of"]:
labels.append(self.get_label("duplicate"))
if issuedata["security_related"]:
labels.append(self.get_label("security"))
if issuedata["tags"]:
labels.extend(
[
self.get_label(tag)
for tag in issuedata["tags"]
if tag in LABELS
]
)
assignee = self.name_to_assignee(issuedata["assignee"])
# todo upload attachments here
issue = self.handle_ratelimit(
lambda: self.repo.create_issue(
title=issuedata["title"],
body=self.format_body(issuedata),
milestone=milestone,
assignee=assignee,
labels=labels,
)
)
self.logger.info("Created issue: %r", issue)
return issue
def import_issuecomment(self, issue, commentdata):
comment = self.handle_ratelimit(
lambda: issue.create_comment(self.format_comment(commentdata))
)
self.logger.info("Created issue comment: %r", comment)
return comment
def run_import(self, lp_issues, lp_milestones):
num_issues = len(lp_issues.values())
for i, issuedata in enumerate(
sorted(
lp_issues.values(), key=lambda x: (x["date_created"], x["id"])
),
1,
):
gh_issue_number = issuedata.get("gh_issue_number")
if gh_issue_number:
issue_milestone = issuedata["milestone"]
if (
issuedata.get("gh_comments_imported")
== len(issuedata["comments"])
and issuedata.get("gh_status_comment_imported", False)
and issue_milestone is None
):
continue
self.logger.info(
f"checking if issue #{gh_issue_number} requires fixup"
)
issue = self.handle_ratelimit(
lambda: self.repo.get_issue(gh_issue_number)
)
# fixup milestone ownership in case it got lost
# (happens during bulk issue transfer between repos using
# githubs internal tools)
if issue_milestone is not None:
milestone = self.name_to_milestone(issue_milestone)
if issue.milestone != milestone:
self.logger.info(
f"fixing up milestone "
f'"{issue_milestone}" for '
f"issue #{gh_issue_number}"
)
# issue on launchpad is attached to milestone
# but corresponding pre-existing issue on gh does not
# have the milestone. Attach it here.
self.handle_ratelimit(
lambda: issue.edit(milestone=milestone)
)
else:
issue = self.import_issue(issuedata)
lp_issues[issuedata["id"]]["gh_issue_number"] = issue.number
comments_imported = issuedata.get("gh_comments_imported", 0)
lp_issues[issuedata["id"]][
"gh_comments_imported"
] = comments_imported
comments = issuedata["comments"][comments_imported:]
for comment in comments:
comment = self.import_issuecomment(issue, comment)
lp_issues[issuedata["id"]]["gh_comments_imported"] += 1
if issuedata.get("gh_status_comment_imported", False):
continue
if issuedata["status"] in (
"Fix Released",
"Fix Committed",
"Invalid",
"Won't Fix",
"Expired",
"Incomplete",
):
comment = (
f'Issue closed with status **{issuedata["status"]}**.'
)
self.handle_ratelimit(lambda: issue.create_comment(comment))
if issue.state != "closed":
self.handle_ratelimit(lambda: issue.edit(state="closed"))
lp_issues[issuedata["id"]]["gh_status_comment_imported"] = True
self.logger.info(
f"Imported {i}/{num_issues} "
f"({'{:.2f}'.format((i / num_issues) * 100)}%)"
)
for issuedata in sorted(
lp_issues.values(), key=lambda x: (x["date_created"], x["id"])
):
if issuedata.get("gh_duplicate_comment_imported", False):
continue
duplicate_of = issuedata.get("duplicate_of")
if duplicate_of is not None:
duplicate_issue_number = lp_issues[issuedata["id"]][
"gh_issue_number"
]
original = lp_issues.get(duplicate_of)
if original is None:
# Bug this is marked as a duplicate of does not
# belong to mixxxx
# so link to the original on launchpad
original = (
f"[lp:{duplicate_of}]"
f"(https://bugs.launchpad.net/bugs/{duplicate_of})"
)
else:
original = f"#{original['gh_issue_number']}"
self.logger.info(
f"Marking #{duplicate_issue_number} "
f"as duplicate of {original}"
)
issue = self.handle_ratelimit(
lambda: self.repo.get_issue(duplicate_issue_number)
)
comment = f"Duplicate of {original}"
self.handle_ratelimit(lambda: issue.create_comment(comment))
if issue.state != "closed":
self.handle_ratelimit(lambda: issue.edit(state="closed"))
lp_issues[issuedata["id"]]["gh_duplicate_comment_imported"] = True
self.logger.info(f"Successfully imported {num_issues} issues")
def main(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument("--repo", required=True)
parser.add_argument("--token", required=True)
parser.add_argument("--output-file")
parser.add_argument(
"--mention", default=False, action=argparse.BooleanOptionalAction
)
parser.add_argument("bugs_file", type=argparse.FileType("r"))
parser.add_argument("milestone_file", type=argparse.FileType("r"))
args = parser.parse_args(argv)
logging.basicConfig(
format="%(asctime)s %(levelname)-8s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.INFO,
)
lp_milestones = json.load(args.milestone_file)
lp_issues = {x["id"]: x for x in json.load(args.bugs_file)}
importer = LaunchpadImporter(
args.token, args.repo, lp_milestones, args.mention
)
import_start = datetime.datetime.utcnow()
try:
importer.run_import(lp_issues, lp_milestones)
finally:
logging.info(
f"Import took {datetime.datetime.utcnow() - import_start}"
)
if args.output_file:
with open(args.output_file, mode="w") as f:
json.dump(list(lp_issues.values()), f)
if __name__ == "__main__":
main()