From 8ea5138d84555a5615419b4c53ba9687379ad283 Mon Sep 17 00:00:00 2001
From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
Date: Wed, 10 Jul 2024 19:06:44 -0400
Subject: [PATCH] feat(repo): add beta/pre-release packages

---
 .github/workflows/build-repo.yml | 127 ++++++++++++++++++++++++++++---
 README.md                        |   9 +++
 beta-repos.conf                  |   2 +
 build-pacman-repo.yaml           |  19 -----
 4 files changed, 128 insertions(+), 29 deletions(-)
 create mode 100644 beta-repos.conf
 delete mode 100644 build-pacman-repo.yaml

diff --git a/.github/workflows/build-repo.yml b/.github/workflows/build-repo.yml
index 4cb1f30..ccdea64 100644
--- a/.github/workflows/build-repo.yml
+++ b/.github/workflows/build-repo.yml
@@ -17,22 +17,31 @@ jobs:
   build-pacman-repo:
     runs-on: ubuntu-latest
     container:
-      image: archlinux:base-devel
+      image: archlinux/archlinux:base-devel
     env:
       DISPLAY: ":1"
+    strategy:
+      fail-fast: false
+      matrix:
+        include:
+          - repo: lizardbyte
+            release_name: stable
+          - repo: lizardbyte-beta
+            release_name: beta
     steps:
       - name: Container Setup
         run: |
+          pacman -Syy --disable-download-timeout --needed --noconfirm \
+            archlinux-keyring
           pacman -Syu --disable-download-timeout --needed --noconfirm \
-            archlinux-keyring \
+            base-devel \
+            cmake \
+            cuda \
             git \
-            reflector \
+            jq \
             wget \
             xorg-server-xvfb
-
-      - name: Update mirrors
-        run: |
-          reflector --latest 10 --protocol http,https --sort rate --save /etc/pacman.d/mirrorlist
+          pacman -Scc --noconfirm
 
       - name: Download Pacman Repo Builder
         env:
@@ -44,17 +53,114 @@ jobs:
             -O /usr/bin/build-pacman-repo
           chmod +x /usr/bin/build-pacman-repo
 
+          # patch makepkg to allow running as root
+          build-pacman-repo patch-makepkg --replace
+
       - name: Checkout
         uses: actions/checkout@v4
 
+      - name: Download and Patch beta PKGBUILDs
+        if: matrix.repo == 'lizardbyte-beta'
+        run: |
+          # we don't want any stable packages to be built, so remove them
+          rm -rf pkgbuilds
+          mkdir -p pkgbuilds
+          cd pkgbuilds
+
+          while IFS=' ' read -r repo og_pkg_name updated_pkg_name release_asset; do
+            # Skip lines that start with #
+            [[ $repo =~ ^#.*$ ]] && continue
+
+            echo "repo: ${repo}"
+            echo "og_pkg_name: ${og_pkg_name}"
+            echo "updated_pkg_name: ${updated_pkg_name}"
+            echo "release_asset: ${release_asset}"
+
+            # get the first release with the specified asset (pre-releases first, followed by stable)
+            latest_release=$(
+              curl -s "https://api.github.com/repos/${repo}/releases" | \
+              jq -r --arg release_asset "${release_asset}" '
+                map(.assets[] | select(.name == $release_asset) | .browser_download_url) |
+                first
+              '
+            )
+            echo "latest_release: ${latest_release}"
+            if [[ -z "${latest_release}" ]]; then
+              echo "::warning:: No pre-release found for ${repo} with asset ${release_asset}"
+              continue
+            fi
+
+            mkdir -p ${updated_pkg_name}
+            pushd ${updated_pkg_name}
+
+            wget "${latest_release}" -O "${updated_pkg_name}.pkg.tar.gz"
+
+            # extract the package
+            tar -xvf "${updated_pkg_name}.pkg.tar.gz"
+            rm "${updated_pkg_name}.pkg.tar.gz"
+
+            # sed PKGBUILD
+            sed -i "s/pkgname='${og_pkg_name}'/pkgname='${updated_pkg_name}'/" PKGBUILD
+            sed -i "s/provides=(.*)/provides=('${og_pkg_name}')/" PKGBUILD
+            sed -i "s/conflicts=(.*)/conflicts=('${og_pkg_name}')/" PKGBUILD
+
+            # Check if provides is missing and append if necessary
+            if ! grep -q "^provides=" PKGBUILD; then
+              echo "provides=('${og_pkg_name}')" >> PKGBUILD
+            fi
+
+            # Check if conflicts is missing and append if necessary
+            if ! grep -q "^conflicts=" PKGBUILD; then
+              echo "conflicts=('${og_pkg_name}')" >> PKGBUILD
+            fi
+
+            echo "new PKGBUILD:"
+            cat PKGBUILD
+
+            # re-generate .SRCINFO
+            makepkg --printsrcinfo > .SRCINFO
+
+            # list files
+            echo "files in ${updated_pkg_name}:"
+            ls -a
+
+            popd
+          done < ../beta-repos.conf
+
       - name: Init
         run: |
+          # create the destination directory for built packages
           mkdir -p repo
+
+          # patch the build flags
+          sed -i 's,#MAKEFLAGS="-j2",MAKEFLAGS="-j$(nproc)",g' /etc/makepkg.conf
+
+          # start Xvfb which may be used for unit tests
           Xvfb ${DISPLAY} -screen 0 1024x768x24 &
 
       - name: Build Pacman Repo
         run: |
-          build-pacman-repo patch-makepkg --replace
+          source /etc/profile  # ensure cuda is in the PATH
+
+          # generate build-pacman-repo.yaml
+          build-pacman-repo print-config \
+            --repository repo/${{ matrix.repo }}.db.tar.gz \
+            --container pkgbuilds \
+            --require-pkgbuild \
+            --require-srcinfo \
+            --with-record-failed-builds repo/failed-build-records.yaml \
+            --with-install-missing-dependencies true \
+            --with-clean-before-build false \
+            --with-clean-after-build false \
+            --with-force-rebuild true \
+            --with-pacman pacman \
+            --with-arch-filter x86_64 \
+            --with-check inherit \
+            --with-packager "${{ secrets.GH_BOT_NAME }} <${{ secrets.GH_BOT_EMAIL }}>" \
+            --with-allow-failure false \
+            --with-dereference-database-symlinks true \
+            > build-pacman-repo.yaml
+
           build-pacman-repo build
 
       - name: Create/Update GitHub Release
@@ -66,7 +172,8 @@ jobs:
           artifacts: repo/*
           bodyFile: README.md
           commit: master
-          name: repo
+          makeLatest: ${{ matrix.repo == 'lizardbyte' && 'true' || 'false' }}
+          name: ${{ matrix.release_name }}
           prerelease: false
-          tag: repo
+          tag: ${{ matrix.release_name }}
           token: ${{ secrets.GH_BOT_TOKEN }}
diff --git a/README.md b/README.md
index ec3d464..b3c4338 100644
--- a/README.md
+++ b/README.md
@@ -17,6 +17,12 @@ SigLevel = Optional
 Server = https://github.com/LizardByte/pacman-repo/releases/latest/download
 ```
 
+```conf
+[lizardbyte-beta]
+SigLevel = Optional
+Server = https://github.com/LizardByte/pacman-repo/releases/beta/download
+```
+
 Then, run `sudo pacman -Sy` to update repository.
 
 ## Packages
@@ -25,17 +31,20 @@ Then, run `sudo pacman -Sy` to update repository.
 
 ```bash
 pacman -Sl lizardbyte
+pacman -Sl lizardbyte-beta
 ```
 
 ### Install repo packages
 
 ```bash
 sudo pacman -S lizardbyte/<package-name>
+sudo pacman -S lizardbyte-beta/<package-name>
 ```
 
 e.g.
 ```bash
 sudo pacman -S lizardbyte/sunshine
+sudo pacman -S lizardbyte-beta/sunshine-git
 ```
 
 ## Considerations
diff --git a/beta-repos.conf b/beta-repos.conf
new file mode 100644
index 0000000..9b0afd0
--- /dev/null
+++ b/beta-repos.conf
@@ -0,0 +1,2 @@
+# repo_owner/repo_name og_pkg_name updated_pkg_name release_asset_file_name
+lizardbyte/sunshine sunshine sunshine-git sunshine.pkg.tar.gz
diff --git a/build-pacman-repo.yaml b/build-pacman-repo.yaml
deleted file mode 100644
index fc88689..0000000
--- a/build-pacman-repo.yaml
+++ /dev/null
@@ -1,19 +0,0 @@
----
-global-settings:
-  repository: repo/lizardbyte.db.tar.gz
-  container: pkgbuilds
-  read-build-metadata: either
-  record-failed-builds: failed-builds.yaml
-  install-missing-dependencies: true
-  clean-before-build: false
-  clean-after-build: false
-  force-rebuild: true
-  pacman: pacman
-  arch-filter: [x86_64]
-  check: inherit
-  packager: LizardByte-bot <108553330+LizardByte-bot@users.noreply.github.com>
-  allow-failure: false
-  dereference-database-symlinks: true
-members:
-  - directory: sunshine
-    read-build-metadata: pkgbuild