From 739bbfd60d7a4db6b21d68afc0b5c0af125b27de Mon Sep 17 00:00:00 2001 From: Hemal Patel Date: Tue, 4 Dec 2018 02:02:40 +0530 Subject: [PATCH 1/2] refactor: redefine log module job --- bin/good-first-issue.js | 8 ++++---- lib/log.js | 34 +++++++++++++++------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/bin/good-first-issue.js b/bin/good-first-issue.js index ed7243d..ac03cee 100755 --- a/bin/good-first-issue.js +++ b/bin/good-first-issue.js @@ -51,13 +51,13 @@ cli process.exit(0) } - // Call the log functionality, output the result to the console. - let output = await log(issues, projects[input].name) + let key = cmd.first ? 0 : Math.floor(Math.random() * Math.floor(issues.length - 1)) - let key = cmd.first ? 0 : Math.floor(Math.random() * Math.floor(output.length - 1)) + // Call the log functionality, output the result to the console. + let output = await log(issues[key], projects[input].name) // Log the issue! - console.log(output[key].toString()) + console.log(output.toString()) if (cmd.open) { opn(issues[key].url) diff --git a/lib/log.js b/lib/log.js index c981250..c6f9072 100644 --- a/lib/log.js +++ b/lib/log.js @@ -6,27 +6,23 @@ var boxenOptions = { borderColor: 'green', borderStyle: 'round' } -async function log (issues, project) { - var set = [] - for (var issue in issues) { - var data = { - leftpad: ' ', - doublepad: ' ', - header: 'Good First Issue in ' + chalk.yellow(project) + ': ', - title: chalk.green(issues[issue].title), - issue: chalk.cyan('#' + issues[issue].pr), - state: chalk.green(issues[issue].state), - unassigned: chalk.green('unassigned!'), // All assigned issues are being filtered out at the search level, so all issues will always be unassigned - link: chalk.cyan(issues[issue].url), - repo: chalk.green(issues[issue].url.toString().slice(19, issues[issue].url.toString().indexOf('/issue'))), - labels: issues[issue].labels - } +async function log (issue, project) { + const data = { + leftpad: ' ', + doublepad: ' ', + header: 'Good First Issue in ' + chalk.yellow(project) + ': ', + title: chalk.green(issue.title), + issue: chalk.cyan('#' + issue.pr), + state: chalk.green(issue.state), + unassigned: chalk.green('unassigned!'), // All assigned issues are being filtered out at the search level, so all issues will always be unassigned + link: chalk.cyan(issue.url), + repo: chalk.green(issue.url.toString().slice(19, issue.url.toString().indexOf('/issue'))), + labels: issue.labels + } - var output = '\n' + boxen(data.header + '\n\n' + ' - Title: ' + data.title + '\n' + ' - Repository: ' + data.repo + '\n' + ' - Issue: ' + data.issue + '\n' + ' - Status: ' + data.state + '\n' + ' - Assigned to: ' + data.unassigned + '\n\n' + 'Start now: ' + data.link, boxenOptions) + '\n' + const output = '\n' + boxen(data.header + '\n\n' + ' - Title: ' + data.title + '\n' + ' - Repository: ' + data.repo + '\n' + ' - Issue: ' + data.issue + '\n' + ' - Status: ' + data.state + '\n' + ' - Assigned to: ' + data.unassigned + '\n\n' + 'Start now: ' + data.link, boxenOptions) + '\n' - set.push(output) - } - return set + return output } module.exports = log From 58fd6df744a17b04788570e6cf47dc3c82cc1146 Mon Sep 17 00:00:00 2001 From: Hemal Patel Date: Tue, 4 Dec 2018 02:03:22 +0530 Subject: [PATCH 2/2] test: add test to validate log output structure --- lib/log.spec.js | 39 +++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lib/log.spec.js diff --git a/lib/log.spec.js b/lib/log.spec.js new file mode 100644 index 0000000..61510ac --- /dev/null +++ b/lib/log.spec.js @@ -0,0 +1,39 @@ +const log = require('./log') +const chalk = require('chalk') +const stripAnsi = require('strip-ansi'); + +test('should return formatted log', async () => { + const org = 'foojs' + const repo = 'barjs' + const issue = { + title: 'Add BAZ support', + pr: 123, + labels: [[Object], [Object], [Object]], + state: 'open', + repo: `https://api.github.com/${org}/${repo}`, + url: `https://github.com/${org}/${repo}/issues/123`, + assignee: null, + assignees: [], + locked: false + } + + const expectedOutput = ` +╭──────────────────────────────────────────────────────────╮ +│ │ +│ Good First Issue in foojs: │ +│ │ +│ - Title: Add BAZ support │ +│ - Repository: foojs/barjs │ +│ - Issue: #123 │ +│ - Status: open │ +│ - Assigned to: unassigned! │ +│ │ +│ Start now: https://github.com/foojs/barjs/issues/123 │ +│ │ +╰──────────────────────────────────────────────────────────╯ +` + + const actualOutput = await log(issue, org) + + expect(expectedOutput).toBe(stripAnsi(actualOutput)) +}) diff --git a/package.json b/package.json index d2fe9ff..bf7cd6f 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "devDependencies": { "jest": "^23.6.0", "markdown-magic": "^0.1.25", - "standard": "^12.0.1" + "standard": "^12.0.1", + "strip-ansi": "^5.0.0" } }