From bec2a1bd3c52a98c9366e5b474026d90c4866a65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Hollander?= Date: Sun, 18 Dec 2022 11:27:32 +0100 Subject: [PATCH] feat: create_if_not_exists (#173) --- .github/workflows/ci.yaml | 9 ++++++++- README.md | 1 + action.yml | 3 +++ lib/index.js | 7 ++++++- src/main.ts | 8 +++++++- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f6249b67..650e999d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -26,4 +26,11 @@ jobs: filePath: README.md comment_tag: nrt_file reactions: eyes, rocket - mode: recreate \ No newline at end of file + mode: recreate + + - name: Do not comment PR if not exists + uses: ./ + with: + message: Should not be printed + comment_tag: nrt_create_if_not_exists + create_if_not_exists: false \ No newline at end of file diff --git a/README.md b/README.md index d4009462..cf3a9277 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ Note: the input `mode` can be used to either `upsert` (by default) or `recreate` | `pr_number` | The number of the pull request where to create the comment | | current pull-request/issue number (deduced from context) | | `comment_tag` | A tag on your comment that will be used to identify a comment in case of replacement | | | | `mode` | Mode that will be used to update comment (upsert/recreate) | | upsert | +| `create_if_not_exists` | Whether a comment should be created even if `comment_tag` is not found | | true | ## Contributing diff --git a/action.yml b/action.yml index 9b0eb2b5..41f9a78f 100644 --- a/action.yml +++ b/action.yml @@ -21,6 +21,9 @@ inputs: mode: description: 'Mode that will be used to update comment (upsert/recreate)' default: 'upsert' + create_if_not_exists: + description: 'Whether a comment should be created even if comment_tag is not found.' + default: 'true' runs: using: 'node16' main: 'lib/index.js' diff --git a/lib/index.js b/lib/index.js index 5a490ef0..e9dbaa90 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9554,6 +9554,7 @@ async function run() { const comment_tag = core.getInput('comment_tag'); const reactions = core.getInput('reactions'); const mode = core.getInput('mode'); + const create_if_not_exists = core.getInput('create_if_not_exists') === 'true'; if (!message && !filePath) { core.setFailed('Either "filePath" or "message" should be provided as input'); return; @@ -9631,9 +9632,13 @@ async function run() { return; } } - else { + else if (create_if_not_exists) { core.info('No comment has been found with asked pattern. Creating a new comment.'); } + else { + core.info('Not creating comment as the pattern has not been found. Use `create_if_not_exists: true` to create a new comment anyway.'); + return; + } } const { data: comment } = await octokit.rest.issues.createComment({ ...context.repo, diff --git a/src/main.ts b/src/main.ts index 9115936f..ee9c3057 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,6 +17,7 @@ async function run() { const comment_tag: string = core.getInput('comment_tag'); const reactions: string = core.getInput('reactions'); const mode: string = core.getInput('mode'); + const create_if_not_exists: boolean = core.getInput('create_if_not_exists') === 'true'; if (!message && !filePath) { core.setFailed('Either "filePath" or "message" should be provided as input'); @@ -109,8 +110,13 @@ async function run() { core.setFailed(`Mode ${mode} is unknown. Please use 'upsert' or 'recreate'.`); return; } - } else { + } else if (create_if_not_exists) { core.info('No comment has been found with asked pattern. Creating a new comment.'); + } else { + core.info( + 'Not creating comment as the pattern has not been found. Use `create_if_not_exists: true` to create a new comment anyway.', + ); + return; } }