Skip to content

Commit 682e5d7

Browse files
author
Ang
committed
chore: add auto-update script for awesome-cli-apps stars badge
1 parent f27755d commit 682e5d7

3 files changed

Lines changed: 129 additions & 1 deletion

File tree

.github/workflows/update-stars.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Update Stars Badge
2+
3+
on:
4+
schedule:
5+
# Run every day at 00:00 UTC
6+
- cron: '0 0 * * *'
7+
workflow_dispatch: # Allow manual trigger
8+
9+
jobs:
10+
update-stars:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: write
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: '3.11'
23+
24+
- name: Install dependencies
25+
run: pip install requests
26+
27+
- name: Run update script
28+
run: python scripts/update_stars.py
29+
30+
- name: Check for changes
31+
id: verify-changed-files
32+
run: |
33+
if git diff --quiet; then
34+
echo "changed=false" >> $GITHUB_OUTPUT
35+
else
36+
echo "changed=true" >> $GITHUB_OUTPUT
37+
fi
38+
39+
- name: Commit and push changes
40+
if: steps.verify-changed-files.outputs.changed == 'true'
41+
run: |
42+
git config --local user.email "action@github.com"
43+
git config --local user.name "GitHub Action"
44+
git add README.md docs/README.md 2>/dev/null || true
45+
git commit -m "chore: update stars badge [skip ci]"
46+
git push

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<div align="center">
1313

1414
[![Downloads](https://img.shields.io/pepy/dt/onecite?style=flat-square&label=Downloads)](https://pepy.tech/project/onecite)
15-
[![Awesome CLI Apps](https://img.shields.io/badge/Featured-Awesome%20CLI%20Apps%2018.2k⭐-FF6B35?style=flat-square&logo=awesome-lists&logoColor=white)](https://github.com/agarrharr/awesome-cli-apps?tab=readme-ov-file#academia)
15+
[![Awesome CLI Apps](https://img.shields.io/badge/Featured-Awesome%20CLI%20Apps%2019.2k⭐-FF6B35?style=flat-square&logo=awesome-lists&logoColor=white)](https://github.com/agarrharr/awesome-cli-apps?tab=readme-ov-file#academia)
1616

1717
[![Tests](https://img.shields.io/github/actions/workflow/status/HzaCode/OneCite/tests.yml?style=flat-square&logo=github)](https://github.com/HzaCode/OneCite/actions)
1818
[![codecov](https://img.shields.io/codecov/c/github/HzaCode/OneCite?style=flat-square&logo=codecov)](https://codecov.io/gh/HzaCode/OneCite)

scripts/update_stars.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python3
2+
"""Auto-update awesome-cli-apps GitHub stars badge in README files."""
3+
4+
import re
5+
import sys
6+
from pathlib import Path
7+
8+
import requests
9+
10+
# Target repo to track stars for (awesome-cli-apps)
11+
REPO_OWNER = "agarrharr"
12+
REPO_NAME = "awesome-cli-apps"
13+
GITHUB_API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}"
14+
15+
# Badge pattern to update the "Featured-Awesome CLI Apps XXk" badge
16+
BADGE_PATTERN = r'(https://img\.shields\.io/badge/Featured-Awesome%20CLI%20Apps%20)([\d.]+k)(%E2%AD%90)'
17+
18+
19+
def format_stars(count: int) -> str:
20+
"""Format star count to human readable string."""
21+
if count >= 1000:
22+
return f"{count / 1000:.1f}k".replace(".0k", "k")
23+
return str(count)
24+
25+
26+
def get_github_stars() -> int:
27+
"""Fetch star count from GitHub API."""
28+
try:
29+
response = requests.get(GITHUB_API_URL, timeout=30)
30+
response.raise_for_status()
31+
data = response.json()
32+
return data.get("stargazers_count", 0)
33+
except Exception as e:
34+
print(f"Error fetching stars: {e}")
35+
sys.exit(1)
36+
37+
38+
def update_readme_stars_badge(filepath: Path, stars: int) -> bool:
39+
"""Update awesome-cli-apps stars badge in README."""
40+
content = filepath.read_text(encoding="utf-8")
41+
original_content = content
42+
43+
formatted = format_stars(stars)
44+
45+
# Pattern for the awesome-cli-apps featured badge
46+
# Matches: https://img.shields.io/badge/Featured-Awesome%20CLI%20Apps%20XXk%E2%AD%90-...
47+
badge_pattern = r'(https://img\.shields\.io/badge/Featured-Awesome%20CLI%20Apps%20)([\d.]+k)(%E2%AD%90)'
48+
49+
def replace_stars(match):
50+
return match.group(1) + formatted + match.group(3)
51+
52+
content = re.sub(badge_pattern, replace_stars, content)
53+
54+
if content != original_content:
55+
filepath.write_text(content, encoding="utf-8")
56+
print(f"Updated awesome-cli-apps stars in {filepath}: {formatted}")
57+
return True
58+
else:
59+
print(f"No changes needed in {filepath}")
60+
return False
61+
62+
63+
def main():
64+
"""Main entry point."""
65+
stars = get_github_stars()
66+
print(f"Current GitHub stars: {stars} ({format_stars(stars)})")
67+
68+
# Update main README
69+
readme_path = Path("README.md")
70+
if readme_path.exists():
71+
update_readme_stars_badge(readme_path, stars)
72+
else:
73+
print("README.md not found")
74+
75+
# Update docs README if exists
76+
docs_readme = Path("docs/README.md")
77+
if docs_readme.exists():
78+
update_readme_stars_badge(docs_readme, stars)
79+
80+
81+
if __name__ == "__main__":
82+
main()

0 commit comments

Comments
 (0)