/actions-comment-pull-request

GitHub action to comment pull request

Primary LanguageJavaScriptMIT LicenseMIT

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@v1
        with:
          message: |
            Hello world ! :wave:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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@v1
  with:
    message: |
      Hello world ! :wave:
    reactions: eyes, rocket
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

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@v1
  with:
    message: |
      Hello world ! :wave:
    pr_number: 123 # This will comment on pull request #123
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Upsert a comment

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

It will search through all the comments of the PR and get the first one that has the provided text in it. If the comment body is not found, 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
  uses: thollander/actions-comment-pull-request@v1
  with:
    message: 'Loading ...'
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
...
- name: Edit PR comment
  uses: thollander/actions-comment-pull-request@v1
  with:
    message: 'Content loaded ! (edited)'
    comment_includes: 'Loading'
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Inputs

Action inputs

Name Description Required Default
GITHUB_TOKEN Token that is used to create comments
message The comment body
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 number (deduced from context)
comment_includes The text that should be used to find comment in case of replacement.

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