ForcePilot/backend/package/yuxi/channel/extensions/confluence/status.py

98 lines
3.2 KiB
Python
Raw Normal View History

from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from yuxi.channel.extensions.confluence.gateway import ConfluenceClient
from yuxi.channel.extensions.confluence.types import ConfluenceAccount
@dataclass
class ChannelAccountSnapshot:
account_id: str
configured: bool
status_state: str
running: bool
connected: bool
health_state: str
dm_policy: str
class ConfluenceStatus:
async def probe(
self,
account: ConfluenceAccount,
client: ConfluenceClient,
) -> dict:
if not account.is_configured:
return {"success": False, "error": "Not configured"}
try:
spaces = await client.get_spaces(limit=1)
total = spaces.get("meta", {}).get("total_count", -1) if "meta" in spaces else -1
return {"success": True, "total_spaces": total}
except Exception as e:
return {"success": False, "error": str(e)}
async def check_space_permissions(
self,
account: ConfluenceAccount,
client: ConfluenceClient,
) -> list[dict]:
results = []
for space_key in account.space_keys:
try:
cql = f'space = "{space_key}" AND type = "page"'
search = await client.cql_search(cql, limit=1)
results.append(
{
"space_key": space_key,
"accessible": len(search.get("results", [])) > 0,
"error": None,
}
)
except Exception as e:
results.append(
{
"space_key": space_key,
"accessible": False,
"error": str(e),
}
)
return results
def build_account_snapshot(
self,
account: ConfluenceAccount,
running: bool,
probe_result: dict,
) -> ChannelAccountSnapshot:
return ChannelAccountSnapshot(
account_id=account.account_id,
configured=account.is_configured,
status_state="linked" if running else "stopped",
running=running,
connected=probe_result.get("success", False),
health_state="ok" if probe_result.get("success") else "unknown",
dm_policy=account.comment_policy,
)
def collect_status_issues(
self,
account: ConfluenceAccount,
probe_result: dict,
running: bool,
) -> list[str]:
issues = []
if not account.is_configured:
issues.append("未配置:请设置 CONFLUENCE_URL, CONFLUENCE_EMAIL, CONFLUENCE_API_TOKEN")
if not probe_result.get("success"):
issues.append(f"API 连通性检查失败: {probe_result.get('error', 'Unknown error')}")
if not running:
issues.append("Gateway 未启动")
if account.space_keys and probe_result.get("success"):
space_list = ", ".join(account.space_keys)
issues.append(f"监听空间: {space_list},请确认 Bot 用户对各空间有 View + Add Comments 权限")
return issues