name: Pylint

on:
  workflow_dispatch:
  pull_request:
  push:
    branches: [ master ]

jobs:
  pylint:

    runs-on: ubuntu-20.04
    defaults:
      run:
        shell: bash
    outputs:
      branch: ${{ steps.extract_branch.outputs.branch }}
      rating: ${{ steps.analyze.outputs.rating }}
      path: ${{ steps.analyze.outputs.path }}

    steps:
    - uses: actions/checkout@v3

    - name: Extract base branch name
      id: extract_branch
      shell: bash
      run: |
        TMP_PULL_BASE_REF="${{ github.base_ref }}"
        TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}"
        EXPORT_VALUE=""
        if [ "${TMP_PULL_BASE_REF}" != "" ]
        then
            EXPORT_VALUE="${TMP_PULL_BASE_REF}"
        else
            EXPORT_VALUE="${TMP_GITHUB_REF}"
        fi
        echo "##[set-output name=branch;]${EXPORT_VALUE}"

    - name: Set up Python 3.8
      uses: actions/setup-python@v4
      with:
        python-version: 3.8

    - name: Install tox
      run: |
        python -m pip install --upgrade pip wheel
        pip install tox tox-gh-actions

    - name: Run pylint
      id: analyze
      env:
        BADGE_PATH: badges/pylint-score.svg
      run: |
        rating=$(bash -c 'tox -e lint' | grep 'Your code has been rated at' | cut -f7 -d " ")
        echo "Pylint score: ${rating}"
        echo "##[set-output name=rating;]${rating}"
        echo "##[set-output name=path;]${BADGE_PATH}"

  badge:
    # Only generate and publish if these conditions are met:
    # - The previous job/analyze step ended successfully
    # - At least one of these is true:
    #   - This is a push event and the push event is on branch 'master' or 'develop'
    # Note: if this repo is personal (ie, not an org repo) then you can
    #       use the following to change the scope of the next 2 jobs
    #       instead of running on branch push as shown below:
    #   - This is a pull request event and the pull actor is the same as the repo owner
    #     if: ${{ ( github.event_name == 'pull_request' && github.actor == github.repository_owner ) || github.ref == 'refs/heads/master' }}
    name: Generate badge image with pylint score
    runs-on: ubuntu-20.04
    needs: [pylint]
    if: ${{ github.event_name == 'push' }}
    #outputs:
      #url: ${{ steps.url.outputs.url }}
      #markdown: ${{ steps.url.outputs.markdown }}

    steps:
    - uses: actions/checkout@v3
      with:
        ref: badges
        path: badges

    # Use the output from the `analyze` step
    - name: Create pylint badge
      uses: emibcn/badge-action@v1.2.4
      id: badge
      with:
        label: 'Pylint score'
        status: ${{ needs.pylint.outputs.rating }}
        color: 'green'
        path: ${{ needs.pylint.outputs.path }}

    - name: Commit badge
      env:
        BRANCH: ${{ needs.pylint.outputs.branch }}
        FILE: 'pylint-score.svg'
      working-directory: ./badges
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        mkdir -p "${BRANCH}"
        mv "${FILE}" "${BRANCH}"
        git add "${BRANCH}/${FILE}"
        # Will give error if badge has not changed
        git commit -m "Add/Update badge" || true

    - name: Push badge commit
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: badges
        directory: badges
