Files
claude-config/bin/do_epic/e2e/settings.py
T

49 lines
2.1 KiB
Python

"""What the e2e command needs to know about its sandbox: three environment variables, so that neither the site nor the credentials' place is in the repo."""
import dataclasses
import pathlib
import re
import typing
from atlassian.client import basic_auth
from failure import Failure
from project_config.settings import SITE
JIRA_URL = "DO_EPIC_E2E_JIRA_URL"
ENV_FILE = "DO_EPIC_E2E_ENV_FILE"
PROJECT = "DO_EPIC_E2E_PROJECT"
PROJECT_KEY = r"[A-Z][A-Z0-9]+"
MEANING = {
JIRA_URL: "the sandbox Jira site's https address without a path, such as https://acme.atlassian.net",
ENV_FILE: "the absolute path of the file holding the sandbox account's ATLASSIAN_EMAIL and ATLASSIAN_API_TOKEN",
PROJECT: "the key of the sandbox project, a team-managed space where the account may create, edit, transition (to Done), comment on and delete issues, such as SBX",
}
@dataclasses.dataclass(frozen=True)
class Sandbox:
jira_url: str
env_file: pathlib.Path
project: str
def read(env: typing.Mapping[str, str]) -> Sandbox | Failure:
"""The sandbox that env names; a Failure naming each setting that is missing or malformed, and why the sandbox's env-file cannot be used."""
values = {name: env.get(name, "").strip() for name in MEANING}
url, project = values[JIRA_URL], values[PROJECT]
env_file = pathlib.Path(values[ENV_FILE]) if values[ENV_FILE] else None
usable = env_file if env_file is not None and env_file.is_absolute() else None
faults = [
*(f"{name} is missing: set it to {meaning}" for name, meaning in MEANING.items() if not values[name]),
*([f"{JIRA_URL} is {url!r}, not an https address without a path"] if url and not re.fullmatch(SITE, url) else []),
*([f"{PROJECT} is {project!r}, not a project key such as SBX"] if project and not re.fullmatch(PROJECT_KEY, project) else []),
*([f"{ENV_FILE} is {values[ENV_FILE]!r}, not an absolute path"] if env_file is not None and usable is None else []),
]
if usable is not None:
login = basic_auth(usable)
if isinstance(login, Failure):
faults.append(login.reason)
if faults:
return Failure("; ".join(faults))
assert env_file is not None
return Sandbox(url, env_file, project)