name: Check Release Notes

on:
  pull_request:
    types:
      - opened
      - reopened
      - synchronize
      - ready_for_review
      - labeled
      - unlabeled
    paths:
      - "**.py"
      - "pyproject.toml"
      - "!.github/**/*.py"
      - "releasenotes/notes/*.yaml"

permissions:
  contents: read

jobs:
  reno:
    runs-on: ubuntu-slim
    env:
      PYTHON_VERSION: "3.10"
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false
          # With the default value of 1, there are corner cases where tj-actions/changed-files
          # fails with a `no merge base` error
          fetch-depth: 0

      - uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6.3.0
        with:
          python-version: "${{ env.PYTHON_VERSION }}"
      - name: Get release note files
        id: changed-files
        uses: tj-actions/changed-files@9426d40962ed5378910ee2e21d5f8c6fcbf2dd96 # v47.0.6
        with:
          files: releasenotes/notes/*.yaml

      - name: Check release notes
        if: steps.changed-files.outputs.any_changed == 'false' && !contains( github.event.pull_request.labels.*.name, 'ignore-for-release-notes')
        run: |
          # Check if any of the commit messages contain tags ci/docs/test
          if git log --pretty=%s origin/main..HEAD | grep -E '^(ci:|docs:|test:)' > /dev/null; then
            echo "Skipping release note check for commits with 'ci:', 'docs:', or 'test:' tags."
          else
            echo "::error::The release notes file is missing, please add one or attach the label 'ignore-for-release-notes' to this PR."
            exit 1
          fi

      - name: Verify release notes formatting
        if: steps.changed-files.outputs.any_changed == 'true' && !contains( github.event.pull_request.labels.*.name, 'ignore-for-release-notes')
        run: |
          python -m pip install --upgrade pip
          pip install "reno<5" --uploaded-prior-to=P1D
          reno lint .  # it is not possible to pass a list of files to reno lint

      - name: Check reStructuredText code formatting
        if: steps.changed-files.outputs.any_changed == 'true' && !contains( github.event.pull_request.labels.*.name, 'ignore-for-release-notes')
        shell: python
        run: |
          files = "${{ steps.changed-files.outputs.all_changed_files }}".split()
          errors = []

          for filepath in files:
            with open(filepath) as f:
              for line_no, line in enumerate(f, start=1):
                # Check for triple backticks (Markdown code blocks)
                if "```" in line:
                  err = (f"Format error in {filepath}:{line_no}: "
                         "Found triple backticks. Use reStructuredText code block directive instead: .. code:: python")
                  errors.append(err)

                # Check for single backticks (Markdown inline code)
                if "`" in line.replace("```", "").replace("``", ""):
                  err = (f"Format error in {filepath}:{line_no}: "
                         "Found single backticks. Use double backticks (``code``) for inline code.")
                  errors.append(err)

          if errors:
              raise Exception("\n".join(errors))
