name: Release
on:
  pull_request_review:
    types: [submitted]

env:
  YARN_ENABLE_GLOBAL_CACHE: false

jobs:
  release:
    name: Publish releases
    if:
      ${{ github.event.review.state == 'approved' && github.event.sender.login
      == github.event.pull_request.assignee.login &&
      github.event.pull_request.head.ref == 'release-candidate' }}
    outputs:
      companionWasReleased:
        ${{ steps.checkIfCompanionWasReleased.outputs.version }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          fetch-depth: 2
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run:
          echo "dir=$(corepack yarn config get cacheFolder)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v4
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: lts/*
      - name: Install dependencies
        run: corepack yarn install --immutable
        env:
          # https://docs.cypress.io/guides/references/advanced-installation#Skipping-installation
          CYPRESS_INSTALL_BINARY: 0
      - name: Get CHANGELOG diff
        run:
          git --no-pager diff HEAD^ -- CHANGELOG.md | awk '{ if( substr($0,0,1)
          == "+" && $1 != "+##" && $1 != "+Released:" && $1 != "+++" ) { print
          substr($0,2) } }' > CHANGELOG.diff.md
      - name: Copy README for `uppy` package
        run: cp README.md packages/uppy/.
      - name: Build before publishing
        run: corepack yarn run build
      - name: Hack to allow the publish of the Angular package
        run: corepack yarn workspace @uppy/angular prepublishOnly
      - name: Publish to the npm registry
        run:
          corepack yarn workspaces foreach --all --no-private npm publish
          --access public --tolerate-republish
        env:
          YARN_NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - name: Merge PR
        id: merge
        run: |
          gh api -X PUT repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/merge \
            -F merge_method="squash" \
            -F commit_message="$(cat CHANGELOG.diff.md)" \
            --jq 'if .merged then "sha="+.sha else error("not merged") end' >> $GITHUB_OUTPUT
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Create tags
        run: |
          git --no-pager diff --name-only HEAD^ | awk '$0 ~ /^packages\/.+\/package\.json$/ { print "jq -r '"'"'\"gh api /repos/{owner}/{repo}/git/refs -f ref=\\\"refs/tags/\"+.name+\"@\"+.version+\"\\\" -f sha=${{ steps.merge.outputs.sha }}\"'"'"' < " $0 }' > createTags.sh
          cat createTags.sh
          sh createTags.sh | sh
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Get Uppy version number
        id: uppyVersion
        run:
          jq -r '"version="+.version' < packages/uppy/package.json >>
          $GITHUB_OUTPUT
      - name: Create GitHub release
        run:
          gh release create uppy@${{ steps.uppyVersion.outputs.version }} -t
          "Uppy ${{ steps.uppyVersion.outputs.version }}" -F CHANGELOG.diff.md
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Upload `uppy` to CDN
        run: corepack yarn run uploadcdn uppy
        env:
          EDGLY_KEY: ${{secrets.EDGLY_KEY}}
          EDGLY_SECRET: ${{secrets.EDGLY_SECRET}}
      - name: Upload `@uppy/locales` to CDN if it was released
        run:
          git diff --exit-code --quiet HEAD^ --
          packages/@uppy/locales/package.json ||corepack yarn run uploadcdn
          @uppy/locales
        env:
          EDGLY_KEY: ${{secrets.EDGLY_KEY}}
          EDGLY_SECRET: ${{secrets.EDGLY_SECRET}}
      - name: Check if Companion was released
        id: checkIfCompanionWasReleased
        run:
          git diff --exit-code --quiet HEAD^ --
          packages/@uppy/companion/package.json || echo "version=$(jq -r
          .version < packages/@uppy/companion/package.json)" >> $GITHUB_OUTPUT
      - name: Remove release-candidate branch
        run:
          gh api -X DELETE repos/${{ github.repository
          }}/git/refs/heads/release-candidate || echo "Already deleted"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Remove release branch
        run:
          gh api -X DELETE repos/${{ github.repository }}/git/refs/heads/release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Disable Release workflow
        run: gh workflow disable Release --repo ${{ github.repository }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: In case of failure
        if: ${{ failure() }}
        run:
          gh pr comment ${{ github.event.pull_request.number }} --body "Release
          job failed, please take action."
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # See also companion-deploy.yml
  docker:
    name: DockerHub
    needs: release
    if: ${{ needs.release.outputs.companionWasReleased }}
    runs-on: ubuntu-latest
    env:
      DOCKER_BUILDKIT: 0
      COMPOSE_DOCKER_CLI_BUILD: 0
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
      - name: Docker meta
        id: docker_meta
        uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
        with:
          images: transloadit/companion
          tags: |
            type=edge
            type=semver,pattern={{version}},value=${{ needs.release.outputs.companionWasReleased }}
            # set latest tag for default branch
            type=raw,value=latest,enable=true
      - uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0
      - uses: docker/setup-buildx-action@v3
      - name: Log in to DockerHub
        uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0
        with:
          username: ${{secrets.DOCKER_USERNAME}}
          password: ${{secrets.DOCKER_PASSWORD}}
      - name: Build and push
        uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
        with:
          push: true
          context: .
          platforms: linux/amd64,linux/arm64
          file: Dockerfile
          tags: ${{ steps.docker_meta.outputs.tags }}
          labels: ${{ steps.docker_meta.outputs.labels }}