name: changelog

on:
  pull_request:
    types: [opened, synchronize, labeled, unlabeled, reopened]
    branches-ignore:
      - "maintenance/**"
env:
  CACHE_VERSION: 1
  KEY_PREFIX: base-venv
  DEFAULT_PYTHON: "3.12"

permissions:
  contents: read

jobs:
  check-changelog:
    if: contains(github.event.pull_request.labels.*.name, 'skip news :mute:') != true
    name: Changelog Entry Check
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Check out code from GitHub
        uses: actions/checkout@v4.1.7
        with:
          # `towncrier check` runs `git diff --name-only origin/main...`, which
          # needs a non-shallow clone.
          fetch-depth: 0
      - name: Set up Python ${{ env.DEFAULT_PYTHON }}
        id: python
        uses: actions/setup-python@v5.2.0
        with:
          python-version: ${{ env.DEFAULT_PYTHON }}
          check-latest: true
      - name: Generate partial Python venv restore key
        id: generate-python-key
        run: >-
          echo "key=${{ env.KEY_PREFIX }}-${{ env.CACHE_VERSION }}-${{
            hashFiles('pyproject.toml', 'requirements_test.txt',
          'requirements_test_min.txt', 'requirements_test_pre_commit.txt') }}" >>
          $GITHUB_OUTPUT
      - name: Restore Python virtual environment
        id: cache-venv
        uses: actions/cache@v4.2.0
        with:
          path: venv
          key: >-
            ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{
            steps.generate-python-key.outputs.key }}
      - name: Create Python virtual environment
        if: steps.cache-venv.outputs.cache-hit != 'true'
        run: |
          python -m venv venv
          . venv/bin/activate
          python -m pip install -U pip setuptools wheel
          pip install -U -r requirements_test.txt
          pip install -U -r doc/requirements.txt
      - name: Emit warning if news fragment is missing
        env:
          BASE_BRANCH: ${{ github.base_ref }}
        run: |
          # Fetch the pull request' base branch so towncrier will be able to
          # compare the current branch with the base branch.
          git fetch --no-tags origin +refs/heads/${BASE_BRANCH}:refs/remotes/origin/${BASE_BRANCH}
          . venv/bin/activate
          towncrier check --compare-with origin/${{ github.base_ref }}
