From 9e40e7fed81de87447b028f89eeea230bb9670b9 Mon Sep 17 00:00:00 2001 From: Michael Ingvarsson Date: Thu, 16 Jul 2026 12:15:45 +0200 Subject: [PATCH] fix logging --- gitea/client.py | 30 +++++++++++++++++------------- gitea/tools/issue_tools.py | 6 +++++- gitea/tools/pr_tools.py | 6 +++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gitea/client.py b/gitea/client.py index 311f2c5..7d2efb3 100644 --- a/gitea/client.py +++ b/gitea/client.py @@ -1,7 +1,11 @@ import httpx import json import base64 +import logging from typing import Any, Optional + +logger: logging.Logger = logging.getLogger("gitea.client") + from .config import GITEA_URL, GITEA_TOKEN from .models import ( UserModel, @@ -38,7 +42,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep response.raise_for_status() return UserModel(**response.json()) except Exception as e: - print(f"Error getting authenticated user: {e}") + logger.error(f"Error getting authenticated user: {e}", exc_info=True) return None def list_all_user_repos(self) -> list[RepositoryModel]: @@ -58,7 +62,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep result.append(RepositoryModel(**r)) return result except Exception as e: - print(f"Error listing user repos: {e}") + logger.error(f"Error listing user repos: {e}", exc_info=True) return [] def list_repo_issues(self, owner: str, repo: str, state: str = "open") -> list[IssueModel]: @@ -158,8 +162,8 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep all_issues: list[IssueModel] = [] repos = self.list_all_user_repos() for r in repos: - repo_owner = r.owner if hasattr(r, 'owner') else (r.get("owner") or {}).get("login", "") - repo_name = r.name if hasattr(r, 'name') else r.get("name", "") + repo_owner = r.owner + repo_name = r.name resp = httpx.get( f"{self.base_url}/api/v1/repos/{repo_owner}/{repo_name}/issues?assignee={username}&state=open&type=issues", headers=self.headers, @@ -173,7 +177,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep all_issues.append(issue) return all_issues except Exception as e: - print(f"DEBUG: list_assigned_issues error: {e}") + logger.error(f"Error listing assigned issues: {e}", exc_info=True) return [] def list_assigned_pull_requests(self, owner: str = "", repo: str = "") -> list[PullRequestModel]: @@ -199,8 +203,8 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep all_prs: list[PullRequestModel] = [] repos = self.list_all_user_repos() for r in repos: - repo_owner = r.owner if hasattr(r, 'owner') else (r.get("owner") or {}).get("login", "") - repo_name = r.name if hasattr(r, 'name') else r.get("name", "") + repo_owner = r.owner + repo_name = r.name resp = httpx.get( f"{self.base_url}/api/v1/repos/{repo_owner}/{repo_name}/pulls?state=open", headers=self.headers, @@ -215,7 +219,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep all_prs.append(pr) return all_prs except Exception as e: - print(f"DEBUG: list_assigned_pull_requests error: {e}") + logger.error(f"Error listing assigned pull requests: {e}", exc_info=True) return [] def create_pull_request( @@ -234,7 +238,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep response.raise_for_status() return PullRequestModel(**response.json()) except Exception as e: - print(f"Error creating pull request: {e}") + logger.error(f"Error creating pull request: {e}", exc_info=True) raise def update_pull_request( @@ -260,7 +264,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep response.raise_for_status() return PullRequestModel(**response.json()) except Exception as e: - print(f"Error updating pull request: {e}") + logger.error(f"Error updating pull request: {e}", exc_info=True) raise def create_pr_via_tea( @@ -417,7 +421,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep result.append(n) return result except Exception as e: - print(f"Error listing unread notifications: {e}") + logger.error(f"Error listing unread notifications: {e}", exc_info=True) return [] def mark_notification_as_read(self, thread_id: int) -> bool: @@ -428,7 +432,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep response.raise_for_status() return True except Exception as e: - print(f"Error marking notification thread {thread_id} as read: {e}") + logger.error(f"Error marking notification thread {thread_id} as read: {e}", exc_info=True) return False def merge_pull_request( @@ -446,6 +450,6 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep response.raise_for_status() return True except Exception as e: - print(f"Error merging pull request {pull_number}: {e}") + logger.error(f"Error merging pull request {pull_number}: {e}", exc_info=True) raise diff --git a/gitea/tools/issue_tools.py b/gitea/tools/issue_tools.py index 3a29f9c..53994bc 100644 --- a/gitea/tools/issue_tools.py +++ b/gitea/tools/issue_tools.py @@ -1,10 +1,14 @@ """Tools for Gitea issue operations.""" import json +import logging from typing import Any from gitea.client import GiteaClient from gitea.models import IssueModel, CommentModel, LabelModel +logger: logging.Logger = logging.getLogger("gitea.tools.issue_tools") + + class IssueTools: """Tools for Gitea issue operations.""" @@ -69,7 +73,7 @@ class IssueTools: all_issues.extend([issue.model_dump() if hasattr(issue, 'model_dump') else issue for issue in issues]) return all_issues except Exception as e: - print(f"DEBUG: list_assigned_issues error: {e}") + logger.error(f"Error listing assigned issues: {e}", exc_info=True) return [] def list_issues(self, owner: str, repo: str, state: str = "open") -> str: diff --git a/gitea/tools/pr_tools.py b/gitea/tools/pr_tools.py index b2b473e..65668f6 100644 --- a/gitea/tools/pr_tools.py +++ b/gitea/tools/pr_tools.py @@ -1,10 +1,14 @@ """Tools for Gitea pull request operations.""" import json +import logging from typing import Any from gitea.client import GiteaClient from gitea.models import PullRequestModel, CommentModel +logger: logging.Logger = logging.getLogger("gitea.tools.pr_tools") + + _MAX_DIFF_CHARS: int = 15_000 @@ -93,7 +97,7 @@ class PRTools: 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}") + logger.error(f"Error listing assigned pull requests: {e}", exc_info=True) return [] def list_pull_requests(self, owner: str, repo: str, state: str = "open") -> str: