Skip to content

Commit 1e68dcc

Browse files
authored
Merge pull request #111 from dblock/search-existing
Added search_existing param.
2 parents f74c314 + 4c25743 commit 1e68dcc

File tree

6 files changed

+66
-20
lines changed

6 files changed

+66
-20
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,12 @@ steps:
8787
assignees: JasonEtco, octocat
8888
milestone: 1
8989
update_existing: true
90+
search_existing: all
9091
```
9192

92-
The `assignees` and `milestone` speak for themselves, 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.
93+
* 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 and `false` if you don't want to create a new issue, but skip updating an existing one.
95+
* The `search_existing` param lets you specify whether to search `open`, `closed`, or `all` existing issues for duplicates (default is `open`).
9396

9497
### Outputs
9598

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ inputs:
2020
update_existing:
2121
description: Update an open existing issue with the same title if it exists
2222
required: false
23+
search_existing:
24+
description: Existing types of issues to search for (comma-separated)
25+
required: false
26+
default: open
2327
outputs:
2428
number:
2529
description: Number of the issue that was created

package-lock.json

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/action.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ 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 searchExistingType = tools.inputs.search_existing || 'open'
13+
1214
let updateExisting: Boolean | null = null
1315
if (tools.inputs.update_existing) {
1416
if (tools.inputs.update_existing === 'true') {
@@ -19,6 +21,7 @@ export async function createAnIssue (tools: Toolkit) {
1921
tools.exit.failure(`Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false`)
2022
}
2123
}
24+
2225
const env = nunjucks.configure({ autoescape: false })
2326
env.addFilter('date', dateFilter)
2427

@@ -48,7 +51,7 @@ export async function createAnIssue (tools: Toolkit) {
4851
tools.log.info(`Fetching issues with title "${templated.title}"`)
4952
try {
5053
const existingIssues = await tools.github.search.issuesAndPullRequests({
51-
q: `is:open is:issue repo:${process.env.GITHUB_REPOSITORY} in:title ${templated.title}`
54+
q: `is:${searchExistingType} is:issue repo:${process.env.GITHUB_REPOSITORY} in:title ${templated.title}`
5255
})
5356
existingIssue = existingIssues.data.items.find(issue => issue.title === templated.title)
5457
} catch (err) {
@@ -66,7 +69,7 @@ export async function createAnIssue (tools: Toolkit) {
6669
body: templated.body
6770
})
6871
setOutputs(tools, issue)
69-
tools.exit.success(`Updated issue ${existingIssue.title}#${issue.data.number}: ${issue.data.html_url}`)
72+
tools.exit.success(`Updated issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`)
7073
} catch (err) {
7174
tools.exit.failure(err)
7275
}

tests/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Array [
239239
]
240240
`;
241241

242-
exports[`create-an-issue updates an existing issue with the same title 1`] = `
242+
exports[`create-an-issue updates an existing open issue with the same title 1`] = `
243243
Object {
244244
"assignees": Array [
245245
"octocat",

tests/index.test.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,32 @@ describe('create-an-issue', () => {
151151
expect(tools.log.success).toHaveBeenCalled()
152152
})
153153

154+
it('updates an existing open issue with the same title', async () => {
155+
nock.cleanAll()
156+
nock('https://api.github.com')
157+
.get(/\/search\/issues.*/)
158+
.query(parsedQuery => {
159+
const q = parsedQuery['q']
160+
if (typeof(q) === 'string') {
161+
const args = q.split(' ')
162+
return (args.includes('is:open') || args.includes('is:closed'))
163+
&& args.includes('is:issue')
164+
} else {
165+
return false
166+
}
167+
})
168+
.reply(200, {
169+
items: [{ number: 1, title: 'Hello!' }]
170+
})
171+
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(200, {})
172+
173+
process.env.INPUT_UPDATE_EXISTING = 'true'
174+
175+
await createAnIssue(tools)
176+
expect(params).toMatchSnapshot()
177+
expect(tools.exit.success).toHaveBeenCalled()
178+
})
179+
154180
it('checks the value of update_existing', async () => {
155181
process.env.INPUT_UPDATE_EXISTING = 'invalid'
156182

@@ -159,23 +185,28 @@ describe('create-an-issue', () => {
159185
expect(tools.exit.failure).toHaveBeenCalledWith('Invalid value update_existing=invalid, must be one of true or false')
160186
})
161187

162-
it('updates an existing issue with the same title', async () => {
188+
it('updates an existing closed issue with the same title', async () => {
163189
nock.cleanAll()
164190
nock('https://api.github.com')
165-
.get(/\/search\/issues.*/).reply(200, {
166-
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
167-
})
168-
.patch(/\/repos\/.*\/.*\/issues/).reply(200, (_, body: any) => {
169-
return {
170-
title: body.title,
171-
number: 1,
172-
html_url: '/issues/1'
191+
.get(/\/search\/issues.*/)
192+
.query(parsedQuery => {
193+
const q = parsedQuery['q']
194+
if (typeof(q) === 'string') {
195+
const args = q.split(' ')
196+
return args.includes('is:all') && args.includes('is:issue')
197+
} else {
198+
return false
173199
}
174200
})
201+
.reply(200, {
202+
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
203+
})
204+
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(200, {})
205+
175206
process.env.INPUT_UPDATE_EXISTING = 'true'
207+
process.env.INPUT_SEARCH_EXISTING = 'all'
176208

177209
await createAnIssue(tools)
178-
expect(params).toMatchSnapshot()
179210
expect(tools.exit.success).toHaveBeenCalledWith('Updated issue Hello!#1: /issues/1')
180211
})
181212

0 commit comments

Comments
 (0)