Skip to content
Open
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
20 changes: 8 additions & 12 deletions bin/good-first-issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const packageJSON = require('../package.json')
const log = require('../lib/log')
const prompt = require('../lib/prompt')
const projects = require('../data/projects.json')
const handleGfiResponse = require('../lib/handleGfiResponse')

cli
.version(packageJSON.version, '-v, --version')
Expand All @@ -35,22 +36,17 @@ cli

try {
const issues = await gfi(input, options)
const result = await handleGfiResponse(issues, input, projects, project, cmd, log)

if (issues.length === 0) {
process.exitCode = 0
return console.log(chalk.yellow(`\nNo Good First Issues were found for the GitHub organization, repo, or project ${chalk.white(input)}.\n`))
if (result.message) {
process.exitCode = result.code
return console.log(chalk.yellow(`\n${result.message}\n`))
}

const key = cmd.first ? 0 : Math.floor(Math.random() * Math.floor(issues.length - 1))

// Call the log functionality, output the result to the console.
const output = await log(issues[key], (input in projects) ? projects[input].name : project)

// Log the issue!
console.log(output.toString())
console.log(result.output)

if (cmd.open) {
opn(issues[key].url)
if (cmd.open && result.url) {
opn(result.url)
process.exitCode = 0
}
} catch (err) {
Expand Down
19 changes: 19 additions & 0 deletions lib/handleGfiResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
async function handleGfiResponse (issues, input, projects, project, cmd, log) {
if (!Array.isArray(issues)) {
throw new Error(
typeof issues === 'string' && issues.trim() !== ''
? `API Error: ${issues}`
: 'Unexpected response format from gfi()'
)
}

if (issues.length === 0) {
return { message: `No Good First Issues were found for the GitHub organization, repo, or project ${input}.`, code: 0 }
}

const key = cmd.first ? 0 : Math.floor(Math.random() * Math.floor(issues.length - 1))
const output = await log(issues[key], (input in projects) ? projects[input].name : project)
return { output: output.toString(), url: issues[key].url }
}

module.exports = handleGfiResponse
35 changes: 35 additions & 0 deletions tests/handleGfiResponse.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const handleGfiResponse = require('../lib/handleGfiResponse')

const fakeLog = jest.fn(async () => 'LOGGED')

const projects = { foo: { name: 'Foo' } }
const project = 'foo'
const input = 'foo'
const cmd = { first: false }

describe('handleGfiResponse', () => {
it('throws on string error response', async () => {
await expect(
handleGfiResponse('API rate limit exceeded', input, projects, project, cmd, fakeLog)
).rejects.toThrow(/API Error: API rate limit exceeded/)
})

it('throws on unexpected response type', async () => {
await expect(
handleGfiResponse({ not: 'an array' }, input, projects, project, cmd, fakeLog)
).rejects.toThrow(/Unexpected response format/)
})

it('returns message if no issues', async () => {
const result = await handleGfiResponse([], input, projects, project, cmd, fakeLog)
expect(result.message).toMatch(/No Good First Issues were found/)
expect(result.code).toBe(0)
})

it('returns output and url for valid issues', async () => {
const issues = [{ url: 'http://example.com' }]
const result = await handleGfiResponse(issues, input, projects, project, cmd, fakeLog)
expect(result.output).toBe('LOGGED')
expect(result.url).toBe('http://example.com')
})
})