Skip to content

🎮 Smart Game List Updater (Auto PR) #2498

🎮 Smart Game List Updater (Auto PR)

🎮 Smart Game List Updater (Auto PR) #2498

name: 🎮 Smart Game List Updater (Auto PR)
on:
push:
branches: [ main ]
pull_request:
types: [closed]
workflow_dispatch:
schedule:
- cron: "0 * * * *" # Run every 1 hour (UTC)
permissions:
contents: write
pull-requests: write
jobs:
update-game-list:
name: 🧾 Generate Game List and Create PR
runs-on: ubuntu-latest
if: github.event_name != 'pull_request' || github.event.pull_request.merged == true
steps:
- name: 🧩 Checkout repository
uses: actions/checkout@v4
- name: 📦 Get current folder structure
id: current
run: |
find content/contribution -mindepth 1 -maxdepth 1 -type d | sort > current_games.txt
md5sum current_games.txt | cut -d " " -f1 > current_hash.txt
echo "hash=$(cat current_hash.txt)" >> $GITHUB_OUTPUT
- name: 💾 Get previous folder hash (safe mode)
id: previous
run: |
if [ -f .games_hash ]; then
# Ambil baris pertama yang valid (32 karakter hex)
HASH_LINE=$(head -n 1 .games_hash | tr -d '\r\n')
# Validasi format hash
if echo "$HASH_LINE" | grep -Eq '^[0-9a-f]{32}$'; then
echo "Previous hash: $HASH_LINE"
echo "hash=$HASH_LINE" >> $GITHUB_OUTPUT
else
echo "⚠️ Invalid or multiple hashes detected in .games_hash. Resetting it..."
echo "hash=none" >> $GITHUB_OUTPUT
echo "" > .games_hash
fi
else
echo "hash=none" >> $GITHUB_OUTPUT
fi
- name: 🔍 Compare folder list
id: compare
run: |
if [ "${{ steps.current.outputs.hash }}" = "${{ steps.previous.outputs.hash }}" ]; then
echo "same=true" >> $GITHUB_OUTPUT
echo "✅ Folder structure unchanged, skipping update."
else
echo "same=false" >> $GITHUB_OUTPUT
echo "🔄 Folder structure changed, regenerating list-game.md..."
fi
- name: 🧠 Generate list-game.md (only if changed)
if: steps.compare.outputs.same == 'false'
run: |
echo "# 🎮 List of JavaScript Games" > list-game.md
echo "" >> list-game.md
echo "Automatically generated from the [content/contribution](./content/contribution) folder." >> list-game.md
echo "" >> list-game.md
echo "| No. | Game Title | Path |" >> list-game.md
echo "|:--:|:----------------|:----------------------------|" >> list-game.md
i=1
for d in content/contribution/*/ ; do
game_name=$(basename "$d")
title=$(echo "$game_name" | sed -r 's/-/ /g' | sed -r 's/\b(.)/\u\1/g')
echo "| $i | **$title** | [./$d](./$d) |" >> list-game.md
((i++))
done
echo "" >> list-game.md
echo "> 🧩 Auto-updated on $(date +'%Y-%m-%d %H:%M:%S')" >> list-game.md
- name: 🔑 Authenticate GitHub CLI
if: steps.compare.outputs.same == 'false'
run: |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token
- name: 🪄 Commit and create pull request safely
if: steps.compare.outputs.same == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
cp current_hash.txt .games_hash
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "GitHub Action"
branch="auto/update-game-list"
# Save change for secure branch checkout
git add list-game.md .games_hash
git stash push -m "temp-changes"
# Check if the branch already exists
if git ls-remote --exit-code origin "$branch" >/dev/null 2>&1; then
echo "🌿 Branch $branch already exists. Updating..."
git fetch origin "$branch"
git checkout "$branch"
git pull origin "$branch" --rebase || echo "⚠️ Rebase skipped"
else
echo "🌱 Creating new branch $branch..."
git checkout -b "$branch"
fi
# Reapply saved changes
git stash pop || echo "ℹ️ Nothing to pop, continue..."
# Commit & Push
git add list-game.md .games_hash
git commit -m "🎮 Auto update game list" || echo "No new commits to make"
git push origin "$branch" --force-with-lease
# Create PR only if it doesn't exist yet
gh pr list --head "$branch" --json number --jq '.[0].number' | grep -q '[0-9]' \
&& echo "⚠️ PR already exists, skipping creation." \
|| gh pr create \
--title "🎮 Auto update game list" \
--body "This PR automatically updates \`list-game.md\` based on new folders in \`content/contribution/\`." \
--base main \
--head "$branch"