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

Skip updating an existing issue when update_existing=false. #112

Merged
merged 2 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ steps:
update_existing: true
```

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.
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.

### Outputs

Expand Down
41 changes: 28 additions & 13 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ 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 updateExisting = Boolean(tools.inputs.update_existing)
let updateExisting: Boolean | null = null
if (tools.inputs.update_existing) {
if (tools.inputs.update_existing === 'true') {
updateExisting = true
} else if (tools.inputs.update_existing === 'false') {
updateExisting = false
} else {
tools.exit.failure(`Invalid value update_existing=${tools.inputs.update_existing}, must be one of true or false`)
}
}
Comment on lines +12 to +21
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks for adding the validation check (and the test) 🔥 ✨

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

Expand All @@ -34,7 +43,7 @@ export async function createAnIssue (tools: Toolkit) {
}
tools.log.debug('Templates compiled', templated)

if (updateExisting) {
if (updateExisting !== null) {
let existingIssue
tools.log.info(`Fetching issues with title "${templated.title}"`)
try {
Expand All @@ -46,19 +55,25 @@ export async function createAnIssue (tools: Toolkit) {
tools.exit.failure(err)
}
if (existingIssue) {
try {
const issue = await tools.github.issues.update({
...tools.context.repo,
issue_number: existingIssue.number,
body: templated.body
})
setOutputs(tools, issue)
tools.exit.success(`Updated issue ${issue.data.title}#${issue.data.number}: ${issue.data.html_url}`)
} catch (err) {
tools.exit.failure(err)
if (updateExisting === false) {
tools.exit.success(`Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated`)
} else {
try {
tools.log.info(`Updating existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url}`)
const issue = await tools.github.issues.update({
...tools.context.repo,
issue_number: existingIssue.number,
body: templated.body
})
setOutputs(tools, issue)
tools.exit.success(`Updated issue ${existingIssue.title}#${issue.data.number}: ${issue.data.html_url}`)
} catch (err) {
tools.exit.failure(err)
}
}
} else {
tools.log.info('No existing issue found to update')
}
tools.log.info('No existing issue found to update')
}

// Create the new issue
Expand Down
26 changes: 26 additions & 0 deletions tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`create-an-issue checks the value of update_existing 1`] = `
Object {
"assignees": Array [
"octocat",
"JasonEtco",
],
"body": "Goodbye!",
"labels": Array [],
"milestone": 1,
"title": "Hello!",
}
`;

exports[`create-an-issue creates a new issue 1`] = `
Object {
"assignees": Array [],
Expand Down Expand Up @@ -184,6 +197,19 @@ Array [
]
`;

exports[`create-an-issue finds, but does not update an existing issue with the same title 1`] = `
Object {
"assignees": Array [
"octocat",
"JasonEtco",
],
"body": "Goodbye!",
"labels": Array [],
"milestone": 1,
"title": "Hello!",
}
`;

exports[`create-an-issue logs a helpful error if creating an issue throws an error 1`] = `
Array [
Array [
Expand Down
35 changes: 31 additions & 4 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,52 @@ describe('create-an-issue', () => {
expect(tools.log.success).toHaveBeenCalled()
})

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

await createAnIssue(tools)
expect(params).toMatchSnapshot()
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 () => {
nock.cleanAll()
nock('https://api.github.com')
.get(/\/search\/issues.*/).reply(200, {
items: [{ number: 1, title: 'Hello!' }]
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'
}
})
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(200, {})
process.env.INPUT_UPDATE_EXISTING = 'true'

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

it('finds, but does not update an existing 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' }]
})
process.env.INPUT_UPDATE_EXISTING = 'false'

await createAnIssue(tools)
expect(params).toMatchSnapshot()
expect(tools.exit.success).toHaveBeenCalledWith('Existing issue Hello!#1: /issues/1 found but not updated')
})

it('exits when updating an issue fails', async () => {
nock.cleanAll()
nock('https://api.github.com')
.get(/\/search\/issues.*/).reply(200, {
items: [{ number: 1, title: 'Hello!' }]
items: [{ number: 1, title: 'Hello!', html_url: '/issues/1' }]
})
.patch(/\/repos\/.*\/.*\/issues\/.*/).reply(500, {
message: 'Updating issue failed'
Expand Down