Skip to content

Commit

Permalink
Update 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 4254b2a commit a842c07
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions .github/scripts/validate_pr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import os
import sys
from github import Github
from datetime import datetime
from github import GithubException

def validate_env_vars():
"""Validate required environment variables."""
required_vars = ['GITHUB_TOKEN', 'PR_NUMBER', 'REPO_NAME', 'AUTHOR']
missing_vars = [var for var in required_vars if not os.getenv(var)]

if missing_vars:
print(f"Error: Missing required environment variables: {', '.join(missing_vars)}")
sys.exit(1)

def calculate_score(g, repo_name, author, pr_number):
"""Calculate contribution score based on various metrics."""
Expand Down Expand Up @@ -47,6 +57,11 @@ def calculate_score(g, repo_name, author, pr_number):

return total_score, scores

except GithubException as e:
print(f"GitHub API Error: {str(e)}")
print(f"Status: {e.status}")
print(f"Data: {e.data}")
raise
except Exception as e:
print(f"Error calculating scores: {str(e)}")
raise
Expand Down Expand Up @@ -79,18 +94,27 @@ def create_review_message(total_score, scores):
def main():
"""Main function to evaluate PR and create review."""
try:
print("Validating environment variables...")
validate_env_vars()

# 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}")
print(f"Repository: {repo_name}")

# Initialize GitHub client
g = Github(token)
print("GitHub client initialized")

repo = g.get_repo(repo_name)
print("Repository accessed successfully")

pr = repo.get_pull(pr_number)
print("Pull request accessed successfully")

# Calculate scores
total_score, scores = calculate_score(g, repo_name, author, pr_number)
Expand All @@ -112,7 +136,7 @@ def main():

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

if __name__ == "__main__":
main()

0 comments on commit a842c07

Please sign in to comment.