4 var FS = require('q-io/fs');
5 var util = require('util');
6 var colors = require('colors');
7 var csv = require('csv');
8 var rest = require('restler-q');
9 var async = require('async');
10 var _ = require('lodash');
11 var argv = require('optimist')
12 .demand(['i', 'c', 'g', 'p', 't', 's'])
15 .alias('g', 'gitlaburl')
16 .alias('p', 'project')
20 .describe('i', 'CSV file exported from Mantis (Example: issues.csv)')
21 .describe('c', 'Configuration file (Example: config.json)')
22 .describe('g', 'GitLab URL hostname (Example: https://gitlab.com)')
23 .describe('p', 'GitLab project name including namespace (Example: mycorp/myproj)')
24 .describe('t', 'An admin user\'s private token (Example: a2r33oczFyQzq53t23Vj)')
25 .describe('s', 'The username performing the import (Example: bob)')
26 .describe('f', 'The first issue # to import (Example: 123)')
29 var inputFile = __dirname + '/' + argv.input;
30 var configFile = __dirname + '/' + argv.config;
31 var fromIssueId = Number(argv.from||0);
32 var gitlabAPIURLBase = argv.gitlaburl + '/api/v3';
33 var gitlabProjectName = argv.project;
34 var gitlabAdminPrivateToken = argv.token;
35 var gitlabSudo = argv.sudo;
39 var promise = getConfig()
40 .then(readMantisIssues)
41 .then(getGitLabProject)
42 .then(getGitLabProjectMembers)
43 .then(mapGitLabUserIds)
44 .then(validateMantisIssues)
45 .then(getGitLabProjectIssues)
46 .then(importGitLabIssues)
49 promise.then(function() {
50 console.log(("Done!").bold.green);
56 * Read and parse config.json file - assigns config
58 function getConfig() {
59 log_progress("Reading configuration...");
60 return FS.read(configFile, {encoding: 'utf8'})
61 .then(function(data) {
62 var config = JSON.parse(data);
63 config.users = _.extend({
66 gl_username: gitlabSudo
70 }).then(function(cfg) {
73 throw new Error('Cannot read config file: ' + configFile);
78 * Read and parse import.csv file - assigns gitLab.mantisIssues
80 function readMantisIssues() {
81 log_progress("Reading Mantis export file...");
82 return FS.read(inputFile, {encoding: 'utf8'}).then(function(data) {
86 csv().from(data, {delimiter: ',', escape: '"', columns: true})
87 .on('record', function(row, index) { rows.push(row) })
88 .on('end', function(error, data) {
93 .then(function(rows) {
94 _.forEach(rows, function(row) {
95 row.Id = Number(row.Id);
99 rows = _.filter(rows, function(row) {
100 return row.Id >= fromIssueId;
104 return gitLab.mantisIssues = _.sortBy(rows, "Id");
106 throw new Error('Cannot read input file: ' + inputFile + " - " + error);
112 * Fetch project info from GitLab - assigns gitLab.project
114 function getGitLabProject() {
115 log_progress("Fetching project from GitLab...");
116 var url = gitlabAPIURLBase + '/projects';
117 var data = { per_page: 100, private_token: gitlabAdminPrivateToken, sudo: gitlabSudo };
119 return rest.get(url, {data: data}).then(function(result) {
121 gitLab.project = _.find(result, { path_with_namespace : gitlabProjectName }) || null;
123 if (!gitLab.project) {
124 throw new Error('Cannot find GitLab project: ' + gitlabProjectName);
127 return gitLab.project;
129 throw new Error('Cannot get list of projects from gitlab: ' + url);
134 * Fetch project members from GitLab - assigns gitLab.gitlabUsers
136 function getGitLabProjectMembers() {
137 log_progress("getGitLabProjectMembers");
138 var url = gitlabAPIURLBase + '/projects/' + gitLab.project.id + "/members";
139 var data = { per_page: 100, private_token: gitlabAdminPrivateToken, sudo: gitlabSudo };
141 return rest.get(url, {data: data}).then(function(result) {
142 return gitLab.gitlabUsers = result;
144 throw new Error('Cannot get list of users from gitlab: ' + url);
149 * Sets config.users[].gl_id based gitLab.gitlabUsers
151 function mapGitLabUserIds() {
152 var users = config.users,
153 gitlabUsers = gitLab.gitlabUsers;
154 _.forEach(users, function(user) {
155 user.gl_id = (_.find(gitlabUsers, { id: user.gl_username }) || {}).id;
160 * Ensure that Mantise user names in gitLab.mantisIssues have corresponding GitLab user mapping
162 function validateMantisIssues() {
163 log_progress("Validating Mantis Users...");
165 var mantisIssues = gitLab.mantisIssues;
166 var users = config.users;
168 var missingUsernames = [];
170 for (var i = 0; i < mantisIssues.length; i++) {
171 var assignee = mantisIssues[i]["Assigned To"];
173 if (!getUserByMantisUsername(assignee) && missingUsernames.indexOf(assignee) == -1)
174 missingUsernames.push(assignee);
177 for (var i = 0; i < mantisIssues.length; i++) {
178 var reporter = mantisIssues[i].Reporter;
180 if (!getUserByMantisUsername(reporter) && missingUsernames.indexOf(reporter) == -1)
181 missingUsernames.push(reporter);
184 if (missingUsernames.length > 0) {
185 for (var i = 0; i < missingUsernames.length; i++)
186 console.error('Error: Cannot map Mantis user with username: ' + missingUsernames[i]);
188 throw new Error("User Validation Failed");
193 * Import gitLab.mantisIssues into GitLab
196 function importGitLabIssues() {
197 log_progress("Importing Mantis issues into GitLab from #" + fromIssueId + " ...");
198 return _.reduce(gitLab.mantisIssues, function(p, mantisIssue) {
199 return p.then(function() {
200 return importIssue(mantisIssue);
206 function importIssue(mantisIssue) {
207 var issueId = mantisIssue.Id;
208 var title = mantisIssue.Summary;
209 var description = getDescription(mantisIssue);
210 var assignee = getUserByMantisUsername(mantisIssue["Assigned To"]);
211 var milestoneId = '';
212 var labels = getLabels(mantisIssue);
213 var author = getUserByMantisUsername(mantisIssue.Reporter);
215 log_progress("Importing: #" + issueId + " - " + title + " ...");
219 description: description,
220 assignee_id: assignee && assignee.gl_id,
221 milestone_id: milestoneId,
224 private_token: gitlabAdminPrivateToken
227 return getIssue(gitLab.project.id, issueId)
228 .then(function(gitLabIssue) {
230 return updateIssue(gitLab.project.id, gitLabIssue.id, _.extend({
231 state_event: isClosed(mantisIssue) ? 'close' : 'reopen'
234 console.log(("#" + issueId + ": Updated successfully.").green);
237 return insertSkippedIssues(issueId-1)
239 return insertAndCloseIssue(issueId, data, isClosed(mantisIssue));
245 function insertSkippedIssues(issueId) {
246 if (gitLab.gitlabIssues[issueId]) {
250 console.warn(("Skipping Missing Mantis Issue (<= #" + issueId + ") ...").yellow);
253 title: "Skipped Mantis Issue",
255 private_token: gitlabAdminPrivateToken
258 return insertAndCloseIssue(issueId, data, true, getSkippedIssueData)
260 return insertSkippedIssues(issueId);
263 function getSkippedIssueData(gitLabIssue) {
264 var issueId = gitLabIssue.iid;
266 if (config.mantisUrl) {
267 description = "[Mantis Issue " + issueId + "](" + config.mantisUrl + "/view.php?id=" + issueId + ")";
269 description = "Mantis Issue " + issueId;
272 title: "Skipped Mantis Issue " + issueId,
273 description: "_Skipped " + description + "_"
278 function insertAndCloseIssue(issueId, data, close, custom) {
280 return insertIssue(gitLab.project.id, data).then(function(issue) {
281 gitLab.gitlabIssues[issue.iid] = issue;
283 return closeIssue(issue, custom && custom(issue)).then(
285 console.log((issueId + ': Inserted and closed successfully. #' + issue.iid).green);
287 console.warn((issueId + ': Inserted successfully but failed to close. #' + issue.iid).yellow);
291 console.log((issueId + ': Inserted successfully. #' + issue.iid).green);
293 console.error((issueId + ': Failed to insert.').red, error);
298 * Fetch all existing project issues from GitLab - assigns gitLab.gitlabIssues
300 function getGitLabProjectIssues() {
301 return getRemainingGitLabProjectIssues(0, 100)
302 .then(function(result) {
303 log_progress("Fetched " + result.length + " GitLab issues.");
304 var issues = _.indexBy(result, 'iid');
305 return gitLab.gitlabIssues = issues;
310 * Recursively fetch the remaining issues in the project
314 function getRemainingGitLabProjectIssues(page, per_page) {
315 var from = page * per_page;
316 log_progress("Fetching Project Issues from GitLab [" + (from + 1) + "-" + (from + per_page) + "]...");
317 var url = gitlabAPIURLBase + '/projects/' + gitLab.project.id + "/issues";
322 private_token: gitlabAdminPrivateToken, sudo: gitlabSudo };
324 return rest.get(url, {data: data}).then(function(issues) {
325 if(issues.length < per_page) {
328 return getRemainingGitLabProjectIssues(page+1, per_page)
329 .then(function(remainingIssues) {
330 return issues.concat(remainingIssues);
333 throw new Error('Cannot get list of issues from gitlab: ' + url + " page=" + page);
337 function getUserByMantisUsername(username) {
338 return (username && config.users[username]) || config.users[""] || null;
341 function getDescription(row) {
343 var issueId = row.Id;
345 if (config.mantisUrl) {
346 attributes.push("[Mantis Issue " + issueId + "](" + config.mantisUrl + "/view.php?id=" + issueId + ")");
348 attributes.push("Mantis Issue " + issueId);
351 if (value = row.Reporter) {
352 attributes.push("Reported By: " + value);
355 if (value = row["Assigned To"]) {
356 attributes.push("Assigned To: " + value);
359 if (value = row.Created) {
360 attributes.push("Created: " + value);
363 if (value = row.Updated && value != row.Created) {
364 attributes.push("Updated: " + value);
367 var description = "_" + attributes.join(", ") + "_\n\n";
369 description += row.Description;
371 if (value = row.Info) {
372 description += "\n\n" + value;
375 if (value = row.Notes) {
376 description += "\n\n" + value.split("$$$$").join("\n\n")
382 function getLabels(row) {
384 var labels = (row.tags || []).slice(0);
386 if(label = config.category_labels[row.Category]) {
390 if(label = config.priority_labels[row.Priority]) {
394 if(label = config.severity_labels[row.Severity]) {
398 return labels.join(",");
401 function isClosed(row) {
402 return config.closed_statuses[row.Status];
405 function getIssue(projectId, issueId) {
406 return Q(gitLab.gitlabIssues[issueId]);
408 //var url = gitlabAPIURLBase + '/projects/' + projectId + '/issues?iid=' + issueId;
409 //var data = { private_token: gitlabAdminPrivateToken, sudo: gitlabSudo };
411 //return rest.get(url, {data: data})
412 // .then(function(issues) {
413 // var issue = issues[0];
415 // throw new Error("Issue not found: " + issueId);
421 function insertIssue(projectId, data) {
422 var url = gitlabAPIURLBase + '/projects/' + projectId + '/issues';
424 return rest.post(url, {data: data})
425 .then(null, function(error) {
426 throw new Error('Failed to insert issue into GitLab: ' + url);
430 function updateIssue(projectId, issueId, data) {
431 var url = gitlabAPIURLBase + '/projects/' + projectId + '/issues/' + issueId;
433 return rest.put(url, {data: data})
434 .then(null, function(error) {
435 throw new Error('Failed to update issue in GitLab: ' + url + " " + JSON.stringify(error));
439 function closeIssue(issue, custom) {
440 var url = gitlabAPIURLBase + '/projects/' + issue.project_id + '/issues/' + issue.id;
441 var data = _.extend({
442 state_event: 'close',
443 private_token: gitlabAdminPrivateToken,
447 return rest.put(url, {data: data})
448 .then(null, function(error) {
449 throw new Error('Failed to close issue in GitLab: ' + url);
454 function log_progress(message) {
455 console.log(message.grey);