name: Enterprise Check Migrations

on:
  pull_request:
    paths:
      - 'enterprise/migrations/**'

jobs:
  check-sync:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout PR branch
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0


      - name: Fetch base branch
        run: git fetch origin ${{ github.event.pull_request.base.ref }}

      - name: Check if base branch is ancestor of PR
        id: check_up_to_date
        shell: bash
        run: |
          BASE="origin/${{ github.event.pull_request.base.ref }}"
          HEAD="${{ github.event.pull_request.head.sha }}"
          if git merge-base --is-ancestor "$BASE" "$HEAD"; then
            echo "We're up to date with base $BASE"
            exit 0
          else
            echo "NOT up to date with base $BASE"
            exit 1
          fi

      - name: Find Comment
        uses: peter-evans/find-comment@v3
        id: find-comment
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'github-actions[bot]'
          body-includes: |
            ⚠️ This PR contains **migrations**

      - name: Comment warning on PR
        uses: peter-evans/create-or-update-comment@v4
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-id: ${{ steps.find-comment.outputs.comment-id }}
          edit-mode: replace
          body: |
            ⚠️ This PR contains **migrations**. Please synchronize before merging to prevent conflicts.
