name: Publish
run-name: Publish Repository Actions 🛫
on:
  release:
    types:
      - published
    branches:
      - master
      - 'release/**'
permissions:
  contents: read
concurrency:
  group: '${{ github.workflow }} @ ${{ github.head_ref || github.ref }}'
  cancel-in-progress: true
jobs:
  check-tag:
    name: Check Release Tag
    if: github.repository_owner == 'obsproject'
    runs-on: ubuntu-22.04
    outputs:
      validTag: ${{ steps.check.outputs.validTag }}
      flatpakMatrix: ${{ steps.check.outputs.flatpakMatrix }}
    steps:
      - name: Check Release Tag ☑️
        id: check
        run: |
          : Check Release Tag ☑️
          if [[ "${RUNNER_DEBUG}" ]]; then set -x; fi
          shopt -s extglob

          case "${GITHUB_REF_NAME}" in
            +([0-9]).+([0-9]).+([0-9]) )
              echo 'validTag=true' >> $GITHUB_OUTPUT
              echo 'flatpakMatrix=["beta", "stable"]' >> $GITHUB_OUTPUT
              ;;
            +([0-9]).+([0-9]).+([0-9])-@(beta|rc)*([0-9]) )
              echo 'validTag=true' >> $GITHUB_OUTPUT
              echo 'flatpakMatrix=["beta"]' >> $GITHUB_OUTPUT
              ;;
            *) echo 'validTag=false' >> $GITHUB_OUTPUT ;;
          esac

  flatpak-publish:
    name: Flathub 📦
    needs: check-tag
    if: github.repository_owner == 'obsproject' && fromJSON(needs.check-tag.outputs.validTag)
    runs-on: ubuntu-22.04
    defaults:
      run:
        shell: bash
    env:
      FLATPAK_BUILD_SHARE_PATH: flatpak_app/files/share
      TWITCH_CLIENTID: ${{ secrets.TWITCH_CLIENT_ID }}
      TWITCH_HASH: ${{ secrets.TWITCH_HASH }}
      RESTREAM_CLIENTID: ${{ secrets.RESTREAM_CLIENTID }}
      RESTREAM_HASH: ${{ secrets.RESTREAM_HASH }}
      YOUTUBE_CLIENTID: ${{ secrets.YOUTUBE_CLIENTID }}
      YOUTUBE_CLIENTID_HASH: ${{ secrets.YOUTUBE_CLIENTID_HASH }}
      YOUTUBE_SECRET: ${{ secrets.YOUTUBE_SECRET }}
      YOUTUBE_SECRET_HASH: ${{ secrets.YOUTUBE_SECRET_HASH }}
    container:
      image: ghcr.io/flathub-infra/flatpak-github-actions:kde-6.6
      options: --privileged
    strategy:
      matrix:
        branch: ${{ fromJSON(needs.check-tag.outputs.flatpakMatrix) }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
          set-safe-directory: ${{ env.GITHUB_WORKSPACE }}

      - name: Set Up Environment 🔧
        id: setup
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          : Set Up Environment 🔧
          if [[ "${RUNNER_DEBUG}" ]]; then set -x; fi

          git config --global --add safe.directory "${GITHUB_WORKSPACE}"

          gh extension install actions/gh-actions-cache

          cache_key='flatpak-builder-${{ hashFiles('build-aux/**/*.json') }}'
          cache_ref='master'
          read -r key size unit _ ref _ <<< \
            "$(gh actions-cache list -B ${cache_ref} --key "${cache_key}-x86_64" | head -1)"

          if [[ "${key}" ]]; then
            echo "cacheHit=true" >> $GITHUB_OUTPUT
          else
            echo "cacheHit=false" >> $GITHUB_OUTPUT
          fi

          echo "cacheKey=${cache_key}" >> $GITHUB_OUTPUT
          echo "commitHash=${GITHUB_SHA:0:9}" >> $GITHUB_OUTPUT

      - name: Validate Flatpak manifest
        uses: ./.github/actions/flatpak-builder-lint
        with:
          artifact: manifest
          path: build-aux/com.obsproject.Studio.json

      - name: Build Flatpak Manifest
        uses: flathub-infra/flatpak-github-actions/flatpak-builder@23796715b3dfa4c86ddf50cf29c3cc8b3c82dca8
        with:
          bundle: obs-studio-${{ steps.setup.outputs.commitHash }}.flatpak
          manifest-path: ${{ github.workspace }}/build-aux/com.obsproject.Studio.json
          cache: ${{ fromJSON(steps.setup.outputs.cacheHit) }}
          cache-key: ${{ steps.setup.outputs.cacheKey }}
          mirror-screenshots-url: https://dl.flathub.org/media
          branch: ${{ matrix.branch }}

      - name: Validate AppStream
        uses: ./.github/actions/flatpak-builder-lint
        with:
          artifact: appstream
          path: ${{ env.FLATPAK_BUILD_SHARE_PATH }}/metainfo/com.obsproject.Studio.metainfo.xml

      - name: Verify Icon and Metadata in app-info
        working-directory: ${{ env.FLATPAK_BUILD_SHARE_PATH }}
        run: |
          : Verify Icon and Metadata in app-info
          test -f app-info/icons/flatpak/128x128/com.obsproject.Studio.png || { echo "::error::Missing 128x128 icon in app-info"; exit 1; }
          test -f app-info/xmls/com.obsproject.Studio.xml.gz || { echo "::error::Missing com.obsproject.Studio.xml.gz in app-info"; exit 1; }

      - name: Validate build directory
        uses: ./.github/actions/flatpak-builder-lint
        with:
          artifact: builddir
          path: flatpak_app

      - name: Validate repository
        uses: ./.github/actions/flatpak-builder-lint
        with:
          artifact: repo
          path: repo

      - name: Publish to Flathub Beta
        uses: flathub-infra/flatpak-github-actions/flat-manager@23796715b3dfa4c86ddf50cf29c3cc8b3c82dca8
        if: ${{ matrix.branch == 'beta' }}
        with:
          flat-manager-url: https://hub.flathub.org/
          repository: beta
          token: ${{ secrets.FLATHUB_BETA_TOKEN }}
          build-log-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

      - name: Publish to Flathub
        uses: flathub-infra/flatpak-github-actions/flat-manager@23796715b3dfa4c86ddf50cf29c3cc8b3c82dca8
        if: ${{ matrix.branch == 'stable' }}
        with:
          flat-manager-url: https://hub.flathub.org/
          repository: stable
          token: ${{ secrets.FLATHUB_TOKEN }}
          build-log-url: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}

  steam-upload:
    name: Upload Steam Builds 🚂
    needs: check-tag
    if: github.repository_owner == 'obsproject' && fromJSON(needs.check-tag.outputs.validTag)
    runs-on: macos-14
    steps:
      - uses: actions/checkout@v4
      - uses: ./.github/actions/steam-upload
        with:
          steamSecret: ${{ secrets.STEAM_SHARED_SECRET }}
          steamUser: ${{ secrets.STEAM_USER }}
          steamPassword: ${{ secrets.STEAM_PASSWORD }}
          workflowSecret: ${{ github.token }}
          tagName: ${{ github.ref_name }}
          preview: false
