# Python code linting

name: lint

on:
  push:
  pull_request:
  workflow_dispatch:

jobs:
  flake8-lint:
    name: Lint using flake8
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: python -m pip install flake8

      - name: Lint with flake8
        run: |
          # Lint all files in the repository
          OUTPUT_FLAKE8="$(python -m flake8 .)" || true
          
          # Save the output to a file
          if [ -z "$OUTPUT_FLAKE8" ]; then
            exit 0
          else
            echo "# Flake8 linting" > output_flake8.md
            echo "The following issues were found:" >> output_flake8.md
            echo "\`\`\`" >> output_flake8.md
            echo "$OUTPUT_FLAKE8" >> output_flake8.md
            echo "\`\`\`" >> output_flake8.md
            exit 1
          fi

      - name: Comment the flake8 output on commit
        uses: peter-evans/commit-comment@v3
        if: failure()
        with:
          body-path: output_flake8.md
