name: Check PR Labels

on:
  pull_request:
    types: [edited, labeled]

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

permissions:
  pull-requests: read
  contents: read

jobs:
  check-label:
    # Skip bot PRs — they already have labels from the workflows/bots that create them
    if: >-
      github.event.pull_request.user.login != 'renovate[bot]' &&
      github.event.pull_request.user.login != 'github-actions[bot]' &&
      github.event.pull_request.user.login != 'dependabot[bot]' &&
      github.event.pull_request.head.ref != 'scheduled-updates' &&
      github.event.pull_request.head.ref != 'l10n_main'
    runs-on: ubuntu-24.04-arm
    steps:
      - name: Check for PR labels
        uses: actions/github-script@v9
        with:
          script: |
            // Extract labels from the payload directly to avoid extra API calls
            const latestLabels = context.payload.pull_request.labels.map(label => label.name);
            const requiredLabels = ['bugfix', 'enhancement', 'automation', 'dependencies', 'repo', 'release', 'refactor', 'desktop', 'chore', 'ci', 'build', 'testing', 'documentation'];
            console.log('Labels from payload:', latestLabels);
            const hasRequiredLabel = latestLabels.some(label => requiredLabels.includes(label));
            if (!hasRequiredLabel) {
              core.setFailed(`PR must have at least one of the following labels before it can be merged: ${requiredLabels.join(', ')}.`);
            }
