Skip to content

Commit 85ae6e2

Browse files
committed
Update index.js
1 parent 9df0c9d commit 85ae6e2

File tree

1 file changed

+157
-63
lines changed

1 file changed

+157
-63
lines changed

index.js

Lines changed: 157 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if (settings.gitlab.projectID === null) {
3030
// required
3131
version: "3.0.0",
3232
// optional
33-
debug: true,
33+
//debug: true,
3434
protocol: "https",
3535
host: "api.github.com",
3636
pathPrefix: "",
@@ -45,12 +45,6 @@ if (settings.gitlab.projectID === null) {
4545
password: settings.github.password
4646
});
4747

48-
49-
50-
51-
52-
// TODO check whether user has created all milestones on github
53-
5448
gitlab.projects.milestones.list(settings.gitlab.projectID, function(data) {
5549
data = data.sort(function(a, b) {
5650
return a.id - b.id;
@@ -80,70 +74,109 @@ if (settings.gitlab.projectID === null) {
8074
console.log('Creating new Milestone', item.title);
8175
createMilestone(item, function(err, createMilestoneData) {
8276
console.log(createMilestoneData);
83-
cb(err);
77+
return cb(err);
8478
});
8579
} else {
86-
cb(err);
80+
return cb(err);
8781
}
8882
}, function(err) {
8983
if (err) return console.log(err);
9084
// all milestones are created
91-
createAllIssuesAndComments(milestoneData);
92-
93-
85+
createAllIssuesAndComments(milestoneData, function(err, data){
86+
console.log('\n\n\n\nFinished creating all issues and Comments\n\n\n\n')
87+
console.log(err, data)
88+
});
9489
}); // async
95-
9690
}); // closed Issues
9791
}); // opend issues
9892
}); // gitlab list milestones
9993
}
10094

10195

102-
function createAllIssuesAndComments(milestoneData) {
96+
function createAllIssuesAndComments(milestoneData, callback) {
10397
// select all issues and comments from this project
10498
gitlab.projects.issues.list(settings.gitlab.projectID, function(issueData) {
99+
// TODO return all issues via pagination
105100
// look whether issue is already created
106-
console.log('length:', issueData.length)
107-
108-
// console.log('~', issueData);
109-
async.eachSeries(issueData, function(item, cb) {
110-
console.log(settings.github.username);
111-
var props = {
112-
user: settings.github.username,
113-
repo: settings.github.repo,
114-
title: item.title,
115-
body: item.description
116-
};
117-
if(item.assignee && item.assignee.username == settings.github.username){
118-
props.assignee = item.assignee.username;
119-
}
120-
if (item.milestone) {
121-
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
122-
if (title !== null) {
123-
props.milestone = title;
124-
} else {
125-
// don't import issues where milestone got deleted
126-
return cb();
127-
}
128-
}
129-
console.log('props:', props);
130-
github.issues.create({
131-
props
132-
}, function(err, data) {
133-
console.log('errData' , err, data);
134-
if (!err) {
135-
createAllIssueComments(settings.gitlab.projectID, item.id, data, cb);
101+
issueData = issueData.sort(function(a, b) {
102+
return a.id - b.id;
103+
});
104+
console.log('length Issue GitLab:', issueData.length)
105+
106+
107+
getAllGHIssues(function(err, ghIssues){
108+
ghIssuesMapped = ghIssues.map(function(item){
109+
return item.title;
110+
});
111+
console.log('length Issue GitLab:', ghIssues.length)
112+
113+
114+
async.eachSeries(issueData, function(item, cb) {
115+
if (ghIssuesMapped.indexOf(item.title) < 0) {
116+
console.log('Creating new Issue', item.title);
117+
createIssueAndComments(item, function(err, createIssueData) {
118+
console.log(createIssueData);
119+
return cb(err);
120+
});
136121
} else {
137-
cb(err);
122+
var ghIssue = ghIssues.filter(function(element, index, array){
123+
return element.title == item.title;
124+
});
125+
return makeCorrectState(ghIssue[0], item.state, cb);
138126
}
139-
});
140-
}, function(err) {
141-
console.log('error with issueData:', err);
142-
});
127+
}, function(err) {
128+
if(err)console.log('error with issueData:', err);
129+
callback(err);
130+
}); // each series
131+
});// getAllGHIssues
132+
}); // gitlab project Issues
133+
}
143134

144-
})
135+
function getAllGHIssues(callback){
136+
var lastItem = null;
137+
var curPage = 1;
138+
var allGhIssues = [];
139+
async.whilst(function(){
140+
return hasNext(lastItem)
141+
}, function(cb){
142+
github.issues.repoIssues({
143+
user: settings.github.username,
144+
repo: settings.github.repo,
145+
state: 'all',
146+
per_page: 100,
147+
page: curPage
148+
}, function(err, ghIssues){
149+
console.log('got page', curPage, 'with', ghIssues.length, 'entries');
150+
console.log('\n\n\n');
151+
console.log(ghIssues.meta);
152+
153+
curPage++;
154+
lastItem = ghIssues;
155+
var l = ghIssues.length;
156+
for(var i = 0; i < l; i++){
157+
allGhIssues[allGhIssues.length] = ghIssues[i];
158+
}
159+
cb(err);
160+
});// gh repo Issues
161+
}, function(err){
162+
console.log('issue Count on GH:', allGhIssues.length)
163+
callback(err, allGhIssues);
164+
}); // async whilst
165+
}
166+
function hasNext(item){
167+
if(item === null){
168+
return true;
169+
}
170+
else if(item.meta.link == undefined || item.meta.link.indexOf('next') < 0){
171+
return false
172+
}
173+
else{
174+
return true
175+
}
176+
145177
}
146178

179+
147180
function findMileStoneforTitle(milestoneData, title) {
148181
for (var i = milestoneData.length - 1; i >= 0; i--) {
149182
if (milestoneData[i].title == title) {
@@ -153,28 +186,89 @@ function findMileStoneforTitle(milestoneData, title) {
153186
}
154187
return null;
155188
}
189+
function createIssueAndComments(item, callback){
190+
var props = {
191+
user: settings.github.username,
192+
repo: settings.github.repo,
193+
title: item.title,
194+
body: item.description
195+
};
196+
if(item.assignee && item.assignee.username == settings.github.username){ // TODO create Username mapping
197+
props.assignee = item.assignee.username;
198+
}
199+
if (item.milestone) {
200+
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
201+
if (title !== null) {
202+
props.milestone = title;
203+
} else {
204+
205+
// TODO also import issues where milestone got deleted
206+
// return callback();
207+
}
208+
}
209+
github.issues.create(props, function(err, newIssueData) {
210+
if (!err) {
211+
createAllIssueComments(settings.gitlab.projectID, item.id, newIssueData, function(err, issueData){
212+
makeCorrectState(newIssueData, item.state, callback)
213+
});
214+
} else {
215+
console.log('errData' , err, newIssueData);
216+
return callback(err);
217+
}
218+
});
219+
}
156220

157-
function createAllIssueComments(projectID, issueID, newIssueData, cb) {
221+
222+
function makeCorrectState(ghIssueData, state, callback){
223+
if(state != 'closed' || ghIssueData.state == 'closed'){
224+
// standard is open so we don't have to update
225+
return callback(null, ghIssueData);
226+
}
227+
228+
// TODO get props
229+
var props = {
230+
user: settings.github.username,
231+
repo: settings.github.repo,
232+
number: ghIssueData.number,
233+
state: 'closed'
234+
};
235+
236+
console.log('makeCorrectState', ghIssueData.number, state);
237+
github.issues.edit(props, callback);
238+
}
239+
function createAllIssueComments(projectID, issueID, newIssueData, callback) {
158240
// get all comments add them to the comment
159-
gitlab.issues.notes.all(projectID, issueID, function(data) {
241+
gitlab.projects.issues.notes.all(projectID, issueID, function(data) {
160242
if (data.length) {
161-
for (var i = data.length - 1; i >= 0; i--) {
162-
163-
github.issues.createComment({
164-
user: settings.github.username,
165-
repo: settings.github.repo,
166-
number: newIssueData.number,
167-
body: data[i].body
168-
}, cb)
169-
}
243+
data = data.sort(function(a, b) {
244+
return a.id - b.id;
245+
});
246+
async.eachSeries(data, function(item, cb){
247+
if((/Status changed to .*/.test(item.body) && !/Status changed to closed by commit.*/.test(item.body))
248+
|| /Milestone changed to.*/.test(item.body) || /Reassigned to /.test(item.body)){
249+
// don't transport when the state changed (is a note in gitlab)
250+
return cb();
251+
}
252+
else{
253+
github.issues.createComment({
254+
user: settings.github.username,
255+
repo: settings.github.repo,
256+
number: newIssueData.number,
257+
body: item.body
258+
}, cb);
259+
}
260+
}, callback)
170261
}
171-
})
262+
else{
263+
callback();
264+
}
265+
});
172266
}
173267

174268

175269
function createMilestone(data, cb) {
176270
github.issues.createMilestone({
177-
username: settings.github.username,
271+
user: settings.github.username,
178272
repo: settings.github.repo,
179273
title: data.title,
180274
description: data.description,

0 commit comments

Comments
 (0)