fix the Code Review Leaderboard so it shows the users who have reviewed PRs on the projects / repos
this was not fixed, it's not showing the reviewers correctly @coderabbitai can you show the code for a fix that will correctly show the github users who have reviewed a pr?
[!TIP] For best results, initiate chat on the files or code changes.
Oops, something went wrong! Please try again later. 🐰 💔
@coderabbitai can you please explain the issue?
[!TIP] For best results, initiate chat on the files or code changes.
🧩 Analysis chain
@Krishiv-Mahajan Let me search for the Code Review Leaderboard implementation to explain what's wrong.
🏁 Script executed:
#!/bin/bash
# Find leaderboard related files
fd -e py -e html "leaderboard" --type f
Length of output: 358
🏁 Script executed:
#!/bin/bash
# Search for code review leaderboard implementation
rg -i "code.?review.?leaderboard|reviewed.?pr.?leaderboard" --type py -C 5
Length of output: 4153
🏁 Script executed:
#!/bin/bash
# Look for the leaderboard views
cat website/views/user.py | grep -A 50 "leaderboard_global"
Length of output: 2327
🏁 Script executed:
#!/bin/bash
# Check if there's a GitHubIssue model with reviews relationship
rg "class GitHubIssue" --type py -A 30
Length of output: 8369
🏁 Script executed:
#!/bin/bash
# Search for Review model or reviewer relationships
rg "class.*Review" --type py -A 20 | grep -v "test"
Length of output: 1784
🏁 Script executed:
#!/bin/bash
# Check the template to see how code review leaderboard is displayed
cat website/templates/leaderboard_global.html | grep -A 30 "code.?review" -i
Length of output: 0
🏁 Script executed:
#!/bin/bash
# Let's see the full reviewed_pr_leaderboard query implementation
cat website/views/user.py | grep -A 20 "Reviewed PR Leaderboard"
Length of output: 911
🏁 Script executed:
#!/bin/bash
# Check what data GitHubReview actually has - let's see if there are any records
rg "GitHubReview" --type py -C 3 | head -100
Length of output: 6051
🏁 Script executed:
#!/bin/bash
# Let's check if there's a management command that syncs GitHub reviews
cat website/management/commands/leaderboard.py
Length of output: 861
🏁 Script executed:
#!/bin/bash
# Let's see the full template to understand how the leaderboard is displayed
cat website/templates/leaderboard_global.html
Length of output: 16992
🏁 Script executed:
#!/bin/bash
# Check how GitHub reviews are being synced
cat website/management/commands/update_github_issues.py | grep -A 50 "reviews"
Length of output: 3058
🏁 Script executed:
#!/bin/bash
# Check if UserProfile has github_url field
rg "class UserProfile" --type py -A 50 | grep -i "github"
Length of output: 220
@Krishiv-Mahajan I found the issue! Let me explain what's wrong with the Code Review Leaderboard.
The Problem:
The leaderboard is supposed to show GitHub users who have reviewed pull requests, but there's a critical bug in the data sync command that prevents reviewer data from being collected correctly.
Root Cause:
In website/management/commands/update_github_issues.py, when the command fetches PR reviews from GitHub, it has this logic:
# Fetch reviews for this pull request
reviews_url = pr["pull_request"]["url"] + "/reviews"
reviews_response = requests.get(reviews_url, headers=headers)
reviews_data = reviews_response.json()
# Store reviews made by the user
if isinstance(reviews_data, list):
for review in reviews_data:
if review.get("user") and review["user"].get("login") == github_username:
GitHubReview.objects.update_or_create(...)
The bug is in this line:
if review.get("user") and review["user"].get("login") == github_username:
Why This is Wrong:
github_usernameis the PR author (the person being processed in the outer loop)- The condition only saves reviews where the reviewer's username matches the PR author's username
- But people don't review their own PRs! Other people review them.
- So the
GitHubReviewtable ends up empty or with very few records - That's why the leaderboard shows no reviewers
The Fix:
The command should store ALL reviews on each PR, not just reviews by the PR author. Here's what needs to be changed:
# Store reviews made by ANY user (not just the PR author)
if isinstance(reviews_data, list):
for review in reviews_data:
reviewer_login = review.get("user", {}).get("login")
if reviewer_login:
# Try to find the reviewer's UserProfile
try:
reviewer_profile = UserProfile.objects.filter(
github_url__icontains=reviewer_login
).first()
if reviewer_profile:
GitHubReview.objects.update_or_create(
review_id=review["id"],
defaults={
"pull_request": github_issue,
"reviewer": reviewer_profile, # The actual reviewer, not the PR author
"body": review.get("body", ""),
"state": review["state"],
"submitted_at": timezone.make_aware(
datetime.strptime(review["submitted_at"], "%Y-%m-%dT%H:%M:%SZ")
),
"url": review["html_url"],
},
)
except Exception as e:
self.stdout.write(
self.style.WARNING(f"Could not find UserProfile for reviewer {reviewer_login}: {str(e)}")
)
Summary:
The leaderboard query itself is correct, but the data isn't being populated properly because the sync command only saves reviews made by PR authors (which rarely happens). The fix is to save ALL reviews and match each reviewer to their UserProfile in the database.
Would you like me to create an issue to track this fix, or do you have any other questions?
/assign
Hello @Krishiv-Mahajan! You've been assigned to OWASP-BLT/BLT issue #3440. You have 24 hours to complete a pull request.
⏰ This issue has been automatically unassigned from Krishiv-Mahajan due to 24 hours of inactivity. The issue is now available for anyone to work on again.
This is still not working, reopening
There is another PR in progress that looks like it may fix it. Please link that PR here @copilot