From e34108e1dc024e420964632c171e120540f0cf0e Mon Sep 17 00:00:00 2001 From: Olivier Jacques Date: Wed, 19 Jul 2017 14:07:10 +0200 Subject: [PATCH 1/5] Add GitHub Enterprise support --- index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 74ffff5..a4800ba 100644 --- a/index.js +++ b/index.js @@ -10,13 +10,21 @@ const fs = require('fs'); const Bottleneck = require("bottleneck"); program - .version('0.1.0') + .version('0.2.0') .arguments('') - .option('-t, --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; @@ -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 From cad69e3de7f4f69a127d237ac7e1f134f9bc13f8 Mon Sep 17 00:00:00 2001 From: Olivier Jacques Date: Wed, 19 Jul 2017 14:22:56 +0200 Subject: [PATCH 2/5] Add support of assignee --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index a4800ba..68e9983 100644 --- a/index.js +++ b/index.js @@ -70,6 +70,7 @@ program var bodyIndex = cols.indexOf('description'); var labelsIndex = cols.indexOf('labels'); var milestoneIndex = cols.indexOf('milestone'); + var assigneeIndex = cols.indexOf('assignee'); if (titleIndex === -1) { console.error('Title required by GitHub, but not found in CSV.'); @@ -96,6 +97,11 @@ program if (milestoneIndex > -1 && row[milestoneIndex] !== '') { sendObj.milestone = row[milestoneIndex]; } + + // 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) { From cd5e9378ad30e02865ca7d25a8594f69c2dd9914 Mon Sep 17 00:00:00 2001 From: Olivier Jacques Date: Wed, 19 Jul 2017 14:24:30 +0200 Subject: [PATCH 3/5] Slower rate for GitHub API requests --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 68e9983..a28f31b 100644 --- a/index.js +++ b/index.js @@ -45,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", From 70adbb18ba47df6fd0d666590169e011faed587b Mon Sep 17 00:00:00 2001 From: Olivier Jacques Date: Wed, 19 Jul 2017 14:27:07 +0200 Subject: [PATCH 4/5] Add support of closed issues with 2 steps process --- index.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index a28f31b..cf39c8a 100644 --- a/index.js +++ b/index.js @@ -71,6 +71,8 @@ program 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.'); @@ -102,12 +104,32 @@ program 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 (limiter.nbQueued() === 0) { - process.exit(0); + + 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); + } + } } }); }); From 7c0be68dbfb0ef19378b85536dcf12f89e0e45d5 Mon Sep 17 00:00:00 2001 From: Olivier Jacques Date: Wed, 19 Jul 2017 14:31:59 +0200 Subject: [PATCH 5/5] Add test files --- test/3.csv | 3 +++ test/4.csv | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 test/3.csv create mode 100644 test/4.csv diff --git a/test/3.csv b/test/3.csv new file mode 100644 index 0000000..88586d6 --- /dev/null +++ b/test/3.csv @@ -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 diff --git a/test/4.csv b/test/4.csv new file mode 100644 index 0000000..4c8f1cc --- /dev/null +++ b/test/4.csv @@ -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