name: PR Label Apply

on:
  workflow_run:
    workflows: ["PR Label Analysis"]
    types:
      - completed

permissions:
  contents: read

jobs:
  apply-labels:
    name: Apply labels to PR
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    permissions:
      contents: read
      pull-requests: write
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Download PR metadata artifact
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          run-id: ${{ github.event.workflow_run.id }}
          pattern: pr-metadata-*
          merge-multiple: false
        id: download-artifact
      
      - name: Read PR number
        id: pr-number
        run: |
          PR_NUMBER=$(cat "pr-number.txt")
          echo "number=${PR_NUMBER}" >> $GITHUB_OUTPUT
          echo "PR Number: ${PR_NUMBER}"
      
      - name: Apply labels using labeler
        uses: actions/labeler@634933edcd8ababfe52f92936142cc22ac488b1b # v6
        with:
          pr-number: ${{ steps.pr-number.outputs.number }}
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          configuration-path: .github/labeler.yml
          sync-labels: true