Skip to content

Update Download Count on Release #75

Update Download Count on Release

Update Download Count on Release #75

name: Update Download Count on Release
on:
schedule:
- cron: '0 0 * * 0' # Runs once a week at midnight.
workflow_dispatch: # Allows manual triggering
release:
types: [published] # when a new release is made
jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Fetch Download Count
id: fetch_downloads
run: |
# Fetch the releases
releases=$(curl -s https://api.github.com/repos/shupershuff/diablo2rloader/releases)
# Debug: Print releases to check the response
echo "Releases data: $releases" > releases.json
# Validate JSON format and check for errors
if ! jq empty releases.json > /dev/null 2>&1; then
echo "Error: Invalid JSON response."
exit 1
fi
# Initialize variables for max downloads
max_download_count=0
release_with_max_downloads=""
# Iterate through each release using a for loop
for release in $(echo "$releases" | jq -c '.[]'); do
release_data=$(echo "$release" | jq '.')
release_name=$(echo "$release_data" | jq -r '.tag_name')
download_count=$(echo "$release_data" | jq '[.assets[].download_count // 0] | add')
# Log the release name and download count for debugging
echo "Release: $release_name, Download Count: $download_count"
# Check if this release has more downloads than the current max
if [ "$download_count" -gt "$max_download_count" ]; then
echo "Release: $release_name, has the most downloads so far: $download_count"
max_download_count=$download_count
release_with_max_downloads=$release_name
fi
done
# Log the final result for debugging
echo "Final Max downloads: $max_download_count for release: $release_with_max_downloads"
# Assign to GitHub output
echo "download_count=$max_download_count" >> $GITHUB_OUTPUT
echo "release_with_max_downloads=$release_with_max_downloads" >> $GITHUB_OUTPUT
- name: Update JSON File
run: |
echo '{
"schemaVersion": 1,
"label": "Downloads",
"message": "'${{ steps.fetch_downloads.outputs.download_count }}'",
"color": "blue"
}' > .github/max-release-download-count.json
- name: Commit and Push Changes
env:
TOKEN: ${{ secrets.ACTIONS_PUSH_TOKEN }}
run: |
git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com'
git add .github/max-release-download-count.json
git diff --cached --exit-code || git commit -m 'Update download count' || echo "No changes to commit"
git push https://$TOKEN@github.com/shupershuff/Diablo2RLoader.git HEAD:main || echo "No changes to push"