Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added search_existing param. #111

Merged
merged 6 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ steps:
assignees: JasonEtco, octocat
milestone: 1
update_existing: true
search_existing: all
```

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.
* 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.
* The `search_existing` param lets you specify whether to search `open`, `closed`, or `all` existing issues for duplicates (default is `open`).

### Outputs

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
update_existing:
description: Update an open existing issue with the same title if it exists
required: false
search_existing:
description: Existing types of issues to search for (comma-separated)
required: false
default: open
outputs:
number:
description: Number of the issue that was created
Expand Down
17 changes: 11 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { FrontMatterAttributes, listToArray, setOutputs } from './helpers'
export async function createAnIssue (tools: Toolkit) {
const template = tools.inputs.filename || '.github/ISSUE_TEMPLATE.md'
const assignees = tools.inputs.assignees
const searchExistingType = tools.inputs.search_existing || 'open'

let updateExisting: Boolean | null = null
if (tools.inputs.update_existing) {
if (tools.inputs.update_existing === 'true') {
Expand All @@ -19,6 +21,7 @@ export async function createAnIssue (tools: Toolkit) {
tools.exit.failure(`Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false`)
}
}

const env = nunjucks.configure({ autoescape: false })
env.addFilter('date', dateFilter)

Expand Down Expand Up @@ -48,7 +51,7 @@ export async function createAnIssue (tools: Toolkit) {
tools.log.info(`Fetching issues with title "${templated.title}"`)
try {
const existingIssues = await tools.github.search.issuesAndPullRequests({
q: `is:open is:issue repo:${process.env.GITHUB_REPOSITORY} in:title ${templated.title}`
q: `is:${searchExistingType} is:issue repo:${process.env.GITHUB_REPOSITORY} in:title ${templated.title}`
})
existingIssue = existingIssues.data.items.find(issue => issue.title === templated.title)
} catch (err) {
Expand All @@ -66,7 +69,7 @@ export async function createAnIssue (tools: Toolkit) {
body: templated.body
})
setOutputs(tools, issue)
tools.exit.success(`Updated issue ${existingIssue.title}#${issue.data.number}: ${issue.data.html_url}`)
tools.exit.success(`Updated issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`)
} catch (err) {
tools.exit.failure(err)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Array [
]
`;

exports[`create-an-issue updates an existing issue with the same title 1`] = `
exports[`create-an-issue updates an existing open issue with the same title 1`] = `
Object {
"assignees": Array [
"octocat",
Expand Down
51 changes: 41 additions & 10 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ describe('create-an-issue', () => {
expect(tools.log.success).toHaveBeenCalled()
})

it('updates an existing open issue with the same title', async () => {
nock.cleanAll()
nock('https://api.github.com')
.get(/\/search\/issues.*/)
.query(parsedQuery => {
const q = parsedQuery['q']
if (typeof(q) === 'string') {
const args = q.split(' ')
return (args.includes('is:open') || args.includes('is:closed'))
&& args.includes('is:issue')
} else {
return false
}
})
.reply(200, {
items: [{ number: 1, title: 'Hello!' }]
})
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(200, {})

process.env.INPUT_UPDATE_EXISTING = 'true'

await createAnIssue(tools)
expect(params).toMatchSnapshot()
expect(tools.exit.success).toHaveBeenCalled()
})

it('checks the value of update_existing', async () => {
process.env.INPUT_UPDATE_EXISTING = 'invalid'

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

it('updates an existing issue with the same title', async () => {
it('updates an existing closed issue with the same title', async () => {
nock.cleanAll()
nock('https://api.github.com')
.get(/\/search\/issues.*/).reply(200, {
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
})
.patch(/\/repos\/.*\/.*\/issues/).reply(200, (_, body: any) => {
return {
title: body.title,
number: 1,
html_url: '/issues/1'
.get(/\/search\/issues.*/)
.query(parsedQuery => {
const q = parsedQuery['q']
if (typeof(q) === 'string') {
const args = q.split(' ')
return args.includes('is:all') && args.includes('is:issue')
} else {
return false
}
})
.reply(200, {
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
})
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(200, {})

process.env.INPUT_UPDATE_EXISTING = 'true'
process.env.INPUT_SEARCH_EXISTING = 'all'

await createAnIssue(tools)
expect(params).toMatchSnapshot()
expect(tools.exit.success).toHaveBeenCalledWith('Updated issue Hello!#1: /issues/1')
})

Expand Down