Search code examples
pipegithub-actionspylint

Github action: Post comment to PR with pylint result


I am setting up my github CI pipeline, currently I am trying to setup pylint to be ran automatically on pull requests. How do I write the results from pylint into a PR comment?

This is what I have. I try to use the github action on mshick/add-pr-comment@v1. However, I am not sure how to pipe the result from the previous step. Is it possible to only write the final score instead of the whole result, because it's very long.

name: Python Linting

on:
  pull_request:
    branches: [ main, dev ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - 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: Lint with pylint
      run: |
        pip install pylint
        pylint ./src --exit-zero
    - name: Post result to PR
    - uses: mshick/add-pr-comment@v1
      with:
        message: |
          **Hello**
        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

This is my pylint result, at least he last line, the full result is really long:

-----------------------------------
Your code has been rated at 3.31/10

Solution

  • To achieve what you want, you would have to use a script or a shell command (which I don't know as it depends on the context) to extract just the part of the command output you want (e.g: Your code has been rated at 3.31/10), then add it as env variable (or output) to use it in the next step.

    I would do something like this in your job:

        - name: Lint with pylint
          run: |
            pip install pylint
            OUTPUT=$(pylint ./src --exit-zero)
            #OUTPUT=$(shell command or script to extract the part your want)
            echo "MESSAGE=$OUTPUT" >> $GITHUB_ENV
        - name: Post result to PR
          uses: mshick/add-pr-comment@v1
          with:
            message: ${{ env.MESSAGE }}
            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
    

    Where the echo "MESSAGE=$OUTPUT" >> $GITHUB_ENV would add the MESSAGE to the github context environment, to be able to use in the next step with ${{ env.MESSAGE }}.