Skip to content

Update Download Stats #22

Update Download Stats

Update Download Stats #22

name: Update Download Stats
on:
schedule:
- cron: "0 10 * * *"
workflow_dispatch:
jobs:
stats:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- name: Ensure stats directory exists
run: mkdir -p stats
- name: Fetch release downloads
run: |
curl -s https://api.github.com/repos/${{ github.repository }}/releases \
| jq '
[
.[] |
{
tag: .tag_name,
total: (.assets | map(.download_count) | add)
}
]
' > stats/downloads.json
- name: Append history
run: |
TOTAL=$(jq '[.[].total] | add' stats/downloads.json)
DATE=$(date -u +"%Y-%m-%d %H:%M")
echo "$DATE,$TOTAL" >> stats/history.csv
- name: Generate chart (SVG)
run: |
sudo apt install -y gnuplot
gnuplot <<EOF
# Terminal settings
set terminal svg size 800,400 dynamic enhanced font 'Arial,12'
set output "stats/chart.svg"
# Data settings
set datafile separator ","
set xdata time
set timefmt "%Y-%m-%d %H:%M"
set format x "%d %b"
# Colors for dark mode compatibility (light gray/white text)
set border linecolor rgb "#a0a0a0" linewidth 1.5
set grid linecolor rgb "#404040" linewidth 0.5
set title "📊 Total Downloads Over Time" textcolor rgb "#e0e0e0" font ",16"
set xlabel "Date" textcolor rgb "#b0b0b0" font ",11"
set ylabel "Total Downloads" textcolor rgb "#b0b0b0" font ",11"
# Axis tick colors
set xtics textcolor rgb "#b0b0b0" font ",10"
set ytics textcolor rgb "#b0b0b0" font ",10"
# Legend/Key styling
set key right top textcolor rgb "#e0e0e0" box linecolor rgb "#606060"
# Y-axis starts from 0 for better visualization
set yrange [0:*]
# Enable grid
set grid xtics ytics
# Plot with line and points for better visibility
plot "stats/history.csv" using 1:2 with linespoints \
linecolor rgb "#00d4aa" linewidth 2.5 pointtype 7 pointsize 1.2 \
title "Downloads"
EOF
- name: Check for changes
id: changes
run: |
git add stats/
if git diff --cached --quiet; then
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "has_changes=true" >> $GITHUB_OUTPUT
fi
- name: Create branch and PR
if: steps.changes.outputs.has_changes == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Setup git
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Create unique branch name with timestamp
BRANCH="chore/update-stats-$(date +%Y%m%d-%H%M%S)"
# Create and push branch
git checkout -b "$BRANCH"
git commit -m "chore: update download stats [skip ci]"
git push origin "$BRANCH"
# Create PR
PR_URL=$(gh pr create \
--title "chore: update download stats" \
--body "🤖 Automated PR to update download statistics.
This PR was created by the scheduled workflow.
---
_This PR will be auto-merged._" \
--base main \
--head "$BRANCH")
echo "Created PR: $PR_URL"
# Wait a moment for PR to be processed
sleep 5
# Try to merge directly first (works if PR is already clean/mergeable)
# If that fails, try to enable auto-merge
if gh pr merge "$PR_URL" --squash --delete-branch; then
echo "✅ PR merged successfully!"
else
echo "⏳ Direct merge failed, enabling auto-merge..."
gh pr merge "$PR_URL" --auto --squash --delete-branch || echo "⚠️ Auto-merge setup may have failed, PR will need manual merge"
fi