Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 50 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ const fs = require('fs');
const Bottleneck = require("bottleneck");

program
.version('0.1.0')
.version('0.2.0')
.arguments('<file>')
.option('-t, --token <token>', 'The GitHub token. https://github.com/settings/tokens')
.action(function(file) {
.option('--github_enterprise [github.my-company.com]')
.option('--token [token]', 'The GitHub token. https://github.com/settings/tokens')
.action(function(file, options) {
co(function*() {
var retObject = {};
retObject.token = yield prompt('token (get from https://github.com/settings/tokens): ');
retObject.githubUrl = options.github_enterprise || "github.com";
if (retObject.githubUrl != 'github.com') {
retObject.pathPrefix = "/api/v3"
}
retObject.token = options.token || "";
if (retObject.token == "") {
retObject.token = yield prompt('token (get from https://' + retObject.githubUrl + '/settings/tokens): ');
};
retObject.userOrOrganization = yield prompt('user or organization: ');
retObject.repo = yield prompt('repo: ');
return retObject;
Expand All @@ -25,9 +33,10 @@ program
// required
version: '3.0.0',
// optional
pathPrefix: values.pathPrefix,
debug: true,
protocol: 'https',
host: 'api.github.com',
host: values.githubUrl,
timeout: 5000,
headers: {
'user-agent': 'My-Cool-GitHub-App' // GitHub is happy with a unique user agent
Expand All @@ -36,8 +45,8 @@ program

// abuse rate limits apply for concurrent content creation
// requests by a single GitHub user.
var limiter = new Bottleneck(20,200);

var limiter = new Bottleneck(5,500);
// OAuth2
github.authenticate({
type: "oauth",
Expand All @@ -61,6 +70,9 @@ program
var bodyIndex = cols.indexOf('description');
var labelsIndex = cols.indexOf('labels');
var milestoneIndex = cols.indexOf('milestone');
var assigneeIndex = cols.indexOf('assignee');
var stateIndex = cols.indexOf('state');
var issueNumber = 0;

if (titleIndex === -1) {
console.error('Title required by GitHub, but not found in CSV.');
Expand All @@ -87,12 +99,37 @@ program
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);

// if we have an assignee column, pass that.
if (assigneeIndex > -1 && row[assigneeIndex] !== '') {
sendObj.assignee = row[assigneeIndex];
}

limiter.submit(github.issues.create,sendObj, function(err, res) {
// Debugging console.log(JSON.stringify(res));
if (err) {console.error('ERROR', err)} else {
console.log("===> Created issue #" + res.number)
// if we have a state column and state=closed, close the issue
if (stateIndex > -1 && row[stateIndex] == 'closed') {
console.log("===> Closing issue #" + res.number);
var updateIssue = {
user: values.userOrOrganization,
repo: values.repo,
number: res.number,
state: row[stateIndex]
};
limiter.submit(github.issues.edit, updateIssue, function(err,res) {
// Debugging console.log(JSON.stringify(res));
if (err) {console.error('ERROR', err)};
if (limiter.nbQueued() === 0 && limiter.nbRunning() === 0) {
process.exit(0);
}
});
} else {
if (limiter.nbQueued() === 0 && limiter.nbRunning() === 0) {
process.exit(0);
}
}
}
});
});
Expand Down
3 changes: 3 additions & 0 deletions test/3.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title,description,labels,state
Test 1, This is the test1 desc, bug,open
Test 2, This is the test2 desc, "question,bug",closed
3 changes: 3 additions & 0 deletions test/4.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title,description,labels,assignee
Test 1, This is the test1 desc, bug,github-user
Test 2, This is the test2 desc, "question,bug",github-user