name: "Check that changes are described"
on:
  pull_request_target:
    types:
      - "opened"
      - "reopened"
      - "synchronize"
      - "labeled"
      - "unlabeled"

jobs:
  changes-required:
    runs-on: ubuntu-latest
    if: "!contains(github.event.pull_request.labels.*.name, 'skip news')"
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: '0'

      - name: Set up Python 3.12
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install towncrier
        run: pip install -U towncrier

      - name: Check changelog
        env:
          BASE_BRANCH: ${{ github.base_ref }}
        run: |
          git fetch --no-tags origin +refs/heads/${BASE_BRANCH}:refs/remotes/origin/${BASE_BRANCH}
          towncrier check --compare-with origin/${BASE_BRANCH}

      - name: Find bot comment
        if: "always()"
        uses: peter-evans/find-comment@v3
        id: fc
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'github-actions[bot]'
          body-includes: Changelog

      - name: Ask for changelog
        if: "failure()"
        uses: peter-evans/create-or-update-comment@v4
        with:
          edit-mode: replace
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            # :x: Changelog is required!

            You need to add a brief description of the changes to the `CHANGES` directory.

            Changes file should be named like `<issue or PR number>.<category>.rst`,
            example `1234.bugfix.rst` where `1234` is the PR or issue number and `bugfix` is the category.

            The content of the file should be a brief description of the changes in
            the PR in the format of a description of what has been done.

            Possible categories are: `feature`, `bugfix`, `doc`, `removal` and `misc`.

      - name: Changelog found
        if: "success()"
        uses: peter-evans/create-or-update-comment@v4
        with:
          edit-mode: replace
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            # :heavy_check_mark: Changelog found.

            Thank you for adding a description of the changes

  skip-news:
    runs-on: ubuntu-latest
    if: "contains(github.event.pull_request.labels.*.name, 'skip news')"
    steps:
      - name: Find bot comment
        uses: peter-evans/find-comment@v3
        id: fc
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'github-actions[bot]'
          body-includes: Changelog

      - name: Comment when docs is not needed
        uses: peter-evans/create-or-update-comment@v4
        with:
          edit-mode: replace
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            # :corn: Changelog is not needed.

            This PR does not require a changelog because `skip news` label is present.
