实现了 GitLab 全功能集成,包含 Webhook 处理、API 客户端、消息收发、会话管理、安全校验、限流器等完整模块,支持 Issues、Merge Requests、流水线等事件监听与交互。
90 lines
3.2 KiB
Python
90 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class GitLabStatus:
|
|
@staticmethod
|
|
async def probe(account: dict) -> dict:
|
|
base_url = account.get("base_url", "")
|
|
private_token = account.get("private_token", "")
|
|
project_id = account.get("project_id", 0)
|
|
ssl_verify = account.get("ssl_verify", True)
|
|
|
|
if not base_url or not private_token:
|
|
return {"healthy": False, "reason": "missing credentials"}
|
|
|
|
from yuxi.channel.extensions.gitlab.api import AsyncGitLabClient
|
|
|
|
try:
|
|
client = AsyncGitLabClient(
|
|
base_url=base_url,
|
|
private_token=private_token,
|
|
ssl_verify=ssl_verify,
|
|
)
|
|
version = await client.probe()
|
|
|
|
if version:
|
|
return {
|
|
"healthy": True,
|
|
"version": version.get("version", ""),
|
|
"revision": version.get("revision", "")[:8] if version.get("revision") else "",
|
|
"base_url": base_url,
|
|
"project_id": project_id,
|
|
}
|
|
return {"healthy": False, "reason": "api probe returned empty"}
|
|
except Exception as e:
|
|
logger.exception("gitlab probe error")
|
|
return {"healthy": False, "reason": str(e)}
|
|
|
|
@staticmethod
|
|
async def check_webhook(account: dict) -> dict:
|
|
base_url = account.get("base_url", "")
|
|
private_token = account.get("private_token", "")
|
|
project_id = account.get("project_id", 0)
|
|
ssl_verify = account.get("ssl_verify", True)
|
|
webhook_path = account.get("webhook_path", "/api/webhook/gitlab")
|
|
|
|
from yuxi.channel.extensions.gitlab.api import AsyncGitLabClient
|
|
|
|
try:
|
|
client = AsyncGitLabClient(
|
|
base_url=base_url,
|
|
private_token=private_token,
|
|
ssl_verify=ssl_verify,
|
|
)
|
|
hooks = await client.list_hooks(project_id)
|
|
matching_hooks = [h for h in hooks if webhook_path in str(getattr(h, "url", ""))]
|
|
return {
|
|
"webhook_configured": len(matching_hooks) > 0,
|
|
"webhook_count": len(matching_hooks),
|
|
}
|
|
except Exception as e:
|
|
return {"webhook_configured": False, "error": str(e)}
|
|
|
|
@staticmethod
|
|
def build_summary(account: dict, probe_result: dict | None = None) -> dict:
|
|
summary: dict = {
|
|
"account_id": account.get("account_id", ""),
|
|
"configured": bool(
|
|
account.get("base_url")
|
|
and account.get("private_token")
|
|
and account.get("webhook_token")
|
|
and account.get("project_id")
|
|
),
|
|
"dm_policy": account.get("dm_policy", "open"),
|
|
"base_url": account.get("base_url", ""),
|
|
"project_id": account.get("project_id", 0),
|
|
"ssl_verify": account.get("ssl_verify", True),
|
|
}
|
|
if probe_result:
|
|
summary.update(
|
|
{
|
|
"healthy": probe_result.get("healthy", False),
|
|
"version": probe_result.get("version", ""),
|
|
}
|
|
)
|
|
return summary
|