name: PR Ready for Review on Label Removal

on:
  pull_request:
    types:
      - unlabeled

env:
  DEFAULT_BRANCH: 'master'

jobs:
  mark-ready:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write

    steps:
      - name: Check if specific label was removed
        id: check-label
        run: |
          if [[ "${{ github.event.label.name }}" == "conflicts" ]]; then
            echo "The removed label is conflicts."
            echo "removed_conflicts=true" >> $GITHUB_OUTPUT
          fi

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          repository: ${{ github.repository }}
          ref: ${{ env.DEFAULT_BRANCH }}
          token: ${{ secrets.AUTO_BACKPORT_TOKEN }}
          fetch-depth: 1

      - name: Mark pull request as ready for review
        if: steps.check-label.outputs.removed_conflicts == 'true'
        run:  gh pr ready "${{ github.event.pull_request.number }}"
        env:
          GITHUB_TOKEN: ${{ secrets.AUTO_BACKPORT_TOKEN }}
