name: Version Bump

on:
  workflow_dispatch:
    inputs:
      version_number_override:
        description: "New version override (leave blank for automatic calculation, example: '2024.1.0')"
        required: false
        type: string

permissions: {}

jobs:
  bump_version:
    name: Bump Version
    runs-on: ubuntu-24.04
    permissions:
      contents: write
      id-token: write
    steps:
      - name: Validate version input
        if: ${{ inputs.version_number_override != '' }}
        uses: bitwarden/gh-actions/version-check@main
        with:
          version: ${{ inputs.version_number_override }}

      - name: Log in to Azure
        uses: bitwarden/gh-actions/azure-login@main
        with:
          subscription_id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
          tenant_id: ${{ secrets.AZURE_TENANT_ID }}
          client_id: ${{ secrets.AZURE_CLIENT_ID }}

      - name: Get Azure Key Vault secrets
        id: get-kv-secrets
        uses: bitwarden/gh-actions/get-keyvault-secrets@main
        with:
          keyvault: gh-org-bitwarden
          secrets: "BW-GHAPP-ID,BW-GHAPP-KEY"

      - name: Log out from Azure
        uses: bitwarden/gh-actions/azure-logout@main

      - name: Generate GH App token
        uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
        id: app-token
        with:
          app-id: ${{ steps.get-kv-secrets.outputs.BW-GHAPP-ID }}
          private-key: ${{ steps.get-kv-secrets.outputs.BW-GHAPP-KEY }}

      - name: Checkout Branch
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          token: ${{ steps.app-token.outputs.token }}
          persist-credentials: true

      - name: Setup git
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

      - name: Get current version
        id: current-version
        run: |
          CURRENT_VERSION=$(cat package.json | jq -r '.version')
          echo "version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"

      - name: Verify input version
        if: ${{ inputs.version_number_override != '' }}
        env:
          CURRENT_VERSION: ${{ steps.current-version.outputs.version }}
          NEW_VERSION: ${{ inputs.version_number_override }}
        run: |
          # Error if version has not changed.
          if [[ "$NEW_VERSION" == "$CURRENT_VERSION" ]]; then
            echo "Version has not changed."
            exit 1
          fi

          # Check if version is newer.
          if printf '%s\n' "${CURRENT_VERSION}" "${NEW_VERSION}" | sort -C -V; then
            echo "Version check successful."
          else
            echo "Version check failed."
            exit 1
          fi

      - name: Calculate next release version
        if: ${{ inputs.version_number_override == '' }}
        id: calculate-next-version
        uses: bitwarden/gh-actions/version-next@main
        with:
          version: ${{ steps.current-version.outputs.version }}

      - name: Bump Version - Package - Version Override
        if: ${{ inputs.version_number_override != '' }}
        id: bump-version-override
        uses: bitwarden/gh-actions/version-bump@main
        with:
          file_path: "./package.json"
          version: ${{ inputs.version_number_override }}

      - name: Bump Version - Package - Automatic Calculation
        if: ${{ inputs.version_number_override == '' }}
        id: bump-version-automatic
        uses: bitwarden/gh-actions/version-bump@main
        with:
          file_path: "./package.json"
          version: ${{ steps.calculate-next-version.outputs.version }}

      - name: Set final version output
        id: set-final-version-output
        env:
          _BUMP_VERSION_OVERRIDE_OUTCOME: ${{ steps.bump-version-override.outcome }}
          _INPUT_VERSION_NUMBER_OVERRIDE: ${{ inputs.version_number_override }}
          _BUMP_VERSION_AUTOMATIC_OUTCOME: ${{ steps.bump-version-automatic.outcome }}
          _CALCULATE_NEXT_VERSION: ${{ steps.calculate-next-version.outputs.version }}
          
        run: |
          if [[ "$_BUMP_VERSION_OVERRIDE_OUTCOME" == "success" ]]; then
            echo "version=$_INPUT_VERSION_NUMBER_OVERRIDE" >> "$GITHUB_OUTPUT"
          elif [[ "$_BUMP_VERSION_AUTOMATIC_OUTCOME" == "success" ]]; then
            echo "version=$_CALCULATE_NEXT_VERSION" >> "$GITHUB_OUTPUT"
          fi

      - name: Check if version changed
        id: version-changed
        run: |
          if [ -n "$(git status --porcelain)" ]; then
            echo "changes_to_commit=TRUE" >> "$GITHUB_OUTPUT"
          else
            echo "changes_to_commit=FALSE" >> "$GITHUB_OUTPUT"
            echo "No changes to commit!";
          fi

      - name: Commit files
        if: ${{ steps.version-changed.outputs.changes_to_commit == 'TRUE' }}
        env:
          _VERSION: ${{ steps.set-final-version-output.outputs.version }}
        run: git commit -m "Bumped version to $_VERSION" -a

      - name: Push changes
        if: ${{ steps.version-changed.outputs.changes_to_commit == 'TRUE' }}
        run: git push
