Skip to content

Commit a54e7ca

Browse files
CopilotOpenVMM Team
authored andcommitted
Add PR statistics workflow to track code review metrics
1 parent 68aeb89 commit a54e7ca

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed
Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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@v4
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
66+
with open('review-data/prs.json', 'r') as f:
67+
prs = [json.loads(line) for line in f if line.strip()]
68+
69+
print(f"Processing {len(prs)} PRs...")
70+
71+
# Initialize metrics - track both reviewers and contributors
72+
reviewer_stats = defaultdict(lambda: {
73+
'reviews_given': 0,
74+
'prs_reviewed': set()
75+
})
76+
77+
contributor_stats = defaultdict(lambda: {
78+
'prs_authored': 0
79+
})
80+
81+
total_reviews = 0
82+
83+
# Process each PR to count reviews per reviewer and track contributors
84+
for pr in prs:
85+
pr_number = pr['number']
86+
author = pr['author']['login']
87+
88+
# Track PR authors (contributors)
89+
contributor_stats[author]['prs_authored'] += 1
90+
91+
# Process reviews
92+
for review in pr.get('reviews', []):
93+
reviewer = review['author']['login']
94+
95+
total_reviews += 1
96+
reviewer_stats[reviewer]['reviews_given'] += 1
97+
reviewer_stats[reviewer]['prs_reviewed'].add(pr_number)
98+
99+
# Convert sets to counts for JSON serialization
100+
for reviewer in reviewer_stats:
101+
reviewer_stats[reviewer]['prs_reviewed'] = len(reviewer_stats[reviewer]['prs_reviewed'])
102+
103+
# Find contributors who haven't done reviews
104+
all_contributors = set(contributor_stats.keys())
105+
all_reviewers = set(reviewer_stats.keys())
106+
contributors_not_reviewing = all_contributors - all_reviewers
107+
108+
# Save comprehensive metrics
109+
metrics = {
110+
'summary': {
111+
'total_prs_analyzed': len(prs),
112+
'total_reviews': total_reviews,
113+
'total_reviewers': len(reviewer_stats),
114+
'total_contributors': len(contributor_stats),
115+
'contributors_not_reviewing': len(contributors_not_reviewing)
116+
},
117+
'reviewer_stats': dict(reviewer_stats),
118+
'contributor_stats': dict(contributor_stats),
119+
'contributors_not_reviewing': list(contributors_not_reviewing)
120+
}
121+
122+
with open('review-data/metrics.json', 'w') as f:
123+
json.dump(metrics, f, indent=2)
124+
125+
print("Review metrics generated successfully")
126+
print(f"Total reviewers: {len(reviewer_stats)}")
127+
print(f"Total reviews: {total_reviews}")
128+
print(f"Total contributors: {len(contributor_stats)}")
129+
print(f"Contributors not reviewing: {len(contributors_not_reviewing)}")
130+
EOF
131+
132+
- name: Generate Report
133+
run: |
134+
mkdir -p .github/reports
135+
report_date=$(date +%Y-%m-%d)
136+
137+
# Create Python script for simplified report generation
138+
cat > generate_report.py << 'PYTHON_SCRIPT'
139+
import json
140+
import os
141+
import sys
142+
from datetime import datetime
143+
144+
try:
145+
# Load metrics
146+
with open('review-data/metrics.json', 'r') as f:
147+
metrics = json.load(f)
148+
149+
summary = metrics['summary']
150+
reviewer_stats = metrics['reviewer_stats']
151+
contributor_stats = metrics['contributor_stats']
152+
contributors_not_reviewing = metrics['contributors_not_reviewing']
153+
154+
# Sort reviewers by review count
155+
sorted_reviewers = sorted(reviewer_stats.items(), key=lambda x: x[1]['reviews_given'], reverse=True)
156+
157+
# Sort contributors by PR count
158+
sorted_contributors = sorted(contributor_stats.items(), key=lambda x: x[1]['prs_authored'], reverse=True)
159+
160+
repo_name = os.environ.get('GITHUB_REPOSITORY', 'Unknown')
161+
analysis_days = os.environ.get('ANALYSIS_DAYS', '30')
162+
163+
# Generate comprehensive markdown report
164+
report_lines = [
165+
"# Code Review Metrics Report",
166+
"",
167+
f"**Repository:** {repo_name}",
168+
f"**Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S UTC')}",
169+
f"**Period:** Last {analysis_days} days",
170+
"",
171+
"## Summary",
172+
"",
173+
f"- **Total PRs Analyzed:** {summary['total_prs_analyzed']}",
174+
f"- **Total Reviews Given:** {summary['total_reviews']}",
175+
f"- **Active Reviewers:** {summary['total_reviewers']}",
176+
f"- **Total Contributors:** {summary['total_contributors']}",
177+
f"- **Contributors Not Reviewing:** {summary['contributors_not_reviewing']}",
178+
"",
179+
"## Who Is Reviewing Code",
180+
"",
181+
"| Reviewer | Reviews Given | PRs Reviewed |",
182+
"|----------|---------------|--------------|"
183+
]
184+
185+
# Add all reviewers to table (focused on who and how many)
186+
for reviewer, stats in sorted_reviewers:
187+
report_lines.append(f"| {reviewer} | {stats['reviews_given']} | {stats['prs_reviewed']} |")
188+
189+
# Add section for contributors who haven't done reviews
190+
report_lines.extend([
191+
"",
192+
"## Contributors Who Have Not Done Reviews",
193+
""
194+
])
195+
196+
if contributors_not_reviewing:
197+
report_lines.extend([
198+
"| Contributor | PRs Authored |",
199+
"|-------------|--------------|"
200+
])
201+
202+
for contributor in contributors_not_reviewing:
203+
prs_authored = contributor_stats[contributor]['prs_authored']
204+
report_lines.append(f"| {contributor} | {prs_authored} |")
205+
else:
206+
report_lines.append("*All contributors are also participating in code reviews* ✅")
207+
208+
# Add insights focused on reviewer activity
209+
most_active = sorted_reviewers[0] if sorted_reviewers else ('N/A', {'reviews_given': 0})
210+
avg_reviews = summary['total_reviews'] / summary['total_reviewers'] if summary['total_reviewers'] > 0 else 0
211+
review_participation = (summary['total_reviewers'] / summary['total_contributors'] * 100) if summary['total_contributors'] > 0 else 0
212+
213+
report_lines.extend([
214+
"",
215+
"## Key Insights",
216+
"",
217+
f"- **Most Active Reviewer:** {most_active[0]} ({most_active[1]['reviews_given']} reviews)",
218+
f"- **Average Reviews per Reviewer:** {avg_reviews:.1f} reviews",
219+
f"- **Review Participation Rate:** {review_participation:.1f}% of contributors are also reviewing",
220+
f"- **Review Distribution:** {summary['total_reviews']} total reviews across {summary['total_prs_analyzed']} PRs",
221+
"",
222+
"---",
223+
"*Report shows who is reviewing code, review volume per person, and contributors who could participate more in reviews*"
224+
])
225+
226+
# Save report
227+
report_content = "\n".join(report_lines)
228+
output_file = f'.github/reports/code-review-metrics-{os.environ.get("GITHUB_RUN_NUMBER", "test")}.md'
229+
with open(output_file, 'w') as f:
230+
f.write(report_content)
231+
232+
print("Report generated successfully")
233+
print(f"Output file: {output_file}")
234+
235+
except Exception as e:
236+
print(f"Error generating report: {e}")
237+
sys.exit(1)
238+
PYTHON_SCRIPT
239+
240+
# Run the report generation
241+
python3 generate_report.py
242+
env:
243+
GITHUB_REPOSITORY: ${{ github.repository }}
244+
ANALYSIS_DAYS: ${{ github.event.inputs.days || '30' }}
245+
GITHUB_RUN_NUMBER: ${{ github.run_number }}
246+
247+
- name: Upload Artifacts
248+
uses: actions/upload-artifact@v4
249+
with:
250+
name: code-review-metrics-${{ github.run_number }}
251+
path: |
252+
.github/reports/code-review-metrics-*.md
253+
review-data/metrics.json
254+
retention-days: 90
255+
256+
- name: Job Summary
257+
run: |
258+
echo "# Code Review Metrics Generated 📊" >> $GITHUB_STEP_SUMMARY
259+
days="${{ github.event.inputs.days || '30' }}"
260+
echo "Period: ${days} days" >> $GITHUB_STEP_SUMMARY
261+
echo "Focus: Who is reviewing code and review volume per reviewer" >> $GITHUB_STEP_SUMMARY
262+
echo "Report artifacts uploaded with 90-day retention" >> $GITHUB_STEP_SUMMARY
263+
264+
# Add summary stats to GitHub Actions summary
265+
if [ -f review-data/metrics.json ]; then
266+
python3 << 'EOF'
267+
import json
268+
import os
269+
270+
with open('review-data/metrics.json', 'r') as f:
271+
metrics = json.load(f)
272+
273+
summary = metrics['summary']
274+
reviewer_stats = metrics['reviewer_stats']
275+
276+
# Find most active reviewer
277+
if reviewer_stats:
278+
top_reviewer = max(reviewer_stats.items(), key=lambda x: x[1]['reviews_given'])
279+
top_reviewer_name, top_reviewer_stats = top_reviewer
280+
else:
281+
top_reviewer_name, top_reviewer_stats = 'N/A', {'reviews_given': 0}
282+
283+
with open(os.environ['GITHUB_STEP_SUMMARY'], 'a') as f:
284+
f.write(f"\n## Key Metrics\n")
285+
f.write(f"- **Active Reviewers:** {summary['total_reviewers']}\n")
286+
f.write(f"- **Total Reviews:** {summary['total_reviews']}\n")
287+
f.write(f"- **PRs Analyzed:** {summary['total_prs_analyzed']}\n")
288+
f.write(f"- **Total Contributors:** {summary['total_contributors']}\n")
289+
f.write(f"- **Contributors Not Reviewing:** {summary['contributors_not_reviewing']}\n")
290+
f.write(f"- **Most Active Reviewer:** {top_reviewer_name} ({top_reviewer_stats['reviews_given']} reviews)\n")
291+
EOF
292+
fi

0 commit comments

Comments
 (0)