Skip to content

Commit 1ff4fb0

Browse files
authored
Merge branch 'main' into search-existing
2 parents 9930430 + f74c314 commit 1ff4fb0

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ steps:
9191
```
9292

9393
* The `assignees` and `milestone` speak for themselves.
94-
* The `update_existing` param can be passed and set to `true` when you want to update an open issue with the **exact same title** when it exists.
94+
* The `update_existing` param can be passed and set to `true` when you want to update an open issue with the **exact same title** when it exists and `false` if you don't want to create a new issue, but skip updating an existing one.
9595
* The `search_existing` param lets you specify whether to search `open`, `closed`, or `all` existing issues for duplicates (default is `open`).
9696

9797
### Outputs

src/action.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ import { FrontMatterAttributes, listToArray, setOutputs } from './helpers'
99
export async function createAnIssue (tools: Toolkit) {
1010
const template = tools.inputs.filename || '.github/ISSUE_TEMPLATE.md'
1111
const assignees = tools.inputs.assignees
12-
const updateExisting = Boolean(tools.inputs.update_existing)
1312
const searchExistingType = tools.inputs.search_existing || 'open'
13+
14+
let updateExisting: Boolean | null = null
15+
if (tools.inputs.update_existing) {
16+
if (tools.inputs.update_existing === 'true') {
17+
updateExisting = true
18+
} else if (tools.inputs.update_existing === 'false') {
19+
updateExisting = false
20+
} else {
21+
tools.exit.failure(`Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false`)
22+
}
23+
}
24+
1425
const env = nunjucks.configure({ autoescape: false })
1526
env.addFilter('date', dateFilter)
1627

@@ -35,7 +46,7 @@ export async function createAnIssue (tools: Toolkit) {
3546
}
3647
tools.log.debug('Templates compiled', templated)
3748

38-
if (updateExisting) {
49+
if (updateExisting !== null) {
3950
let existingIssue
4051
tools.log.info(`Fetching issues with title "${templated.title}"`)
4152
try {
@@ -47,19 +58,25 @@ export async function createAnIssue (tools: Toolkit) {
4758
tools.exit.failure(err)
4859
}
4960
if (existingIssue) {
50-
try {
51-
const issue = await tools.github.issues.update({
52-
...tools.context.repo,
53-
issue_number: existingIssue.number,
54-
body: templated.body
55-
})
56-
setOutputs(tools, issue)
57-
tools.exit.success(`Updated issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
58-
} catch (err) {
59-
tools.exit.failure(err)
61+
if (updateExisting === false) {
62+
tools.exit.success(`Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated`)
63+
} else {
64+
try {
65+
tools.log.info(`Updating existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`)
66+
const issue = await tools.github.issues.update({
67+
...tools.context.repo,
68+
issue_number: existingIssue.number,
69+
body: templated.body
70+
})
71+
setOutputs(tools, issue)
72+
tools.exit.success(`Updated issue ${existingIssue.title}#${issue.data.number}: ${issue.data.html_url}`)
73+
} catch (err) {
74+
tools.exit.failure(err)
75+
}
6076
}
77+
} else {
78+
tools.log.info('No existing issue found to update')
6179
}
62-
tools.log.info('No existing issue found to update')
6380
}
6481

6582
// Create the new issue

tests/__snapshots__/index.test.ts.snap

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`create-an-issue checks the value of update_existing 1`] = `
4+
Object {
5+
"assignees": Array [
6+
"octocat",
7+
"JasonEtco",
8+
],
9+
"body": "Goodbye!",
10+
"labels": Array [],
11+
"milestone": 1,
12+
"title": "Hello!",
13+
}
14+
`;
15+
316
exports[`create-an-issue creates a new issue 1`] = `
417
Object {
518
"assignees": Array [],
@@ -184,6 +197,19 @@ Array [
184197
]
185198
`;
186199

200+
exports[`create-an-issue finds, but does not update an existing issue with the same title 1`] = `
201+
Object {
202+
"assignees": Array [
203+
"octocat",
204+
"JasonEtco",
205+
],
206+
"body": "Goodbye!",
207+
"labels": Array [],
208+
"milestone": 1,
209+
"title": "Hello!",
210+
}
211+
`;
212+
187213
exports[`create-an-issue logs a helpful error if creating an issue throws an error 1`] = `
188214
Array [
189215
Array [

tests/index.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ describe('create-an-issue', () => {
176176
expect(params).toMatchSnapshot()
177177
expect(tools.exit.success).toHaveBeenCalled()
178178
})
179+
180+
it('checks the value of update_existing', async () => {
181+
process.env.INPUT_UPDATE_EXISTING = 'invalid'
182+
183+
await createAnIssue(tools)
184+
expect(params).toMatchSnapshot()
185+
expect(tools.exit.failure).toHaveBeenCalledWith('Invalid value update_existing=invalid, must be one of true or false')
186+
})
179187

180188
it('updates an existing closed issue with the same title', async () => {
181189
nock.cleanAll()
@@ -201,14 +209,27 @@ describe('create-an-issue', () => {
201209

202210
await createAnIssue(tools)
203211
expect(params).toMatchSnapshot()
204-
expect(tools.exit.success).toHaveBeenCalled()
212+
expect(tools.exit.success).toHaveBeenCalledWith('Updated issue Hello!#1: /issues/1')
213+
})
214+
215+
it('finds, but does not update an existing issue with the same title', async () => {
216+
nock.cleanAll()
217+
nock('https://api.github.com')
218+
.get(/\/search\/issues.*/).reply(200, {
219+
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
220+
})
221+
process.env.INPUT_UPDATE_EXISTING = 'false'
222+
223+
await createAnIssue(tools)
224+
expect(params).toMatchSnapshot()
225+
expect(tools.exit.success).toHaveBeenCalledWith('Existing issue Hello!#1: /issues/1 found but not updated')
205226
})
206227

207228
it('exits when updating an issue fails', async () => {
208229
nock.cleanAll()
209230
nock('https://api.github.com')
210231
.get(/\/search\/issues.*/).reply(200, {
211-
items: [{ number: 1, title: 'Hello!' }]
232+
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
212233
})
213234
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(500, {
214235
message: 'Updating issue failed'

0 commit comments

Comments
 (0)