name: Download translation files from Localazy
on:
  workflow_dispatch:
    secrets:
      ELEMENT_BOT_TOKEN:
        required: true

jobs:
  download:
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - name: Checkout the code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          persist-credentials: false

      - name: Enable Corepack
        run: corepack enable

      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
        with:
          cache: "pnpm"
          node-version-file: ".node-version"

      - name: Install Deps
        # ignore-pnpmfile should never be commited. Make CI crash if it happened (`pnpmfileChecksum` is present)
        run: "pnpm install --frozen-lockfile --ignore-pnpmfile"

      - name: Prune i18n
        run: "rm -R locales"

      - name: Download translation files
        uses: localazy/download@0a79880fb66150601e3b43606fab69c88123c087 # v1.1.0
        with:
          groups: "-p includeSourceLang:true"

      - name: Fix the owner of the downloaded files
        run: "sudo chown runner:docker -R locales"

      - name: Formatting
        run: pnpm format

      - name: Create Pull Request
        id: cpr
        uses: peter-evans/create-pull-request@22a9089034f40e5a961c8808d113e2c98fb63676 # v7.0.11
        with:
          token: ${{ secrets.ELEMENT_BOT_TOKEN }}
          branch: actions/localazy-download
          delete-branch: true
          title: Localazy Download
          commit-message: Translations updates
          labels: |
            T-Task

      - name: Enable automerge
        run: gh pr merge --merge --auto "$PR_NUMBER"
        if: steps.cpr.outputs.pull-request-operation == 'created'
        env:
          GH_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
          PR_NUMBER: ${{ steps.cpr.outputs.pull-request-number }}
