|
| 1 | +var GitHubApi = require("github"); |
| 2 | +var Gitlab = require('gitlab'); |
| 3 | +var async = require('async'); |
| 4 | + |
| 5 | +var settings = require('./settings.json'); |
| 6 | +console.log(settings); |
| 7 | + |
| 8 | +var gitlab = Gitlab({ |
| 9 | + url: settings.gitlab.url, |
| 10 | + token: settings.gitlab.token |
| 11 | +}); |
| 12 | + |
| 13 | +if (settings.gitlab.projectID === null) { |
| 14 | + gitlab.projects.all(function(projects) { |
| 15 | + projects = projects.sort(function(a, b) { |
| 16 | + return a.id - b.id; |
| 17 | + }); |
| 18 | + for (var i = 0; i < projects.length; i++) { |
| 19 | + console.log(projects[i].id, projects[i].description, projects[i].name); |
| 20 | + } |
| 21 | + console.log('\n\n'); |
| 22 | + console.log('Select which project ID should be transported to github. Edit the settings.json accordingly. (gitlab.projectID)'); |
| 23 | + console.log('\n\n'); |
| 24 | + }); |
| 25 | +} else { |
| 26 | + // user has choosen a project |
| 27 | + |
| 28 | + |
| 29 | + var github = new GitHubApi({ |
| 30 | + // required |
| 31 | + version: "3.0.0", |
| 32 | + // optional |
| 33 | + debug: true, |
| 34 | + protocol: "https", |
| 35 | + host: "api.github.com", |
| 36 | + pathPrefix: "", |
| 37 | + timeout: 5000, |
| 38 | + headers: { |
| 39 | + "user-agent": "node-gitlab-2-github" // GitHub is happy with a unique user agent |
| 40 | + } |
| 41 | + }); |
| 42 | + github.authenticate({ |
| 43 | + type: "basic", |
| 44 | + username: settings.github.username, |
| 45 | + password: settings.github.password |
| 46 | + }); |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + // TODO check whether user has created all milestones on github |
| 53 | + |
| 54 | + gitlab.projects.milestones.list(settings.gitlab.projectID, function(data) { |
| 55 | + data = data.sort(function(a, b) { |
| 56 | + return a.id - b.id; |
| 57 | + }); |
| 58 | + github.issues.getAllMilestones({ |
| 59 | + user: settings.github.username, |
| 60 | + repo: settings.github.repo |
| 61 | + }, function(err, milestoneDataOpen) { |
| 62 | + github.issues.getAllMilestones({ |
| 63 | + user: settings.github.username, |
| 64 | + repo: settings.github.repo, |
| 65 | + state: 'closed' |
| 66 | + }, function(err, milestoneDataClosed) { |
| 67 | + milestoneData = milestoneDataClosed.concat(milestoneDataOpen); |
| 68 | + milestoneDataMapped = milestoneData.map(function(item) { |
| 69 | + return item.title; |
| 70 | + }); |
| 71 | + |
| 72 | + console.log('\n\n\n\n\n\n\n>>>>'); |
| 73 | + console.log(milestoneDataMapped); |
| 74 | + console.log('\n\n\n\n\n\n\n'); |
| 75 | + console.log(milestoneDataClosed[0]); |
| 76 | + |
| 77 | + console.log('\n\n\n\n\n\n\n'); |
| 78 | + async.each(data, function(item, cb) { |
| 79 | + if (milestoneDataMapped.indexOf(item.title) < 0) { |
| 80 | + console.log('Creating new Milestone', item.title); |
| 81 | + createMilestone(item, function(err, createMilestoneData) { |
| 82 | + console.log(createMilestoneData); |
| 83 | + cb(err); |
| 84 | + }); |
| 85 | + } else { |
| 86 | + cb(err); |
| 87 | + } |
| 88 | + }, function(err) { |
| 89 | + if (err) return console.log(err); |
| 90 | + // all milestones are created |
| 91 | + createAllIssuesAndComments(milestoneData); |
| 92 | + |
| 93 | + |
| 94 | + }); // async |
| 95 | + |
| 96 | + }); // closed Issues |
| 97 | + }); // opend issues |
| 98 | + }); // gitlab list milestones |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +function createAllIssuesAndComments(milestoneData) { |
| 103 | + // select all issues and comments from this project |
| 104 | + gitlab.projects.issues.list(settings.gitlab.projectID, function(issueData) { |
| 105 | + // 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); |
| 136 | + } else { |
| 137 | + cb(err); |
| 138 | + } |
| 139 | + }); |
| 140 | + }, function(err) { |
| 141 | + console.log('error with issueData:', err); |
| 142 | + }); |
| 143 | + |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +function findMileStoneforTitle(milestoneData, title) { |
| 148 | + for (var i = milestoneData.length - 1; i >= 0; i--) { |
| 149 | + if (milestoneData[i].title == title) { |
| 150 | + console.log(milestoneData[i].number); |
| 151 | + return milestoneData[i].number; |
| 152 | + } |
| 153 | + } |
| 154 | + return null; |
| 155 | +} |
| 156 | + |
| 157 | +function createAllIssueComments(projectID, issueID, newIssueData, cb) { |
| 158 | + // get all comments add them to the comment |
| 159 | + gitlab.issues.notes.all(projectID, issueID, function(data) { |
| 160 | + 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 | + } |
| 170 | + } |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +function createMilestone(data, cb) { |
| 176 | + github.issues.createMilestone({ |
| 177 | + username: settings.github.username, |
| 178 | + repo: settings.github.repo, |
| 179 | + title: data.title, |
| 180 | + description: data.description, |
| 181 | + state: (data.state === 'active') ? 'open' : 'closed', |
| 182 | + due_on: data.due_date + 'T00:00:00Z' |
| 183 | + }, cb); |
| 184 | +} |
0 commit comments