forked from piceaTech/node-gitlab-2-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
482 lines (410 loc) · 14.8 KB
/
index.js
File metadata and controls
482 lines (410 loc) · 14.8 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
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
import GithubHelper from './githubHelper';
import GitlabHelper from './gitlabHelper';
const GitHubApi = require('@octokit/rest');
const { Gitlab } = require('gitlab');
const async = require('async');
const fs = require('fs');
const issueCounters = {
nrOfPlaceholderIssues: 0,
nrOfReplacementIssues: 0,
nrOfFailedIssues: 0,
};
let settings = null;
try {
settings = require('./settings.js');
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
console.log('\n\nPlease copy the sample_settings.js to settings.js.');
} else {
console.log(e);
}
process.exit(1);
}
// Ensure that the GitLab token has been set in settings.js
if (
!settings.gitlab.token ||
settings.gitlab.token === '{{gitlab private token}}'
) {
console.log(
'\n\nYou have to enter your GitLab private token in the settings.js file.'
);
process.exit(1);
}
// Create a GitLab API object
const gitlabApi = new Gitlab({
host: settings.gitlab.url ? settings.gitlab.url : 'http://gitlab.com',
token: settings.gitlab.token,
});
// Create a GitHub API object
const githubApi = new GitHubApi({
debug: false,
baseUrl: settings.github.baseUrl
? settings.github.baseUrl
: 'https://api.github.com',
timeout: 5000,
headers: {
'user-agent': 'node-gitlab-2-github', // GitHub is happy with a unique user agent
accept: 'application/vnd.github.v3+json',
},
});
const gitlabHelper = new GitlabHelper(gitlabApi, settings.gitlab);
const githubHelper = new GithubHelper(githubApi, settings.github, gitlabHelper);
// If no project id is given in settings.js, just return
// all of the projects that this user is associated with.
if (!settings.gitlab.projectId) {
gitlabHelper.listProjects();
} else {
// user has chosen a project
migrate();
}
// ----------------------------------------------------------------------------
/*
* TODO description
*/
function createPlaceholderIssue(expectedIdx) {
return {
iid: expectedIdx,
title: `[PLACEHOLDER ISSUE] - for issue #${expectedIdx}`,
description:
'This is to ensure the issue numbers in GitLab and GitHub are the same',
state: 'closed',
isPlaceholder: true,
};
}
// ----------------------------------------------------------------------------
/*
* TODO description
*/
function createReplacementIssue(id, title, state) {
const originalGitlabIssueLink = 'TODO'; // TODO
const description = `The original issue\n\n\tId: ${id}\n\tTitle: ${title}\n\ncould not be created.\nThis is a dummy issue, replacing the original one. It contains everything but the original issue description. In case the gitlab repository is still existing, visit the following link to show the original issue:\n\n${originalGitlabIssueLink}`;
return {
iid: id,
title: `${title} [REPLACEMENT ISSUE]`,
description,
state,
};
}
// ----------------------------------------------------------------------------
/**
* Performs all of the migration tasks to move a GitLab repo to GitHub
*/
async function migrate() {
githubApi.authenticate({
type: 'token',
token: settings.github.token,
});
//
// Sequentially transfer repo things
//
try {
// transfer GitLab milestones to GitHub
await transferMilestones();
// transfer GitLab labels to GitHub
await transferLabels(true, settings.conversion.useLowerCaseLabels);
// Transfer issues with their comments; do this before transferring the merge requests
await transferIssues();
if (settings.mergeRequests.log) {
// log merge requests
await logMergeRequests(settings.mergeRequests.logFile);
} else {
await transferMergeRequests();
}
} catch (err) {
console.error('Error during transfer:');
console.error(err);
}
console.log('\n\nTransfer complete!\n\n');
}
// ----------------------------------------------------------------------------
/**
* Transfer any milestones that exist in GitLab that do not exist in GitHub.
*/
async function transferMilestones() {
inform('Transferring Milestones');
// Get a list of all milestones associated with this project
let milestones = await gitlabApi.ProjectMilestones.all(
settings.gitlab.projectId
);
// sort milestones in ascending order of when they were created (by id)
milestones = milestones.sort((a, b) => a.id - b.id);
// get a list of the current milestones in the new GitHub repo (likely to be empty)
const githubMilestones = await githubHelper.getAllGithubMilestones();
// if a GitLab milestone does not exist in GitHub repo, create it.
for (let milestone of milestones) {
if (!githubMilestones.find(m => m.title === milestone.title)) {
console.log('Creating: ' + milestone.title);
try {
// process asynchronous code in sequence
await githubHelper.createMilestone(milestone);
} catch (err) {
console.error('Could not create milestone', milestone.title);
console.error(err);
}
} else {
console.log('Already exists: ' + milestone.title);
}
}
}
// ----------------------------------------------------------------------------
/**
* Transfer any labels that exist in GitLab that do not exist in GitHub.
*/
async function transferLabels(attachmentLabel = true, useLowerCase = true) {
inform('Transferring Labels');
// Get a list of all labels associated with this project
let labels = await gitlabApi.Labels.all(settings.gitlab.projectId);
// get a list of the current label names in the new GitHub repo (likely to be just the defaults)
let githubLabels = await githubHelper.getAllGithubLabelNames();
// create a hasAttachment label for manual attachment migration
if (attachmentLabel) {
const hasAttachmentLabel = { name: 'has attachment', color: '#fbca04' };
labels.push(hasAttachmentLabel);
}
// create gitlabMergeRequest label for non-migratable merge requests
const gitlabMergeRequestLabel = {
name: 'gitlab merge request',
color: '#b36b00',
};
labels.push(gitlabMergeRequestLabel);
// if a GitLab label does not exist in GitHub repo, create it.
for (let label of labels) {
// GitHub prefers lowercase label names
if (useLowerCase) {
label.name = label.name.toLowerCase();
}
if (!githubLabels.find(l => l === label.name)) {
console.log('Creating: ' + label.name);
try {
// process asynchronous code in sequence
await githubHelper.createLabel(label).catch(x => {});
} catch (err) {
console.error('Could not create label', label.name);
console.error(err);
}
} else {
console.log('Already exists: ' + label.name);
}
}
}
// ----------------------------------------------------------------------------
/**
* Transfer any issues and their comments that exist in GitLab that do not exist in GitHub.
*/
async function transferIssues() {
inform('Transferring Issues');
// Because each
let milestoneData = await githubHelper.getAllGithubMilestones();
// get a list of all GitLab issues associated with this project
// TODO return all issues via pagination
let issues = await gitlabApi.Issues.all({
projectId: settings.gitlab.projectId,
});
// sort issues in ascending order of their issue number (by iid)
issues = issues.sort((a, b) => a.iid - b.iid);
// get a list of the current issues in the new GitHub repo (likely to be empty)
let githubIssues = await githubHelper.getAllGithubIssues();
console.log(`Transferring ${issues.length} issues.`);
if (settings.usePlaceholderIssuesForMissingIssues) {
for (let i = 0; i < issues.length; i++) {
// GitLab issue internal Id (iid)
let expectedIdx = i + 1;
// is there a gap in the GitLab issues?
// Create placeholder issues so that new GitHub issues will have the same
// issue number as in GitLab. If a placeholder is used it is because there
// was a gap in GitLab issues -- likely caused by a deleted GitLab issue.
if (issues[i].iid !== expectedIdx) {
issues.splice(i, 0, createPlaceholderIssue(expectedIdx));
issueCounters.nrOfPlaceholderIssues++;
console.log(
`Added placeholder issue for GitLab issue #${expectedIdx}.`
);
}
}
}
//
// Create GitHub issues for each GitLab issue
//
// if a GitLab issue does not exist in GitHub repo, create it -- along with comments.
for (let issue of issues) {
// try to find a GitHub issue that already exists for this GitLab issue
let githubIssue = githubIssues.find(
i => i.title.trim() === issue.title.trim()
);
if (!githubIssue) {
console.log(`\nMigrating issue #${issue.iid} ('${issue.title}')...`);
try {
// process asynchronous code in sequence -- treats the code sort of like blocking
await githubHelper.createIssueAndComments(milestoneData, issue);
console.log(`\t...DONE migrating issue #${issue.iid}.`);
} catch (err) {
console.log(`\t...ERROR while migrating issue #${issue.iid}.`);
console.error('DEBUG:\n', err); // TODO delete this after issue-migration-fails have been fixed
if (settings.useReplacementIssuesForCreationFails) {
console.log('\t-> creating a replacement issue...');
const replacementIssue = createReplacementIssue(
issue.iid,
issue.title,
issue.state
);
try {
await githubHelper.createIssueAndComments(
milestoneData,
replacementIssue
);
issueCounters.nrOfReplacementIssues++;
console.error('\t...DONE.');
} catch (err) {
issueCounters.nrOfFailedIssues++;
console.error(
'\t...ERROR: Could not create replacement issue either!'
);
}
}
}
} else {
console.log(`Updating issue #${issue.iid} - ${issue.title}...`);
try {
await githubHelper.updateIssueState(githubIssue, issue);
console.log(`...Done updating issue #${issue.iid}.`);
} catch (err) {
console.log(`...ERROR while updating issue #${issue.iid}.`);
}
}
}
// print statistics about issue migration:
console.log(`DONE creating issues.`);
console.log(`\n\tStatistics:`);
console.log(`\tTotal nr. of issues: ${issues.length}`);
console.log(
`\tNr. of used placeholder issues: ${issueCounters.nrOfPlaceholderIssues}`
);
console.log(
`\tNr. of used replacement issues: ${issueCounters.nrOfReplacementIssues}`
);
console.log(
`\tNr. of issue migration fails: ${issueCounters.nrOfFailedIssues}`
);
}
// ----------------------------------------------------------------------------
/**
* Transfer any merge requests that exist in GitLab that do not exist in GitHub
* TODO - Update all text references to use the new issue numbers;
* GitHub treats pull requests as issues, therefore their numbers are changed
* @returns {Promise<void>}
*/
async function transferMergeRequests() {
inform('Transferring Merge Requests');
let milestoneData = await githubHelper.getAllGithubMilestones();
// Get a list of all pull requests (merge request equivalent) associated with
// this project
let mergeRequests = await gitlabApi.MergeRequests.all({
projectId: settings.gitlab.projectId,
});
// Sort merge requests in ascending order of their number (by iid)
mergeRequests = mergeRequests.sort((a, b) => a.iid - b.iid);
// Get a list of the current pull requests in the new GitHub repo (likely to
// be empty)
let githubPullRequests = await githubHelper.getAllGithubPullRequests();
// get a list of the current issues in the new GitHub repo (likely to be empty)
// Issues are sometimes created from Gitlab merge requests. Avoid creating duplicates.
let githubIssues = await githubHelper.getAllGithubIssues();
console.log(
'Transferring ' + mergeRequests.length.toString() + ' merge requests'
);
//
// Create GitHub pull request for each GitLab merge request
//
// if a GitLab merge request does not exist in GitHub repo, create it -- along
// with comments
for (let request of mergeRequests) {
// Try to find a GitHub pull request that already exists for this GitLab
// merge request
let githubRequest = githubPullRequests.find(
i => i.title.trim() === request.title.trim()
);
let githubIssue = githubIssues.find(
// allow for issues titled "Original Issue Name [merged]"
i => i.title.trim().includes(request.title.trim())
);
if (!githubRequest && !githubIssue) {
console.log(
'Creating pull request: !' + request.iid + ' - ' + request.title
);
try {
// process asynchronous code in sequence
await githubHelper.createPullRequestAndComments(milestoneData, request);
} catch (err) {
console.error(
'Could not create pull request: !' +
request.iid +
' - ' +
request.title
);
console.error(err);
}
} else {
if (githubRequest) {
console.log(
'Gitlab merge request already exists (as github pull request): ' +
request.iid +
' - ' +
request.title
);
githubHelper.updatePullRequestState(githubRequest, request);
} else {
console.log(
'Gitlab merge request already exists (as github issue): ' +
request.iid +
' - ' +
request.title
);
}
}
}
}
//-----------------------------------------------------------------------------
/**
* logs merge requests that exist in GitLab to a file.
*/
async function logMergeRequests(logFile) {
inform('Logging Merge Requests');
// get a list of all GitLab merge requests associated with this project
// TODO return all MRs via pagination
let mergeRequests = await gitlabApi.MergeRequests.all({
projectId: settings.gitlab.projectId,
});
// sort MRs in ascending order of when they were created (by id)
mergeRequests = mergeRequests.sort((a, b) => a.id - b.id);
console.log('Logging ' + mergeRequests.length.toString() + ' merge requests');
for (let mergeRequest of mergeRequests) {
let mergeRequestDiscussions = await gitlabApi.MergeRequestDiscussions.all(
projectId,
mergeRequest.iid
);
let mergeRequestNotes = await gitlabApi.MergeRequestNotes.all(
projectId,
mergeRequest.iid
);
mergeRequest.discussions = mergeRequestDiscussions
? mergeRequestDiscussions
: [];
mergeRequest.notes = mergeRequestNotes ? mergeRequestNotes : [];
}
//
// Log the merge requests to a file
//
const output = {
mergeRequests: mergeRequests,
};
fs.writeFileSync(logFile, JSON.stringify(output, null, 2));
}
// ----------------------------------------------------------------------------
/**
* Print out a section heading to let the user know what is happening
*/
function inform(msg) {
console.log('==================================');
console.log(msg);
console.log('==================================');
}