name: GP Upload Strings

on:
  push:
    branches:
      - "release-*"
    paths:
      - "src/frontend/src/locales/en.json"
  workflow_dispatch:

jobs:
  upload:
    runs-on: ubuntu-latest
    name: Upload English strings to Globalization Pipeline
    environment: GP-test

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Check if this is the latest release branch
        id: check-branch
        run: |
          LATEST=$(git ls-remote --heads https://github.com/${{ github.repository }} 'refs/heads/release-*' \
            | awk '{print $2}' \
            | sed 's|refs/heads/||' \
            | grep -E '^release-[0-9]+\.[0-9]+\.[0-9]+$' \
            | sort -V \
            | tail -n 1)
          CURRENT="${{ github.ref_name }}"
          if [ "$CURRENT" != "$LATEST" ]; then
            echo "skip=true" >> "$GITHUB_OUTPUT"
            echo "::notice::Skipping upload — $CURRENT is not the latest release branch ($LATEST)"
          else
            echo "skip=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Set up Python
        if: steps.check-branch.outputs.skip != 'true'
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Install dependencies
        if: steps.check-branch.outputs.skip != 'true'
        run: pip install -r scripts/gp/requirements.txt

      - name: Upload strings to GP
        if: steps.check-branch.outputs.skip != 'true'
        working-directory: scripts/gp
        env:
          GP_ADMIN_USER_ID: ${{ secrets.GP_ADMIN_USER_ID }}
          GP_ADMIN_PASSWORD: ${{ secrets.GP_ADMIN_PASSWORD }}
          GP_INSTANCE: ${{ vars.GP_INSTANCE }}
          GP_BUNDLE: ${{ vars.GP_BUNDLE }}
        run: python upload_strings.py --source ../../src/frontend/src/locales/en.json
