Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Action: update existing comment if one exist or create a new comment

I built a pylint git action, for pull request, which actually works really well:

name: Python Linting

on:
  pull_request:
    branches: [ main, dev ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Setup action
      uses: actions/checkout@v2
      with:
        ref: ${{github.event.pull_request.head.ref}}
        repository: ${{github.event.pull_request.head.repo.full_name}}
    - name: Set up Python 3.10
      uses: actions/setup-python@v2
      with:
        python-version: "3.10"
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Changed Files Exporter
      id: files
      uses: umani/[email protected]
      with:
        repo-token: ${{ github.token }}
        pattern: '^src.*\.(py)$'
        result-encoding: 'string'
    - name: Lint with pylint
      working-directory: ./
      run: |
        echo  '${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }}'
        pip install pylint
        OUTPUT=$(pylint ${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }} --exit-zero)
        echo 'MESSAGE<<EOF' >> $GITHUB_ENV
        echo  "$OUTPUT"  >> $GITHUB_ENV
        echo 'EOF' >> $GITHUB_ENV
    - name: Post result to PR
      uses: mshick/add-pr-comment@v1
      with:
        message: |
          **🥇 Pylint Result**
          ```
          ${{ env.MESSAGE }}
          ```
          Please aim to have a quality of score of at least 5.0 or higher
        repo-token: ${{ secrets.GITHUB_TOKEN }}
        repo-token-user-login: 'github-actions[bot]' # The user.login for temporary GitHub tokens
        allow-repeats: false # This is the default

The main problem with this is that it create a new message every time the PR is updated, is it possible to just update the existing comment that is already created? Here is what the comment look like:

enter image description here

Is it possible to edit for an existing comment? How can I tell which comment is the one to update?

like image 643
Bill Software Engineer Avatar asked Sep 15 '25 06:09

Bill Software Engineer


1 Answers

Yes it's doable.

I am using combination of 2 actions:

  • one to find comment id
  • second one to update or create comment

Here you have a working example coming from an action I use for comments editing and creation: https://github.com/peter-evans/create-or-update-comment#where-to-find-the-id-of-a-comment

    - name: Find Comment
      uses: peter-evans/find-comment@v1
      id: fc
      with:
        issue-number: ${{ github.event.pull_request.number }}
        comment-author: 'github-actions[bot]'
        body-includes: This comment was written by a bot!

    - name: Create comment
      if: steps.fc.outputs.comment-id == ''
      uses: peter-evans/create-or-update-comment@v1
      with:
        issue-number: ${{ github.event.pull_request.number }}
        body: |
          This comment was written by a bot!
        reactions: rocket

    - name: Update comment
      if: steps.fc.outputs.comment-id != ''
      uses: peter-evans/create-or-update-comment@v1
      with:
        comment-id: ${{ steps.fc.outputs.comment-id }}
        body: |
          This comment has been updated!
        reactions: hooray

Of course you need to adapt the filtering for finding comments - you can also use just comment-author and direction: last if your bot will have just one comment each thread.

like image 65
Grzegorz Krukowski Avatar answered Sep 17 '25 10:09

Grzegorz Krukowski