Initial commit.
authorStepan Riha <github@nonplus.net>
Fri, 23 Oct 2015 03:13:33 +0000 (22:13 -0500)
committerStepan Riha <github@nonplus.net>
Fri, 23 Oct 2015 03:13:33 +0000 (22:13 -0500)
.gitignore [new file with mode: 0755]
LICENSE [new file with mode: 0755]
README.md [new file with mode: 0755]
m2gl.js [new file with mode: 0755]
package.json [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100755 (executable)
index 0000000..b512c09
--- /dev/null
@@ -0,0 +1 @@
+node_modules
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100755 (executable)
index 0000000..8445543
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,9 @@
+Copyright (c) 2015 Stepan Riha\r
+\r
+Based on https://github.com/soheilpro/youtrack2gitlab\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\r
+\r
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100755 (executable)
index 0000000..8ce9577
--- /dev/null
+++ b/README.md
@@ -0,0 +1,82 @@
+# YouTrack2GitLab
+Import YouTrack issues into GitLab.
+
+## Install
+
+```
+npm install -g youtrack2gitlab
+```
+
+## Usage
+
+```
+yt2gl -i <input> -u <users> -g <gitlaburl> -p <project> -t <token>
+```
+
+## Options
+
+```
+-i, --input
+ CSV file exported from YouTrack (Example: issues.csv)
+
+-u, --users
+ User mapping file (Example: users.json)
+
+-g, --gitlaburl
+ GitLab URL hostname (Example: gitlab.example.com)
+
+-p, --project
+ GitLab project name including namespace (Example: mycorp/myproj)
+
+-t, --token
+ An admin user's private token (Example: a2r33oczFyQzq53t23Vj)
+```
+
+## User Mapping File
+In order to correctly map users you should create a JSON file with the following format and specify it with the **-u** switch:
+
+```
+[
+  {
+    "yt_username": "USER'S USERNAME IN YOUTRACK",
+    "yt_name": "USER'S NAME IN YOUTRACK",
+    "gl_username": "USER'S USERNAME IN GITLAB",
+    "gl_private_token": "USER'S PRIVATE TOKEN IN GITLAB"
+  },
+  …
+]
+```
+
+## Notes
+- Make sure the input CSV file only includes issues for the project you want to import.
+- Make sure that all users have write access to the specified repository or some issues will fail to import. A safer approach is to set repository's **Visibility Level** to **Public** and revert it when the import process is complete.
+- In version 6.4.3, GitLab API does not support setting creation date of issues. So all imported issues will have a creation time of now.
+- In version 6.4.3, GitLab API fails to import issues with very long titles.
+- In version 6.4.3, GitLab does not allow issues to be deleted. So be careful when importing issues into an active project.
+- Milestones are not currently supported.
+
+## Version History
++ **1.0**
+       + Initial release
+
+## Author
+**Soheil Rashidi**
+
++ http://soheilrashidi.com
++ http://twitter.com/soheilpro
++ http://github.com/soheilpro
+
+## Copyright and License
+Copyright 2014 Soheil Rashidi
+
+Licensed under the The MIT License (the "License");
+you may not use this work except in compliance with the License.
+You may obtain a copy of the License in the LICENSE file, or at:
+
+http://www.opensource.org/licenses/mit-license.php
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/m2gl.js b/m2gl.js
new file mode 100755 (executable)
index 0000000..042fa7e
--- /dev/null
+++ b/m2gl.js
@@ -0,0 +1,337 @@
+#!/usr/bin/env node
+
+var fs = require('fs');
+var util = require('util');
+var colors = require('colors');
+var csv = require('csv');
+var rest = require('restler');
+var async = require('async');
+var _ = require('lodash');
+var argv = require('optimist')
+    .demand(['i', 'c', 'g', 'p', 't', 's'])
+    .alias('i', 'input')
+    .alias('c', 'config')
+    .alias('g', 'gitlaburl')
+    .alias('p', 'project')
+    .alias('t', 'token')
+    .alias('s', 'sudo')
+    .describe('i', 'CSV file exported from Mantis (Example: issues.csv)')
+    .describe('c', 'Configuration file (Example: config.json)')
+    .describe('g', 'GitLab URL hostname (Example: https://gitlab.com)')
+    .describe('p', 'GitLab project name including namespace (Example: mycorp/myproj)')
+    .describe('t', 'An admin user\'s private token (Example: a2r33oczFyQzq53t23Vj)')
+    .describe('s', 'The username performing the import (Example: bob)')
+    .argv;
+
+var inputFile = __dirname + '/' + argv.input;
+var configFile = __dirname + '/' + argv.config;
+var gitlabAPIURLBase = argv.gitlaburl + '/api/v3';
+var gitlabProjectName = argv.project;
+var gitlabAdminPrivateToken = argv.token;
+var gitlabSudo = argv.sudo;
+var config = {};
+
+getGitLabProject(gitlabProjectName, gitlabAdminPrivateToken, function(error, project) {
+  if (error) {
+    console.error('Error: Cannot get list of projects from gitlab: ' + gitlabAPIURLBase);
+    return;
+  }
+
+  if (!project) {
+    console.error('Error: Cannot find GitLab project: ' + gitlabProjectName);
+    return;
+  }
+
+  getGitLabUsers(gitlabAdminPrivateToken, function(error, gitlabUsers) {
+    if (error) {
+      console.error('Error: Cannot get list of users from gitlab: ' + gitlabAPIURLBase);
+      return;
+    }
+
+    getConfig(configFile, function(error, cfg) {
+      if (error) {
+        console.error('Error: Cannot read config file: ' + configFile);
+        return;
+      }
+
+      config = cfg;
+
+      var users = config.users;
+
+      setGitLabUserIds(users, gitlabUsers);
+
+      readRows(inputFile, function(error, rows) {
+        if (error) {
+          console.error('Error: Cannot read input file: ' + inputFile);
+          return;
+        }
+
+        validate(rows, users, function(missingUsernames, missingNames) {
+          if (missingUsernames.length > 0 || missingNames.length > 0) {
+            for (var i = 0; i < missingUsernames.length; i++)
+              console.error('Error: Cannot map Mantis user with username: ' + missingUsernames[i]);
+
+            for (var i = 0; i < missingNames.length; i++)
+              console.error('Error: Cannot map Mantis user with name: ' + missingNames[i]);
+
+            return;
+          }
+
+          rows = _.sortBy(rows, function(row) { return Date.parse(row.Created); });
+
+          async.eachSeries(rows, function(row, callback) {
+            var issueId = row.Id;
+            var title = row.Summary;
+            var description = getDescription(row);
+            var assignee = getUserByMantisUsername(users, row["Assigned To"]);
+            var milestoneId = '';
+            var labels = getLabels(row);
+            var author = getUserByMantisUsername(users, row.Reporter);
+
+            insertIssue(project.id, title, description, assignee && assignee.gl_id, milestoneId, labels, author.gl_username, gitlabAdminPrivateToken, function(error, issue) {
+              setTimeout(callback, 1000);
+
+              if (error) {
+                console.error((issueId + ': Failed to insert.').red, error);
+                return;
+              }
+
+              if (isClosed(row)) {
+                closeIssue(issue, assignee.gl_private_token || gitlabAdminPrivateToken, function(error) {
+                  if (error)
+                    console.warn((issueId + ': Inserted successfully but failed to close. #' + issue.iid).yellow);
+                  else
+                    console.error((issueId + ': Inserted and closed successfully. #' + issue.iid).green);
+                });
+
+                return;
+              }
+
+              console.log((issueId + ': Inserted successfully. #' + issue.iid).green);
+            });
+          });
+        });
+      });
+    });
+  });
+})
+
+function getGitLabProject(name, privateToken, callback) {
+  var url = gitlabAPIURLBase + '/projects';
+  var data = { per_page: 100, private_token: privateToken, sudo: gitlabSudo };
+
+  rest.get(url, {data: data}).on('complete', function(result, response) {
+    if (util.isError(result)) {
+      callback(result);
+      return;
+    }
+
+    if (response.statusCode != 200) {
+      callback(result);
+      return;
+    }
+
+    for (var i = 0; i < result.length; i++) {
+      if (result[i].path_with_namespace === name) {
+        callback(null, result[i]);
+        return;
+      }
+    };
+
+    callback(null, null);
+  });
+}
+
+function getGitLabUsers(privateToken, callback) {
+  var url = gitlabAPIURLBase + '/users';
+  var data = { per_page: 100, private_token: privateToken, sudo: gitlabSudo };
+
+  rest.get(url, {data: data}).on('complete', function(result, response) {
+    if (util.isError(result)) {
+      callback(result);
+      return;
+    }
+
+    if (response.statusCode != 200) {
+      callback(result);
+      return;
+    }
+
+    callback(null, result);
+  });
+}
+
+function getConfig(configFile, callback) {
+  fs.readFile(configFile, {encoding: 'utf8'}, function(error, data) {
+    if (error) {
+      callback(error);
+      return;
+    }
+
+    var config = JSON.parse(data);
+    config.users = config.users || [];
+
+    callback(null, config);
+  });
+}
+
+function setGitLabUserIds(users, gitlabUsers) {
+  for (var i = 0; i < users.length; i++) {
+    for (var j = 0; j < gitlabUsers.length; j++) {
+      if (users[i].gl_username === gitlabUsers[j].username) {
+        users[i].gl_id = gitlabUsers[j].id;
+        break;
+      }
+    }
+  }
+}
+
+function readRows(inputFile, callback) {
+  fs.readFile(inputFile, {encoding: 'utf8'}, function(error, data) {
+    if (error) {
+      callback(error);
+      return;
+    }
+
+    var rows = [];
+
+    csv().from(data, {delimiter: ',', escape: '"', columns: true})
+    .on('record', function(row, index) { rows.push(row) })
+    .on('end', function() { callback(null, rows) });
+  });
+}
+
+function validate(rows, users, callback) {
+  var missingUsername = [];
+  var missingNames = [];
+
+  for (var i = 0; i < rows.length; i++) {
+    var assignee = rows[i]["Assigned To"];
+
+    if (!getUserByMantisUsername(users, assignee) && missingUsername.indexOf(assignee) == -1)
+      missingUsername.push(assignee);
+  }
+
+  for (var i = 0; i < rows.length; i++) {
+    var reporter = rows[i].Reporter;
+
+    if (!getUserByMantisUsername(users, reporter) && missingNames.indexOf(reporter) == -1)
+      missingNames.push(reporter);
+  }
+
+  callback(missingUsername, missingNames);
+}
+
+function getUserByMantisUsername(users, username) {
+  return (username && _.find(users, {username: username || null })) || null;
+}
+
+function getDescription(row) {
+  var attributes = [];
+  var issueId = row.Id;
+  var value;
+  if (config.mantisUrl) {
+    attributes.push("[Mantis Issue " + issueId + "](" + config.mantisUrl + "/view.php?id=" + issueId + ")");
+  } else {
+    attributes.push("Mantis Issue " + issueId);
+  }
+
+  if (value = row.Reporter) {
+    attributes.push("Reported By: " + value);
+  }
+
+  if (value = row["Assigned To"]) {
+    attributes.push("Assigned To: " + value);
+  }
+
+  if (value = row.Created) {
+    attributes.push("Created: " + value);
+  }
+
+  if (value = row.Updated && value != row.Created) {
+    attributes.push("Updated: " + value);
+  }
+
+  var description = "_" + attributes.join(", ") + "_\n\n";
+
+  description += row.Description;
+
+  if (value = row.Info) {
+    description += "\n\n" + value;
+  }
+
+  return description;
+}
+
+function getLabels(row) {
+  var label;
+  var labels = (row.tags || []).slice(0);
+
+  if(label = config.category_labels[row.Category]) {
+    labels.push(label);
+  }
+
+  if(label = config.priority_labels[row.Priority]) {
+    labels.push(label);
+  }
+
+  if(label = config.severity_labels[row.Severity]) {
+    labels.push(label);
+  }
+
+  return labels.join(",");
+}
+
+function isClosed(row) {
+  return config.closed_statuses[row.Status];
+}
+
+function insertIssue(projectId, title, description, assigneeId, milestoneId, labels, creatorId, privateToken, callback) {
+  var url = gitlabAPIURLBase + '/projects/' + projectId + '/issues';
+  var data = {
+    title: title,
+    description: description,
+    assignee_id: assigneeId,
+    milestone_id: milestoneId,
+    labels: labels,
+    sudo: creatorId,
+    private_token: privateToken
+  };
+
+  rest.post(url, {data: data}).on('complete', function(result, response) {
+    if (util.isError(result)) {
+      callback(result);
+      return;
+    }
+
+    if (response.statusCode != 201) {
+      callback(result);
+      return;
+    }
+
+    callback(null, result);
+  });
+}
+
+function closeIssue(issue, privateToken, callback) {
+  var url = gitlabAPIURLBase + '/projects/' + issue.project_id + '/issues/' + issue.id;
+  var data = {
+    state_event: 'close',
+    private_token: privateToken,
+    sudo: gitlabSudo
+  };
+
+  rest.put(url, {data: data}).on('complete', function(result, response) {
+    if (util.isError(result)) {
+      callback(result);
+      return;
+    }
+
+    if (response.statusCode != 200) {
+      callback(result);
+      return;
+    }
+
+    callback(null);
+  });
+}
diff --git a/package.json b/package.json
new file mode 100644 (file)
index 0000000..44158ef
--- /dev/null
@@ -0,0 +1,37 @@
+{
+  "name": "mantis2gitlab",
+  "version": "1.0.0",
+  "description": "Import Mantis issues into GitLab.",
+  "dependencies": {
+    "async": "~0.2.9",
+    "colors": "~0.6.2",
+    "csv": "~0.3.6",
+    "lodash": "^3.10.1",
+    "optimist": "~0.6.0",
+    "restler": "~3.1.0"
+  },
+  "devDependencies": {},
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "preferGlobal": true,
+  "main": "m2gl.js",
+  "bin": {
+    "m2gl": "./m2gl.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/nonplus/mantis2gitlab"
+  },
+  "bugs": {
+    "url": "https://github.com/nonplus/mantis2gitlab/issues"
+  },
+  "keywords": [
+    "gitlab",
+    "mantis",
+    "issue"
+  ],
+  "author": "Stepan Riha",
+  "license": "MIT",
+  "readmeFilename": "README.md"
+}