|
| 1 | +name: Update Repository Statistics |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ["main"] |
| 6 | + paths: |
| 7 | + - "easy/**" |
| 8 | + - "medium/**" |
| 9 | + - "hard/**" |
| 10 | + |
| 11 | +jobs: |
| 12 | + update-stats: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: write |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v4 |
| 18 | + |
| 19 | + - name: Set up Python |
| 20 | + uses: actions/setup-python@v4 |
| 21 | + with: |
| 22 | + python-version: "3.x" |
| 23 | + |
| 24 | + - name: Update Statistics |
| 25 | + run: | |
| 26 | + python3 - <<EOF |
| 27 | + import os |
| 28 | + from collections import Counter |
| 29 | +
|
| 30 | + def count_solutions(): |
| 31 | + counts = Counter() |
| 32 | + for difficulty in ['easy', 'medium', 'hard']: |
| 33 | + if os.path.exists(difficulty): |
| 34 | + counts[difficulty] = len([name for name in os.listdir(difficulty) if os.path.isdir(os.path.join(difficulty, name))]) |
| 35 | + return counts |
| 36 | +
|
| 37 | + def update_readme(counts): |
| 38 | + with open('README.md', 'r') as f: |
| 39 | + content = f.read() |
| 40 | + |
| 41 | + # Add statistics section if it doesn't exist |
| 42 | + stats_section = f""" |
| 43 | + ## Statistics 📊 |
| 44 | +
|
| 45 | + - Easy: {counts['easy']} solutions |
| 46 | + - Medium: {counts['medium']} solutions |
| 47 | + - Hard: {counts['hard']} solutions |
| 48 | + - Total: {sum(counts.values())} solutions |
| 49 | + """ |
| 50 | + |
| 51 | + if '## Statistics' in content: |
| 52 | + # Replace existing statistics section |
| 53 | + import re |
| 54 | + content = re.sub(r'## Statistics.*?(?=##|$)', stats_section, content, flags=re.DOTALL) |
| 55 | + else: |
| 56 | + # Add statistics section before License |
| 57 | + content = content.replace('## License', stats_section + '\n## License') |
| 58 | + |
| 59 | + with open('README.md', 'w') as f: |
| 60 | + f.write(content) |
| 61 | +
|
| 62 | + counts = count_solutions() |
| 63 | + update_readme(counts) |
| 64 | + EOF |
| 65 | +
|
| 66 | + - name: Commit changes |
| 67 | + run: | |
| 68 | + git config --local user.email "action@github.com" |
| 69 | + git config --local user.name "GitHub Action" |
| 70 | + git add README.md |
| 71 | + git diff --quiet && git diff --staged --quiet || git commit -m "Update repository statistics" |
| 72 | + git push |
0 commit comments