name: weblate-sync-pot
on:
  schedule:
    # Run this every morning
    - cron: '45 2 * * *'
  # can be run manually on https://github.com/cockpit-project/cockpit/actions
  workflow_dispatch:

jobs:
  pot-upload:
    environment: cockpit-weblate
    runs-on: ubuntu-latest
    permissions: {}
    container:
      image: ghcr.io/cockpit-project/tasks
      options: --user root
    timeout-minutes: 10
    steps:
      # https://github.blog/2022-04-12-git-security-vulnerability-announced/
      - name: Pacify git's permission check
        run: git config --global --add safe.directory /__w/

      - name: Clone source repository
        uses: actions/checkout@v4
        with:
          path: src
          fetch-depth: 0

      - name: Generate .pot file
        run: |
          cd src
          ./autogen.sh
          make po/cockpit.pot

      - name: Clone weblate repository
        uses: actions/checkout@v4
        with:
          path: weblate
          repository: ${{ github.repository }}-weblate
          ssh-key: ${{ secrets.DEPLOY_KEY }}

      - name: Commit .pot to weblate repo
        run: |
          cp src/po/cockpit.pot weblate/cockpit.pot
          git config --global user.name "GitHub Workflow"
          git config --global user.email "cockpituous@cockpit-project.org"
          git -C weblate commit -m "Update source file" -- cockpit.pot
          git -C weblate push
