Skip to content

Commit 9c1d591

Browse files
committed
now also works with milestones again
1 parent bb649b8 commit 9c1d591

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

index.js

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (settings.gitlab.projectID === null) {
1616
return a.id - b.id;
1717
});
1818
for (var i = 0; i < projects.length; i++) {
19-
console.log(projects[i].id, projects[i].description, projects[i].name);
19+
console.log('projects:', projects[i].id, projects[i].description, projects[i].name);
2020
}
2121
console.log('\n\n');
2222
console.log('Select which project ID should be transported to github. Edit the settings.json accordingly. (gitlab.projectID)');
@@ -46,6 +46,7 @@ if (settings.gitlab.projectID === null) {
4646
});
4747

4848
gitlab.projects.milestones.list(settings.gitlab.projectID, function(data) {
49+
console.log('Amount of gitlab milestones', data.length);
4950
data = data.sort(function(a, b) {
5051
return a.id - b.id;
5152
});
@@ -58,7 +59,12 @@ if (settings.gitlab.projectID === null) {
5859
repo: settings.github.repo,
5960
state: 'closed'
6061
}, function(err, milestoneDataClosed) {
61-
milestoneData = milestoneDataClosed.concat(milestoneDataOpen);
62+
milestoneData = milestoneDataClosed.concat(milestoneDataOpen).map(function(item) {
63+
return {
64+
number: item.number,
65+
title: item.title
66+
};
67+
});
6268
milestoneDataMapped = milestoneData.map(function(item) {
6369
return item.title;
6470
});
@@ -82,7 +88,7 @@ if (settings.gitlab.projectID === null) {
8288
}, function(err) {
8389
if (err) return console.log(err);
8490
// all milestones are created
85-
createAllIssuesAndComments(milestoneData, function(err, data){
91+
createAllIssuesAndComments(milestoneData, function(err, data) {
8692
console.log('\n\n\n\nFinished creating all issues and Comments\n\n\n\n')
8793
console.log(err, data)
8894
});
@@ -98,158 +104,171 @@ function createAllIssuesAndComments(milestoneData, callback) {
98104
gitlab.projects.issues.list(settings.gitlab.projectID, function(issueData) {
99105
// TODO return all issues via pagination
100106
// look whether issue is already created
101-
issueData = issueData.sort(function(a, b) {
107+
issueData = issueData.sort(function(a, b) {
102108
return a.id - b.id;
103109
});
104110
console.log('length Issue GitLab:', issueData.length)
105-
106-
107-
getAllGHIssues(function(err, ghIssues){
108-
ghIssuesMapped = ghIssues.map(function(item){
109-
return item.title;
111+
112+
113+
getAllGHIssues(function(err, ghIssues) {
114+
ghIssuesMapped = ghIssues.map(function(item) {
115+
return item.title;
110116
});
111117
console.log('length Issue GitLab:', ghIssues.length)
112-
113-
118+
119+
114120
async.eachSeries(issueData, function(item, cb) {
115-
if (ghIssuesMapped.indexOf(item.title) < 0) {
116-
console.log('Creating new Issue', item.title);
121+
if (item.milestone) {
122+
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
123+
if (title !== null) {
124+
console.log('title', title);
125+
}
126+
}
127+
if (ghIssuesMapped.indexOf(item.title.trim()) < 0) {
128+
console.log('Creating new Issue', item.title.trim());
117129
createIssueAndComments(item, function(err, createIssueData) {
118130
console.log(createIssueData);
119131
return cb(err);
120132
});
121133
} else {
122-
var ghIssue = ghIssues.filter(function(element, index, array){
123-
return element.title == item.title;
134+
var ghIssue = ghIssues.filter(function(element, index, array) {
135+
return element.title == item.title.trim();
124136
});
125-
return makeCorrectState(ghIssue[0], item.state, cb);
137+
return makeCorrectState(ghIssue[0], item, cb);
126138
}
127139
}, function(err) {
128-
if(err)console.log('error with issueData:', err);
140+
if (err) console.log('error with issueData:', err);
129141
callback(err);
130142
}); // each series
131-
});// getAllGHIssues
143+
}); // getAllGHIssues
132144
}); // gitlab project Issues
133145
}
134146

135-
function getAllGHIssues(callback){
147+
function getAllGHIssues(callback) {
136148
var lastItem = null;
137149
var curPage = 1;
138150
var allGhIssues = [];
139-
async.whilst(function(){
151+
async.whilst(function() {
140152
return hasNext(lastItem)
141-
}, function(cb){
142-
github.issues.repoIssues({
153+
}, function(cb) {
154+
github.issues.repoIssues({
143155
user: settings.github.username,
144156
repo: settings.github.repo,
145157
state: 'all',
146158
per_page: 100,
147159
page: curPage
148-
}, function(err, ghIssues){
160+
}, function(err, ghIssues) {
149161
console.log('got page', curPage, 'with', ghIssues.length, 'entries');
150162
console.log('\n\n\n');
151-
console.log(ghIssues.meta);
152-
163+
console.log('ghIssues.meta', ghIssues.meta);
164+
153165
curPage++;
154166
lastItem = ghIssues;
155167
var l = ghIssues.length;
156-
for(var i = 0; i < l; i++){
168+
for (var i = 0; i < l; i++) {
157169
allGhIssues[allGhIssues.length] = ghIssues[i];
158170
}
159171
cb(err);
160-
});// gh repo Issues
161-
}, function(err){
172+
}); // gh repo Issues
173+
}, function(err) {
162174
console.log('issue Count on GH:', allGhIssues.length)
163175
callback(err, allGhIssues);
164176
}); // async whilst
165177
}
166-
function hasNext(item){
167-
if(item === null){
178+
179+
function hasNext(item) {
180+
if (item === null) {
168181
return true;
169-
}
170-
else if(item.meta.link == undefined || item.meta.link.indexOf('next') < 0){
182+
} else if (item.meta.link == undefined || item.meta.link.indexOf('next') < 0) {
171183
return false
172-
}
173-
else{
184+
} else {
174185
return true
175186
}
176-
187+
177188
}
178189

179190

180191
function findMileStoneforTitle(milestoneData, title) {
181192
for (var i = milestoneData.length - 1; i >= 0; i--) {
182193
if (milestoneData[i].title == title) {
183-
console.log(milestoneData[i].number);
194+
console.log('findMileStoneforTitle', milestoneData[i].number);
184195
return milestoneData[i].number;
185196
}
186197
}
187198
return null;
188199
}
189-
function createIssueAndComments(item, callback){
200+
201+
function createIssueAndComments(item, callback) {
190202
var props = {
191203
user: settings.github.username,
192204
repo: settings.github.repo,
193-
title: item.title,
205+
title: item.title.trim(),
194206
body: item.description
195207
};
196-
if(item.assignee && item.assignee.username == settings.github.username){ // TODO create Username mapping
208+
if (item.assignee && item.assignee.username == settings.github.username) { // TODO create Username mapping
197209
props.assignee = item.assignee.username;
198210
}
199211
if (item.milestone) {
200212
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
201213
if (title !== null) {
202214
props.milestone = title;
203215
} else {
204-
216+
205217
// TODO also import issues where milestone got deleted
206218
// return callback();
207219
}
208220
}
221+
console.log('props', props);
209222
github.issues.create(props, function(err, newIssueData) {
210223
if (!err) {
211-
createAllIssueComments(settings.gitlab.projectID, item.id, newIssueData, function(err, issueData){
212-
makeCorrectState(newIssueData, item.state, callback)
224+
createAllIssueComments(settings.gitlab.projectID, item.id, newIssueData, function(err, issueData) {
225+
makeCorrectState(newIssueData, item, callback)
213226
});
214227
} else {
215-
console.log('errData' , err, newIssueData);
228+
console.log('errData', err, newIssueData);
216229
return callback(err);
217230
}
218231
});
219232
}
220233

221234

222-
function makeCorrectState(ghIssueData, state, callback){
223-
if(state != 'closed' || ghIssueData.state == 'closed'){
235+
function makeCorrectState(ghIssueData, item, callback) {
236+
if (item.state != 'closed' || ghIssueData.state == 'closed') {
224237
// standard is open so we don't have to update
225238
return callback(null, ghIssueData);
226239
}
227-
240+
228241
// TODO get props
229242
var props = {
230243
user: settings.github.username,
231244
repo: settings.github.repo,
232245
number: ghIssueData.number,
233-
state: 'closed'
246+
state: 'closed',
234247
};
235-
236-
console.log('makeCorrectState', ghIssueData.number, state);
248+
if (item.milestone) {
249+
var title = findMileStoneforTitle(milestoneData, item.milestone.title)
250+
if (title !== null) {
251+
props.milestone = title;
252+
}
253+
}
254+
255+
console.log('makeCorrectState', ghIssueData.number, item.state, props.milestone);
256+
console.log('makeCorrectState props', props);
237257
github.issues.edit(props, callback);
238258
}
259+
239260
function createAllIssueComments(projectID, issueID, newIssueData, callback) {
240261
// get all comments add them to the comment
241262
gitlab.projects.issues.notes.all(projectID, issueID, function(data) {
242263
if (data.length) {
243264
data = data.sort(function(a, b) {
244265
return a.id - b.id;
245266
});
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)){
267+
async.eachSeries(data, function(item, cb) {
268+
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)) {
249269
// don't transport when the state changed (is a note in gitlab)
250270
return cb();
251-
}
252-
else{
271+
} else {
253272
github.issues.createComment({
254273
user: settings.github.username,
255274
repo: settings.github.repo,
@@ -258,8 +277,7 @@ function createAllIssueComments(projectID, issueID, newIssueData, callback) {
258277
}, cb);
259278
}
260279
}, callback)
261-
}
262-
else{
280+
} else {
263281
callback();
264282
}
265283
});

0 commit comments

Comments
 (0)