Skip to content

Commit

Permalink
feat: manage delete modes in a better way
Browse files Browse the repository at this point in the history
BREAKING CHANGE: delete now deletes a comment immediately.
To delete the comment at the end of the job, use `delete-on-completion`

Co-authored-by: mlahargou <michaellahargou@gmail.com>
  • Loading branch information
thollander and mlahargou committed Oct 9, 2024
1 parent ce644a4 commit 107ab45
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 137 deletions.
40 changes: 27 additions & 13 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,66 @@ jobs:

- name: Comment PR with message
uses: ./
id: nrt_message
id: nrt-message
with:
message: |
Current branch is `${{ github.head_ref }}`.
_(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
comment_tag: nrt_message
comment-tag: nrt-message
reactions: eyes, rocket
mode: recreate

- name: Create a comment to delete it after
uses: ./
with:
message: |
This message will be deleted
comment-tag: nrt-message-delete
reactions: eyes

- name: Delete comment
uses: ./
with:
comment-tag: nrt-message-delete
mode: delete

- name: Comment PR with message that will be deleted
uses: ./
with:
message: |
This PR is being built...
comment_tag: nrt_message_delete
comment-tag: nrt-message-delete-on-completion
reactions: eyes
mode: delete
mode: delete-on-completion

- name: Comment PR with file
uses: ./
with:
filePath: README.md
comment_tag: nrt_file
file-path: README.md
comment-tag: nrt-file
reactions: eyes, rocket
mode: recreate

- name: Comment PR with file (absolute path)
uses: ./
with:
filePath: /tmp/foobar.txt
comment_tag: nrt_file_absolute
file-path: /tmp/foobar.txt
comment-tag: nrt-file-absolute
reactions: eyes, rocket
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
comment-tag: nrt-create-if-not-exists
create-if-not-exists: false

- name: Check outputs
run: |
echo "id : ${{ steps.nrt_message.outputs.id }}"
echo "body : ${{ steps.nrt_message.outputs.body }}"
echo "html_url : ${{ steps.nrt_message.outputs.html_url }}"
echo "id : ${{ steps.nrt-message.outputs.id }}"
echo "body : ${{ steps.nrt-message.outputs.body }}"
echo "html-url : ${{ steps.nrt-message.outputs.html-url }}"
- name: (AFTER) Setup test cases
run: rm /tmp/foobar.txt
51 changes: 32 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: actions/checkout@v3

- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
message: |
Hello world ! :wave:
Expand All @@ -35,7 +35,7 @@ You can either pass an absolute filePath or a relative one that will be by defau

```yml
- name: PR comment with file
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
filePath: /path/to/file.txt
```
Expand All @@ -48,7 +48,7 @@ It takes only valid reactions and adds it to the comment you've just created. (S

```yml
- name: PR comment with reactions
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
message: |
Hello world ! :wave:
Expand All @@ -63,7 +63,7 @@ That is particularly useful for manual workflow for instance (`workflow_run`).
```yml
...
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
message: |
Hello world ! :wave:
Expand All @@ -83,7 +83,7 @@ _That is particularly interesting while committing multiple times in a PR and th
```yml
...
- name: Comment PR with execution number
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
message: |
_(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
Expand All @@ -94,20 +94,33 @@ Note: the input `mode` can be used to either `upsert` (by default) or `recreate`

### Delete a comment

Deleting an existing comment is also possible thanks to the `comment_tag` input combined with `mode: delete`.

Deleting a comment with a specific `comment_tag` is possible with the `mode: delete`. If a comment with the `comment_tag` exists, it will be deleted when ran.

```yml
...
- name: Delete a comment
uses: thollander/actions-comment-pull-request@v3
with:
comment_tag: to_delete
mode: delete
```

### Delete a comment on job completion

Deleting an existing comment on job completion is also possible thanks to the `comment_tag` input combined with `mode: delete-on-completion`.

This will delete the comment at the end of the job.

```yml
...
- name: Write a comment that will be deleted at the end of the job
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
with:
message: |
The PR is being built...
comment_tag: to_delete
mode: delete
comment_tag: to_delete_on_completion
mode: delete-on-completion
```

## Inputs
Expand All @@ -116,14 +129,14 @@ This will delete the comment at the end of the job.

| Name | Description | Required | Default |
| --- | --- | --- | --- |
| `GITHUB_TOKEN` | Token that is used to create comments. Defaults to ${{ github.token }} | ✅ | |
| `github-token` | Token that is used to create comments. Defaults to ${{ github.token }} | ✅ | |
| `message` | Comment body | | |
| `filePath` | Path of the file that should be commented | | |
| `file-path` | Path of the file that should be commented | | |
| `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/delete) | | upsert |
| `create_if_not_exists` | Whether a comment should be created even if `comment_tag` is not found | | true |
| `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/delete/delete-on-completion) | | upsert |
| `create-if-not-exists` | Whether a comment should be created even if `comment-tag` is not found | | true |


## Outputs
Expand All @@ -136,13 +149,13 @@ You can get some outputs from this actions :
| --- | --- |
| `id` | Comment id that was created or updated |
| `body` | Comment body |
| `html_url` | URL of the comment created or updated |
| `html-url` | URL of the comment created or updated |

### Example output

```yaml
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
uses: thollander/actions-comment-pull-request@v3
id: hello
with:
message: |
Expand All @@ -151,7 +164,7 @@ You can get some outputs from this actions :
run: |
echo "id : ${{ steps.hello.outputs.id }}"
echo "body : ${{ steps.hello.outputs.body }}"
echo "html_url : ${{ steps.hello.outputs.html_url }}"
echo "html-url : ${{ steps.hello.outputs.html-url }}"
```

## Permissions
Expand Down
14 changes: 7 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ description: 'Comments a pull request with the provided message'
inputs:
message:
description: 'Message that should be printed in the pull request'
filePath:
file-path:
description: 'Path of the file that should be commented'
GITHUB_TOKEN:
github-token:
description: 'Github token of the repository (automatically created by Github)'
default: ${{ github.token }}
required: false
reactions:
description: 'You can set some reactions on your comments through the `reactions` input.'
pr_number:
pr-number:
description: 'Manual pull request number'
comment_tag:
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)'
description: 'Mode that will be used (upsert/recreate/delete/delete-on-completion)'
default: 'upsert'
create_if_not_exists:
description: 'Whether a comment should be created even if comment_tag is not found.'
create-if-not-exists:
description: 'Whether a comment should be created even if comment-tag is not found.'
default: 'true'
runs:
using: 'node20'
Expand Down
26 changes: 13 additions & 13 deletions lib/cleanup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9540,32 +9540,32 @@ const github = __importStar(__nccwpck_require__(5438));
const core = __importStar(__nccwpck_require__(2186));
async function run() {
try {
const github_token = core.getInput('GITHUB_TOKEN');
const pr_number = core.getInput('pr_number');
const comment_tag = core.getInput('comment_tag');
const githubToken = core.getInput('github-token');
const prNumber = core.getInput('pr-number');
const commentTag = core.getInput('comment-tag');
const mode = core.getInput('mode');
if (mode !== 'delete') {
core.debug('This comment was not to be deleted. Skipping');
if (mode !== 'delete-on-completion') {
core.debug('This comment was not to be deleted on completion. Skipping');
return;
}
if (!comment_tag) {
if (!commentTag) {
core.debug("No 'comment_tag' parameter passed in. Cannot search for something to delete.");
return;
}
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);
if (!issue_number) {
const issueNumber = parseInt(prNumber) || context.payload.pull_request?.number || context.payload.issue?.number;
const octokit = github.getOctokit(githubToken);
if (!issueNumber) {
core.setFailed('No issue/pull request in input neither in current context.');
return;
}
const comment_tag_pattern = `<!-- thollander/actions-comment-pull-request "${comment_tag}" -->`;
if (comment_tag_pattern) {
const commentTagPattern = `<!-- thollander/actions-comment-pull-request "${commentTag}" -->`;
if (commentTagPattern) {
for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, {
...context.repo,
issue_number,
issue_number: issueNumber,
})) {
const commentsToDelete = comments.filter((comment) => comment?.body?.includes(comment_tag_pattern));
const commentsToDelete = comments.filter((comment) => comment?.body?.includes(commentTagPattern));
for (const commentToDelete of commentsToDelete) {
core.info(`Deleting comment ${commentToDelete.id}.`);
await octokit.rest.issues.deleteComment({
Expand Down
Loading

0 comments on commit 107ab45

Please sign in to comment.