forked from gavinr/github-csv-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (94 loc) · 3.88 KB
/
Copy pathindex.js
File metadata and controls
106 lines (94 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
/* jshint esversion: 6 */
const program = require('commander');
const co = require('co');
const prompt = require('co-prompt');
const GitHubApi = require('github');
const csv = require('csv');
const fs = require('fs');
const Bottleneck = require("bottleneck");
program
.version('0.1.0')
.arguments('<file>')
.option('-t, --token <token>', 'The GitHub token. https://github.com/settings/tokens')
.action(function(file) {
co(function*() {
var retObject = {};
retObject.token = yield prompt('token (get from https://github.com/settings/tokens): ');
retObject.userOrOrganization = yield prompt('user or organization: ');
retObject.repo = yield prompt('repo: ');
return retObject;
}).then(function(values) {
var github = new GitHubApi({
// required
version: '3.0.0',
// optional
debug: true,
protocol: 'https',
host: 'api.github.com',
timeout: 5000,
headers: {
'user-agent': 'My-Cool-GitHub-App' // GitHub is happy with a unique user agent
}
});
// abuse rate limits apply for concurrent content creation
// requests by a single GitHub user.
var limiter = new Bottleneck(20,200);
// OAuth2
github.authenticate({
type: "oauth",
token: values.token
});
fs.readFile(file, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file.');
process.exit(1);
}
csv.parse(data, {
trim: true
}, (err, csvRows) => {
if (err) throw err;
var cols = csvRows[0];
csvRows.shift();
// get indexes of the fields we need
var titleIndex = cols.indexOf('title');
var bodyIndex = cols.indexOf('description');
var labelsIndex = cols.indexOf('labels');
var milestoneIndex = cols.indexOf('milestone');
if (titleIndex === -1) {
console.error('Title required by GitHub, but not found in CSV.');
process.exit(1);
}
csvRows.forEach((row) => {
var sendObj = {
user: values.userOrOrganization,
repo: values.repo,
title: row[titleIndex]
};
// if we have a body column, pass that.
if (bodyIndex > -1) {
sendObj.body = row[bodyIndex];
}
// if we have a labels column, pass that.
if (labelsIndex > -1 && row[labelsIndex] !== '') {
sendObj.labels = row[labelsIndex].split(',');
}
// if we have a milestone column, pass that.
if (milestoneIndex > -1 && row[milestoneIndex] !== '') {
sendObj.milestone = row[milestoneIndex];
}
limiter.submit(github.issues.create,sendObj, function(err, res)
{
// debugging: console.log(JSON.stringify(res));
if (limiter.nbQueued() === 0) {
process.exit(0);
}
});
});
});
});
}, function(err) {
console.error('ERROR', err);
});
})
.parse(process.argv);