Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] Add workflow to auto-update download count in README #610

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/download_count.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Update Download Count

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"

jobs:
update-downloads:
name: Update Download Count
runs-on: ubuntu-latest

permissions:
contents: write

steps:
- uses: actions/checkout@v4

- name: Get download count
run: |
# check downloads
downloads=$(curl https://api.npmjs.org/downloads/point/1970-01-01:$(date -d '+1 day' +"%Y-%m-%d")/colorjs.io | jq -r .downloads)
echo "Latest download count: $downloads"
# into millions
downloads=$((downloads / 1000000))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also round? I'm not very familiar with these and can't tell where the number of downloads is rounded.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will just truncate the number instead of rounding to the nearest million (eg. 5,999,999 downloads would become be 5 million). I can make it round instead, if preferred

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truncating is probably fine, rounding might be better if easy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just changed it to round instead of truncate, but I've also just realized that the README says "over 10 million total downloads," so it might be a bit misleading to round? I'm not sure how important that is, but figured it's worth mentioning

# try to find current download count in readme
readme=$(<README.md)
if [[ "$readme" =~ ([0-9]+)" million total npm downloads" ]]; then
current_downloads="${BASH_REMATCH[1]}"
# if the new count is bigger, update it
if [[ "$downloads" -gt "$current_downloads" ]]; then
echo "New count ($downloads million) is greater than current $current_downloads million, updating..."
readme=$(echo "$readme" | sed -E "s/\[[0-9]+ million total npm downloads\]/[$downloads million total npm downloads]/")
echo "$readme" > README.md
else
echo "New count ($downloads million) is less than or equal to current $current_downloads million; not doing anything."
fi
fi
echo Done!

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@8621497c8c39c72f3e2a999a26b4ca1b5058a842 # v5.0.1
with:
commit_message: Update README download count
file_pattern: README.md
# default github-actions user info as commit author
commit_author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Loading