diff --git a/index.js b/index.js index da84979..29696a8 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,9 @@ const GitHubApi = require("github"); const Promise = require('bluebird'); -const readlineSync = require('readline-sync'); -const organization = process.argv[2]; -const labelName = process.argv[3]; -const labelColor = process.argv[4]; - -const username = readlineSync.question('Enter your Github Username: '); -const password = readlineSync.question('Enter your Github Password: ', { hideEchoBack: true }); - -if(!organization || !labelName || !labelColor) { - return; -} +const inquirer = require('inquirer'); const github = new GitHubApi({ - debug: true, + debug: false, protocol: "https", host: "api.github.com", pathPrefix: "", @@ -25,12 +15,6 @@ const github = new GitHubApi({ timeout: 5000 }); -github.authenticate({ - type: "basic", - username: username, - password: password -}); - Promise.promisifyAll(github.issues); Promise.promisifyAll(github.repos); @@ -54,7 +38,7 @@ function getIssues(repository, owner) { return github.issues.getForRepo({ owner: owner, repo: repository.name - }); + }).each(issue => issue.repo = repository); } function addLabels(repository, issue, owner, labels) { @@ -66,9 +50,86 @@ function addLabels(repository, issue, owner, labels) { }); } -getRepositories(organization) - .each(repository => createLabel(repository, organization, labelName, labelColor)) - .each(repository => { - getIssues(repository, organization) - .each(issue => addLabels(repository, issue, organization, [labelName])) - }); +Promise.coroutine(function*() { + const {username, password} = yield inquirer.prompt([ + { + type: 'input', + name: 'username', + message: 'Enter your GitHub username:' + }, + { + type: 'password', + name: 'password', + message: 'Enter your GitHub password:' + } + ]); + + // This is synchronous + github.authenticate({ + type: "basic", + username: username, + password: password + }); + + const {owner} = yield inquirer.prompt([ + { + type: 'input', + name: 'owner', + message: 'Enter the owner of the repositories:' + } + ]); + + const repositories = yield getRepositories(owner); + const {selectedRepositories} = yield inquirer.prompt([ + { + type: 'checkbox', + name: 'selectedRepositories', + choices: repositories.map(repo => ({name: repo.name, value: repo})), + message: 'Please select the repositories:' + } + ]); + + const issues = yield Promise.all(selectedRepositories.map(repo => getIssues(repo, owner))) + .reduce((all, current) => all.concat(current)); + + const {selectedIssues} = yield inquirer.prompt([ + { + type: 'checkbox', + name: 'selectedIssues', + choices: issues.map(issue => ({name: issue.title, value: issue})), + message: 'Please select the issues:' + } + ]); + + if(selectedIssues.length === 0) + return; + + const label = yield inquirer.prompt([ + { + type: 'input', + name: 'name', + message: 'Please enter the label name:' + }, + { + type: 'input', + name: 'color', + message: 'Please select the color:', + validate: color => /^[0-9a-fA-F]{6}$/.test(color) || 'Input a color as a 6 digit hex code.' + } + ]); + + yield Promise.resolve(selectedRepositories) + .each(repo => createLabel(repo, owner, label.name, label.color)); + + yield Promise.resolve(selectedIssues) + .each(issue => addLabels(issue.repo, issue, owner, [label.name])); +})(); + +/* +then(() => getRepositories(organization)) + .each() + .each(repository => { + getIssues(repository, organization) + .each(issue => addLabels(repository, issue, organization, [labelName])) + }); + */ diff --git a/package.json b/package.json index df2bb31..cf4453f 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "gh-label": "./index.js" }, "scripts": { + "start": "node index.js", "test": "mocha" }, "keywords": [ @@ -20,8 +21,8 @@ "bluebird": "^3.4.6", "chai": "^3.5.0", "github": "^5.2.2", - "mocha": "^3.1.2", - "readline-sync": "^1.4.4" + "inquirer": "^1.2.2", + "mocha": "^3.1.2" }, "devDependencies": { "shelljs": "^0.7.4"