From ae43a7876610225549098bdad19b855404f9243e Mon Sep 17 00:00:00 2001 From: dblock Date: Tue, 21 Sep 2021 11:02:34 -0400 Subject: [PATCH 1/2] Skip updating an existing issue when update_existing=false. --- README.md | 2 +- src/action.ts | 32 +++++++++++++++----------- tests/__snapshots__/index.test.ts.snap | 13 +++++++++++ tests/index.test.ts | 27 ++++++++++++++++++---- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index f1d154c..0cad246 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/action.ts b/src/action.ts index 6a6fc1f..4dd019e 100644 --- a/src/action.ts +++ b/src/action.ts @@ -9,7 +9,7 @@ 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) + const updateExisting: Boolean | null = tools.inputs.update_existing ? tools.inputs.update_existing === 'true' : null const env = nunjucks.configure({ autoescape: false }) env.addFilter('date', dateFilter) @@ -34,7 +34,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 { @@ -46,19 +46,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) { + 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 diff --git a/tests/__snapshots__/index.test.ts.snap b/tests/__snapshots__/index.test.ts.snap index d75013c..e244e42 100644 --- a/tests/__snapshots__/index.test.ts.snap +++ b/tests/__snapshots__/index.test.ts.snap @@ -184,6 +184,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 [ diff --git a/tests/index.test.ts b/tests/index.test.ts index 34b16f0..1e8e326 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -155,21 +155,40 @@ describe('create-an-issue', () => { 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' From 3eebcfd1b2549f53a369ab79696f198f48eb985b Mon Sep 17 00:00:00 2001 From: dblock Date: Tue, 21 Sep 2021 11:07:48 -0400 Subject: [PATCH 2/2] Check value of update_existing for true/false. --- src/action.ts | 13 +++++++++++-- tests/__snapshots__/index.test.ts.snap | 13 +++++++++++++ tests/index.test.ts | 8 ++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/action.ts b/src/action.ts index 4dd019e..2eb5048 100644 --- a/src/action.ts +++ b/src/action.ts @@ -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 | null = tools.inputs.update_existing ? tools.inputs.update_existing === 'true' : null + 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`) + } + } const env = nunjucks.configure({ autoescape: false }) env.addFilter('date', dateFilter) @@ -46,7 +55,7 @@ export async function createAnIssue (tools: Toolkit) { tools.exit.failure(err) } if (existingIssue) { - if (! updateExisting) { + if (updateExisting === false) { tools.exit.success(`Existing issue ${existingIssue.title}#${existingIssue.number}: ${existingIssue.html_url} found but not updated`) } else { try { diff --git a/tests/__snapshots__/index.test.ts.snap b/tests/__snapshots__/index.test.ts.snap index e244e42..afc91ea 100644 --- a/tests/__snapshots__/index.test.ts.snap +++ b/tests/__snapshots__/index.test.ts.snap @@ -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 [], diff --git a/tests/index.test.ts b/tests/index.test.ts index 1e8e326..3f18922 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -151,6 +151,14 @@ 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')