name: Publish

on:
  workflow_run:
    workflows:
      - Build
    types:
      - completed

jobs:
  pr-context:
    name: PR context
    if: ${{ github.event.workflow_run.event == 'pull_request' }}
    runs-on: ubuntu-latest
    outputs:
      branch: ${{ env.pr_branch }}
      commit: ${{ env.pr_sha }}
      pr_number: ${{ env.pr_number }}

    steps:
      - name: Get PR context
        uses: dawidd6/action-download-artifact@09f2f74827fd3a8607589e5ad7f9398816f540fe # v3.1.4
        id: pr_context
        with:
          run_id: ${{ github.event.workflow_run.id }}
          name: PR_context

      - name: Set PR context environment variables
        if: ${{ steps.pr_context.conclusion == 'success' }}
        run: |
          echo "pr_branch=$(cat PR_branch)" >> $GITHUB_ENV
          echo "pr_number=$(cat PR_number)" >> $GITHUB_ENV
          echo "pr_sha=$(cat PR_sha)" >> $GITHUB_ENV

  publish:
    permissions:
      contents: read
      deployments: write

    name: Deploy to Cloudflare Pages
    if: ${{ always() }}
    runs-on: ubuntu-latest
    needs:
      - pr-context
    outputs:
      url: ${{ steps.cf.outputs.url }}

    steps:
      - name: Download workflow artifact
        uses: dawidd6/action-download-artifact@09f2f74827fd3a8607589e5ad7f9398816f540fe # v3.1.4
        with:
          run_id: ${{ github.event.workflow_run.id }}
          name: jellyfin-web__prod
          path: dist

      - name: Publish
        id: cf
        uses: cloudflare/pages-action@f0a1cd58cd66095dee69bfa18fa5efd1dde93bca # 1
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          projectName: jellyfin-web
          branch: ${{ needs.pr-context.outputs.branch || github.ref_name }}
          directory: dist
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}

  compose-comment:
    name: Compose comment
    if: ${{ always() }}
    uses: ./.github/workflows/job-messages.yml
    needs:
      - publish
      - pr-context

    with:
      branch: ${{ needs.pr-context.outputs.branch || github.ref_name }}
      commit: ${{ needs.pr-context.outputs.commit != '' && needs.pr-context.outputs.commit || github.event.workflow_run.head_sha }}
      preview_url: ${{ needs.publish.outputs.url }}
      build_workflow_run_id: ${{ github.event.workflow_run.id }}
      commenting_workflow_run_id: ${{ github.run_id }}
      in_progress: false

  comment-status:
    name: Create comment status
    if: |
      always() &&
      github.event.workflow_run.event == 'pull_request' &&
      needs.pr-context.outputs.pr_number != ''
    runs-on: ubuntu-latest
    needs:
      - compose-comment
      - pr-context

    steps:
      - name: Update job summary in PR comment
        uses: thollander/actions-comment-pull-request@fabd468d3a1a0b97feee5f6b9e499eab0dd903f6 # v2.5.0
        with:
          GITHUB_TOKEN: ${{ secrets.JF_BOT_TOKEN }}
          message: ${{ needs.compose-comment.outputs.msg }}
          pr_number: ${{ needs.pr-context.outputs.pr_number }}
          comment_tag: ${{ needs.compose-comment.outputs.marker }}
          mode: recreate
