fix logging

This commit is contained in:
Michael Ingvarsson
2026-07-16 12:15:45 +02:00
parent 88d9ac2105
commit 9e40e7fed8
3 changed files with 27 additions and 15 deletions
+17 -13
View File
@@ -1,7 +1,11 @@
import httpx import httpx
import json import json
import base64 import base64
import logging
from typing import Any, Optional from typing import Any, Optional
logger: logging.Logger = logging.getLogger("gitea.client")
from .config import GITEA_URL, GITEA_TOKEN from .config import GITEA_URL, GITEA_TOKEN
from .models import ( from .models import (
UserModel, UserModel,
@@ -38,7 +42,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
response.raise_for_status() response.raise_for_status()
return UserModel(**response.json()) return UserModel(**response.json())
except Exception as e: except Exception as e:
print(f"Error getting authenticated user: {e}") logger.error(f"Error getting authenticated user: {e}", exc_info=True)
return None return None
def list_all_user_repos(self) -> list[RepositoryModel]: def list_all_user_repos(self) -> list[RepositoryModel]:
@@ -58,7 +62,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
result.append(RepositoryModel(**r)) result.append(RepositoryModel(**r))
return result return result
except Exception as e: except Exception as e:
print(f"Error listing user repos: {e}") logger.error(f"Error listing user repos: {e}", exc_info=True)
return [] return []
def list_repo_issues(self, owner: str, repo: str, state: str = "open") -> list[IssueModel]: 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] = [] all_issues: list[IssueModel] = []
repos = self.list_all_user_repos() repos = self.list_all_user_repos()
for r in repos: for r in repos:
repo_owner = r.owner if hasattr(r, 'owner') else (r.get("owner") or {}).get("login", "") repo_owner = r.owner
repo_name = r.name if hasattr(r, 'name') else r.get("name", "") repo_name = r.name
resp = httpx.get( resp = httpx.get(
f"{self.base_url}/api/v1/repos/{repo_owner}/{repo_name}/issues?assignee={username}&state=open&type=issues", f"{self.base_url}/api/v1/repos/{repo_owner}/{repo_name}/issues?assignee={username}&state=open&type=issues",
headers=self.headers, headers=self.headers,
@@ -173,7 +177,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
all_issues.append(issue) all_issues.append(issue)
return all_issues return all_issues
except Exception as e: except Exception as e:
print(f"DEBUG: list_assigned_issues error: {e}") logger.error(f"Error listing assigned issues: {e}", exc_info=True)
return [] return []
def list_assigned_pull_requests(self, owner: str = "", repo: str = "") -> list[PullRequestModel]: 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] = [] all_prs: list[PullRequestModel] = []
repos = self.list_all_user_repos() repos = self.list_all_user_repos()
for r in repos: for r in repos:
repo_owner = r.owner if hasattr(r, 'owner') else (r.get("owner") or {}).get("login", "") repo_owner = r.owner
repo_name = r.name if hasattr(r, 'name') else r.get("name", "") repo_name = r.name
resp = httpx.get( resp = httpx.get(
f"{self.base_url}/api/v1/repos/{repo_owner}/{repo_name}/pulls?state=open", f"{self.base_url}/api/v1/repos/{repo_owner}/{repo_name}/pulls?state=open",
headers=self.headers, headers=self.headers,
@@ -215,7 +219,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
all_prs.append(pr) all_prs.append(pr)
return all_prs return all_prs
except Exception as e: 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 [] return []
def create_pull_request( def create_pull_request(
@@ -234,7 +238,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
response.raise_for_status() response.raise_for_status()
return PullRequestModel(**response.json()) return PullRequestModel(**response.json())
except Exception as e: except Exception as e:
print(f"Error creating pull request: {e}") logger.error(f"Error creating pull request: {e}", exc_info=True)
raise raise
def update_pull_request( def update_pull_request(
@@ -260,7 +264,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
response.raise_for_status() response.raise_for_status()
return PullRequestModel(**response.json()) return PullRequestModel(**response.json())
except Exception as e: except Exception as e:
print(f"Error updating pull request: {e}") logger.error(f"Error updating pull request: {e}", exc_info=True)
raise raise
def create_pr_via_tea( def create_pr_via_tea(
@@ -417,7 +421,7 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
result.append(n) result.append(n)
return result return result
except Exception as e: except Exception as e:
print(f"Error listing unread notifications: {e}") logger.error(f"Error listing unread notifications: {e}", exc_info=True)
return [] return []
def mark_notification_as_read(self, thread_id: int) -> bool: 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() response.raise_for_status()
return True return True
except Exception as e: 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 return False
def merge_pull_request( def merge_pull_request(
@@ -446,6 +450,6 @@ class GiteaClient(IssuesClient, PullRequestsClient, FilesClient, RefsClient, Rep
response.raise_for_status() response.raise_for_status()
return True return True
except Exception as e: 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 raise
+5 -1
View File
@@ -1,10 +1,14 @@
"""Tools for Gitea issue operations.""" """Tools for Gitea issue operations."""
import json import json
import logging
from typing import Any from typing import Any
from gitea.client import GiteaClient from gitea.client import GiteaClient
from gitea.models import IssueModel, CommentModel, LabelModel from gitea.models import IssueModel, CommentModel, LabelModel
logger: logging.Logger = logging.getLogger("gitea.tools.issue_tools")
class IssueTools: class IssueTools:
"""Tools for Gitea issue operations.""" """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]) all_issues.extend([issue.model_dump() if hasattr(issue, 'model_dump') else issue for issue in issues])
return all_issues return all_issues
except Exception as e: except Exception as e:
print(f"DEBUG: list_assigned_issues error: {e}") logger.error(f"Error listing assigned issues: {e}", exc_info=True)
return [] return []
def list_issues(self, owner: str, repo: str, state: str = "open") -> str: def list_issues(self, owner: str, repo: str, state: str = "open") -> str:
+5 -1
View File
@@ -1,10 +1,14 @@
"""Tools for Gitea pull request operations.""" """Tools for Gitea pull request operations."""
import json import json
import logging
from typing import Any from typing import Any
from gitea.client import GiteaClient from gitea.client import GiteaClient
from gitea.models import PullRequestModel, CommentModel from gitea.models import PullRequestModel, CommentModel
logger: logging.Logger = logging.getLogger("gitea.tools.pr_tools")
_MAX_DIFF_CHARS: int = 15_000 _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]) all_prs.extend([pr.model_dump() if hasattr(pr, 'model_dump') else pr for pr in prs])
return all_prs return all_prs
except Exception as e: 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 [] return []
def list_pull_requests(self, owner: str, repo: str, state: str = "open") -> str: def list_pull_requests(self, owner: str, repo: str, state: str = "open") -> str: