Skip to content

SDL_GameControllerDB Monitor #33

SDL_GameControllerDB Monitor

SDL_GameControllerDB Monitor #33

# SDL_GameControllerDB Monitor Workflow
# This workflow monitors the SDL_GameControllerDB repository for updates
# and automatically processes the database file when changes are detected.
# Also monitors local add_gamecontrollerdb.txt file for changes.
#
# Features:
# - Daily automated checks for repository updates
# - Monitors local custom mappings file for changes
# - Filters out unwanted platforms (Windows, Mac OS X, Android, iOS)
# - Adds custom controller mappings
# - Removes duplicate entries by GUID
# - Commits changes automatically
name: SDL_GameControllerDB Monitor
on:
# Manual trigger - allows running the workflow on demand
workflow_dispatch:
# Scheduled trigger - runs daily at midnight UTC
schedule:
- cron: "0 0 * * *" # Daily execution at midnight UTC
# Push trigger - runs when changes are pushed to main branch
push:
branches: ["main"]
paths:
- "add_gamecontrollerdb.txt" # Trigger when custom mappings file changes
jobs:
check-and-update:
runs-on: ubuntu-latest
# Required permissions to write to repository
permissions:
contents: write
steps:
# Step 1: Checkout the current repository
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history
# Step 2: Get the latest commit from the external repository
- name: Get latest commit from external repository
id: check-remote
run: |
# Remote repository URL
REMOTE_URL="https://github.com/mdqinc/SDL_GameControllerDB.git"
# Get the latest commit hash from the remote repository
LAST_COMMIT=$(git ls-remote $REMOTE_URL HEAD | awk '{print $1}')
# Export the commit hash for use in subsequent steps
echo "last_commit=$LAST_COMMIT" >> $GITHUB_OUTPUT
# Log information for debugging
echo "Latest commit in $REMOTE_URL: $LAST_COMMIT"
echo "Check timestamp: $(date)"
# Step 3: Check for changes in external repo and local custom file
- name: Check for changes
id: check-changes
run: |
CHANGES_DETECTED=false
CHANGE_REASONS=""
# Check external repository changes
if [ -f ".last_known_commit" ]; then
KNOWN_COMMIT=$(cat .last_known_commit)
echo "Last recorded commit: $KNOWN_COMMIT"
echo "New commit found: ${{ steps.check-remote.outputs.last_commit }}"
# Compare commits to detect changes
if [ "$KNOWN_COMMIT" != "${{ steps.check-remote.outputs.last_commit }}" ]; then
CHANGES_DETECTED=true
CHANGE_REASONS="External repository updated"
echo "πŸ”„ Changes detected in external repository!"
fi
else
# First run - no previous commit recorded
CHANGES_DETECTED=true
CHANGE_REASONS="First run - initial setup"
echo "πŸ†• First run - processing initial setup..."
fi
# Check if custom mappings file has changed
if [ -f "add_gamecontrollerdb.txt" ]; then
# Calculate hash of current custom file
CURRENT_HASH=$(sha256sum add_gamecontrollerdb.txt | cut -d' ' -f1)
if [ -f ".last_custom_hash" ]; then
KNOWN_HASH=$(cat .last_custom_hash)
echo "Last recorded custom file hash: $KNOWN_HASH"
echo "Current custom file hash: $CURRENT_HASH"
if [ "$KNOWN_HASH" != "$CURRENT_HASH" ]; then
CHANGES_DETECTED=true
if [ -n "$CHANGE_REASONS" ]; then
CHANGE_REASONS="$CHANGE_REASONS + Custom mappings file updated"
else
CHANGE_REASONS="Custom mappings file updated"
fi
echo "πŸ”§ Changes detected in custom mappings file!"
fi
else
# First time seeing this file
CHANGES_DETECTED=true
if [ -n "$CHANGE_REASONS" ]; then
CHANGE_REASONS="$CHANGE_REASONS + Custom mappings file found"
else
CHANGE_REASONS="Custom mappings file found"
fi
echo "πŸ“ Custom mappings file found for first time"
fi
# Save current hash for next comparison
echo "$CURRENT_HASH" > .last_custom_hash
else
# No custom file exists
if [ -f ".last_custom_hash" ]; then
# Custom file was deleted
CHANGES_DETECTED=true
if [ -n "$CHANGE_REASONS" ]; then
CHANGE_REASONS="$CHANGE_REASONS + Custom mappings file removed"
else
CHANGE_REASONS="Custom mappings file removed"
fi
echo "πŸ—‘οΈ Custom mappings file was removed!"
rm -f .last_custom_hash
fi
fi
# Export results
echo "changes_detected=$CHANGES_DETECTED" >> $GITHUB_OUTPUT
echo "change_reasons=$CHANGE_REASONS" >> $GITHUB_OUTPUT
if [ "$CHANGES_DETECTED" = "true" ]; then
echo "βœ… Changes detected: $CHANGE_REASONS"
else
echo "❌ No changes found."
fi
# Step 4: Process the database file if changes were detected
- name: Process database (if changes detected)
if: steps.check-changes.outputs.changes_detected == 'true'
run: |
echo "πŸ”„ Processing changes: ${{ steps.check-changes.outputs.change_reasons }}"
# Record the new commit hash
echo "${{ steps.check-remote.outputs.last_commit }}" > .last_known_commit
# Clone the external repository
if [ -d "SDL_GameControllerDB" ]; then
rm -rf SDL_GameControllerDB
fi
git clone https://github.com/mdqinc/SDL_GameControllerDB.git
# Process the controller database file
cd SDL_GameControllerDB
# 1. Filter out unwanted platforms
# Remove entries for Windows, Mac OS X, Android, iOS and empty lines
grep -v -e "Windows" -e "Mac OS X" -e "Android" -e "iOS" -e '^[[:space:]]*$' gamecontrollerdb.txt > filtered.txt
echo "βœ… Platform filtering completed"
# 2. Add custom controller mappings
# If custom mappings file exists, append it to the filtered database
if [ -f "$GITHUB_WORKSPACE/add_gamecontrollerdb.txt" ]; then
echo "πŸ“ Adding custom controller mappings..."
cat "$GITHUB_WORKSPACE/add_gamecontrollerdb.txt" >> filtered.txt
echo "βœ… Custom mappings added ($(wc -l < "$GITHUB_WORKSPACE/add_gamecontrollerdb.txt") lines)"
else
echo "ℹ️ No custom mappings file found"
fi
# 3. Remove duplicate entries by GUID
# Use tac + awk + tac to keep only the LAST occurrence of each GUID
# tac reverses the file, awk keeps first occurrence (which is the last in original), tac reverses back
tac filtered.txt | awk -F',' '!seen[$1]++' | tac > processed.txt
echo "βœ… Duplicate removal completed (keeping newest entries)"
# 4. Move the final processed file to the workspace
mv processed.txt "$GITHUB_WORKSPACE/gamecontrollerdb.txt"
# Display final statistics
echo "πŸ“Š Final statistics:"
TOTAL_LINES=$(wc -l < "$GITHUB_WORKSPACE/gamecontrollerdb.txt")
echo "Total controller mappings: $TOTAL_LINES"
# Commit and push changes to the repository
cd "$GITHUB_WORKSPACE"
# Configure Git user for automated commits
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
# Stage all changes (including hash files)
git add .
# Check if there are any changes to commit
if git diff-index --quiet HEAD --; then
echo "⏩ No changes to commit"
else
# Commit with automated message including date and reason
COMMIT_MSG="πŸ”„ Automatic update [$(date +'%m/%d/%Y')] - ${{ steps.check-changes.outputs.change_reasons }}"
git commit -m "$COMMIT_MSG"
git push
echo "πŸš€ Changes pushed to repository"
echo "πŸ“ Commit message: $COMMIT_MSG"
fi
# Step 5: Report status
- name: Report status
run: |
if [ "${{ steps.check-changes.outputs.changes_detected }}" = "true" ]; then
echo "βœ… Workflow completed successfully - Changes processed"
echo "πŸ“‹ Reason: ${{ steps.check-changes.outputs.change_reasons }}"
else
echo "βœ… Workflow completed successfully - No changes needed"
fi
# Step 6: Cleanup temporary files
- name: Final cleanup
run: |
# Remove temporary cloned repository
if [ -d "SDL_GameControllerDB" ]; then
rm -rf SDL_GameControllerDB
echo "🧹 Temporary files removed"
fi
echo "🏁 Cleanup completed"