From 86988dddc00ed11cb83fb3a4891909f64d46bdf9 Mon Sep 17 00:00:00 2001 From: Vishakh Abhayan <94307781+vishakh-abhayan@users.noreply.github.com> Date: Sun, 3 Nov 2024 21:05:20 +0530 Subject: [PATCH] Create validate_pr.py --- .github/scripts/validate_pr.py | 118 +++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 .github/scripts/validate_pr.py diff --git a/.github/scripts/validate_pr.py b/.github/scripts/validate_pr.py new file mode 100644 index 00000000..858deadb --- /dev/null +++ b/.github/scripts/validate_pr.py @@ -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()