Files
Home_assistant/backend/app/integrations/gitea.py
T
2026-06-09 14:36:51 +03:00

55 lines
1.7 KiB
Python

from typing import Any
import httpx
from app.config import get_settings
class GiteaClient:
def __init__(self) -> None:
settings = get_settings()
self.base_url = settings.gitea_base_url.rstrip("/")
self.public_url = settings.gitea_public_url.rstrip("/")
self.token = settings.gitea_token
def _client(self) -> httpx.Client:
return httpx.Client(
base_url=self.base_url,
timeout=30.0,
headers={
"Authorization": f"token {self.token}",
"Content-Type": "application/json",
},
)
def create_issue(
self,
owner: str,
repo: str,
title: str,
body: str,
) -> dict[str, Any]:
with self._client() as client:
response = client.post(
f"/api/v1/repos/{owner}/{repo}/issues",
json={"title": title, "body": body},
)
if response.is_error:
detail = response.text.strip() or response.reason_phrase
raise ValueError(
f"Gitea issue create failed ({response.status_code}): {detail}"
)
return response.json()
def close_issue(self, owner: str, repo: str, issue_number: int) -> dict[str, Any]:
with self._client() as client:
response = client.patch(
f"/api/v1/repos/{owner}/{repo}/issues/{issue_number}",
json={"state": "closed"},
)
response.raise_for_status()
return response.json()
def issue_url(self, owner: str, repo: str, issue_number: int) -> str:
return f"{self.public_url}/{owner}/{repo}/issues/{issue_number}"