name: Labels

on:  # yamllint disable-line rule:truthy
  pull_request:
    types:
      - opened
      - reopened
      - labeled
      - unlabeled
      - synchronize

jobs:
  check-type-label:
    name: Please wait for a maintainer to add a label
    runs-on: ubuntu-latest
    # skip for draft PRs
    if: github.event.pull_request.draft == false
    permissions:
      pull-requests: read  # Only read access to PRs
    steps:
      - name: Check for required label
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          LABELS=$(gh api --jq '.labels.[].name' /repos/${{ github.repository }}/pulls/${{ github.event.number }})
          echo "Labels: $LABELS"
          if [[ "$LABELS" != *"bug"* ]] && [[ "$LABELS" != *"enhancement"* ]] && [[ "$LABELS" != *"api"* ]] && [[ "$LABELS" != *"maintenance"* ]] && [[ "$LABELS" != *"documentation"* ]]; then
            echo "A maintainer needs to add an appropriate label before merge."
            exit 1
          fi
