name: 1 | Push main -> staging

# This CI job is responsible for pushing the current contents of the `main` branch to the
# `releases/staging` branch, which will in turn kick off a deploy to the staging environment.

on:
  workflow_dispatch:

# https://stackoverflow.com/questions/57921401/push-to-origin-from-github-action
jobs:
  push-staging:
    name: 'Push to staging branch'
    runs-on: ubuntu-latest
    environment:
      name: push/staging
    steps:
      - name: Check test status
        uses: actions/github-script@v6.4.1
        with:
          script: |
            const statuses = await github.rest.repos.listCommitStatusesForRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: context.sha
            })
            const status = statuses.data.find(status => status.context === 'Test / promotion')?.state || 'missing'
            core.info('Status: ' + status)
            if (status !== 'success') {
              core.setFailed('"Test / promotion" must be successful before pushing')
            }

      - uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
        with:
          token: ${{ secrets.RELEASE_SERVICE_ACCESS_TOKEN }}
          ref: main

      # The source file must exist for the corresponding translation messages to be downloaded.
      - run: touch src/locales/en-US.po
      - name: Download translations
        uses: crowdin/github-action@3133cc916c35590475cf6705f482fb653d8e36e9
        with:
          upload_sources: false
          download_translations: true
          project_id: 458284
          token: ${{ secrets.CROWDIN_PERSONAL_TOKEN_SECRET }}
          source: 'src/locales/en-US.po'
          translation: 'src/locales/%locale%.po'
          localization_branch_name: main
          create_pull_request: false
          push_translations: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Git config
        run: |
          git config user.name 'UL Service Account'
          git config user.email 'hello-happy-puppy@users.noreply.github.com'

      - name: Add translations
        run: |
          rm src/locales/en-US.po
          git add -f src/locales/*.po
          git commit -m 'ci(t9n): download translations from crowdin'

      - name: Add CODEOWNERS
        run: |
          echo '* @uniswap/web-admins' > CODEOWNERS
          git add CODEOWNERS
          git commit -m 'ci: add global CODEOWNERS'

      - name: Git push
        run: |
          git push origin main:releases/staging --force
