Coverage for src / sentry_tool / utils.py: 100.00%
35 statements
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-15 10:53 -0500
« prev ^ index » next coverage.py v7.13.2, created at 2026-02-15 10:53 -0500
1"""Utility functions for config resolution and API interaction."""
3import os
4from typing import Any
6import typer
7from rich.console import Console
9from sentry_tool.client import NotFoundError, api_call
10from sentry_tool.config import (
11 AppConfig,
12 EnvOverrides,
13 SentryProfile,
14 get_profile,
15 load_config,
16 resolve_sentry_config,
17)
18from sentry_tool.exceptions import ConfigurationError
19from sentry_tool.monitoring import get_logger
21_active_profile: str | None = None
22_active_project: str | None = None
25def mask_token(token: str | None) -> str:
26 if token:
27 return f"***...{token[-4:]}"
28 return "(not set)"
31def set_active_profile(profile: str | None) -> None:
32 global _active_profile # noqa: PLW0603
33 _active_profile = profile
36def set_active_project(project: str | None) -> None:
37 global _active_project # noqa: PLW0603
38 _active_project = project
41def get_config() -> dict[str, Any]:
42 """Precedence (highest to lowest):
43 1. CLI --project/-p flag
44 2. CLI --profile/-P flag
45 3. SENTRY_PROFILE environment variable
46 4. default_profile in config file
47 5. Environment variables (SENTRY_URL, SENTRY_ORG, SENTRY_PROJECT, SENTRY_AUTH_TOKEN)
48 """
49 try:
50 app_config: AppConfig = load_config()
51 profile_config: SentryProfile = get_profile(app_config, _active_profile)
53 overrides = EnvOverrides(
54 cli_project=_active_project,
55 url=os.environ.get("SENTRY_URL"),
56 org=os.environ.get("SENTRY_ORG"),
57 project=os.environ.get("SENTRY_PROJECT"),
58 auth_token=os.environ.get("SENTRY_AUTH_TOKEN"),
59 )
60 resolved = resolve_sentry_config(profile_config, overrides)
61 return resolved
62 except ConfigurationError as exc:
63 Console().print(f"[red]Error: {exc}[/red]")
64 raise typer.Exit(1) from None
67def api(endpoint: str, token: str, base_url: str) -> Any:
68 log = get_logger("cli")
69 try:
70 return api_call(endpoint, token=token, base_url=base_url)
71 except NotFoundError:
72 log.error("not found", endpoint=endpoint)
73 raise typer.Exit(1) from None