on:
  push:
    branches: [ master ]

name: Update translation templates
jobs:
  update-templates:
    name: Update translation templates
    runs-on: ubuntu-24.04
    container:
      image: archlinux
    steps:
      # install git first otherwise checkout will not give us an actual git repo
      - name: Install git
        run: pacman -Syu --noconfirm git
    
      - name: Checkout
        uses: actions/checkout@v6.0.2
        with:
          path: master-head

      - name: Checkout
        uses: actions/checkout@v6.0.2
        with:
          path: master-latest-po-change
          # grab all commits and all repo data
          fetch-depth: 0

      - name: Checkout commit of latest po file change
        run: |
          cd master-latest-po-change
          last_commit=$(git rev-list -1 HEAD -- *.po *.pot)
          git checkout "$last_commit"

      - name: Install deps
        run: |
          # only install deps for translation templates
          pacman -Syu --noconfirm kde-dev-scripts intltool diffutils

      # workaround upstream permissions issue github.com/peter-evans/create-pull-request/issues/1170
      - name: Change git permissions
        run: |
          git config --global --add safe.directory "$PWD/master-head"
          git config --global --add safe.directory "$PWD/master-latest-po-change"

      # it is necessary to normalize the po files as much as possible
      # as otherwise we will have translations moving around everywhere as the translation scripts and weblate tend to pointlessly reorder the contents of these files 
      # so compare master with the last commit we have where either weblate, this script, or someone else modified the translation files
      # so if there have been changes to source code strings since the last translation commit, we know we ought to update everything
      - name: Run extract translation messages script
        run: |
          cd master-head/util
          ./extract-translation-messages.sh

          cd ../..

          cd master-latest-po-change/util
          ./extract-translation-messages.sh

      # we only want changes that will be genuinely useful to translators
      # the lines starting with msgid and msgstr are the ones containing the original string and translated versions, respectively (and there is also occasionally e.g. msgctxt)
      # we don't really care about line number changes or even source file changes as those do not affect the translator workflow, the translators mostly only care about the strings themselves
      # also any other changes like modifications to POT-Creation-Date are also not useful
      # keep in mind this only modifies translations in the po directory, and not the po_news directory
      - name: Check for meaningful line changes
        run: |
          # do some special error handling for diff, since it exits with exit code 1 if changes happen, instead 2 or above indicates an actual error
          # first check we actually have diff installed
          diff --version 1>/dev/null
          set +e
          DIFF_LINES=$(diff --unified=0 --recursive master-latest-po-change/po master-head/po)
          # only these codes indicates an actual diff error
          if [[ "$?" -ge "2" ]]; then
            echo "Could not create diff for translation changes"
            exit 1
          fi
          set -e

          # do similar special error handling for grep, since it exits with exit code 1 if nothing is matched, instead 2 or above indicates an actual error
          # first check we actually have grep installed
          grep --version 1>/dev/null
          set +e
          DIFF_LINES_MSG=$(echo "$DIFF_LINES" | grep '^[+-]msg')
          # only these codes indicates an actual diff error
          if [[ "$?" -ge "2" ]]; then
            echo "Could not match diff with grep for translation changes"
            exit 1
          fi
          set -e

          if [[ "$DIFF_LINES_MSG" == "" ]]; then
            echo "The calculated diff does not include modifications to lines starting with msg, such as msgstr or msgid."
            echo "Restoring original files since this is not a valid reason to open a PR."
            cd master-head
            git restore .
            cd ..
          else
            echo "The calculated diff includes modifications to lines starting with msg, such as msgstr or msgid."
            echo "Letting the PR continue as this is a valid reason to open a PR."
          fi

          echo "All diff lines count: $(echo "$DIFF_LINES" | wc -l)"
          echo "All filtered diff lines count: $(echo "$DIFF_LINES_MSG" | wc -l)"

      # TODO it would be ideal to refresh metainfo fully by copying release notes from the upcoming release in NEWS.yaml to metainfo, and then running the above update template script,
      # however this is not possible without putting a dummy release in the metainfo with said upcoming release notes which would later have to be adjusted to the real release.
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v8.1.1
        with:
          title: Update translation templates
          body: Ensure to merge all other Pull Requests before merging this. Conflicts here will automatically be resolved in case of changes in master.
          commit-message: Update translation templates
          # email sets "EasyEffects Bot" user as commit author
          committer: EasyEffects Bot <110548574+easyeffects-bot@users.noreply.github.com>
          author: EasyEffects Bot <110548574+easyeffects-bot@users.noreply.github.com>
          branch: automatically-update-translation-templates
          # the easyeffects_bot secret/token is only valid for a fork
          # note the machine user (easyeffects-bot) must have already forked the repository  where the action is running for this to work
          push-to-fork: easyeffects-bot/easyeffects
          token: ${{ secrets.EASYEFFECTS_BOT }}
          path: master-head
