-
-
Notifications
You must be signed in to change notification settings - Fork 83
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
MysteryBlokHed
wants to merge
3
commits into
main
Choose a base branch
from
auto-update-downloads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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)) | ||
# 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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