Skip to content

Commit cd88a25

Browse files
author
Create Index Workflow
committed
Add stars
1 parent 43d64e2 commit cd88a25

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

build.sh

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ fi
1212

1313
# Update README with a list of all cheat sheets defined
1414
readme_update() {
15+
16+
# Check "last_updated" field to update stars/forks in extra-cheat-sheets.json
17+
max_updates=15
18+
jq -c '. | to_entries[]' extra-cheat-sheets.json | while read -r entry; do
19+
# Eventual consistency, do no update to much to not hit GitHub API quota
20+
if [ "$max_updates" -le 0 ]; then
21+
break
22+
fi
23+
24+
key=$(echo "$entry" | jq -r '.key')
25+
value=$(echo "$entry" | jq -r '.value')
26+
last_updated=$(echo "$value" | jq -r '.last_updated // empty')
27+
28+
if [ -z "$last_updated" ] || [ "$(date -d "$last_updated" +%s 2>/dev/null || echo 0)" -lt "$(date -d '30 days ago' +%s)" ]; then
29+
echo "Updating stats for $key"
30+
max_updates=$((max_updates - 1))
31+
32+
repo=$(echo "$value" | jq -r '.github')
33+
if [ -n "$repo" ]; then
34+
repo_api_url="https://api.github.com/repos/$repo"
35+
repo_data=$(curl -s "$repo_api_url")
36+
stars=$(echo "$repo_data" | jq -r '.stargazers_count // 0')
37+
forks=$(echo "$repo_data" | jq -r '.forks_count // 0')
38+
39+
jq --arg key "$key" --arg stars "$stars" --arg forks "$forks" --arg date "$(date -I)" \
40+
'.[$key] |= . + {stars: ($stars | tonumber), forks: ($forks | tonumber), last_updated: $date}' extra-cheat-sheets.json >tmp.json && mv tmp.json extra-cheat-sheets.json
41+
set +x
42+
fi
43+
fi
44+
done
45+
1546
sed -i '/<!-- marker -->/,$ d' README.md
1647
(
1748
echo '<!-- marker -->'
@@ -35,11 +66,11 @@ readme_update() {
3566
# Append extra sheet sheets
3667
printf "\n## Installable External Cheat Sheets\n\n"
3768
printf "\nIt is impossible to cover all important topics, so [lzone.de](https://lzone.de) supports adding all the following content too.\n\n"
38-
printf "| Cheat Sheet | Type | Category |\n"
39-
printf "| --- | --- | --- |\n"
69+
printf "| Cheat Sheet | Stars | Type | Category |\n"
70+
printf "| --- | --- | --- | --- |\n"
4071

4172
while read extra; do
42-
jq -r '. | to_entries[] | select(.key == "'"$extra"'") | .value | "| ['"$extra"']("+(.github)+") | "+(.type)+" | "+(.category)+" | "' extra-cheat-sheets.json || true
73+
jq -r '. | to_entries[] | select(.key == "'"$extra"'") | .value | "| ['"$extra"']("+(.github)+") | ⭐"+(.stars // "unknown" | tostring)+" | "+(.type)+" | "+(.category)+" | "' extra-cheat-sheets.json || true
4374
done < <(jq -r ". | to_entries[] | .key" extra-cheat-sheets.json | LANG=C sort) |\
4475
sed -e "s/| Tutorial |/| 💡 Tutorial |/g" \
4576
-e "s/| Book |/| 📕 Book |/g" \

0 commit comments

Comments
 (0)