|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Verify that dashboard-provided Jira config (via Socket API) flows into JiraNotifier. |
| 4 | +
|
| 5 | +This script does NOT call Jira. It only loads Socket Basics config, builds the |
| 6 | +NotificationManager, and inspects JiraNotifier params. |
| 7 | +
|
| 8 | +Expected env vars: |
| 9 | +- SOCKET_SECURITY_API_KEY or SOCKET_SECURITY_API_TOKEN (required) |
| 10 | +- SOCKET_ORG or SOCKET_ORG_SLUG (recommended; auto-discovery is attempted) |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import json |
| 16 | +import logging |
| 17 | +import os |
| 18 | +import sys |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | +from socket_basics.core.config import merge_json_and_env_config |
| 22 | +from socket_basics.core.notification.manager import NotificationManager |
| 23 | + |
| 24 | + |
| 25 | +def _summarize_jira(notifier) -> dict: |
| 26 | + return { |
| 27 | + "server": getattr(notifier, "server", None), |
| 28 | + "project": getattr(notifier, "project", None), |
| 29 | + "email": getattr(notifier, "email", None), |
| 30 | + "api_token_present": bool(getattr(notifier, "api_token", None)), |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | +def _load_dotenv(dotenv_path: Path) -> None: |
| 35 | + if not dotenv_path.exists(): |
| 36 | + return |
| 37 | + try: |
| 38 | + for raw_line in dotenv_path.read_text().splitlines(): |
| 39 | + line = raw_line.strip() |
| 40 | + if not line or line.startswith("#"): |
| 41 | + continue |
| 42 | + if line.startswith("export "): |
| 43 | + line = line[len("export "):].strip() |
| 44 | + if "=" not in line: |
| 45 | + continue |
| 46 | + key, val = line.split("=", 1) |
| 47 | + key = key.strip() |
| 48 | + val = val.strip().strip("'").strip('"') |
| 49 | + if key and key not in os.environ: |
| 50 | + os.environ[key] = val |
| 51 | + except Exception: |
| 52 | + # Best-effort; do not fail if .env parsing is imperfect |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +def main() -> int: |
| 57 | + logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") |
| 58 | + |
| 59 | + repo_root = Path(__file__).resolve().parents[1] |
| 60 | + _load_dotenv(repo_root / ".env") |
| 61 | + |
| 62 | + try: |
| 63 | + config_dict = merge_json_and_env_config() |
| 64 | + except Exception as exc: |
| 65 | + print(f"ERROR: failed to load config: {exc}") |
| 66 | + return 2 |
| 67 | + |
| 68 | + # Load notifications.yaml and build manager |
| 69 | + try: |
| 70 | + import yaml |
| 71 | + |
| 72 | + cfg_path = repo_root / "socket_basics" / "notifications.yaml" |
| 73 | + notif_cfg = None |
| 74 | + if cfg_path.exists(): |
| 75 | + with open(cfg_path, "r") as f: |
| 76 | + notif_cfg = yaml.safe_load(f) |
| 77 | + except Exception as exc: |
| 78 | + print(f"ERROR: failed to load notifications.yaml: {exc}") |
| 79 | + return 2 |
| 80 | + |
| 81 | + nm = NotificationManager(notif_cfg, app_config=config_dict) |
| 82 | + nm.load_from_config() |
| 83 | + |
| 84 | + jira_notifiers = [n for n in nm.notifiers if getattr(n, "name", "").lower() == "jira"] |
| 85 | + if not jira_notifiers: |
| 86 | + print("Jira notifier not enabled. Check that dashboard config includes jira_url or env provides JIRA_URL.") |
| 87 | + return 1 |
| 88 | + |
| 89 | + # Print details for first Jira notifier |
| 90 | + summary = _summarize_jira(jira_notifiers[0]) |
| 91 | + print("Jira notifier config summary:") |
| 92 | + print(json.dumps(summary, indent=2)) |
| 93 | + |
| 94 | + missing = [k for k, v in summary.items() if k in ("server", "project", "email") and not v] |
| 95 | + if missing: |
| 96 | + print(f"WARNING: missing expected fields: {', '.join(missing)}") |
| 97 | + return 1 |
| 98 | + |
| 99 | + print("OK: Jira dashboard config appears to be wired into JiraNotifier.") |
| 100 | + return 0 |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + sys.exit(main()) |
0 commit comments