|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const program = require('commander'); |
| 4 | +const co = require('co'); |
| 5 | +const prompt = require('co-prompt'); |
| 6 | +const GitHubApi = require('github'); |
| 7 | +const csv = require('csv'); |
| 8 | +const fs = require('fs'); |
| 9 | + |
| 10 | +program |
| 11 | + .version('0.1.0') |
| 12 | + .arguments('<file>') |
| 13 | + .option('-t, --token <token>', 'The GitHub token. https://github.com/settings/tokens') |
| 14 | + .action(function(file) { |
| 15 | + co(function*() { |
| 16 | + var retObject = {}; |
| 17 | + retObject.token = yield prompt('token (get from https://github.com/settings/tokens): '); |
| 18 | + retObject.userOrOrganization = yield prompt('user or organization: '); |
| 19 | + retObject.repo = yield prompt('repo: '); |
| 20 | + return retObject; |
| 21 | + }).then(function(values) { |
| 22 | + var github = new GitHubApi({ |
| 23 | + // required |
| 24 | + version: '3.0.0', |
| 25 | + // optional |
| 26 | + debug: true, |
| 27 | + protocol: 'https', |
| 28 | + host: 'api.github.com', |
| 29 | + timeout: 5000, |
| 30 | + headers: { |
| 31 | + 'user-agent': 'My-Cool-GitHub-App' // GitHub is happy with a unique user agent |
| 32 | + } |
| 33 | + }); |
| 34 | + // OAuth2 |
| 35 | + github.authenticate({ |
| 36 | + type: "oauth", |
| 37 | + token: values.token |
| 38 | + }); |
| 39 | + |
| 40 | + fs.readFile(file, 'utf8', (err, data) => { |
| 41 | + if (err) { |
| 42 | + console.error('Error reading file.'); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + csv.parse(data, { |
| 46 | + trim: true |
| 47 | + }, (err, csvRows) => { |
| 48 | + if (err) throw err; |
| 49 | + var cols = csvRows[0]; |
| 50 | + csvRows.shift(); |
| 51 | + |
| 52 | + // get indexes of the fields we need |
| 53 | + var titleIndex = cols.indexOf('title'); |
| 54 | + var bodyIndex = cols.indexOf('description'); |
| 55 | + var labelsIndex = cols.indexOf('labels'); |
| 56 | + |
| 57 | + if (titleIndex === -1) { |
| 58 | + console.error('Title required by GitHub, but not found in CSV.'); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + csvRows.forEach((row) => { |
| 62 | + var sendObj = { |
| 63 | + user: values.userOrOrganization, |
| 64 | + repo: values.repo, |
| 65 | + title: row[titleIndex] |
| 66 | + }; |
| 67 | + |
| 68 | + // if we have a body column, pass that. |
| 69 | + if (bodyIndex > -1) { |
| 70 | + sendObj.body = row[bodyIndex]; |
| 71 | + } |
| 72 | + |
| 73 | + // if we have a labels column, pass that. |
| 74 | + if (labelsIndex > -1) { |
| 75 | + sendObj.labels = row[labelsIndex].split(','); |
| 76 | + } |
| 77 | + |
| 78 | + github.issues.create(sendObj, function(err, res) |
| 79 | + { |
| 80 | + // debugging: console.log(JSON.stringify(res)); |
| 81 | + process.exit(0); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + }, function(err) { |
| 88 | + console.error('ERROR', err); |
| 89 | + }); |
| 90 | + }) |
| 91 | + .parse(process.argv); |
0 commit comments