name: Sync Labels

on:
  # NOTE: github.event is workflow_dispatch payload:
  # https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_dispatch
  workflow_dispatch:
    inputs:
      dryrun:
        description: 'dryrun: Preview changes to labels without editing them (true|false)'
        required: true
        type: boolean
        default: true

jobs:
  sync:
    if: '!github.event.repository.fork'
    runs-on: ubuntu-latest
    env:
      GLOBAL: https://raw.githubusercontent.com/conda/infra/main/.github/global.yml
      LOCAL: .github/labels.yml
    steps:
      - uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5
      - id: has_local
        uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
        with:
          files: ${{ env.LOCAL }}
      - name: Global Only
        uses: EndBug/label-sync@52074158190acb45f3077f9099fea818aa43f97a # v2.3.3
        if: steps.has_local.outputs.files_exists == 'false'
        with:
          config-file: ${{ env.GLOBAL }}
          delete-other-labels: true
          dry-run: ${{ github.event.inputs.dryrun }}
      - name: Global & Local
        uses: EndBug/label-sync@52074158190acb45f3077f9099fea818aa43f97a # v2.3.3
        if: steps.has_local.outputs.files_exists == 'true'
        with:
          config-file: |
            ${{ env.GLOBAL }}
            ${{ env.LOCAL }}
          delete-other-labels: true
          dry-run: ${{ github.event.inputs.dryrun }}
