Update downloads badges #916
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update downloads badges | |
| on: | |
| release: | |
| types: [published, edited, released] | |
| schedule: | |
| - cron: '0 * * * *' # Toutes les heures UTC | |
| workflow_dispatch: # Permet de lancer manuellement | |
| permissions: | |
| contents: read # Plus besoin d'écrire dans le repo | |
| jobs: | |
| update-badges: | |
| runs-on: ubuntu-latest | |
| env: | |
| REPO: ${{ github.repository }} | |
| APPIMAGE_REGEX: '\.AppImage$' | |
| ZSYNC_REGEX: '\.zsync$' | |
| BADGES_DIR: 'badges' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Install jq | |
| run: sudo apt-get update && sudo apt-get install -y jq | |
| - name: Compute totals and write badges | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| REPO: ${{ env.REPO }} | |
| APPIMAGE_REGEX: ${{ env.APPIMAGE_REGEX }} | |
| ZSYNC_REGEX: ${{ env.ZSYNC_REGEX }} | |
| BADGES_DIR: ${{ env.BADGES_DIR }} | |
| run: | | |
| set -euo pipefail | |
| mkdir -p "$BADGES_DIR" | |
| page=1 | |
| total_appimage=0 | |
| total_zsync=0 | |
| echo "Fetching releases from $REPO..." | |
| while true; do | |
| echo "Fetching page $page..." | |
| resp=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| -H "X-GitHub-Api-Version: 2022-11-28" \ | |
| "https://api.github.com/repos/$REPO/releases?per_page=100&page=$page") | |
| len=$(echo "$resp" | jq 'length') | |
| echo "Found $len releases on page $page" | |
| if [ "$len" -eq 0 ]; then | |
| echo "No more releases to fetch" | |
| break | |
| fi | |
| if [ "$page" -eq 1 ]; then | |
| echo "Sample asset structure:" | |
| echo "$resp" | jq '.[0].assets[0]? // empty' || true | |
| fi | |
| page_app=$(echo "$resp" | jq --arg re "$APPIMAGE_REGEX" ' | |
| [.[] | .assets[]? | | |
| select(.name | test($re) and (test("\\.zsync$") | not)) | | |
| .download_count // 0] | add // 0 | |
| ') | |
| page_zsync=$(echo "$resp" | jq --arg re "$ZSYNC_REGEX" ' | |
| [.[] | .assets[]? | | |
| select(.name | test($re)) | | |
| .download_count // 0] | add // 0 | |
| ') | |
| echo "Page $page - AppImage: $page_app, zsync: $page_zsync" | |
| total_appimage=$((total_appimage + page_app)) | |
| total_zsync=$((total_zsync + page_zsync)) | |
| page=$((page + 1)) | |
| done | |
| echo "Total AppImage downloads: $total_appimage" | |
| echo "Total zsync downloads: $total_zsync" | |
| printf '{"schemaVersion":1,"label":"AppImage downloads","message":"%s","color":"blue"}\n' "$total_appimage" > "$BADGES_DIR/appimage.json" | |
| printf '{"schemaVersion":1,"label":"zsync downloads","message":"%s","color":"blue"}\n' "$total_zsync" > "$BADGES_DIR/zsync.json" | |
| - name: Update Gist with badges | |
| env: | |
| GIST_TOKEN: ${{ secrets.GIST_TOKEN }} | |
| GIST_ID: 1a767d8eb615ae6cb497fe90ce0076bb | |
| BADGES_DIR: ${{ env.BADGES_DIR }} | |
| run: | | |
| for badge in appimage zsync; do | |
| FILENAME="$badge.json" | |
| ESCAPED_CONTENT=$(jq -Rs . < "$BADGES_DIR/$FILENAME") | |
| curl -s -X PATCH "https://api.github.com/gists/$GIST_ID" \ | |
| -H "Authorization: token $GIST_TOKEN" \ | |
| -d "{\"files\": { \"$FILENAME\": { \"content\": $ESCAPED_CONTENT }}}" | |
| done |