name: Repository management

on:
  workflow_dispatch:
    inputs:
      task:
        default: "Version Bump"
        description: "Task to execute"
        options:
          - "Version Bump"
          - "Version Bump and Cut rc"
          - "Version Bump and Cut hotfix-rc"
        required: true
        type: choice
      target_ref:
        default: "main"
        description: "Branch/Tag to target for cut"
        required: true
        type: string
      version_number_override:
        description: "New version override (leave blank for automatic calculation, example: '2024.1.0')"
        required: false
        type: string

permissions: {}

jobs:
  setup:
    name: Setup
    runs-on: ubuntu-24.04
    outputs:
      branch: ${{ steps.set-branch.outputs.branch }}
    permissions: {}
    steps:
      - name: Set branch
        id: set-branch
        env:
          TASK: ${{ inputs.task }}
        run: |
          if [[ "$TASK" == "Version Bump" ]]; then
            BRANCH="none"
          elif [[ "$TASK" == "Version Bump and Cut rc" ]]; then
            BRANCH="rc"
          elif [[ "$TASK" == "Version Bump and Cut hotfix-rc" ]]; then
            BRANCH="hotfix-rc"
          fi

          echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"

  bump_version:
    name: Bump Version
    if: ${{ always() }}
    runs-on: ubuntu-24.04
    needs:
      - setup
    outputs:
      version: ${{ steps.set-final-version-output.outputs.version }}
    permissions:
      id-token: write

    steps:
      - 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: Validate version input format
        if: ${{ inputs.version_number_override != '' }}
        uses: bitwarden/gh-actions/version-check@main
        with:
          version: ${{ inputs.version_number_override }}

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

      - name: Check out branch
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          ref: main
          token: ${{ steps.app-token.outputs.token }}
          persist-credentials: true

      - name: Configure Git
        run: |
          git config --local user.email "actions@github.com"
          git config --local user.name "Github Actions"

      - name: Install xmllint
        run: |
          sudo apt-get update
          sudo apt-get install -y libxml2-utils

      - name: Get current version
        id: current-version
        run: |
          CURRENT_VERSION=$(xmllint -xpath "/Project/PropertyGroup/Version/text()" Directory.Build.props)
          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 "Specified override version is the same as the current version." >> "$GITHUB_STEP_SUMMARY"
            exit 1
          fi

          # Check if version is newer.
          if printf '%s\n' "${CURRENT_VERSION}" "${NEW_VERSION}" | sort -C -V; then
            echo "Version is newer than the current version."
          else
            echo "Version is older than the current version." >> "$GITHUB_STEP_SUMMARY"
            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 props - Version Override
        if: ${{ inputs.version_number_override != '' }}
        id: bump-version-override
        uses: bitwarden/gh-actions/version-bump@main
        with:
          file_path: "Directory.Build.props"
          version: ${{ inputs.version_number_override }}

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

      - name: Set final version output
        id: set-final-version-output
        env:
          VERSION: ${{ inputs.version_number_override }}
          BUMP_VERSION_OVERRIDE_OUTCOME: ${{ steps.bump-version-override.outcome }}
          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=$VERSION" >> "$GITHUB_OUTPUT"
          elif [[ "${BUMP_VERSION_AUTOMATIC_OUTCOME}" = "success" ]]; then
            echo "version=${CALCULATE_NEXT_VERSION}" >> "$GITHUB_OUTPUT"
          fi

      - name: Commit files
        env:
          FINAL_VERSION: ${{ steps.set-final-version-output.outputs.version }}
        run: git commit -m "Bumped version to $FINAL_VERSION" -a

      - name: Push changes
        run: git push

  cut_branch:
    name: Cut branch
    if: ${{ needs.setup.outputs.branch != 'none' }}
    needs:
      - setup
      - bump_version
    runs-on: ubuntu-24.04
    permissions:
      id-token: write

    steps:
      - 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@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
        id: app-token
        with:
          app-id: ${{ steps.get-kv-secrets.outputs.BW-GHAPP-ID }}
          private-key: ${{ steps.get-kv-secrets.outputs.BW-GHAPP-KEY }}
          permission-contents: write

      - name: Check out target ref
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          ref: ${{ inputs.target_ref }}
          token: ${{ steps.app-token.outputs.token }}
          persist-credentials: true
          fetch-depth: 0

      - name: Check if ${{ needs.setup.outputs.branch }} branch exists
        env:
          BRANCH_NAME: ${{ needs.setup.outputs.branch }}
        run: |
          if [[ $(git ls-remote --heads origin "$BRANCH_NAME") ]]; then
            echo "$BRANCH_NAME already exists! Please delete $BRANCH_NAME before running again." >> "$GITHUB_STEP_SUMMARY"
            exit 1
          fi

      - name: Cut branch
        env:
          BRANCH_NAME: ${{ needs.setup.outputs.branch }}
        run: |
          git switch --quiet --create "$BRANCH_NAME"
          git push --quiet --set-upstream origin "$BRANCH_NAME"

  move_edd_db_scripts:
    name: Move EDD database scripts
    needs: cut_branch
    permissions: {}
    uses: ./.github/workflows/_move_edd_db_scripts.yml
