Skip to content

Commit f003bdd

Browse files
committed
Add a test
1 parent a009c38 commit f003bdd

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

src/action.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ import nunjucks from 'nunjucks'
66
import dateFilter from 'nunjucks-date-filter'
77
import { FrontMatterAttributes, listToArray, setOutputs } from './helpers'
88

9+
function logError(tools: Toolkit, template: string, action: 'creating' | 'updating', err: any) {
10+
// Log the error message
11+
const errorMessage = `An error occurred while ${action} the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check ${template}!`
12+
tools.log.error(errorMessage)
13+
tools.log.error(err)
14+
15+
// The error might have more details
16+
if (err.errors) tools.log.error(err.errors)
17+
18+
// Exit with a failing status
19+
core.setFailed(errorMessage + '\n\n' + err.message)
20+
return tools.exit.failure()
21+
}
22+
923
export async function createAnIssue (tools: Toolkit) {
1024
const template = tools.inputs.filename || '.github/ISSUE_TEMPLATE.md'
1125
const assignees = tools.inputs.assignees
@@ -47,16 +61,11 @@ export async function createAnIssue (tools: Toolkit) {
4761
tools.log.debug('Templates compiled', templated)
4862

4963
if (updateExisting !== null) {
50-
let existingIssue
5164
tools.log.info(`Fetching issues with title "${templated.title}"`)
52-
try {
53-
const existingIssues = await tools.github.search.issuesAndPullRequests({
54-
q: `is:${searchExistingType} is:issue repo:${process.env.GITHUB_REPOSITORY} in:title ${templated.title}`
55-
})
56-
existingIssue = existingIssues.data.items.find(issue => issue.title === templated.title)
57-
} catch (err: any) {
58-
tools.exit.failure(err)
59-
}
65+
const existingIssues = await tools.github.search.issuesAndPullRequests({
66+
q: `is:${searchExistingType} is:issue repo:${process.env.GITHUB_REPOSITORY} in:title ${templated.title}`
67+
})
68+
const existingIssue = existingIssues.data.items.find(issue => issue.title === templated.title)
6069
if (existingIssue) {
6170
if (updateExisting === false) {
6271
tools.exit.success(`Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated`)
@@ -71,7 +80,7 @@ export async function createAnIssue (tools: Toolkit) {
7180
setOutputs(tools, issue)
7281
tools.exit.success(`Updated issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`)
7382
} catch (err: any) {
74-
tools.exit.failure(err)
83+
return logError(tools, template, 'updating', err)
7584
}
7685
}
7786
} else {
@@ -93,16 +102,6 @@ export async function createAnIssue (tools: Toolkit) {
93102
setOutputs(tools, issue)
94103
tools.log.success(`Created issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
95104
} catch (err: any) {
96-
// Log the error message
97-
const errorMessage = `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}!`
98-
tools.log.error(errorMessage)
99-
tools.log.error(err)
100-
101-
// The error might have more details
102-
if (err.errors) tools.log.error(err.errors)
103-
104-
// Exit with a failing status
105-
core.setFailed(errorMessage + '\n\n' + err.message)
106-
tools.exit.failure()
105+
return logError(tools, template, 'creating', err)
107106
}
108107
}

tests/__snapshots__/index.test.ts.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,24 @@ Array [
239239
]
240240
`;
241241

242+
exports[`create-an-issue logs a helpful error if updating an issue throws an error with more errors 1`] = `
243+
Array [
244+
Array [
245+
"An error occurred while updating the issue. This might be caused by a malformed issue title, or a typo in the labels or assignees. Check .github/ISSUE_TEMPLATE.md!",
246+
],
247+
Array [
248+
[HttpError: Validation error: {"foo":true}],
249+
],
250+
Array [
251+
Array [
252+
Object {
253+
"foo": true,
254+
},
255+
],
256+
],
257+
]
258+
`;
259+
242260
exports[`create-an-issue updates an existing open issue with the same title 1`] = `
243261
Object {
244262
"assignees": Array [

tests/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ describe('create-an-issue', () => {
240240
it('logs a helpful error if creating an issue throws an error', async () => {
241241
nock.cleanAll()
242242
nock('https://api.github.com')
243+
.get(/\/search\/issues.*/).reply(200, { items:[] })
243244
.post(/\/repos\/.*\/.*\/issues/).reply(500, {
244245
message: 'Validation error'
245246
})
@@ -253,6 +254,7 @@ describe('create-an-issue', () => {
253254
it('logs a helpful error if creating an issue throws an error with more errors', async () => {
254255
nock.cleanAll()
255256
nock('https://api.github.com')
257+
.get(/\/search\/issues.*/).reply(200, { items:[] })
256258
.post(/\/repos\/.*\/.*\/issues/).reply(500, {
257259
message: 'Validation error',
258260
errors: [{ foo: true }]
@@ -263,4 +265,22 @@ describe('create-an-issue', () => {
263265
expect((tools.log.error as any).mock.calls).toMatchSnapshot()
264266
expect(tools.exit.failure).toHaveBeenCalled()
265267
})
268+
269+
it('logs a helpful error if updating an issue throws an error with more errors', async () => {
270+
nock.cleanAll()
271+
nock('https://api.github.com')
272+
.get(/\/search\/issues.*/)
273+
.reply(200, { items: [{ number: 1, title: 'Hello!' }] })
274+
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(500, {
275+
message: 'Validation error',
276+
errors: [{ foo: true }]
277+
})
278+
279+
process.env.INPUT_UPDATE_EXISTING = 'true'
280+
281+
await createAnIssue(tools)
282+
expect(tools.log.error).toHaveBeenCalled()
283+
expect((tools.log.error as any).mock.calls).toMatchSnapshot()
284+
expect(tools.exit.failure).toHaveBeenCalled()
285+
})
266286
})

0 commit comments

Comments
 (0)