---
name: Release Browser
run-name: Release Browser ${{ inputs.release_type }}

on:
  workflow_dispatch:
    inputs:
      release_type:
        description: 'Release Options'
        required: true
        default: 'Initial Release'
        type: choice
        options:
          - Initial Release
          - Redeploy
          - Dry Run

defaults:
  run:
    shell: bash

jobs:
  setup:
    name: Setup
    runs-on: ubuntu-22.04
    outputs:
      release-version: ${{ steps.version.outputs.version }}
    steps:
      - name: Checkout repo
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

      - name: Branch check
        if: ${{ github.event.inputs.release_type != 'Dry Run' }}
        run: |
          if [[ "$GITHUB_REF" != "refs/heads/rc" ]] && [[ "$GITHUB_REF" != "refs/heads/hotfix-rc-browser" ]]; then
            echo "==================================="
            echo "[!] Can only release from the 'rc' or 'hotfix-rc-browser' branches"
            echo "==================================="
            exit 1
          fi

      - name: Check Release Version
        id: version
        uses: bitwarden/gh-actions/release-version-check@f1125802b1ccae8c601d7c4f61ce39ea254b10c8
        with:
          release-type: ${{ github.event.inputs.release_type }}
          project-type: ts
          file: apps/browser/src/manifest.json
          monorepo: true
          monorepo-project: browser


  locales-test:
    name: Locales Test
    runs-on: ubuntu-22.04
    needs: setup
    steps:
      - name: Checkout repo
        uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3

      - name: Testing locales - extName length
        run: |
          found_error=false

          echo "Locales Test"
          echo "============"
          echo "extName string must be 40 characters or less"
          echo
          for locale in $(ls src/_locales/); do
            string_length=$(jq '.extName.message | length' src/_locales/$locale/messages.json)
            if [[ $string_length -gt 40 ]]; then
              echo "$locale: $string_length"
              found_error=true
            fi
          done

          if $found_error; then
            echo
            echo "Please fix 'extName' for the locales listed above."
            exit 1
          else
            echo "Test passed!"
          fi
        working-directory: apps/browser


  release:
    name: Create GitHub Release
    runs-on: ubuntu-22.04
    needs:
      - setup
      - locales-test
    steps:
      - name: Create GitHub deployment
        uses: chrnorm/deployment-action@d42cde7132fcec920de534fffc3be83794335c00 # v2.0.5
        id: deployment
        with:
          token: '${{ secrets.GITHUB_TOKEN }}'
          initial-status: 'in_progress'
          environment: 'Browser - Production'
          description: 'Deployment ${{ needs.setup.outputs.release-version }} from branch ${{ github.ref_name }}'
          task: release

      - name: Download latest Release build artifacts
        if: ${{ github.event.inputs.release_type != 'Dry Run' }}
        uses: bitwarden/gh-actions/download-artifacts@f1125802b1ccae8c601d7c4f61ce39ea254b10c8
        with:
          workflow: build-browser.yml
          workflow_conclusion: success
          branch: ${{ github.ref_name }}
          artifacts: 'browser-source-*.zip,
                      dist-chrome-*.zip,
                      dist-opera-*.zip,
                      dist-firefox-*.zip,
                      dist-edge-*.zip'

      - name: Dry Run - Download latest master build artifacts
        if: ${{ github.event.inputs.release_type == 'Dry Run' }}
        uses: bitwarden/gh-actions/download-artifacts@f1125802b1ccae8c601d7c4f61ce39ea254b10c8
        with:
          workflow: build-browser.yml
          workflow_conclusion: success
          branch: master
          artifacts: 'browser-source-*.zip,
                      dist-chrome-*.zip,
                      dist-opera-*.zip,
                      dist-firefox-*.zip,
                      dist-edge-*.zip'

      - name: Rename build artifacts
        env:
          PACKAGE_VERSION: ${{ needs.setup.outputs.release-version }}
        run: |
          mv browser-source.zip browser-source-$PACKAGE_VERSION.zip
          mv dist-chrome.zip dist-chrome-$PACKAGE_VERSION.zip
          mv dist-opera.zip dist-opera-$PACKAGE_VERSION.zip
          mv dist-firefox.zip dist-firefox-$PACKAGE_VERSION.zip
          mv dist-edge.zip dist-edge-$PACKAGE_VERSION.zip

      - name: Create release
        if: ${{ github.event.inputs.release_type != 'Dry Run' }}
        uses: ncipollo/release-action@a2e71bdd4e7dab70ca26a852f29600c98b33153e # v1.12.0
        with:
          artifacts: 'browser-source-${{ needs.setup.outputs.release-version }}.zip,
                      dist-chrome-${{ needs.setup.outputs.release-version }}.zip,
                      dist-opera-${{ needs.setup.outputs.release-version }}.zip,
                      dist-firefox-${{ needs.setup.outputs.release-version }}.zip,
                      dist-edge-${{ needs.setup.outputs.release-version }}.zip'
          commit: ${{ github.sha }}
          tag: "browser-v${{ needs.setup.outputs.release-version }}"
          name: "Browser v${{ needs.setup.outputs.release-version }}"
          body: "<insert release notes here>"
          token: ${{ secrets.GITHUB_TOKEN }}
          draft: true

      - name: Update deployment status to Success
        if: ${{ success() }}
        uses: chrnorm/deployment-status@2afb7d27101260f4a764219439564d954d10b5b0 # v2.0.1
        with:
          token: '${{ secrets.GITHUB_TOKEN }}'
          state: 'success'
          deployment-id: ${{ steps.deployment.outputs.deployment_id }}

      - name: Update deployment status to Failure
        if: ${{ failure() }}
        uses: chrnorm/deployment-status@2afb7d27101260f4a764219439564d954d10b5b0 # v2.0.1
        with:
          token: '${{ secrets.GITHUB_TOKEN }}'
          state: 'failure'
          deployment-id: ${{ steps.deployment.outputs.deployment_id }}