name: Sync PKGBUILD version
run-name: "Sync PKGBUILD: ${{ github.event.pull_request.title }}"
on:
  pull_request_target:
    types:
      - opened
      - synchronize
      - reopened
jobs:
  sync:
    if: github.event.pull_request.head.repo.full_name == github.repository && startsWith(github.head_ref, 'release-plz-')
    runs-on: self-hosted
    container:
      image: archlinux:latest
    defaults:
      run:
        shell: bash
    permissions:
      contents: write
    steps:
      - name: Checkout workflow repo
        uses: actions/checkout@v5
      - name: Setup Arch container
        uses: ./.github/actions/setup-arch
        with:
          packages: git python pacman-contrib sudo
          include-test-base: "false"
      - name: Create builder user
        run: |
          useradd -m builder
          echo "builder ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/builder
          chmod 440 /etc/sudoers.d/builder
      - name: Checkout release PR branch
        uses: actions/checkout@v5
        with:
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}
      - name: Configure git user
        run: |
          git config --global --add safe.directory "$GITHUB_WORKSPACE"
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
      - name: Sync PKGBUILD version
        run: |
          set -euo pipefail

          # Check for merge conflict markers in Cargo.toml before parsing
          if grep -q "<<<<<<<\|=======\|>>>>>>>" Cargo.toml; then
            echo "Error: Cargo.toml contains merge conflict markers. Cannot parse version."
            echo "Please resolve merge conflicts before running sync-pkgbuild."
            exit 1
          fi

          version="$(python3 - <<'PY'
          import sys

          try:
              import tomllib  # Python 3.11+
          except ModuleNotFoundError:  # pragma: no cover - fallback for older interpreters
              import tomli as tomllib

          with open("Cargo.toml", "rb") as f:
              data = tomllib.load(f)

          print(data["package"]["version"])
          PY
          )"
          pkgbuild_file="pkgbuild/ins/PKGBUILD"
          file_version="$(awk -F '=' '/^pkgver=/ {print $2; exit}' "$pkgbuild_file")"
          pkgrel="$(awk -F '=' '/^pkgrel=/ {print $2; exit}' "$pkgbuild_file")"
          if [[ "$file_version" != "$version" ]]; then
            sed -i "s/^pkgver=.*/pkgver=$version/" "$pkgbuild_file"
            sed -i "s/^pkgrel=.*/pkgrel=1/" "$pkgbuild_file"
          fi
          if ! git diff --quiet "$pkgbuild_file"; then
            # Copy PKGBUILD to builder's home and generate .SRCINFO as builder user
            cp "$pkgbuild_file" /home/builder/PKGBUILD
            chown builder:builder /home/builder/PKGBUILD
            srcinfo="$(su builder -c 'cd /home/builder && makepkg --printsrcinfo -p PKGBUILD')"
            printf '%s\n' "$srcinfo" > pkgbuild/ins/.SRCINFO
            git add "$pkgbuild_file" pkgbuild/ins/.SRCINFO
            git commit -m "chore: sync PKGBUILD version"
            git push
          fi
