# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt

name: "Quality"

on:
  push:
    branches:
      - main
      - nedbat/*
  pull_request:
  workflow_dispatch:

defaults:
  run:
    shell: bash

env:
  PIP_DISABLE_PIP_VERSION_CHECK: 1

permissions:
  contents: read

concurrency:
  group: "${{ github.workflow }}-${{ github.ref }}"
  cancel-in-progress: true

jobs:
  changed:
    name: "Check changed files"
    runs-on: ubuntu-latest
    permissions:
      pull-requests: read   # Needed for this check to run on pull requests
    outputs:
      python: ${{ steps.filter.outputs.python }}
      docs: ${{ steps.filter.outputs.docs }}
      actions: ${{ steps.filter.outputs.actions }}
      workflow: ${{ steps.filter.outputs.workflow }}
    steps:
      - name: "Check out the repo"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Examine changed files"
        uses: dorny/paths-filter@fbd0ab8f3e69293af611ebaee6363fc25e6d187d # v4.0.1
        id: filter
        with:
          filters: |
            python:
              - "**.py"
            docs:
              - "doc/**"
              - "coverage/**.py"
            actions:
              - ".github/workflows/**"
            workflow:
              - ".github/workflows/quality.yml"
              - "tox.ini"
              - "requirements/*.pip"

  lint:
    name: "Pylint etc"
    # Because pylint can report different things on different OS's (!)
    # (https://github.com/PyCQA/pylint/issues/3489), run this on Mac where local
    # pylint gets run.
    runs-on: macos-latest

    needs: changed
    if:
      needs.changed.outputs.python == 'true'
      || needs.changed.outputs.actions == 'true'
      || needs.changed.outputs.workflow == 'true'

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install Python"
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: "3.10" # Minimum of PYVERSIONS
          cache: pip
          cache-dependency-path: 'requirements/*.pip'

      - name: "Install dependencies"
        run: |
          python -m pip install -r requirements/tox.pip

      - name: "Tox lint"
        run: |
          python -m tox -e lint

  mypy:
    name: "Check types"
    runs-on: ubuntu-latest

    needs: changed
    if:
      needs.changed.outputs.python == 'true'
      || needs.changed.outputs.workflow == 'true'

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install Python"
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: "3.10" # Minimum of PYVERSIONS
          cache: pip
          cache-dependency-path: 'requirements/*.pip'

      - name: "Install dependencies"
        run: |
          python -m pip install -r requirements/tox.pip

      - name: "Tox mypy"
        run: |
          python -m tox -e mypy

  doc:
    name: "Build docs"
    runs-on: ubuntu-latest

    needs: changed
    if:
      needs.changed.outputs.docs == 'true'
      || needs.changed.outputs.workflow == 'true'

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Fetch origin/main"
        # rediraffe needs to compare to origin/main
        run: |
          git fetch origin main --depth=1

      - name: "Install Python"
        uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        with:
          python-version: "3.12" # Doc version from PYVERSIONS
          cache: pip
          cache-dependency-path: 'requirements/*.pip'

      - name: "Show environment"
        run: |
          set -xe
          python -VV
          python -m site
          env | sort

      - name: "Install dependencies"
        run: |
          set -xe
          python -m pip install -r requirements/tox.pip

      - name: "Tox doc"
        run: |
          python -m tox -e doc

  zizmor:
    name: "Zizmor GHA security check"
    runs-on: ubuntu-latest
    permissions:
      contents: read

    needs: changed
    if:
      needs.changed.outputs.actions == 'true'
      || needs.changed.outputs.workflow == 'true'

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: "Install uv"
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b # v8.1.0
        with:
          enable-cache: false

      - name: "Run zizmor"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: uvx zizmor --pedantic .github/workflows
