forked from piceaTech/node-gitlab-2-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
311 lines (278 loc) · 9.2 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
var GitHubApi = require("github");
var Gitlab = require('gitlab');
var async = require('async');
var settings = require('./settings.json');
console.log(settings);
var gitlab = Gitlab({
url: settings.gitlab.url,
token: settings.gitlab.token
});
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: true,
protocol: "https",
host: "api.github.com",
pathPrefix: "",
timeout: 5000,
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.list(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(err);
}
}, function(err) {
if (err) return console.log(err);
// all milestones are created
getAllGHMilestones(function(milestoneDataA, milestoneDataMappedA) {
// 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
})
}); // 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)
getAllGHIssues(function(err, ghIssues) {
ghIssuesMapped = ghIssues.map(function(item) {
return item.title;
});
console.log('length Issue GitLab:', 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.getAllMilestones({
user: settings.github.username,
repo: settings.github.repo
}, function(err, milestoneDataOpen) {
github.issues.getAllMilestones({
user: settings.github.username,
repo: settings.github.repo,
state: 'closed'
}, function(err, milestoneDataClosed) {
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.repoIssues({
user: settings.github.username,
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 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 = {
user: settings.github.username,
repo: settings.github.repo,
title: item.title.trim(),
body: item.description
};
if (item.assignee && item.assignee.username == settings.github.username) { // TODO create Username mapping
props.assignee = 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();
}
}
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.username,
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) {
// 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)) {
// don't transport when the state changed (is a note in gitlab)
return cb();
} else {
github.issues.createComment({
user: settings.github.username,
repo: settings.github.repo,
number: newIssueData.number,
body: item.body
}, cb);
}
}, callback)
} else {
callback();
}
});
}
function createMilestone(data, cb) {
github.issues.createMilestone({
user: settings.github.username,
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);
}