Update Package Stats #45
This file contains 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 Package Stats | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 0 * * *' | |
jobs: | |
update-stats: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get Stats | |
uses: actionstore/ghcr-stats@v1.0.4 | |
with: | |
owner: ${{ github.repository_owner }} | |
repo: ${{ github.event.repository.name }} | |
name: ${{ github.event.repository.name }} | |
path: /tmp/stats.json | |
- name: Update Gist | |
env: | |
GH_TOKEN: ${{ secrets.GIST_TOKEN }} | |
run: | | |
# Get downloads count from stats.json | |
TOTAL=$(jq -r '.downloads_compact' /tmp/stats.json) | |
MONTHLY=$(jq -r '.downloads_month_compact' /tmp/stats.json) | |
# Fetch the existing gist content | |
EXISTING_CONTENT=$(gh api \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"/gists/7bbbf38fce9710cb995f20defb9bd5a5" | jq -r '.files["package-stats.json"].content') | |
# Update the specific key in the JSON | |
UPDATED_CONTENT=$(echo "$EXISTING_CONTENT" | jq \ | |
--arg repo "${{ github.event.repository.name }}" \ | |
--arg total "$TOTAL" \ | |
--arg monthly "$MONTHLY" \ | |
'. + {($repo): {"total": $total, "monthly": $monthly}}') | |
# Update gist using GitHub API | |
gh api \ | |
--method PATCH \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"/gists/7bbbf38fce9710cb995f20defb9bd5a5" \ | |
-f "files[package-stats.json][content]=$UPDATED_CONTENT" |