microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
.github/workflows/code-review-metrics.yml
298lines · modecode
| 1 | --- |
| 2 | name: Code Review Metrics |
| 3 | |
| 4 | on: |
| 5 | schedule: |
| 6 | - cron: '0 0 * * 1' # Weekly on Mondays at midnight UTC |
| 7 | workflow_dispatch: |
| 8 | inputs: |
| 9 | days: |
| 10 | description: 'Analysis period in days' |
| 11 | required: false |
| 12 | default: '30' |
| 13 | type: string |
| 14 | |
| 15 | permissions: |
| 16 | contents: read |
| 17 | pull-requests: read |
| 18 | issues: read |
| 19 | |
| 20 | jobs: |
| 21 | review-metrics: |
| 22 | runs-on: ubuntu-latest |
| 23 | name: Generate Code Review Metrics |
| 24 | |
| 25 | steps: |
| 26 | - name: Checkout |
| 27 | uses: actions/checkout@v6 |
| 28 | |
| 29 | - name: Calculate Date Range |
| 30 | id: date-range |
| 31 | run: | |
| 32 | days="${{ github.event.inputs.days || '30' }}" |
| 33 | start_date=$(date -d "$days days ago" +%Y-%m-%d) |
| 34 | echo "start_date=$start_date" >> $GITHUB_OUTPUT |
| 35 | |
| 36 | - name: Collect Code Review Metrics |
| 37 | run: | |
| 38 | # Authenticate with GitHub CLI |
| 39 | echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token |
| 40 | |
| 41 | # Get PR data for the specified period |
| 42 | start_date="${{ steps.date-range.outputs.start_date }}" |
| 43 | days="${{ github.event.inputs.days || '30' }}" |
| 44 | |
| 45 | echo "Collecting review data for PRs created since: $start_date" |
| 46 | |
| 47 | # Create output files |
| 48 | mkdir -p review-data |
| 49 | |
| 50 | # Get PRs and their reviews (filtering will be done in Python for better control) |
| 51 | gh pr list \ |
| 52 | --repo "${{ github.repository }}" \ |
| 53 | --state all \ |
| 54 | --limit 1000 \ |
| 55 | --json number,title,author,createdAt,mergedAt,reviews,reviewRequests \ |
| 56 | --jq ".[] | select(.createdAt >= \"$start_date\")" \ |
| 57 | > review-data/prs.json |
| 58 | |
| 59 | # Process review data to generate metrics focused on who is reviewing and review counts |
| 60 | python3 << 'EOF' |
| 61 | import json |
| 62 | import sys |
| 63 | from collections import defaultdict |
| 64 | |
| 65 | # Load PR data from JSONL file (one JSON object per line) |
| 66 | with open('review-data/prs.json', 'r') as f: |
| 67 | prs = [] |
| 68 | for line_num, line in enumerate(f, 1): |
| 69 | if line.strip(): |
| 70 | try: |
| 71 | prs.append(json.loads(line)) |
| 72 | except json.JSONDecodeError as e: |
| 73 | print(f"Warning: Skipping malformed JSON on line {line_num}: {e}", file=sys.stderr) |
| 74 | |
| 75 | print(f"Processing {len(prs)} PRs...") |
| 76 | |
| 77 | # Initialize metrics - track both reviewers and contributors |
| 78 | reviewer_stats = defaultdict(lambda: { |
| 79 | 'reviews_given': 0, |
| 80 | 'prs_reviewed': set() |
| 81 | }) |
| 82 | |
| 83 | contributor_stats = defaultdict(lambda: { |
| 84 | 'prs_authored': 0 |
| 85 | }) |
| 86 | |
| 87 | total_reviews = 0 |
| 88 | |
| 89 | # Process each PR to count reviews per reviewer and track contributors |
| 90 | for pr in prs: |
| 91 | pr_number = pr['number'] |
| 92 | author = pr['author']['login'] |
| 93 | |
| 94 | # Track PR authors (contributors) |
| 95 | contributor_stats[author]['prs_authored'] += 1 |
| 96 | |
| 97 | # Process reviews |
| 98 | for review in pr.get('reviews', []): |
| 99 | reviewer = review['author']['login'] |
| 100 | |
| 101 | total_reviews += 1 |
| 102 | reviewer_stats[reviewer]['reviews_given'] += 1 |
| 103 | reviewer_stats[reviewer]['prs_reviewed'].add(pr_number) |
| 104 | |
| 105 | # Convert sets to counts for JSON serialization |
| 106 | for reviewer in reviewer_stats: |
| 107 | reviewer_stats[reviewer]['prs_reviewed'] = len(reviewer_stats[reviewer]['prs_reviewed']) |
| 108 | |
| 109 | # Find contributors who haven't done reviews |
| 110 | all_contributors = set(contributor_stats.keys()) |
| 111 | all_reviewers = set(reviewer_stats.keys()) |
| 112 | contributors_not_reviewing = all_contributors - all_reviewers |
| 113 | |
| 114 | # Save comprehensive metrics |
| 115 | metrics = { |
| 116 | 'summary': { |
| 117 | 'total_prs_analyzed': len(prs), |
| 118 | 'total_reviews': total_reviews, |
| 119 | 'total_reviewers': len(reviewer_stats), |
| 120 | 'total_contributors': len(contributor_stats), |
| 121 | 'contributors_not_reviewing': len(contributors_not_reviewing) |
| 122 | }, |
| 123 | 'reviewer_stats': dict(reviewer_stats), |
| 124 | 'contributor_stats': dict(contributor_stats), |
| 125 | 'contributors_not_reviewing': list(contributors_not_reviewing) |
| 126 | } |
| 127 | |
| 128 | with open('review-data/metrics.json', 'w') as f: |
| 129 | json.dump(metrics, f, indent=2) |
| 130 | |
| 131 | print("Review metrics generated successfully") |
| 132 | print(f"Total reviewers: {len(reviewer_stats)}") |
| 133 | print(f"Total reviews: {total_reviews}") |
| 134 | print(f"Total contributors: {len(contributor_stats)}") |
| 135 | print(f"Contributors not reviewing: {len(contributors_not_reviewing)}") |
| 136 | EOF |
| 137 | |
| 138 | - name: Generate Report |
| 139 | run: | |
| 140 | mkdir -p .github/reports |
| 141 | report_date=$(date +%Y-%m-%d) |
| 142 | |
| 143 | # Create Python script for simplified report generation |
| 144 | cat > generate_report.py << 'PYTHON_SCRIPT' |
| 145 | import json |
| 146 | import os |
| 147 | import sys |
| 148 | from datetime import datetime |
| 149 | |
| 150 | try: |
| 151 | # Load metrics |
| 152 | with open('review-data/metrics.json', 'r') as f: |
| 153 | metrics = json.load(f) |
| 154 | |
| 155 | summary = metrics['summary'] |
| 156 | reviewer_stats = metrics['reviewer_stats'] |
| 157 | contributor_stats = metrics['contributor_stats'] |
| 158 | contributors_not_reviewing = metrics['contributors_not_reviewing'] |
| 159 | |
| 160 | # Sort reviewers by review count |
| 161 | sorted_reviewers = sorted(reviewer_stats.items(), key=lambda x: x[1]['reviews_given'], reverse=True) |
| 162 | |
| 163 | # Sort contributors by PR count |
| 164 | sorted_contributors = sorted(contributor_stats.items(), key=lambda x: x[1]['prs_authored'], reverse=True) |
| 165 | |
| 166 | repo_name = os.environ.get('GITHUB_REPOSITORY', 'Unknown') |
| 167 | analysis_days = os.environ.get('ANALYSIS_DAYS', '30') |
| 168 | |
| 169 | # Generate comprehensive markdown report |
| 170 | report_lines = [ |
| 171 | "# Code Review Metrics Report", |
| 172 | "", |
| 173 | f"**Repository:** {repo_name}", |
| 174 | f"**Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')}", |
| 175 | f"**Period:** Last {analysis_days} days", |
| 176 | "", |
| 177 | "## Summary", |
| 178 | "", |
| 179 | f"- **Total PRs Analyzed:** {summary['total_prs_analyzed']}", |
| 180 | f"- **Total Reviews Given:** {summary['total_reviews']}", |
| 181 | f"- **Active Reviewers:** {summary['total_reviewers']}", |
| 182 | f"- **Total Contributors:** {summary['total_contributors']}", |
| 183 | f"- **Contributors Not Reviewing:** {summary['contributors_not_reviewing']}", |
| 184 | "", |
| 185 | "## Who Is Reviewing Code", |
| 186 | "", |
| 187 | "| Reviewer | Reviews Given | PRs Reviewed |", |
| 188 | "|----------|---------------|--------------|" |
| 189 | ] |
| 190 | |
| 191 | # Add all reviewers to table (focused on who and how many) |
| 192 | for reviewer, stats in sorted_reviewers: |
| 193 | report_lines.append(f"| {reviewer} | {stats['reviews_given']} | {stats['prs_reviewed']} |") |
| 194 | |
| 195 | # Add section for contributors who haven't done reviews |
| 196 | report_lines.extend([ |
| 197 | "", |
| 198 | "## Contributors Who Have Not Done Reviews", |
| 199 | "" |
| 200 | ]) |
| 201 | |
| 202 | if contributors_not_reviewing: |
| 203 | report_lines.extend([ |
| 204 | "| Contributor | PRs Authored |", |
| 205 | "|-------------|--------------|" |
| 206 | ]) |
| 207 | |
| 208 | for contributor in contributors_not_reviewing: |
| 209 | prs_authored = contributor_stats[contributor]['prs_authored'] |
| 210 | report_lines.append(f"| {contributor} | {prs_authored} |") |
| 211 | else: |
| 212 | report_lines.append("*All contributors are also participating in code reviews* ✅") |
| 213 | |
| 214 | # Add insights focused on reviewer activity |
| 215 | most_active = sorted_reviewers[0] if sorted_reviewers else ('N/A', {'reviews_given': 0}) |
| 216 | avg_reviews = summary['total_reviews'] / summary['total_reviewers'] if summary['total_reviewers'] > 0 else 0 |
| 217 | review_participation = (summary['total_reviewers'] / summary['total_contributors'] * 100) if summary['total_contributors'] > 0 else 0 |
| 218 | |
| 219 | report_lines.extend([ |
| 220 | "", |
| 221 | "## Key Insights", |
| 222 | "", |
| 223 | f"- **Most Active Reviewer:** {most_active[0]} ({most_active[1]['reviews_given']} reviews)", |
| 224 | f"- **Average Reviews per Reviewer:** {avg_reviews:.1f} reviews", |
| 225 | f"- **Review Participation Rate:** {review_participation:.1f}% of contributors are also reviewing", |
| 226 | f"- **Review Distribution:** {summary['total_reviews']} total reviews across {summary['total_prs_analyzed']} PRs", |
| 227 | "", |
| 228 | "---", |
| 229 | "*Report shows who is reviewing code, review volume per person, and contributors who could participate more in reviews*" |
| 230 | ]) |
| 231 | |
| 232 | # Save report |
| 233 | report_content = "\n".join(report_lines) |
| 234 | output_file = f'.github/reports/code-review-metrics-{os.environ.get("GITHUB_RUN_NUMBER", "test")}.md' |
| 235 | with open(output_file, 'w') as f: |
| 236 | f.write(report_content) |
| 237 | |
| 238 | print("Report generated successfully") |
| 239 | print(f"Output file: {output_file}") |
| 240 | |
| 241 | except Exception as e: |
| 242 | print(f"Error generating report: {e}") |
| 243 | sys.exit(1) |
| 244 | PYTHON_SCRIPT |
| 245 | |
| 246 | # Run the report generation |
| 247 | python3 generate_report.py |
| 248 | env: |
| 249 | GITHUB_REPOSITORY: ${{ github.repository }} |
| 250 | ANALYSIS_DAYS: ${{ github.event.inputs.days || '30' }} |
| 251 | GITHUB_RUN_NUMBER: ${{ github.run_number }} |
| 252 | |
| 253 | - name: Upload Artifacts |
| 254 | uses: actions/upload-artifact@v7 |
| 255 | with: |
| 256 | name: code-review-metrics-${{ github.run_number }} |
| 257 | path: | |
| 258 | .github/reports/code-review-metrics-*.md |
| 259 | review-data/metrics.json |
| 260 | retention-days: 90 |
| 261 | |
| 262 | - name: Job Summary |
| 263 | run: | |
| 264 | echo "# Code Review Metrics Generated 📊" >> $GITHUB_STEP_SUMMARY |
| 265 | days="${{ github.event.inputs.days || '30' }}" |
| 266 | echo "Period: ${days} days" >> $GITHUB_STEP_SUMMARY |
| 267 | echo "Focus: Who is reviewing code and review volume per reviewer" >> $GITHUB_STEP_SUMMARY |
| 268 | echo "Report artifacts uploaded with 90-day retention" >> $GITHUB_STEP_SUMMARY |
| 269 | |
| 270 | # Add summary stats to GitHub Actions summary |
| 271 | if [ -f review-data/metrics.json ]; then |
| 272 | python3 << 'EOF' |
| 273 | import json |
| 274 | import os |
| 275 | |
| 276 | with open('review-data/metrics.json', 'r') as f: |
| 277 | metrics = json.load(f) |
| 278 | |
| 279 | summary = metrics['summary'] |
| 280 | reviewer_stats = metrics['reviewer_stats'] |
| 281 | |
| 282 | # Find most active reviewer |
| 283 | if reviewer_stats: |
| 284 | top_reviewer = max(reviewer_stats.items(), key=lambda x: x[1]['reviews_given']) |
| 285 | top_reviewer_name, top_reviewer_stats = top_reviewer |
| 286 | else: |
| 287 | top_reviewer_name, top_reviewer_stats = 'N/A', {'reviews_given': 0} |
| 288 | |
| 289 | with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as f: |
| 290 | f.write(f"\n## Key Metrics\n") |
| 291 | f.write(f"- **Active Reviewers:** {summary['total_reviewers']}\n") |
| 292 | f.write(f"- **Total Reviews:** {summary['total_reviews']}\n") |
| 293 | f.write(f"- **PRs Analyzed:** {summary['total_prs_analyzed']}\n") |
| 294 | f.write(f"- **Total Contributors:** {summary['total_contributors']}\n") |
| 295 | f.write(f"- **Contributors Not Reviewing:** {summary['contributors_not_reviewing']}\n") |
| 296 | f.write(f"- **Most Active Reviewer:** {top_reviewer_name} ({top_reviewer_stats['reviews_given']} reviews)\n") |
| 297 | EOF |
| 298 | fi |