Skip to content

Commit

Permalink
feat: create_if_not_exists (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
thollander authored Dec 18, 2022
1 parent cc09859 commit bec2a1b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ jobs:
filePath: README.md
comment_tag: nrt_file
reactions: eyes, rocket
mode: recreate
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit bec2a1b

Please sign in to comment.