+function insertAndCloseIssue(issueId, data, close, custom) {
+
+ return insertIssue(gitLab.project.id, data).then(function(issue) {
+ gitLab.gitlabIssues[issue.iid] = issue;
+ if (close) {
+ return closeIssue(issue, custom && custom(issue)).then(
+ function() {
+ console.log((issue.iid + ': Inserted and closed successfully. #' + issue.iid).green);
+ }, function(error) {
+ console.warn((issue.iid + ': Inserted successfully but failed to close. #' + issue.iid).yellow);
+ });
+ }
+
+ console.log((issue.iid + ': Inserted successfully. #' + issue.iid).green);
+ }, function(error) {
+ console.error((issue.iid + ': Failed to insert.').red, error);
+ });
+}
+
+/**
+ * Fetch all existing project issues from GitLab - assigns gitLab.gitlabIssues
+ */
+function getGitLabProjectIssues() {
+ return getRemainingGitLabProjectIssues(0, 100)
+ .then(function(result) {
+ log_progress("Fetched " + result.length + " GitLab issues.");
+ var issues = _.indexBy(result, 'iid');
+ return gitLab.gitlabIssues = issues;
+ });
+}
+
+/**
+ * Recursively fetch the remaining issues in the project
+ * @param page
+ * @param per_page
+ */
+function getRemainingGitLabProjectIssues(page, per_page) {
+ var from = page * per_page;
+ log_progress("Fetching Project Issues from GitLab [" + (from + 1) + "-" + (from + per_page) + "]...");
+ var url = gitlabAPIURLBase + '/projects/' + gitLab.project.id + "/issues";
+ var data = {
+ page: page,
+ per_page: per_page,
+ private_token: gitlabAdminPrivateToken, sudo: gitlabSudo };
+
+ return rest.get(url, {data: data})
+ .fail(function(err) {
+ console.log(err);
+ })
+ .then(function(issues) {
+ if(issues.length < per_page) {
+ return issues;
+ }
+ return getRemainingGitLabProjectIssues(page+1, per_page)
+ .then(function(remainingIssues) {
+ return issues.concat(remainingIssues);
+ });
+ }, function(error) {
+ throw new Error('Cannot get list of issues from gitlab: ' + url + " page=" + page);
+ });
+}
+
+function getUserByMantisUsername(username) {
+ return (username && config.users[username]) || config.users[""] || null;