Skip to content

Commit

Permalink
Merge pull request #13 from JasonEtco/helpful-error
Browse files Browse the repository at this point in the history
Throw helpful error when issue creation fails
  • Loading branch information
JasonEtco committed May 3, 2019
2 parents 5c1234b + fed92b6 commit 306d0d8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,27 @@ Toolkit.run(async tools => {
tools.log.info(`Creating new issue ${templated.title}`)

// Create the new issue
const issue = await tools.github.issues.create({
...tools.context.repo,
...templated,
assignees: listToArray(attributes.assignees),
labels: listToArray(attributes.labels),
milestone: attributes.milestone
})

tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
try {
const issue = await tools.github.issues.create({
...tools.context.repo,
...templated,
assignees: listToArray(attributes.assignees),
labels: listToArray(attributes.labels),
milestone: attributes.milestone
})

tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
} catch (err) {
// Log the error message
tools.log.error(`An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check ${template}!`)
tools.log.error(err)

// The error might have more details
if (err.errors) tools.log.error(err.errors)

// Exit with a failing status
tools.exit.failure()
}
}, {
secrets: ['GITHUB_TOKEN']
})
29 changes: 29 additions & 0 deletions tests/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,32 @@ Array [
],
]
`;

exports[`create-an-issue logs a helpful error if creating an issue throws an error 1`] = `
Array [
Array [
"An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check .github/ISSUE_TEMPLATE.md!",
],
Array [
[HttpError: Validation error],
],
]
`;

exports[`create-an-issue logs a helpful error if creating an issue throws an error with more errors 1`] = `
Array [
Array [
"An error occurred while creating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check .github/ISSUE_TEMPLATE.md!",
],
Array [
[HttpError: Validation error],
],
Array [
Array [
Object {
"foo": true,
},
],
],
]
`;
30 changes: 29 additions & 1 deletion tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('create-an-issue', () => {
info: jest.fn(),
success: jest.fn(),
warn: jest.fn(),
debug: jest.fn()
debug: jest.fn(),
error: jest.fn()
}
})

Expand Down Expand Up @@ -70,4 +71,31 @@ describe('create-an-issue', () => {
expect(tools.log.success).toHaveBeenCalled()
expect(tools.log.success.mock.calls).toMatchSnapshot()
})

it('logs a helpful error if creating an issue throws an error', async () => {
nock.cleanAll()
nock('https://api.github.com')
.post(/\/repos\/.*\/.*\/issues/).reply(500, {
message: 'Validation error'
})

await actionFn(tools)
expect(tools.log.error).toHaveBeenCalled()
expect(tools.log.error.mock.calls).toMatchSnapshot()
expect(tools.exit.failure).toHaveBeenCalled()
})

it('logs a helpful error if creating an issue throws an error with more errors', async () => {
nock.cleanAll()
nock('https://api.github.com')
.post(/\/repos\/.*\/.*\/issues/).reply(500, {
message: 'Validation error',
errors: [{ foo: true }]
})

await actionFn(tools)
expect(tools.log.error).toHaveBeenCalled()
expect(tools.log.error.mock.calls).toMatchSnapshot()
expect(tools.exit.failure).toHaveBeenCalled()
})
})

0 comments on commit 306d0d8

Please sign in to comment.