# This workflow will:
#
#   1. Store some github context in env vars.
#   2. Only checkout if base and head ref owners are the same.
#   3. Check if any *.py files are in the diff.
#   4. Only then install python and perform the annotation with flake8.
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: PR-annotate-flake8

on:
  pull_request:
    branches: [ develop ]

jobs:
  flake8_annotate:
    runs-on: ubuntu-18.04

    steps:
      ##################################################################
      # Check if the base and head repos are the same, if not we won't
      # be able to add annotations.
      ##################################################################
      - name: Check head and base repo same
        env:
          BASE_REPO_OWNER: ${{github.event.pull_request.base.repo.owner.login}}
          HEAD_REPO_OWNER: ${{github.event.pull_request.head.repo.owner.login}}
          BASE_REF: ${{github.base_ref}}
        run: |
          # Just to be running something
          env
      ##################################################################

      ##################################################################
      # Perform the checkout
      ##################################################################
      - name: Checkout
        if: ${{ github.event.pull_request.base.repo.owner.login == github.event.pull_request.head.repo.owner.login }}
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      ##################################################################

      ##################################################################
      # flake8-your-pr 'fails' if no *.py files changed
      # So we check if any were affected and store that in env
      ##################################################################
      - name: Check for PY files
        if: ${{ github.event.pull_request.base.repo.owner.login == github.event.pull_request.head.repo.owner.login }}
        run: |
          # Checkout might not have happened.
          if [ ! -d ".git" ]; then exit 0 ; fi
          # Get a list of files ending with ".py", stuff in environment.
          # We don't rely on exit status because Workflows run with
          # "set -e" so any failure in a pipe is total failure.
          PYFILES=$(git diff --name-only "refs/remotes/origin/${BASE_REF}" -- | egrep '.py$' || true)
          # Use magic output to store in env for rest of workflow
          echo "::set-env name=PYFILES::${PYFILES}"
      ##################################################################

      ##################################################################
      # Get Python set up
      ##################################################################
      - name: Set up Python 3.7
        if: ${{ env.PYFILES != '' }}
        uses: actions/setup-python@v2
        with:
          python-version: 3.7
      ##################################################################

      ##################################################################
      # Perform the annotation
      ##################################################################
      - name: Annotate with Flake8
        # Only if at least one *.py file was affected
        if: ${{ env.PYFILES != '' }}
        uses: "tayfun/flake8-your-pr@master"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      ##################################################################
