|
| 1 | +name: Docstring Coverage |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + pull_request: |
| 7 | + branches: [ "main" ] |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + docstring-coverage: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Python |
| 18 | + uses: actions/setup-python@v5 |
| 19 | + with: |
| 20 | + python-version: "3.12" |
| 21 | + |
| 22 | + - name: Install dependencies |
| 23 | + run: | |
| 24 | + python -m pip install --upgrade pip |
| 25 | + |
| 26 | + - name: Check docstring coverage |
| 27 | + run: | |
| 28 | + python scripts/check_docstring_coverage.py --min-coverage 80 |
| 29 | + |
| 30 | + - name: Generate docstring coverage badge |
| 31 | + run: | |
| 32 | + python -c " |
| 33 | + import re |
| 34 | + import subprocess |
| 35 | + import json |
| 36 | + |
| 37 | + # Run the docstring coverage script and capture output |
| 38 | + result = subprocess.run( |
| 39 | + ['python', 'scripts/check_docstring_coverage.py', '--dir', 'src'], |
| 40 | + capture_output=True, |
| 41 | + text=True |
| 42 | + ) |
| 43 | + |
| 44 | + # Extract the overall coverage percentage |
| 45 | + match = re.search(r'Overall docstring coverage: (\d+\.\d+)%', result.stdout) |
| 46 | + if match: |
| 47 | + coverage = float(match.group(1)) |
| 48 | + |
| 49 | + # Determine badge color |
| 50 | + color = 'red' |
| 51 | + if coverage >= 90: |
| 52 | + color = 'brightgreen' |
| 53 | + elif coverage >= 80: |
| 54 | + color = 'green' |
| 55 | + elif coverage >= 70: |
| 56 | + color = 'yellowgreen' |
| 57 | + elif coverage >= 60: |
| 58 | + color = 'yellow' |
| 59 | + elif coverage >= 50: |
| 60 | + color = 'orange' |
| 61 | + |
| 62 | + # Create badge URL |
| 63 | + badge_url = f'https://img.shields.io/badge/docstring%20coverage-{coverage:.1f}%25-{color}' |
| 64 | + |
| 65 | + # Update README.md |
| 66 | + with open('README.md', 'r') as f: |
| 67 | + readme = f.read() |
| 68 | + |
| 69 | + # Look for existing docstring coverage badge |
| 70 | + docstring_badge_pattern = r'!\[Docstring Coverage\]\(https://img\.shields\.io/badge/docstring%20coverage-[\d\.]+%25-[a-z]+\)' |
| 71 | + if re.search(docstring_badge_pattern, readme): |
| 72 | + # Replace existing badge |
| 73 | + readme = re.sub(docstring_badge_pattern, f'', readme) |
| 74 | + else: |
| 75 | + # Look for badge section to add to |
| 76 | + badge_section = re.search(r'(!\[[^\]]+\]\([^\)]+\)[ \t]*)+', readme) |
| 77 | + if badge_section: |
| 78 | + # Add to existing badges |
| 79 | + end_pos = badge_section.end() |
| 80 | + readme = readme[:end_pos] + f' ' + readme[end_pos:] |
| 81 | + else: |
| 82 | + # Add after first line |
| 83 | + first_line_end = readme.find('\\n') |
| 84 | + if first_line_end != -1: |
| 85 | + readme = readme[:first_line_end+1] + f'\\n\\n' + readme[first_line_end+1:] |
| 86 | + else: |
| 87 | + readme = readme + f'\\n\\n\\n' |
| 88 | + |
| 89 | + with open('README.md', 'w') as f: |
| 90 | + f.write(readme) |
| 91 | + |
| 92 | + print(f'Updated README.md with docstring coverage badge: {coverage:.1f}%') |
| 93 | + else: |
| 94 | + print('Could not extract docstring coverage percentage') |
| 95 | + " |
| 96 | + |
| 97 | + - name: Commit updated README with docstring coverage badge |
| 98 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 99 | + run: | |
| 100 | + git config user.name "github-actions[bot]" |
| 101 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 102 | + git add README.md |
| 103 | + git diff --quiet && git diff --staged --quiet || ( |
| 104 | + git commit -m "docs: update docstring coverage badge [skip ci]" |
| 105 | + git push |
| 106 | + ) |
| 107 | + |
| 108 | +permissions: |
| 109 | + contents: write |
0 commit comments