name: Validate Fedora x86_64-v3 Copr SRPMs (Gitea)

on:
  workflow_dispatch:
    inputs:
      package_input:
        description: "Optional comma, space, or newline separated package list. Leave blank to use packages.txt."
        required: false
        default: ""
        type: string
  push:
    paths:
      - '.copr/Makefile'
      - '.gitea/workflows/build-v3-rpms.yml'
      - 'ci/copr-distgit-make-srpm.py'
      - 'ci/prefetch-fedora-specs.py'
      - 'packages.txt'
      - 'packaging/copr-rpm-macros-x86-64-v3.spec'
      - 'SPECS/**'

jobs:
  validate-srpms:
    runs-on: ubuntu-latest
    container:
      image: ghcr.io/funkemunky/kde-x86_64-v4-fedora-rpm-builder:latest
    strategy:
      fail-fast: false
      matrix:
        shard:
          - "001"
          - "002"
          - "003"
          - "004"
          - "005"
          - "006"
          - "007"
          - "008"
          - "009"
          - "010"
          - "011"
          - "012"
          - "013"
          - "014"
          - "015"
          - "016"
          - "017"
          - "018"
          - "019"
          - "020"
          - "021"
          - "022"
          - "023"
          - "024"
          - "025"
          - "026"
          - "027"
          - "028"
          - "029"
          - "030"
          - "031"
          - "032"
          - "033"
          - "034"
          - "035"
          - "036"
          - "037"
          - "038"
          - "039"
          - "040"
          - "041"
          - "042"
          - "043"
          - "044"
          - "045"
          - "046"
          - "047"
          - "048"
          - "049"
          - "050"
          - "051"
          - "052"
          - "053"
          - "054"
          - "055"
          - "056"
          - "057"
          - "058"
          - "059"
          - "060"
          - "061"
          - "062"
          - "063"
          - "064"
          - "065"
          - "066"
          - "067"
          - "068"
          - "069"
          - "070"
          - "071"
          - "072"
          - "073"
          - "074"
          - "075"
          - "076"
          - "077"
          - "078"
          - "079"
          - "080"
          - "081"
          - "082"
          - "083"
          - "084"
          - "085"
          - "086"
          - "087"
          - "088"
          - "089"
          - "090"
          - "091"
          - "092"
          - "093"
          - "094"
          - "095"
          - "096"
          - "097"
          - "098"
          - "099"
          - "100"
          - "101"
          - "102"
          - "103"
          - "104"
          - "105"
          - "106"
          - "107"
          - "108"
          - "109"
          - "110"
          - "111"
          - "112"
          - "113"
          - "114"
          - "115"
          - "116"
          - "117"
          - "118"
          - "119"
          - "120"
          - "121"
          - "122"
          - "123"
          - "124"
          - "125"
          - "126"
          - "127"
          - "128"
    steps:
      - uses: actions/checkout@v4

      - name: Install SRPM validation tools
        run: |
          dnf -y install git make python3 rpm-build ca-certificates curl

      - name: Generate shard SRPMs
        env:
          PACKAGE_INPUT: ${{ inputs.package_input }}
          SHARD_LABEL: ${{ matrix.shard }}
          MAX_SHARDS: "128"
        run: |
          python3 <<'PY'
          import os
          import re
          import subprocess
          from pathlib import Path

          workspace = Path(os.environ["GITHUB_WORKSPACE"])
          packages_file = workspace / "packages.txt"
          package_input = os.environ.get("PACKAGE_INPUT", "")
          shard_label = os.environ["SHARD_LABEL"]
          shard_index = int(shard_label)
          max_shards = int(os.environ["MAX_SHARDS"])

          if package_input.strip():
              all_packages = [
                  entry
                  for entry in re.split(r"[\s,]+", package_input.strip())
                  if entry
              ]
          else:
              if not packages_file.exists():
                  raise SystemExit("packages.txt not found")
              all_packages = [
                  line.strip()
                  for line in packages_file.read_text(encoding="utf-8").splitlines()
                  if line.strip() and not line.startswith("#")
              ]

          shard_count = min(len(all_packages), max_shards)
          if shard_count == 0 or shard_index > shard_count:
              print(
                  f"Skipping shard {shard_label}; "
                  f"package_count={len(all_packages)} shard_count={shard_count}"
              )
              raise SystemExit(0)

          shard_size = max(1, -(-len(all_packages) // shard_count))
          start_index = (shard_index - 1) * shard_size
          end_index = start_index + shard_size
          packages = all_packages[start_index:end_index]

          outdir = workspace / "artifacts" / f"srpm-shard-{shard_label}"
          outdir.mkdir(parents=True, exist_ok=True)

          subprocess.run(
              [
                  "make",
                  "-f",
                  ".copr/Makefile",
                  "srpm",
                  f"outdir={outdir}",
                  "spec=packaging/copr-rpm-macros-x86-64-v3.spec",
              ],
              check=True,
              cwd=workspace,
          )

          for package_name in packages:
              subprocess.run(
                  [
                      "make",
                      "-f",
                      ".copr/Makefile",
                      "srpm",
                      f"outdir={outdir}",
                      f"spec={package_name}",
                  ],
                  check=True,
                  cwd=workspace,
              )
          PY

      - name: Upload SRPM artifacts
        uses: https://github.com/christopherHX/gitea-upload-artifact@v4
        with:
          name: srpm-shard-${{ matrix.shard }}
          path: artifacts/srpm-shard-${{ matrix.shard }}/
          if-no-files-found: ignore
