name: Check Changelog
# This check makes sure that the changelog is properly updated
# when a PR introduces a change in a test file.
# To bypass this check, label the PR with "no changelog needed".
on:
  pull_request:
    types: [opened, edited, labeled, unlabeled, synchronize]

jobs:
  check:
    name: A reviewer will let you know if it is required or can be bypassed
    runs-on: ubuntu-latest
    if: |
      ! contains(github.event.pull_request.labels.*.name, 'no changelog needed') &&
      ! contains(github.event.pull_request.labels.*.name, 'CI')
    steps:
      - name: Get PR number and milestone
        run: |
          echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
          echo "TAGGED_MILESTONE=${{ github.event.pull_request.milestone.title }}" >> $GITHUB_ENV
      - uses: actions/checkout@v6
        with:
          fetch-depth: '0'
      - name: Check the changelog entry
        run: |
          set -xe
          changed_files=$(git diff --name-only origin/main)
          # Changelog should be updated only if tests have been modified
          if [[ ! "$changed_files" =~ tests ]]
          then
            exit 0
          fi
          all_changelogs=$(cat ./CHANGES.rst)
          if [[ "$all_changelogs" =~ :pr:\`$PR_NUMBER\` ]]
          then
            echo "Changelog has been updated."
            # If the pull request is milestoned check the correspondent changelog
            if exist -f ./CHANGES.rst${TAGGED_MILESTONE:0:4}.rst
            then
              expected_changelog=$(cat ./CHANGES.rst${TAGGED_MILESTONE:0:4}.rst)
              if [[ "$expected_changelog" =~ :pr:\`$PR_NUMBER\` ]]
              then
                echo "Changelog and milestone correspond."
              else
                echo "Changelog and milestone do not correspond."
                echo "If you see this error make sure that the tagged milestone for the PR"
                echo "and the edited changelog filename properly match."
                exit 1
              fi
            fi
          else
            echo "A changelog entry is missing."
            echo ""
            echo "Please add an entry to the changelog at 'CHANGES.rst'"
            echo "to document your change assuming that the PR will be merged"
            echo "in time for the next release of skrub."
            echo ""
            echo "Look at other entries in that file for inspiration and please"
            echo "reference this pull request using the ':pr:' directive and"
            echo "credit yourself (and other contributors if applicable) with"
            echo "the ':user:' directive., for instance :pr:`453` by :user:`Jo Blib <JoBlib>`."
            echo ""
            echo "If you see this error and there is already a changelog entry,"
            echo "check that the PR number is correct."
            echo ""
            echo "If you believe that this PR does not warrant a changelog"
            echo "entry, say so in a comment so that a maintainer will label"
            echo "the PR with 'no changelog needed' to bypass this check."
            exit 1
          fi
