|
| 1 | +name: Advanced Metrics Badges |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main ] |
| 6 | + paths: |
| 7 | + - 'src/**' |
| 8 | + - 'tests/**' |
| 9 | + - 'pyproject.toml' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +jobs: |
| 13 | + config: |
| 14 | + name: Configuration |
| 15 | + uses: ./.github/workflows/shared-config.yml |
| 16 | + |
| 17 | + metrics: |
| 18 | + name: Generate Advanced Metrics |
| 19 | + needs: config |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + actions: read |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@v6.0.1 |
| 28 | + |
| 29 | + - name: Set up Python |
| 30 | + uses: actions/setup-python@v5 |
| 31 | + with: |
| 32 | + python-version: ${{ needs.config.outputs.default-python-version }} |
| 33 | + |
| 34 | + - name: Cache management |
| 35 | + uses: ./.github/workflows/cache-management.yml |
| 36 | + with: |
| 37 | + python-version: ${{ needs.config.outputs.default-python-version }} |
| 38 | + |
| 39 | + - name: Install dependencies |
| 40 | + run: | |
| 41 | + python -m pip install --upgrade pip |
| 42 | + pip install coverage pytest cloc |
| 43 | +
|
| 44 | + - name: Run tests with coverage |
| 45 | + run: | |
| 46 | + coverage run -m pytest tests/ --tb=short |
| 47 | + coverage report --format=total > coverage.txt |
| 48 | + coverage xml |
| 49 | +
|
| 50 | + - name: Calculate lines of code |
| 51 | + run: | |
| 52 | + # Install cloc if not available |
| 53 | + sudo apt-get update && sudo apt-get install -y cloc |
| 54 | + |
| 55 | + # Count lines of code (excluding tests, docs, config) |
| 56 | + cloc src/ --json --out=cloc.json |
| 57 | + |
| 58 | + # Extract metrics |
| 59 | + total_lines=$(jq -r '.SUM.code // 0' cloc.json) |
| 60 | + comment_lines=$(jq -r '.SUM.comment // 0' cloc.json) |
| 61 | + |
| 62 | + # Calculate comment percentage |
| 63 | + if [ "$total_lines" -gt 0 ]; then |
| 64 | + comment_percent=$(echo "scale=1; $comment_lines * 100 / $total_lines" | bc) |
| 65 | + else |
| 66 | + comment_percent="0" |
| 67 | + fi |
| 68 | + |
| 69 | + # Format for badges |
| 70 | + if [ "$total_lines" -ge 1000 ]; then |
| 71 | + loc_display=$(echo "scale=1; $total_lines / 1000" | bc)k |
| 72 | + else |
| 73 | + loc_display="$total_lines" |
| 74 | + fi |
| 75 | + |
| 76 | + echo "TOTAL_LOC=$loc_display" >> $GITHUB_ENV |
| 77 | + echo "COMMENT_PERCENT=$comment_percent" >> $GITHUB_ENV |
| 78 | +
|
| 79 | + - name: Run performance test |
| 80 | + run: | |
| 81 | + start_time=$(date +%s.%N) |
| 82 | + python -m pytest tests/ -x --tb=no -q |
| 83 | + end_time=$(date +%s.%N) |
| 84 | + |
| 85 | + # Calculate duration in seconds |
| 86 | + duration=$(echo "$end_time - $start_time" | bc) |
| 87 | + duration_formatted=$(printf "%.1fs" "$duration") |
| 88 | + |
| 89 | + echo "TEST_DURATION=$duration_formatted" >> $GITHUB_ENV |
| 90 | +
|
| 91 | + - name: Extract coverage percentage |
| 92 | + run: | |
| 93 | + coverage_percent=$(cat coverage.txt) |
| 94 | + echo "COVERAGE_PERCENT=$coverage_percent" >> $GITHUB_ENV |
| 95 | + |
| 96 | + # Set coverage color |
| 97 | + if [ "$coverage_percent" -ge 90 ]; then |
| 98 | + coverage_color="brightgreen" |
| 99 | + elif [ "$coverage_percent" -ge 75 ]; then |
| 100 | + coverage_color="yellow" |
| 101 | + elif [ "$coverage_percent" -ge 60 ]; then |
| 102 | + coverage_color="orange" |
| 103 | + else |
| 104 | + coverage_color="red" |
| 105 | + fi |
| 106 | + |
| 107 | + echo "COVERAGE_COLOR=$coverage_color" >> $GITHUB_ENV |
| 108 | +
|
| 109 | + - name: Create coverage badge |
| 110 | + uses: schneegans/dynamic-badges-action@v1.7.0 |
| 111 | + with: |
| 112 | + auth: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + gistID: ${{ secrets.METRICS_GIST_ID }} |
| 114 | + filename: coverage.json |
| 115 | + label: Coverage |
| 116 | + message: ${{ env.COVERAGE_PERCENT }}% |
| 117 | + color: ${{ env.COVERAGE_COLOR }} |
| 118 | + |
| 119 | + - name: Create lines of code badge |
| 120 | + uses: schneegans/dynamic-badges-action@v1.7.0 |
| 121 | + with: |
| 122 | + auth: ${{ secrets.GITHUB_TOKEN }} |
| 123 | + gistID: ${{ secrets.METRICS_GIST_ID }} |
| 124 | + filename: lines-of-code.json |
| 125 | + label: Lines of Code |
| 126 | + message: ${{ env.TOTAL_LOC }} |
| 127 | + color: lightgrey |
| 128 | + |
| 129 | + - name: Create comment percentage badge |
| 130 | + uses: schneegans/dynamic-badges-action@v1.7.0 |
| 131 | + with: |
| 132 | + auth: ${{ secrets.GITHUB_TOKEN }} |
| 133 | + gistID: ${{ secrets.METRICS_GIST_ID }} |
| 134 | + filename: comments.json |
| 135 | + label: Comments |
| 136 | + message: ${{ env.COMMENT_PERCENT }}% |
| 137 | + valColorRange: ${{ env.COMMENT_PERCENT }} |
| 138 | + maxColorRange: 30 |
| 139 | + minColorRange: 0 |
| 140 | + |
| 141 | + - name: Create test duration badge |
| 142 | + uses: schneegans/dynamic-badges-action@v1.7.0 |
| 143 | + with: |
| 144 | + auth: ${{ secrets.GITHUB_TOKEN }} |
| 145 | + gistID: ${{ secrets.METRICS_GIST_ID }} |
| 146 | + filename: test-duration.json |
| 147 | + label: Test Duration |
| 148 | + message: ${{ env.TEST_DURATION }} |
| 149 | + color: blue |
| 150 | + |
| 151 | + - name: Upload coverage report |
| 152 | + uses: actions/upload-artifact@v4.4.3 |
| 153 | + with: |
| 154 | + name: coverage-report |
| 155 | + retention-days: 30 |
| 156 | + path: | |
| 157 | + coverage.xml |
| 158 | + cloc.json |
0 commit comments