Skip to content

Commit

Permalink
Create validate_pr.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vishakh-abhayan authored Nov 3, 2024
1 parent f8afbcc commit 86988dd
Showing 1 changed file with 118 additions and 0 deletions.
118 changes: 118 additions & 0 deletions .github/scripts/validate_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import os
from github import Github
from datetime import datetime

def calculate_score(g, repo_name, author, pr_number):
"""Calculate contribution score based on various metrics."""
try:
# Initialize GitHub repository
repo = g.get_repo(repo_name)
scores = {}

print(f"Evaluating contributions for {author} in {repo_name}")

# Get all PRs by the user
all_prs = list(repo.get_pulls(state='all'))
user_prs = [pr for pr in all_prs if pr.user.login == author]

# PR Volume Score (15%)
pr_volume = len(user_prs)
scores['pr_volume'] = min((pr_volume / 10) * 15, 15)
print(f"PR Volume: {pr_volume} PRs, Score: {scores['pr_volume']:.2f}%")

# Merged PRs Score (15%)
merged_prs = [pr for pr in user_prs if pr.merged]
scores['merged_prs'] = (len(merged_prs) / max(pr_volume, 1)) * 15
print(f"Merged PRs: {len(merged_prs)} PRs, Score: {scores['merged_prs']:.2f}%")

# Issues Score (10%)
user_issues = list(repo.get_issues(creator=author))
scores['issues'] = min((len(user_issues) / 5) * 10, 10)
print(f"Issues: {len(user_issues)} issues, Score: {scores['issues']:.2f}%")

# Comments Score (15%)
issue_comments = list(repo.get_issues_comments())
user_comments = [comment for comment in issue_comments if comment.user.login == author]
scores['comments'] = min((len(user_comments) / 20) * 15, 15)
print(f"Comments: {len(user_comments)} comments, Score: {scores['comments']:.2f}%")

# Documentation Score (5%)
doc_prs = [pr for pr in user_prs if 'doc' in pr.title.lower() or 'readme' in pr.title.lower()]
scores['docs'] = min((len(doc_prs) / 3) * 5, 5)
print(f"Documentation: {len(doc_prs)} contributions, Score: {scores['docs']:.2f}%")

# Calculate total score
total_score = sum(scores.values())
print(f"Total Score: {total_score:.2f}%")

return total_score, scores

except Exception as e:
print(f"Error calculating scores: {str(e)}")
raise

def create_review_message(total_score, scores):
"""Create formatted review message with scores."""
message = "# PR Evaluation Results\n\n"
message += f"Total Score: {total_score:.2f}%\n\n"
message += "## Breakdown:\n"
message += f"- PR Volume: {scores['pr_volume']:.2f}% / 15%\n"
message += f"- Merged PRs: {scores['merged_prs']:.2f}% / 15%\n"
message += f"- Issues: {scores['issues']:.2f}% / 10%\n"
message += f"- Comments: {scores['comments']:.2f}% / 15%\n"
message += f"- Documentation: {scores['docs']:.2f}% / 5%\n\n"

PASSING_SCORE = 60

if total_score >= PASSING_SCORE:
message += "✅ Congratulations! Your contributions meet our criteria."
else:
message += f"❌ Your contributions don't meet our minimum criteria of {PASSING_SCORE}%.\n\n"
message += "### Suggestions for improvement:\n"
message += "- Submit more quality PRs\n"
message += "- Engage more in discussions and code reviews\n"
message += "- Contribute to documentation\n"
message += "- Help resolve issues\n"

return message

def main():
"""Main function to evaluate PR and create review."""
try:
# Get environment variables
token = os.getenv('GITHUB_TOKEN')
repo_name = os.getenv('REPO_NAME')
pr_number = int(os.getenv('PR_NUMBER'))
author = os.getenv('AUTHOR')

print(f"Starting evaluation for PR #{pr_number} by {author}")

# Initialize GitHub client
g = Github(token)
repo = g.get_repo(repo_name)
pr = repo.get_pull(pr_number)

# Calculate scores
total_score, scores = calculate_score(g, repo_name, author, pr_number)

# Create review message
review_message = create_review_message(total_score, scores)
print("Review message created")

# Create PR review
PASSING_SCORE = 60
review_event = 'APPROVE' if total_score >= PASSING_SCORE else 'REQUEST_CHANGES'

pr.create_review(
body=review_message,
event=review_event
)

print(f"PR review created with status: {review_event}")

except Exception as e:
print(f"Error in main function: {str(e)}")
raise SystemExit(1)

if __name__ == "__main__":
main()

0 comments on commit 86988dd

Please sign in to comment.