Skip to content

Conversation

@thollander
Copy link
Owner

@thollander thollander commented Jun 10, 2023

Add support for delete mode.
It basically runs a post action that will check if a comment needs to be deleted at the end of the job.
(docs: https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runspost)

Usage example :

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

Closes #232

@thollander thollander force-pushed the cleanup branch 4 times, most recently from bc1726c to e4b3a14 Compare June 10, 2023 09:11
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
@thollander thollander force-pushed the cleanup branch 4 times, most recently from 8a734bf to b270427 Compare June 10, 2023 09:21
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
Repository owner deleted a comment from github-actions bot Jun 10, 2023
@thollander thollander force-pushed the cleanup branch 2 times, most recently from 04e043a to ae899d0 Compare June 10, 2023 10:58
@github-actions
Copy link

Current branch is cleanup.
(execution 5229877649 / attempt 1)

@github-actions
Copy link

Comment Pull Request - GitHub Actions

What is it ?

A GitHub action that comments with a given message the pull request linked to the pushed branch.
You can even put dynamic data thanks to Contexts and expression syntax.

Usage

Classic usage

on: pull_request

jobs:
  example_comment_pr:
    runs-on: ubuntu-latest
    name: An example job to comment a PR
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Comment PR
        uses: thollander/actions-comment-pull-request@v2
        with:
          message: |
            Hello world ! :wave:

Comment a file content

Thanks to the filePath input, a file content can be commented.
You can either pass an absolute filePath or a relative one that will be by default retrieved from GITHUB_WORKSPACE.
(Note that if both a message and filePath are provided, message will take precedence.)

- name: PR comment with file
  uses: thollander/actions-comment-pull-request@v2
  with:
    filePath: /path/to/file.txt

Setting reactions

You can also set some reactions on your comments through the reactions input.
It takes only valid reactions and adds it to the comment you've just created. (See https://docs.github.com/en/rest/reactions#reaction-types)

- name: PR comment with reactions
  uses: thollander/actions-comment-pull-request@v2
  with:
    message: |
      Hello world ! :wave:
    reactions: eyes, rocket

Specifying which pull request to comment on

You can explicitly input which pull request should be commented on by passing the pr_number input.
That is particularly useful for manual workflow for instance (workflow_run).

...
- name: Comment PR
  uses: thollander/actions-comment-pull-request@v2
  with:
    message: |
      Hello world ! :wave:
    pr_number: 123 # This will comment on pull request #123

Update a comment

Editing an existing comment is also possible thanks to the comment_tag input.

Thanks to this parameter, it will be possible to identify your comment and then to upsert on it.
If the comment is not found at first, it will create a new comment.

That is particularly interesting while committing multiple times in a PR and that you just want to have the last execution report printed. It avoids flooding the PR.

...
- name: Comment PR with execution number
  uses: thollander/actions-comment-pull-request@v2
  with:
    message: |
      _(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_
    comment_tag: execution

Note: the input mode can be used to either upsert (by default) or recreate the comment (= delete and create)

Inputs

Action inputs

Name Description Required Default
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
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
create_if_not_exists Whether a comment should be created even if comment_tag is not found true

Permissions

Depending on the permissions granted to your token, you may lack some rights.
To run successfully, this actions needs at least :

permissions: 
   pull-requests: write 

Add this in case you get Resource not accessible by integration error.
See jobs.<job_id>.permissions for more information.

Contributing

Build

The build steps transpiles the src/main.ts to lib/index.js which is used in a NodeJS environment.
It is handled by vercel/ncc compiler.

$ npm run build

@github-actions
Copy link

Content of file referenced with absolute path

@thollander thollander changed the title feat: cleanup comment to delete feat: mode to delete comment Jun 10, 2023
@thollander thollander merged commit 3229332 into main Jun 10, 2023
@lukasz-mitka
Copy link
Contributor

@thollander
Late to the party, but I would rename delete to temp or cleanup.
delete suggests it will delete the comment on execution, not afterwards.

Anyway, thanks for this feature!

@renefritze
Copy link

@thollander Late to the party, but I would rename delete to temp or cleanup. delete suggests it will delete the comment on execution, not afterwards.

Anyway, thanks for this feature!

I think I misunderstood this feature then. So basically it would create a comment whose lifetime is limited to the job duration from where it's created?
I understood this as "delete existing comment with given tag".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleting the created comment?

4 participants