Update CRAN Downloads Badge #2405
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 CRAN Downloads Badge | |
| on: | |
| workflow_dispatch: # Allows manual triggering | |
| schedule: | |
| - cron: '0 * * * *' # Runs every hour at minute 0 | |
| jobs: | |
| update-badge: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Update CRAN Downloads Badge | |
| run: | | |
| PKG_NAME=$(awk -F': ' '/^Package:/{print $2}' DESCRIPTION | tr -d '\r') | |
| DOWNLOADS_JSON=$(curl -s "https://cranlogs.r-pkg.org/downloads/grand-total/$PKG_NAME") | |
| TOTAL_DOWNLOADS=$(echo "$DOWNLOADS_JSON" | grep -o -E '[0-9]+' | head -1) | |
| if (( TOTAL_DOWNLOADS >= 1000000 )); then | |
| FORMATTED_DOWNLOADS=$(awk -v val=$TOTAL_DOWNLOADS 'BEGIN { printf "%.1fM", val / 1000000 }') | |
| elif (( TOTAL_DOWNLOADS >= 1000 )); then | |
| FORMATTED_DOWNLOADS=$(awk -v val=$TOTAL_DOWNLOADS 'BEGIN { printf "%.1fK", val / 1000 }') | |
| else | |
| FORMATTED_DOWNLOADS=$TOTAL_DOWNLOADS | |
| fi | |
| OLD_BADGE_URL="https://img.shields.io/badge/downloads-.*-orange?style=for-the-badge&logo=download" | |
| NEW_BADGE_URL="https://img.shields.io/badge/downloads-$FORMATTED_DOWNLOADS-orange?style=for-the-badge&logo=download" | |
| sed -i "s|$OLD_BADGE_URL|$NEW_BADGE_URL|g" README.md | |
| echo "Updated badge URL to: $NEW_BADGE_URL" | |
| - name: Commit and push changes | |
| uses: stefanzweifel/git-auto-commit-action@v5 | |
| with: | |
| commit_message: "chore: Update CRAN downloads badge" | |
| commit_user_name: "github-actions[bot]" | |
| commit_user_email: "github-actions[bot]@users.noreply.github.com" | |
| commit_author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>" |