name: Status check labels

on:
  pull_request:
    types: [opened, reopened, labeled, unlabeled, synchronize]

permissions:
  pull-requests: read  # issues.listLabelsOnIssue to detect blocking labels (needs-docs, needs-developer-docs, merge-after-release, chained-pr)

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
  cancel-in-progress: true

jobs:
  check:
    name: Check blocking labels
    runs-on: ubuntu-latest
    steps:
      - name: Check for blocking labels
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        with:
          script: |
            const blockingLabels = ['needs-docs', 'needs-developer-docs', 'merge-after-release', 'chained-pr'];
            const { data: labels } = await github.rest.issues.listLabelsOnIssue({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number
            });
            const labelNames = labels.map(l => l.name);
            const found = blockingLabels.filter(bl => labelNames.includes(bl));
            if (found.length > 0) {
              core.setFailed(`Pull request cannot be merged, it has blocking label(s): ${found.join(', ')}`);
            }
