Skip to content

Commit

Permalink
feat: mode for updating comment (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
thollander committed Dec 14, 2022
1 parent bd2e0c9 commit 73d56db
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Expand Up @@ -17,4 +17,5 @@ jobs:
Current branch is `${{ github.head_ref }}`.
_(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
comment_tag: nrt
reactions: eyes, rocket
reactions: eyes, rocket
mode: recreate
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -88,6 +88,7 @@ _That is particularly interesting while committing multiple times in a PR and th
| `reactions` | List of reactions for the comment (comma separated). See https://docs.github.com/en/rest/reactions#reaction-types | | |
| `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 |

## Contributing

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Expand Up @@ -17,6 +17,9 @@ inputs:
description: 'Manual pull request number'
comment_tag:
description: 'A tag on your comment that will be used to identify a comment in case of replacement.'
mode:
description: 'Mode that will be used to update comment (upsert/recreate)'
default: 'upsert'
runs:
using: 'node16'
main: 'lib/index.js'
34 changes: 27 additions & 7 deletions lib/index.js
Expand Up @@ -9547,6 +9547,7 @@ async function run() {
const pr_number = core.getInput('pr_number');
const comment_tag = core.getInput('comment_tag');
const reactions = core.getInput('reactions');
const mode = core.getInput('mode');
const context = github.context;
const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number;
const octokit = github.getOctokit(github_token);
Expand Down Expand Up @@ -9582,13 +9583,32 @@ async function run() {
break;
}
if (comment) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body,
});
await addReactions(comment.id, reactions);
return;
if (mode === 'upsert') {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body,
});
await addReactions(comment.id, reactions);
return;
}
else if (mode === 'recreate') {
await octokit.rest.issues.deleteComment({
...context.repo,
comment_id: comment.id,
});
const { data: newComment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body,
});
await addReactions(newComment.id, reactions);
return;
}
else {
core.setFailed(`Mode ${mode} is unknown. Please use 'upsert' or 'recreate'.`);
return;
}
}
else {
core.info('No comment has been found with asked pattern. Creating a new comment.');
Expand Down
34 changes: 27 additions & 7 deletions src/main.ts
Expand Up @@ -13,6 +13,7 @@ async function run() {
const pr_number: string = core.getInput('pr_number');
const comment_tag: string = core.getInput('comment_tag');
const reactions: string = core.getInput('reactions');
const mode: string = core.getInput('mode');

const context = github.context;
const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number;
Expand Down Expand Up @@ -60,13 +61,32 @@ async function run() {
}

if (comment) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body,
});
await addReactions(comment.id, reactions);
return;
if (mode === 'upsert') {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body,
});
await addReactions(comment.id, reactions);
return;
} else if (mode === 'recreate') {
await octokit.rest.issues.deleteComment({
...context.repo,
comment_id: comment.id,
});

const { data: newComment } = await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body,
});

await addReactions(newComment.id, reactions);
return;
} else {
core.setFailed(`Mode ${mode} is unknown. Please use 'upsert' or 'recreate'.`);
return;
}
} else {
core.info('No comment has been found with asked pattern. Creating a new comment.');
}
Expand Down

0 comments on commit 73d56db

Please sign in to comment.