forked from piceaTech/node-gitlab-2-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
520 lines (469 loc) · 16.1 KB
/
index.js
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
var GitHubApi = require("github");
var Gitlab = require('gitlab');
var async = require('async');
try{
var settings = require('./settings.json');
}
catch(e){
if(e.code === 'MODULE_NOT_FOUND'){
console.log('\n\nPlease copy the sample_settings.json to settings.json.');
}
else{
}
}
console.log(settings);
console.log('\n\n\n');
if(settings.gitlab.url === "http://gitlab.mycompany.com/"){
console.log('\n\nYou have to enter your gitlab url in the settings.json file.');
process.exit(1);
}
if(settings.gitlab.token === "{{gitlab private token}}"){
console.log('\n\nYou have to enter your gitlab private token in the settings.json file.');
process.exit(1);
}
var gitlab = Gitlab({
url: settings.gitlab.url,
token: settings.gitlab.token
});
var userProjectRe = generateUserProjectRe();
if (settings.gitlab.projectID === null) {
gitlab.projects.all(function(projects) {
projects = projects.sort(function(a, b) {
return a.id - b.id;
});
for (var i = 0; i < projects.length; i++) {
console.log('projects:', projects[i].id, projects[i].description, projects[i].name);
}
console.log('\n\n');
console.log('Select which project ID should be transported to github. Edit the settings.json accordingly. (gitlab.projectID)');
console.log('\n\n');
});
} else {
// user has choosen a project
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
debug: false,
protocol: "https",
host: settings.github.url,
pathPrefix: settings.github.pathPrefix,
timeout: 5000,
followRedirects: true,
headers: {
"user-agent": "node-gitlab-2-github" // GitHub is happy with a unique user agent
}
});
github.authenticate({
type: "basic",
username: settings.github.username,
password: settings.github.password
});
gitlab.projects.milestones.all(settings.gitlab.projectID, function(data) {
console.log('Amount of gitlab milestones', data.length);
data = data.sort(function(a, b) {
return a.id - b.id;
});
getAllGHMilestones(function(milestoneDataA, milestoneDataMappedA) {
// for now use globals
milestoneData = milestoneDataA;
milestoneDataMapped = milestoneDataMappedA;
console.log('\n\n\n\n\n\n\n>>>>');
console.log(milestoneDataMapped);
console.log('\n\n\n\n\n\n\n');
async.each(data, function(item, cb) {
if (milestoneDataMapped.indexOf(item.title) < 0) {
console.log('Creating new Milestone', item.title);
createMilestone(item, function(err, createMilestoneData) {
console.log(createMilestoneData);
return cb(err);
});
} else {
return cb(null);
}
}, function(err) {
if (err) return console.log(err);
// all milestones are created
getAllGHMilestones(function(milestoneDataA, milestoneDataMappedA) {
// create labels
gitlab.projects.labels.all(settings.gitlab.projectID, null, function(glLabels) {
getAllGHLabelNames(function(ghlabelNames) {
async.each(glLabels, function(glLabel, cb) {
if (ghlabelNames.indexOf(glLabel.name) < 0) {
console.log('Creating new Label', glLabel.name);
createLabel(glLabel, function(err, createLabelData) {
console.log(createLabelData);
return cb(err);
});
} else {
return cb(null);
}
}, function(err) {
if (err) return console.log(err);
// all labels are created, create a hasAttachment label for manual attachment migration
var glLabel = {
name: 'hasAttachment',
color: '#fbca04'
}
createLabel(glLabel, function(err, createLabelData) {
console.log(createLabelData);
});
// for now use globals
milestoneData = milestoneDataA;
milestoneDataMapped = milestoneDataMappedA;
createAllIssuesAndComments(milestoneData, function(err, data) {
console.log('\n\n\n\nFinished creating all issues and Comments\n\n\n\n');
console.log(err, data);
});
}); //async
}); // getAllGHLabelNames
}); // gitlab list labels
}); // getAllGHMilestones
}); // async
})
}); // gitlab list milestones
}
function createAllIssuesAndComments(milestoneData, callback) {
// select all issues and comments from this project
gitlab.projects.issues.list(settings.gitlab.projectID, function(issueData) {
// TODO return all issues via pagination
// look whether issue is already created
issueData = issueData.sort(function(a, b) {
return a.id - b.id;
});
console.log('length Issue GitLab:', issueData.length);
// loop through all issues and add placeholder issues if there are gaps
// this is to ensure issue id's are the same in gitlab and GitHub
var placeHolderItem = {
title: 'Place holder issue for issue which does not exist probably deleted in Gitlab',
description: 'This is to ensure the issue ids in Gitlab and GitHub are the same',
state: 'closed'
}
for (var iIssue = 0; iIssue < issueData.length; iIssue++) {
if (issueData[iIssue].iid != iIssue + 1) {
issueData.splice(iIssue, 0, placeHolderItem);
iIssue++;
console.log('Added placeholder item for missing issue with id:', iIssue + 1);
}
}
getAllGHIssues(function(err, ghIssues) {
if(err){
console.log(err);
console.log('getAllGHIssues');
console.log('FAIL!');
process.exit(1);
}
ghIssuesMapped = ghIssues.map(function(item) {
return item.title;
});
console.log('length Issue GitHub:', ghIssues.length);
async.eachSeries(issueData, function(item, cb) {
if (item.milestone) {
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
if (title !== null) {
console.log('title', title);
}
}
if (ghIssuesMapped.indexOf(item.title.trim()) < 0) {
console.log('Creating new Issue', item.title.trim());
createIssueAndComments(item, function(err, createIssueData) {
console.log(createIssueData);
return cb(err);
});
} else {
var ghIssue = ghIssues.filter(function(element, index, array) {
return element.title == item.title.trim();
});
return makeCorrectState(ghIssue[0], item, cb);
}
}, function(err) {
if (err) console.log('error with issueData:', err);
callback(err);
}); // each series
}); // getAllGHIssues
}); // gitlab project Issues
}
function getAllGHMilestones(callback) {
github.issues.getMilestones({
user: settings.github.owner,
repo: settings.github.repo
}, function(err, milestoneDataOpen) {
if(err){
console.log(err);
console.log('getAllGHMilestones1');
console.log('FAIL!');
process.exit(1);
}
github.issues.getMilestones({
user: settings.github.owner,
repo: settings.github.repo,
state: 'closed'
}, function(err, milestoneDataClosed) {
if(err){
console.log(err);
console.log('getAllGHMilestones2');
console.log('FAIL!');
process.exit(1);
}
milestoneData = milestoneDataClosed.concat(milestoneDataOpen).map(function(item) {
return {
number: item.number,
title: item.title
};
});
milestoneDataMapped = milestoneData.map(function(item) {
return item.title;
});
return callback(milestoneData, milestoneDataMapped);
}); // openMilestones
}); //closedMilestones
}
function getAllGHIssues(callback) {
var lastItem = null;
var curPage = 1;
var allGhIssues = [];
async.whilst(function() {
return hasNext(lastItem)
}, function(cb) {
github.issues.getForRepo({
user: settings.github.owner,
repo: settings.github.repo,
state: 'all',
per_page: 100,
page: curPage
}, function(err, ghIssues) {
console.log('got page', curPage, 'with', ghIssues.length, 'entries');
console.log('\n\n\n');
console.log('ghIssues.meta', ghIssues.meta);
curPage++;
lastItem = ghIssues;
var l = ghIssues.length;
for (var i = 0; i < l; i++) {
allGhIssues[allGhIssues.length] = ghIssues[i];
}
cb(err);
}); // gh repo Issues
}, function(err) {
console.log('issue Count on GH:', allGhIssues.length)
callback(err, allGhIssues);
}); // async whilst
}
function getAllGHLabelNames(callback) {
github.issues.getLabels({
user: settings.github.owner,
repo: settings.github.repo,
per_page: 100
}, function(err, labelData) {
if (err){
console.log(err);
console.log('getAllGHLabelNames');
console.log('FAIL!');
process.exit(1);
}
var labelNames = labelData.map(function(item) {
return item.name;
});
return callback(labelNames);
});
}
function hasNext(item) {
if (item === null) {
return true;
} else if (item.meta.link == undefined || item.meta.link.indexOf('next') < 0) {
return false
} else {
return true
}
}
function findMileStoneforTitle(milestoneData, title) {
for (var i = milestoneData.length - 1; i >= 0; i--) {
if (milestoneData[i].title == title) {
console.log('findMileStoneforTitle', milestoneData[i].number);
return milestoneData[i].number;
}
}
return null;
}
function createIssueAndComments(item, callback) {
var props = null;
convertIssuesAndComments(item.description, item, function(bodyConverted) {
props = {
user: settings.github.owner,
repo: settings.github.repo,
title: item.title.trim(),
body: bodyConverted
};
});
if (item.assignee) {
if (item.assignee.username == settings.github.username) {
props.assignee = item.assignee.username;
} else if (settings.usermap && settings.usermap[item.assignee.username]) {
// get github username name from config
props.assignee = settings.usermap[item.assignee.username];
}
}
if (item.milestone) {
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
if (title !== null) {
props.milestone = title;
} else {
// TODO also import issues where milestone got deleted
// return callback();
}
}
if (item.labels) {
props.labels = item.labels;
// add hasAttachment label if body contains an attachment for manual migration
if (props.body && props.body.indexOf('/uploads/') > -1) {
props.labels.push('hasAttachment');
}
}
console.log('props', props);
github.issues.create(props, function(err, newIssueData) {
if (!err) {
createAllIssueComments(settings.gitlab.projectID, item.id, newIssueData, function(err, issueData) {
makeCorrectState(newIssueData, item, callback);
});
} else {
console.log('errData', err, newIssueData);
return callback(err);
}
});
}
function makeCorrectState(ghIssueData, item, callback) {
if (item.state != 'closed' || ghIssueData.state == 'closed') {
// standard is open so we don't have to update
return callback(null, ghIssueData);
}
// TODO get props
var props = {
user: settings.github.owner,
repo: settings.github.repo,
number: ghIssueData.number,
state: 'closed',
};
if (item.milestone) {
var title = findMileStoneforTitle(milestoneData, item.milestone.title);
if (title !== null) {
props.milestone = title;
}
}
console.log('makeCorrectState', ghIssueData.number, item.state, props.milestone);
console.log('makeCorrectState props', props);
github.issues.edit(props, callback);
}
function createAllIssueComments(projectID, issueID, newIssueData, callback) {
if (issueID == null) {
return callback();
}
// get all comments add them to the comment
gitlab.projects.issues.notes.all(projectID, issueID, function(data) {
if (data.length) {
data = data.sort(function(a, b) {
return a.id - b.id;
});
async.eachSeries(data, function(item, cb) {
if ((/Status changed to .*/.test(item.body) && !/Status changed to closed by commit.*/.test(item.body)) ||
/Milestone changed to.*/.test(item.body) ||
/Reassigned to /.test(item.body) ||
/Added .* label/.test(item.body)) {
// don't transport when the state changed (is a note in gitlab)
return cb();
} else {
convertIssuesAndComments(item.body, item, function(bodyConverted) {
github.issues.createComment({
user: settings.github.owner,
repo: settings.github.repo,
number: newIssueData.number,
body: bodyConverted
}, cb);
});
}
}, callback)
} else {
callback();
}
});
}
function createMilestone(data, cb) {
github.issues.createMilestone({
user: settings.github.owner,
repo: settings.github.repo,
title: data.title,
description: data.description,
state: (data.state === 'active') ? 'open' : 'closed',
due_on: data.due_date + 'T00:00:00Z'
}, cb);
}
function createLabel(glLabel, cb) {
github.issues.createLabel({
user: settings.github.owner,
repo: settings.github.repo,
name: glLabel.name,
color: glLabel.color.substr(1) // remove leading "#" because gitlab returns it but github wants the color without it
}, cb);
}
/**
* Converts issue body and issue comments from gitlab to github. That means:
* - Add a line at the beginning indicating which original user created the
* issue or the comment and when - because the github API creates everything
* as the API user
* - Change username from gitlab to github in "mentions" (@username)
*/
function convertIssuesAndComments(str, item, cb){
if ( (settings.usermap == null || Object.keys(settings.usermap).length == 0) &&
(settings.projectmap == null || Object.keys(settings.projectmap).length == 0)) {
addMigrationLine(str, item, cb);
} else {
// - Replace userids as defined in settings.usermap.
// They all start with '@' in the issues but we have them without in usermap
// - Replace cross-project issue references. They are matched on org/project# so 'matched' ends with '#'
// They all have a '#' right after the project name in the issues but we have them without in projectmap
addMigrationLine(str, item, function(strWithMigLine) {
cb(strWithMigLine.replace(userProjectRe, function(matched) {
if (matched.startsWith('@')) {
// this is a userid
return '@' + settings.usermap[matched.substr(1)];
} else if (matched.endsWith('#')) {
// this is a cross-project issue reference
return settings.projectmap[matched.substring(0, matched.length-1)] + '#';
} else {
// something went wrong, do nothing
return matched;
}
}));
});
}
}
function addMigrationLine(str, item, cb) {
if (item == null || item.author == null || item.author.username == null || item.created_at == null) {
return cb(str);
}
var dateformatOptions = {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: 'numeric',
hour12: false
}
var formattedDate = new Date(item.created_at).toLocaleString('en-US', dateformatOptions);
return cb("In gitlab by @" +item.author.username+ " on " +formattedDate+ "\n\n" +str);
}
/**
* Generate regular expression which finds userid and cross-project issue references
* from usermap and projectmap
*/
function generateUserProjectRe() {
var reString = '';
if (settings.usermap != null && Object.keys(settings.usermap).length > 0) {
reString = '@' + Object.keys(settings.usermap).join('|@');
}
if (settings.projectmap != null && Object.keys(settings.projectmap).length > 0) {
if (reString.length > 0) {
reString += '|';
}
reString += Object.keys(settings.projectmap).join('#|') + '#';
}
return new RegExp(reString,'g');
}