"""Tools for Gitea pull request operations.""" import json from typing import Any from gitea.client import GiteaClient from gitea.models import PullRequestModel, CommentModel class PRTools: """Tools for Gitea pull request operations.""" def __init__(self, client: GiteaClient) -> None: self._client = client def get_pull_request(self, owner: str, repo: str, pull_number: int) -> str: try: pr: PullRequestModel = self._client.get_pull_request(owner, repo, pull_number) return pr.model_dump_json(indent=2) except Exception as e: return f"Error getting pull request: {str(e)}" def close_pull_request(self, owner: str, repo: str, pull_number: int) -> str: try: self._client.close_pull_request(owner, repo, pull_number) return f"Pull request #{pull_number} closed successfully." except Exception as e: return f"Error closing pull request: {str(e)}" def get_pull_request_comments(self, owner: str, repo: str, pull_number: int) -> str: try: comments: list[CommentModel] = self._client.get_pull_request_comments(owner, repo, pull_number) return json.dumps([c.model_dump() for c in comments], indent=2) except Exception as e: return f"Error getting PR comments: {str(e)}" def list_assigned_pull_requests(self) -> list[dict[str, Any]]: try: repos = self._client.list_all_user_repos() all_prs: list[dict[str, Any]] = [] for repo_info in repos: repo_owner = repo_info.owner repo_name = repo_info.name prs = self._client.list_assigned_pull_requests(repo_owner, repo_name) if prs: all_prs.extend([pr.model_dump() if hasattr(pr, 'model_dump') else pr for pr in prs]) return all_prs except Exception as e: print(f"DEBUG: list_assigned_pull_requests error: {e}") return [] def list_pull_requests(self, owner: str, repo: str, state: str = "open") -> str: try: prs = self._client.list_repo_pull_requests(owner, repo, state) if not prs: return f"No PRs in {owner}/{repo}." summary = [f"#{pr.number}: {pr.title}" for pr in prs] return "\n".join(summary) except Exception as e: return f"Error listing PRs: {str(e)}" def create_pull_request(self, owner: str, repo: str, head: str, base: str, title: str, description: str = "") -> str: try: pr = self._client.create_pr_via_tea(owner, repo, title, description, head, base) return pr.model_dump_json(indent=2) except Exception as e: return f"Error creating PR: {str(e)}" def add_label_to_pr(self, owner: str, repo: str, pr_number: int, label: str) -> str: try: self._client.add_label_pr(owner, repo, pr_number, label) return f"Label '{label}' added to PR #{pr_number}." except Exception as e: return f"Error adding label to PR #{pr_number}: {e}" def get_pull_request_diff(self, owner: str, repo: str, pull_number: int) -> str: try: return self._client.get_pull_request_diff(owner, repo, pull_number) except Exception as e: return f"Error getting PR diff: {str(e)}" def get_pull_request_patch(self, owner: str, repo: str, pull_number: int) -> str: try: return self._client.get_pull_request_patch(owner, repo, pull_number) except Exception as e: return f"Error getting PR patch: {str(e)}" def approve_pull_request(self, owner: str, repo: str, pull_number: int, comment: str) -> str: try: self._client.approve_pr(owner, repo, pull_number, comment) return f"Approved PR #{pull_number}." except Exception as e: return f"Error approving PR: {str(e)}" def request_changes(self, owner: str, repo: str, pull_number: int, comment: str) -> str: try: self._client.request_changes_pr(owner, repo, pull_number, comment) return f"Requested changes on PR #{pull_number}." except Exception as e: return f"Error requesting changes: {str(e)}"