from __future__ import annotations import os from typing import TYPE_CHECKING if TYPE_CHECKING: from yuxi.channel.extensions.confluence.types import ConfluenceAccount ENV_MAP = { "url": "CONFLUENCE_URL", "email": "CONFLUENCE_EMAIL", "api_token": "CONFLUENCE_API_TOKEN", "oauth_client_id": "CONFLUENCE_OAUTH_CLIENT_ID", "oauth_client_secret": "CONFLUENCE_OAUTH_CLIENT_SECRET", "auth_mode": "CONFLUENCE_AUTH_MODE", "deployment_type": "CONFLUENCE_DEPLOYMENT_TYPE", "space_keys": "CONFLUENCE_SPACE_KEYS", "comment_poll_enabled": "CONFLUENCE_COMMENT_POLL_ENABLED", "poll_interval": "CONFLUENCE_POLL_INTERVAL", "comment_policy": "CONFLUENCE_COMMENT_POLICY", "allow_from": "CONFLUENCE_ALLOW_FROM", } def _env_or_config(key: str) -> str | None: env_key = ENV_MAP.get(key, "") if env_key: return os.getenv(env_key) return None def list_confluence_account_ids(config: dict) -> list[str]: accounts = config.get("channels", {}).get("confluence", {}).get("accounts", {}) if accounts: return list(accounts.keys()) return ["default"] def resolve_confluence_account(config: dict, account_id: str = "default") -> ConfluenceAccount: from yuxi.channel.extensions.confluence.types import ConfluenceAccount confluence_cfg = config.get("channels", {}).get("confluence", {}) account_cfg = confluence_cfg.get("accounts", {}).get(account_id, confluence_cfg) def _get(key: str) -> str | None: val = account_cfg.get(key) if val is not None: return str(val) return _env_or_config(key) space_keys_str = _get("space_keys") or "" allow_from_str = _get("allow_from") or "" poll_enabled_str = _get("comment_poll_enabled") or "true" return ConfluenceAccount( account_id=account_id, url=_get("url") or "", email=_get("email") or "", api_token=_get("api_token") or "", oauth_client_id=_get("oauth_client_id") or "", oauth_client_secret=_get("oauth_client_secret") or "", auth_mode=_get("auth_mode") or "api_token", deployment_type=_get("deployment_type") or "cloud", space_keys=[k.strip() for k in space_keys_str.split(",") if k.strip()], comment_poll_enabled=poll_enabled_str.lower() != "false", poll_interval=float(_get("poll_interval") or "30"), comment_policy=_get("comment_policy") or "open", allow_from=[a.strip() for a in allow_from_str.split(",") if a.strip()], name=account_cfg.get("name", "Confluence Bot"), )