name: Pull Request

on:
  # push:
  #   branches: [main]
  pull_request:
    branches: [main]
    types: [opened, synchronize, closed]
  workflow_dispatch:

permissions:
  contents: write
  checks: write
  issues: write
  pull-requests: write
  actions: read

jobs:
  CodeFormatter:
    runs-on: [ubuntu-latest]

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup ReviewDog
        uses: reviewdog/action-setup@v1
        with:
          reviewdog_version: latest

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Setup Black
        uses: psf/black@stable
        id: action_black
        with:
          options: ""
          src: "."

      - name: Submit Review via ReviewDog [Black]
        run: |
          set -euxo pipefail
          echo "Running Black"
          TEMPFILE="$(mktemp)"
          git diff > $TEMPFILE
          git stash -u || true
          git stash drop || true
          reviewdog -f=diff\
                    -name=BLACK\
                    -reporter=github-pr-review\
                    -filter-mode='nofilter'\
                    -fail-level=error\
                    -diff="git diff FETCH_HEAD"\
                    -level=warning\
                    -tee < $TEMPFILE
        env:
          REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  TestSuite:
    runs-on: [ubuntu-latest]
    strategy:
      matrix:
        python-version: ["3.10", "3.11", "3.12"]
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          cache: "pip"
          cache-dependency-path: "pyproject.toml"

      - name: Install Dependencies
        run: |
          python --version
          python -m pip install -e .[dev]

      - name: Pytest
        uses: microsoft/action-python@0.7.3
        with:
          workdir: .
          python_version: ${{ matrix.python-version }}
          pytest: true
          coverage: false # disable coverage here, it will be handled in a separate step

      - name: Coverage
        run: |
          python -m coverage run -m pytest
          python -m coverage xml -o coverage.xml

      - name: Upload Coverage Report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report-${{ github.sha }}
          path: coverage.xml
          retention-days: 1
          overwrite: true
        continue-on-error: true


  SonarQube:
    runs-on: [ubuntu-latest]
    needs: TestSuite
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Download Coverage Report
        uses: actions/download-artifact@v4
        with:
          name: coverage-report-${{ github.sha }}
          path: .

      - name: Sonarqube Scan
        uses: sonarsource/sonarqube-scan-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

      - name: Delete Coverage Report Artifact
        if: always() # Ensures this step runs even if previous steps fail
        run: |
          curl -X DELETE \
               -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
               https://api.github.com/repos/${{ github.repository }}/actions/artifacts/coverage-report-${{ github.sha }}
