Update git clones shields in READMEs #30
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 git clones shields in READMEs | |
on: | |
schedule: | |
- cron: "37 5 * * 0,2,4" # every Sun/Tue/Thu @ 5:37a | |
jobs: | |
update-readme-clones-shields: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout KudoAI/chatgpt.js | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.REPO_SYNC_PAT }} | |
repository: KudoAI/chatgpt.js | |
path: KudoAI/chatgpt.js | |
- name: Fetch/sum git clones count | |
id: get-clones | |
run: | | |
expand_num() { # expand nums abbreviated w/ 'k' or 'm' suffix to integers | |
local num=$(echo "$1" | tr '[:upper:]' '[:lower:]') # convert to lowercase | |
if [[ $num =~ k$ ]] ; then | |
num="${num%k}" # remove 'k' suffix | |
num=$(awk "BEGIN { printf \"%.0f\", $num * 1000 }") # multiply by 1000 | |
elif [[ $num =~ m$ ]] ; then | |
num="${num%m}" # remove 'm' suffix | |
num=$(awk "BEGIN { printf \"%.0f\", $num * 1000000 }") # multiply by 1000000 | |
fi ; echo "$num" | |
} | |
format_total() { | |
local num=$1 ; first_digit="${num:0:1}" second_digit="${num:1:1}" | |
second_digit_rounded=$(( second_digit < 5 ? 0 : 5 )) | |
if (( num >= 1000000000 )) ; then # 1B+ w/ one decimal place | |
formatted_num="$(( num / 1000000000 ))" | |
remainder=$(( (num % 1000000000) / 100000000 )) | |
if (( remainder != 0 )) ; then formatted_num+=".$remainder" ; fi | |
formatted_num+="B+" | |
elif (( num >= 10000000 )) ; then # abbr 10,000,000+ to 999,000,000+ | |
formatted_num=$(printf "%'.f+" $((( num / 1000000 ) * 1000000 ))) | |
elif (( num >= 1000000 )) ; then # abbr 1,000,000+ to 9,500,000+ | |
formatted_num="${first_digit},${second_digit}00,000+" | |
elif (( num >= 100000 )) ; then # abbr 100,000+ to 950,000+ | |
formatted_num="${first_digit}${second_digit_rounded}0,000+" | |
elif (( num >= 10000 )) ; then # abbr 10,000+ to 90,000+ | |
formatted_num="${first_digit}0,000+" | |
elif (( num >= 1000 )) ; then # abbr 1K to 9.9K | |
formatted_num="$(( num / 1000 ))" | |
remainder=$(( (num % 1000) / 100 )) | |
if (( remainder != 0 )) ; then formatted_num+=".$remainder" ; fi | |
formatted_num+="K" | |
else formatted_num="$num" ; fi # preserve <1K as is | |
echo "$formatted_num" | |
} | |
# Fetch/calculate git clones count | |
biweekly_clones=$(curl -sSL \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.REPO_SYNC_PAT }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
"https://api.github.com/repos/KudoAI/chatgpt.js/traffic/clones" | | |
sed -n -E '0,/.*"count": ([0-9]+).*/ s//\1/p') | |
monthly_clones=$((biweekly_clones * 2)) | |
echo "Biweekly git clones: $biweekly_clones" | |
echo "Monthly git clones: $monthly_clones" | |
# Format total | |
formatted_monthly_clones=$(format_total "$monthly_clones") | |
echo "Formatted monthly git clones: $formatted_monthly_clones" | |
# Expose as output for update step next | |
echo "monthly_clones=$formatted_monthly_clones" >> $GITHUB_OUTPUT | |
- name: Update README shields | |
run: | | |
cd ${{ github.workspace }}/KudoAI/chatgpt.js | |
monthly_clones="${{ steps.get-clones.outputs.monthly_clones }}" | |
if [ "$monthly_clones" == "0" ] ; then echo "Error getting total git clones" | |
else | |
for readme in $(find . -name "README.md") ; do | |
old_readme=$(<"$readme") | |
sed -i -E "s|(badge/[^-]+-)[0-9.,km+]+(/[^?]+\?logo=github)|\1$monthly_clones\2|gI" "$readme" | |
new_readme=$(<"$readme") | |
if [ "$old_readme" != "$new_readme" ] ; then shield_updated=true ; fi | |
done | |
if [ "$shield_updated" = true ] ; then echo "Git clones shields updated to $monthly_clones" | |
else echo "Git clones shields up-to-date already" ; fi | |
fi | |
- name: Push to KudoAI/chatgpt.js | |
uses: stefanzweifel/git-auto-commit-action@v4 | |
with: | |
push_options: --force | |
add_options: --all | |
commit_user_email: [email protected] | |
commit_author: kudo-sync-bot <[email protected]> | |
commit_message: Updated git clones shield counters | |
file_pattern: "README.md **/README.md" | |
repository: KudoAI/chatgpt.js |