import re import requests import os import logging import sys from pathlib import Path # Add the gitea-shim to the path gitea_shim_path = Path(__file__).parent.parent.parent.parent.parent / "gitea-shim" / "python" sys.path.insert(0, str(gitea_shim_path)) try: from gitea_shim import get_github_client, is_gitea_mode except ImportError: # Fallback to regular GitHub if shim is not available from github import Github as get_github_client is_gitea_mode = lambda: False logger = logging.getLogger(__name__) def verify_pr_ownership( pr_url, expected_username, expected_owner, expected_repo, ): try: # Use the shim to get GitHub-compatible client gh = get_github_client() # Log which API we're using logger.info(f"Using {'Gitea' if is_gitea_mode() else 'GitHub'} API for PR verification") match = re.match(r"https://github.com/([^/]+)/([^/]+)/pull/(\d+)", pr_url) if not match: logger.error(f"Invalid PR URL: {pr_url}") return False owner, repo_name, pr_number = match.groups() if owner != expected_owner or repo_name != expected_repo: logger.error( f"PR URL mismatch: {pr_url} != {expected_owner}/{expected_repo}" ) return False repo = gh.get_repo(f"{owner}/{repo_name}") pr = repo.get_pull(int(pr_number)) if pr.user.login != expected_username: logger.error( f"PR username mismatch: {pr.user.login} != {expected_username}" ) return False return True except Exception as e: logger.error(f"Error verifying PR ownership: {str(e)}") return True