name: Check for changelog entry file
"on":
  pull_request:
    types: [opened, synchronize, reopened, edited]
    branches:
      - main
jobs:
  # Check if the PR creates a separate file with changelog entry in the
  # ".unreleased"  folder
  #
  # This check can be disabled by adding the following line in the PR text
  #
  # Disable-check: force-changelog-file
  #
  # The file having the changelog entry is expected to have lines in the
  # following format
  #
  # Fixes: #NNNN <bug description> (mandatory in case of bugfixes)
  # Thanks: @name <thank you note> (optional)
  # Implements: #NNNN <feature description> (mandatory in case of new features)
  check_changelog_file:
    name: Check for file with CHANGELOG entry
    runs-on: timescaledb-runner-arm64
    steps:
      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.13'

      - name: Install Python Dependencies
        run: |
          pip install PyGithub

      - name: Checkout source
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0

      - name: Check if the pull request adds file in ".unreleased" folder
        shell: bash --norc --noprofile {0}
        env:
          BODY: ${{ github.event.pull_request.body }}
          GH_TOKEN: ${{ github.token }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          set -euo pipefail

          folder=".unreleased"

          # Get the list of modified files as a bash array. Exclude the
          # development-related files because they don't require a changelog.
          mapfile -t modified_files < <(gh pr view $PR_NUMBER --json files --jq '
            [.files.[].path | select(
                    (startswith(".github") or startswith("test")
                        or startswith("tsl/test") or startswith("scripts"))
                | not)] | .[]')
          echo "Modified files: ${modified_files[@]}"

          # Get the changelog files as a bash array
          mapfile -t changelog_files < <(gh pr view $PR_NUMBER --json files --jq "
            [.files.[].path | select(startswith(\"${folder}\"))] | .[]")
          echo "Changelog files: ${changelog_files[@]}"

          if echo "$BODY" | egrep -qsi "Disable-check:[[:space:]]*force-changelog-file"
          then
            # skip changelog checks if forced
            :
          elif (( ${#modified_files[@]} > 0 && ${#changelog_files[@]} == 0 ))
          then
            # if no changelog files found, and the PR does not have the force disable check option
            echo "PR does not add a change log file in .unreleased/ folder"
            echo "Check .unreleased/template.rfc822 for the format of the change log file."
            echo
            echo "To disable changelog updated check, add this trailer to pull request message:"
            echo
            echo "Disable-check: force-changelog-file"
            echo
            echo "Trailers follow RFC2822 conventions, so no whitespace"
            echo "before field name and the check is case-insensitive for"
            echo "both the field name and the field body."
            exit 1
          else
            # check the format of the files in .unreleased folder
            for file in "${changelog_files[@]}"
            do
              if ! scripts/check_changelog_format.py "${file}"
              then
                echo "Invalid CHANGELOG entries in ${file}."
                exit 1
              fi
            done
          fi
