This repository was archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.ts
executable file
·1171 lines (1023 loc) · 33.2 KB
/
migrate.ts
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ts-node
import { default as colors } from "colors/safe";
import { default as childProcess } from "child_process";
import { default as fs } from "fs";
import { default as yaml } from "js-yaml";
import { default as commander } from "commander";
import axiosStatic, { AxiosResponse, AxiosInstance, Method } from "axios";
const axios: AxiosInstance = axiosStatic.create();
const program = new commander.Command();
program.version("0.1.0")
.option("-c, --config <path>",
"Configuration file path. Defaults to ./config.yml")
.option("-s, --source <repo>",
"Source repository, overrides the source in the configuration")
.option("-d, --destination <repo>",
"Destination repository, overrides the destination in the configuration")
.option("-r, --reset",
"Deletes and recreate the destination repository. Use with caution!")
.option("-o, --overwrite",
"Overwrites steps if a file already exists instead of resuming.")
.option("-f, --for-real",
"Perform the migration while using the actual user tokens " +
"and actually mentioning users. This wil cause GitHub to send" +
"a lot of emails.")
.parse(process.argv);
process.on("unhandledRejection", reason => {
console.log("Unhandled Rejection");
console.dir(reason, {depth: 4});
process.exit(1);
});
/**
* Execute in shell, while connecting stdin, stdout and stderr to that of
* the current process
*/
function sh(commandline: string, options?: object): Buffer {
console.log(commandline);
return childProcess.execSync(commandline, {stdio: "inherit", ...options});
}
function warn(message: string): void {
const date = new Date().toString().slice(0, 24);
console.log(colors.red(`[${date}] ${message}`));
}
function log(message: string): void {
const date = new Date().toString().slice(0, 24);
console.log(colors.blue(colors.bold(`[${date}] ${message}`)));
}
function progress(message: string): void {
const date = new Date().toString().slice(0, 24);
console.log(colors.blue(`[${date}] ${message}`));
}
function writeJson(path: string, item: object): void {
fs.writeFileSync(path, JSON.stringify(item, null, 2));
}
function readJson<T>(path: string): T {
return JSON.parse(fs.readFileSync(path).toString());
}
interface Repository {
remote: string;
owner: string;
name: string;
url: URL;
}
class Token {
private token: string;
public constructor(token: string) {
this.token = token;
}
public toString(): string {
return this.token;
}
}
class URL {
private url: string;
public constructor(url: string) {
this.url = url;
}
public toString(): string {
return this.url;
}
}
function url(url: string): URL {
return new URL(url);
}
function parseRepoUrl(cloneUrl: string): Repository {
const matchSsh = /^git@([^:]+):([^/]+)\/(.+)\.git$/.exec(cloneUrl);
if (matchSsh) {
return {remote: matchSsh[1], owner: matchSsh[2], name: matchSsh[3], url: url(cloneUrl)};
}
const matchHttp = /^https?:\/\/([^/]+)\/([^/]+)\/(.+)\.git$/.exec(cloneUrl);
if (matchHttp) {
return {remote: matchHttp[1], owner: matchHttp[2], name: matchHttp[3], url: url(cloneUrl)};
}
throw `Repository '${cloneUrl}' is not in ssh or https format.`;
}
interface Config {
reset: boolean;
overwrite: boolean;
for_real: boolean;
source: SourceConfig;
destination: DestinationConfig;
}
interface SourceConfig {
url: URL;
api: string;
repo: Repository;
token: Token;
}
interface DestinationConfig {
url: URL;
api: string;
repo: Repository;
default_token: Token;
admin_token: Token;
committer_name: string;
committer_email: string;
usernames: StringMap<string>;
tokens: StringMap<Token>;
}
interface StringMap<T> {
[key: string]: T;
}
const configFile = program.config || "./config.yml";
if (!fs.existsSync(configFile)) {
warn(`Config file ${configFile} does not exist. `
+ " You can copy the example configuration from config.example.yml");
process.exit(1);
}
const cfgObj = yaml.safeLoad(fs.readFileSync(configFile).toString());
/* eslint @typescript-eslint/camelcase: "off"*/
cfgObj.destination.admin_token = cfgObj.destination.admin_token || cfgObj.destination.default_token;
cfgObj.source.repository = program.source || cfgObj.source.repository;
cfgObj.destination.repository = program.destination || cfgObj.destination.repository;
const srcRepo = parseRepoUrl(cfgObj.source.repository);
const dstRepo = parseRepoUrl(cfgObj.destination.repository);
const tokens: StringMap<Token> = {};
for (const [key, token] of Object.entries(cfgObj.destination.tokens)) {
tokens[key] = new Token(token as string);
}
const config: Config = {
reset: program.reset,
overwrite: program.overwrite,
for_real: program.forReal || cfgObj.for_real,
source: {
url: url(`${cfgObj.source.api}/repos/${srcRepo.owner}/${srcRepo.name}`),
repo: srcRepo,
...cfgObj.source,
token: new Token(cfgObj.source.token),
},
destination: {
url: url(`${cfgObj.destination.api}/repos/${dstRepo.owner}/${dstRepo.name}`),
repo: dstRepo,
...cfgObj.destination,
default_token: new Token(cfgObj.destination.default_token),
admin_token: new Token(cfgObj.destination.admin_token),
tokens: tokens,
}
}
const repoDir = "data/" + config.source.repo.name;
fs.mkdirSync(repoDir, {recursive: true});
const cloneDir = repoDir + "/clone";
const mirrorDir = repoDir + "/mirror.git";
const issuePath = repoDir + "/issues.json";
const labelsPath = repoDir + "/labels.json";
const milestonesPath = repoDir + "/milestones.json";
const missingPath = repoDir + "/missing.json";
const commentPath = repoDir + "/comments.json";
const releasesPath = repoDir + "/releases.json";
const mentionsPath = repoDir + "/mentions.json";
const creatorsPath = repoDir + "/creators.json";
async function invoke(method: Method, url: URL, token?: Token, data?: object): Promise<AxiosResponse> {
try {
console.log(`${method.toUpperCase()} ${url}`);
return await axios.request({
method: method,
url: url.toString(),
data: data,
headers: {
"Authorization": "token " + (token || config.source.token).toString(),
"User-Agent": "node.js",
},
});
} catch (e) {
const response = e.response;
if (response.status === 403 &&
response.headers["x-ratelimit-remaining"] == 0) {
warn("Ratelimit encountered, retrying in 1 second");
sh("sleep 1");
return await invoke(method, url, token, data);
} if (response.status === 500) {
warn("Got internal server error, retrying in 1 second");
sh("sleep 1");
return await invoke(method, url, token, data);
} else {
throw e;
}
}
}
function get(url: URL, token?: Token): Promise<AxiosResponse> {
return invoke("get", url, token);
}
function destroy(url: URL, token?: Token): Promise<AxiosResponse> {
return invoke("delete", url, token);
}
function patch(url: URL, data: object, token?: Token): Promise<AxiosResponse> {
return invoke("patch", url, token, data);
}
function post(url: URL, data: object, token?: Token): Promise<AxiosResponse> {
return invoke("post", url, token, data);
}
function put(url: URL, data: object, token?: Token): Promise<AxiosResponse> {
return invoke("put", url, token, data);
}
/**
* Call and await the given callback with an increasing integer while
* collecting the result in an array. Continues until the callback returns
* null.
*/
async function collectUntilNull<T>(
callback: (n: number) => Promise<T | null>
): Promise<T[]> {
let i = 1;
const collector: T[] = [];
let result = await callback(i);
while (result) {
i += 1;
collector.push(result);
result = await callback(i);
}
return collector;
}
interface Item {
id: number;
url: URL;
created_at: string;
}
interface Authorable extends Item {
user: User;
html_url: string;
body: string;
}
interface Issue extends Authorable {
number: number;
title: string;
state: string;
events_url: string;
closed_by: User;
closed_at: string;
labels: Label[];
milestone?: Milestone;
}
function isIssue(item: Item): item is Issue {
return (item as Issue).labels !== undefined;
}
interface PullRequest extends Issue {
pull_request: object;
base: Commit;
head: Commit;
merged: boolean;
}
function isPR(item: Item): item is PullRequest {
return (item as PullRequest).pull_request !== undefined;
}
interface Comment extends Authorable {
}
interface User {
login: string;
}
interface Release extends Item {
name: string;
body: string;
tag_name: string;
draft: boolean;
prerelease: boolean;
author: User;
}
interface Label {
name: string;
color: string;
description: string;
}
interface Milestone {
number: number;
title: string;
state: string;
description?: string;
due_on?: string;
}
interface Commit {
ref?: string;
url: URL;
sha: string;
}
interface PRComment extends Comment {
pull_request_url: string;
path?: string;
original_commit_id: string;
original_position?: number;
}
function isPRComment(item: Item): item is PRComment {
const comment = item as PRComment;
return comment.pull_request_url !== undefined &&
comment.original_commit_id !== undefined;
}
interface CommitComment extends Comment {
commit_id: string;
path: string;
original_position: number;
submitted_at: string;
}
function isCommitComment(item: Item): item is CommitComment {
return (item as CommitComment).commit_id !== undefined &&
(item as Review).pull_request_url === undefined;
}
interface IssueComment extends Comment {
issue_url: string;
}
function isIssueComment(item: Item): item is IssueComment {
return (item as IssueComment).issue_url !== undefined;
}
interface Review extends Comment {
commit_id: string;
event: string;
pull_request_url: string;
submitted_at: string;
}
function isReview(item: Item): item is Review {
const review = item as Review;
return review.pull_request_url !== undefined && review.event !== undefined;
}
interface Event {
event: string;
}
async function collectPages<T>(type: string): Promise<T[]> {
return [].concat(...(await collectUntilNull(async page => {
const response = await get(url(`${config.source.url}/${type}?` +
`page=${page}&state=all&per_page=100`));
if (response.data.length === 0) {
return null;
}
return response.data;
})));
}
async function fetchPR(issue: Issue): Promise<PullRequest> {
const pr = (await get(url(`${config.source.url}/pulls/${issue.number}`))).data;
const events: Event[] = (await get(url(issue.events_url))).data;
return {
...issue,
...pr,
merged: events.some(e => e.event === "merged")
};
}
async function fetchIssue(issueNumber: number): Promise<Issue | null> {
progress(`Fetching issue ${issueNumber}`);
try {
const request = await get(url(`${config.source.url}/issues/${issueNumber}`));
const issue = request.data as Issue;
if (isPR(issue)) {
return await fetchPR(issue);
} else {
return issue;
}
} catch (e) {
if (e.response && e.response.status === 404) {
return null;
}
throw e;
}
}
async function fetchIssues(): Promise<Issue[]> {
log("Fetching issues");
const issues = (await collectUntilNull<Issue>(fetchIssue))
.sort((a, b) => a.number - b.number);
writeJson(issuePath, issues);
return issues;
}
async function fetchLabels(): Promise<Label[]> {
log("Fetching labels");
const labels = await collectPages<Label>("labels");
writeJson(labelsPath, labels);
return labels;
}
async function fetchMilestones(): Promise<Milestone[]> {
log("Fetching milestones");
const milestones = (await collectPages<Milestone>("milestones"))
.sort((a, b) => a.number - b.number);
writeJson(milestonesPath, milestones);
return milestones;
}
async function fetchReleases(): Promise<Release[]> {
log("Fetching releases");
const releases = await collectPages<Release>("releases");
writeJson(releasesPath, releases);
return releases;
}
interface ReviewObj {
submitted_at: string;
state: string;
body: string;
}
async function fetchReviews(issues: Issue[]): Promise<Review[]> {
const reviews: Review[] = [];
const reviewEvent: Map<string, string> = new Map(
[["APPROVED", "APPROVE"],
["COMMENTED", "COMMENT"],
["CHANGES_REQUESTED", "REQUEST_CHANGES"]]
);
for (const issue of issues.filter(issue => isPR(issue))) {
const reviewObjs = await collectPages<ReviewObj>(`pulls/${issue.number}/reviews`);
for (const reviewObj of reviewObjs) {
// GitHub API ლ(ಠ益ಠ)ლ
const event = reviewEvent.get(reviewObj.state);
if (event === null) { throw "Unknown event state: " + reviewObj.state }
const review = {
...reviewObj,
created_at: reviewObj.submitted_at,
event: event,
};
reviews.push((review as unknown) as Review);
}
}
return reviews;
}
async function fetchComments(issues: Issue[]): Promise<Comment[]> {
log("Fetching comments");
progress("Fetching pull comments");
const pullComments = await collectPages<PRComment>("pulls/comments");
progress("Fetching issue comments");
const issueComments = await collectPages<IssueComment>("issues/comments");
progress("Fetching commit comments");
const commitComments = await collectPages<CommitComment>("comments");
progress("Fetching review comments");
const reviewComments = await fetchReviews(issues);
const comments = ([] as Comment[])
.concat(pullComments, issueComments, commitComments, reviewComments)
.sort((a, b) =>
new Date(a.created_at).valueOf() - new Date(b.created_at).valueOf());
writeJson(commentPath, comments);
return comments;
}
function checkIfNeeded(path: string): boolean {
if (fs.existsSync(path)) {
if (config.overwrite) {
sh(`rm -rf ${path}`);
} else {
return false;
}
}
return true;
}
function moveRepository(): void {
log("Mirroring repository");
if (checkIfNeeded(mirrorDir)) {
progress("Creating mirror from source");
sh(`git clone --mirror ${config.source.repo.url} ${mirrorDir}`);
sh(`git -C ${mirrorDir} remote set-url origin ${config.destination.repo.url}`);
sh(`git -C ${mirrorDir} remote set-url --push origin ${config.destination.repo.url}`);
progress("Replacing packed-refs");
sh(`sed -i.bak 's_ refs/pull/_ refs/pr/_' ${mirrorDir}/packed-refs`);
}
progress("Pushing to destination");
sh(`git -C ${mirrorDir} push --mirror ${config.destination.repo.url}`);
if (checkIfNeeded(cloneDir)) {
progress("Creating clone from destination");
sh(`git clone ${config.destination.repo.url} ${cloneDir}`);
}
}
async function commitExists(sha: string): Promise<boolean> {
try {
await get(url(`${config.destination.url}/git/commits/${sha}`),
config.destination.default_token);
return true;
} catch (e) {
if (e.response.status == 404) {
return false;
} else {
throw e;
}
}
}
async function missingCommits(items: Item[]): Promise<Commit[]> {
const commits = items
.map((item): Commit | null => {
if (isPR(item)) {
return {url: url(item.html_url), sha: item.base.sha};
} else if (isPRComment(item)) {
return {url: url(item.html_url), sha: item.original_commit_id};
} else if (isCommitComment(item)) {
return {url: url(item.html_url), sha: item.commit_id};
} else if (isReview(item)) {
return {url: url(item.html_url), sha: item.commit_id};
} else {
return null;
}
})
.filter((item): item is Commit => item !== null);
const checked = new Set();
const missing: Commit[] = [];
for (const commit of commits) {
if (!checked.has(commit.sha)) {
checked.add(commit.sha);
const exists = await commitExists(commit.sha);
if (!exists) {
missing.push(commit);
}
}
}
writeJson(missingPath, missing);
return missing;
}
async function createBranch(branchname: string, sha: string): Promise<void> {
progress(`Creating branch ${branchname}`);
try {
await post(url(`${config.destination.url}/git/refs`),
{
"ref": `refs/heads/${branchname}`,
"sha": sha,
},
config.destination.default_token);
} catch (e) {
if (e.response.data.message === "Reference already exists") {
warn(`Branch #${branchname} already exists. Ignoring...`);
} else {
throw e;
}
}
}
function formatDate(date: string): string {
const items = new Date(date).toString().split(" ");
const day = items.slice(0, 4).join(" ");
const time = items[4].split(":").slice(0, 2).join(":");
return `${day} at ${time}`;
}
function authorToken(srcUser: User): Token {
const token = config.destination.tokens[srcUser.login];
if (!token) {
warn(`Missing token for ${srcUser.login}, using default token`);
}
if (config.for_real) {
return token || config.destination.default_token;
} else {
return config.destination.default_token;
}
}
function author(srcUser: User): string {
const user = config.destination.usernames[srcUser.login];
if (config.for_real) {
return user ? `@${user}` : srcUser.login;
} else {
return user ? `\`@${user}\`` : srcUser.login;
}
}
function suffix(item: Authorable): string {
let type;
if (isPR(item)) {
type = "pull request";
} else if (isIssue(item)) {
type = "issue";
} else if (isReview(item)) {
if (item.event === "APPROVE") {
type = "approval";
} else if (item.event === "REQUEST_CHANGES") {
type = "change request";
} else {
type = "review";
}
} else {
type = "comment";
}
let suffix = `_[Original ${type}](${item.html_url})`;
suffix += ` by ${author(item.user)}`;
suffix += ` on ${formatDate(item.created_at)}._`;
if (isIssue(item) && item.state === "closed") {
suffix += `\n_${isPR(item) ? "Merged" : "Closed"} `
suffix += `by ${author(item.closed_by)}`;
suffix += ` on ${formatDate(item.closed_at)}._`;
}
return suffix;
}
interface BranchNames {
head: string;
base: string;
}
function branchNames(pull: PullRequest): BranchNames {
return {
head: `migrated/pr-${pull.number}/${pull.head.ref}`,
base: `migrated/pr-${pull.number}/${pull.base.ref}`,
};
}
async function createBrokenPullRequest(pull: PullRequest): Promise<void> {
progress(`Creating dummy branches for PR #${pull.number}`);
const {head, base} = branchNames(pull);
sh(`git -C ${cloneDir} checkout master`);
sh(`git -C ${cloneDir} checkout -B ${base}`);
sh(`git -C ${cloneDir} push ${config.destination.repo.url} ${base}`);
sh(`git -C ${cloneDir} checkout -B ${head}`);
const authorName = config.destination.committer_name;
const authorEmail = config.destination.committer_email;
sh(`GIT_COMMITTER_NAME="${authorName}" GIT_COMITTER_EMAIL="${authorEmail}" ` +
`GIT_AUTHOR_NAME="${authorName}" GIT_AUTHOR_EMAIL="${authorEmail}" ` +
`git -C ${cloneDir} commit --allow-empty --message "Dummy commit for PR #${pull.number}"`);
sh(`git -C ${cloneDir} push ${config.destination.repo.url} ${head}`);
sh(`git -C ${cloneDir} checkout master`);
progress(`Creating broken PR #${pull.number}`);
const notice = `_**Note:** the base commit (${pull.base.sha}) is lost,` +
" so unfortunately we cannot show you the changes added by this PR._";
await post(url(`${config.destination.url}/pulls`),
{
title: pull.title,
body: `${pull.body}\n\n${suffix(pull)}\n\n${notice}\n\n`,
head: head,
base: base,
},
authorToken(pull.user));
}
async function createPullRequest(pull: PullRequest): Promise<void> {
progress(`Creating PR #${pull.number}`);
let head, base;
if (pull.state == "open") {
head = pull.head.ref;
base = pull.base.ref;
} else {
({head, base} = branchNames(pull));
await createBranch(head, pull.head.sha);
await createBranch(base, pull.base.sha);
}
try {
await post(url(`${config.destination.url}/pulls`),
{
title: pull.title,
body: `${pull.body}\n\n${suffix(pull)}`,
head: head,
base: base,
},
authorToken(pull.user));
} catch (e) {
if (e.response.status == 422) {
console.dir(e.response.data);
if (e.response.data.message === "Reference update failed") {
warn("Branch creation failed somehow. Retrying ...");
sh("sleep 1");
} else if (e.response.data.errors[0].message.startsWith("No commits between ")) {
warn(`Trying to fix PR #${pull.number}`);
sh(`git -C ${cloneDir} pull`);
await createBrokenPullRequest(pull);
} else {
throw e;
}
} else {
throw e;
}
}
}
async function createIssue(issue: Issue): Promise<void> {
progress(`Creating issue #${issue.number}`);
await post(url(`${config.destination.url}/issues`),
{
title: issue.title,
body: `${issue.body}\n\n${suffix(issue)}`,
},
authorToken(issue.user));
}
async function createIssuesAndPulls(issues: Issue[], missing: Commit[]): Promise<void> {
log("Creating issues and pull requests");
const missingHashes = new Set(missing.map((c): string => c.sha));
for (const issue of issues) {
if (isPR(issue)) {
if (missingHashes.has(issue.base.sha)) {
await createBrokenPullRequest(issue);
} else {
await createPullRequest(issue);
}
} else {
await createIssue(issue);
}
}
}
async function findCreators(items: Authorable[]): Promise<User[]> {
log("Searching for creator usernames");
const creatorSet = new Set(items.map((item: Authorable): User => item.user));
const creators = Array.from(creatorSet);
writeJson(creatorsPath, creators);
return creators;
}
async function filterMentions(items: Authorable[]): Promise<Map<string, string>> {
log("Filtering mentioned usernames");
const regex = /@[-A-z0-9]{1,39}/;
const mentions = new Map();
for (const item of items) {
const body = item.body;
const parts: string[] = [];
let last = 0;
let match = regex.exec(body);
while (match) {
parts.push(body.slice(last, last + match.index));
const username = match[0].slice(1);
if (!mentions.has(username)) {
try {
const response = await get(url(`${config.source.api}/users/${username}`));
const user = response.data;
mentions.set(username, user.email);
} catch (e) {
// ignore, user does not exist
}
}
const newName = config.destination.usernames[username];
if (newName) {
if (config.for_real) {
parts.push(`@${newName}`);
} else {
parts.push(`\`@${newName}\``);
}
} else {
parts.push(`\`@${username}\``);
}
last += match.index + match[0].length;
match = regex.exec(body.slice(last));
}
parts.push(body.slice(last));
item.body = parts.join("");
}
writeJson(mentionsPath, Array.from(mentions));
return mentions;
}
/**
* Reset the destination repository by deleting and recreating it. This needs
* the 'delete_repositories' scope. Use with caution.
*/
async function resetDestination(): Promise<void> {
progress("Deleting destination repository");
try {
await destroy(config.destination.url, config.destination.admin_token);
} catch (e) {
warn(e);
}
progress("Creating destination repository");
await post(url(`${config.destination.api}/orgs/${config.destination.repo.owner}/repos`),
{name: config.destination.repo.name, private: true},
config.destination.admin_token);
}
async function createPullComment(comment: PRComment): Promise<void> {
const pullNumber = comment.pull_request_url.split("/").pop();
progress(`Creating PR comment for PR #${pullNumber} (${comment.id})`);
try {
await post(url(`${config.destination.url}/pulls/${pullNumber}/comments`),
{
body: `${comment.body}\n\n${suffix(comment)}`,
commit_id: comment.original_commit_id,
path: comment.path,
position: comment.original_position,
},
authorToken(comment.user));
} catch (e) {
if (e.response.status == 422 &&
e.response.data.errors[0].message.endsWith("is not part of the pull request")) {
warn(`Ignoring PR comment because the original commit is gone. (${comment.html_url})`);
console.dir(comment);
} else {
throw e;
}
}
}
async function createCommitComment(comment: CommitComment): Promise<void> {
progress(`Creating commit comment for ${comment.commit_id} (${comment.id})`);
await post(url(`${config.destination.url}/commits/${comment.commit_id}/comments`),
{
body: `${comment.body}\n\n${suffix(comment)}`,
commit_id: comment.commit_id,
path: comment.path,
position: comment.original_position,
},
authorToken(comment.user));
}
async function createIssueComment(comment: IssueComment): Promise<void> {
const issueNumber = comment.issue_url.split("/").pop();
progress(`Creating issue comment for #${issueNumber} (${comment.id})`);
await post(url(`${config.destination.url}/issues/${issueNumber}/comments`),
{
body: `${comment.body}\n\n${suffix(comment)}`,
},
authorToken(comment.user));
}
async function createReview(review: Review): Promise<void> {
progress(`Creating review ${review.html_url}`);
const pr = review.pull_request_url.split("/").pop();
const body = review.body.length > 0
? `${review.body}\n\n${suffix(review)}`
: suffix(review);
try {
if (review.event !== "COMMENT" || review.body.length > 0) {
await post(url(`${config.destination.url}/pulls/${pr}/reviews`),
{
body: body,
event: review.event,
},
authorToken(review.user));
} else {
warn(`Ignoring review comment ${review.html_url}`);
}
} catch (e) {
warn(`Error creating review :${e}`);
}
}
async function createComments(comments: Comment[], missingCommits: Commit[]): Promise<void> {
log("Creating comments");
const missingHashes = new Set(missingCommits.map(c => c.sha));
for (const comment of comments) {
if (isPRComment(comment)) {
if(!missingHashes.has(comment.original_commit_id)){
await createPullComment(comment);
}
} else if (isCommitComment(comment)) {
if (!missingHashes.has(comment.commit_id)) {
await createCommitComment(comment);
}
} else if (isIssueComment(comment)) {
await createIssueComment(comment);
} else if (isReview(comment)) {
await createReview(comment);
} else {
throw "Unknown comment type";
}
}
}
async function createLabels(labels: Label[]): Promise<void> {
log("Creating labels");
labels.push({
name: "migrated",
color: "ffffff",
description: "This item originates from the migrated github.ugent.be repository",
});
for (const label of labels) {
try {
await post(url(`${config.destination.url}/labels`),
{
name: label.name,
color: label.color,
description: label.description,
},
config.destination.default_token);
} catch (e) {
if (e.response.status == 422) {
warn(`Label ${label.name} already exists`);
} else {
throw e;
}
}
}
}
async function createMilestones(milestones: Milestone[]): Promise<void> {
log("Creating milestones");
for (const milestone of milestones) {
try {
const data: any = {
title: milestone.title,
state: milestone.state,
};
if (milestone.description) {
data.description = milestone.description;
}
if (milestone.due_on) {
data.due_on = milestone.due_on;
}
await post(url(`${config.destination.url}/milestones`),
data,
config.destination.default_token);
} catch (e) {
if (e.response.status == 422) {
warn(`Milestone ${milestone.title} already exists`);
} else {
throw e;
}
}
}
}
async function showRateLimit(): Promise<void> {
const response = await get(config.destination.url,
config.destination.default_token);
const remaining = response.headers["x-ratelimit-remaining"];