This commit is contained in:
@@ -25,6 +25,9 @@ COMMIT_TITLE = "commit-title"
|
||||
PULL_REQUEST_TITLE = "pull-request-title"
|
||||
BRANCH = "branch"
|
||||
OPTIONAL = (BRANCH, COMMIT_TITLE, ENV_FILE, LANGUAGE, PULL_REQUEST_TITLE, WAIT_FOR_APPROVAL)
|
||||
# The one setting that may name something other than a path: a plan kept as the child issues of a Jira Cloud epic, spelt as `jira:` and the epic's address as the browser shows it. Three readers, seated as the human who writes the config, picked that spelling from four candidates, and one ATLASSIAN_ pair of env-file keys for Jira and Confluence over a pair for each.
|
||||
JIRA = "jira:"
|
||||
EPIC_SPELLING = '"jira:https://<site>/browse/<epic\'s key>" for a plan kept as a Jira Cloud epic\'s child issues'
|
||||
OUTSIDE = "a path that starts with / or ~/ and lies outside the checkout and the config's folder, so that no git remote receives the credentials"
|
||||
|
||||
|
||||
@@ -33,9 +36,21 @@ class Unnamed(Failure):
|
||||
"""No config names a checkout here: none names the one asked for, or git finds no checkout. A reader of titles takes them bare there, as in a third party's clone; a config that is faulty or cannot be read is a plain Failure."""
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Epic:
|
||||
"""A Jira Cloud epic: site is its site's address, such as https://acme.atlassian.net, and key its issue key, such as ABC-12."""
|
||||
site: str
|
||||
key: str
|
||||
|
||||
@property
|
||||
def spelling(self) -> str:
|
||||
"""The epic as a config's `plan` setting spells it."""
|
||||
return f"{JIRA}{self.site}/browse/{self.key}"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class ProjectConfig:
|
||||
"""folder is `~/.claude-projects/<key>`, top is the worktree that asked, and paths holds each of SETTINGS as an absolute path."""
|
||||
"""folder is `~/.claude-projects/<key>`, top is the worktree that asked, and paths holds each of SETTINGS as an absolute path, but for a plan kept in Jira, which epic holds."""
|
||||
key: str
|
||||
folder: pathlib.Path
|
||||
top: pathlib.Path
|
||||
@@ -47,6 +62,23 @@ class ProjectConfig:
|
||||
commit_title: str = templates.TITLE
|
||||
pull_request_title: str = templates.TITLE
|
||||
branch: str = templates.BRANCH
|
||||
# The epic whose child issues are the plan, where the config's `plan` names one; paths then holds no "plan".
|
||||
epic: Epic | None = None
|
||||
|
||||
@property
|
||||
def plan_file(self) -> pathlib.Path | None:
|
||||
"""The plan's file; None where the plan is kept in Jira."""
|
||||
return self.paths.get("plan")
|
||||
|
||||
@property
|
||||
def plan_label(self) -> str:
|
||||
"""What a message calls the plan: its path, or the epic as the config spells it."""
|
||||
return self.epic.spelling if self.epic else str(self.paths["plan"])
|
||||
|
||||
@property
|
||||
def places(self) -> dict[str, str]:
|
||||
"""Where each of SETTINGS is kept, in SETTINGS' order: a path, or the epic as the config spells it."""
|
||||
return {name: self.plan_label if name == "plan" else str(self.paths[name]) for name in SETTINGS if name == "plan" or name in self.paths}
|
||||
|
||||
@property
|
||||
def texts(self) -> dict[str, str]:
|
||||
@@ -59,7 +91,9 @@ class ProjectConfig:
|
||||
|
||||
def tracked(self, setting: str) -> str | None:
|
||||
"""setting's path from top, as git names a file in a commit; None where the config puts it outside the checkout, whose commits hold no version of it."""
|
||||
path = self.paths[setting]
|
||||
path = self.paths.get(setting)
|
||||
if path is None:
|
||||
return None
|
||||
return path.relative_to(self.top).as_posix() if path.is_relative_to(self.top) else None
|
||||
|
||||
|
||||
@@ -82,6 +116,7 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo
|
||||
return Failure(f"{' and '.join(str(file) for file, _ in named)} each name the checkout {main}; only one config may")
|
||||
file, values = named[0]
|
||||
paths = {name: placed(values[name], top, file.parent, home) for name in SETTINGS if name in values}
|
||||
epic = epic_of(values.get("plan"))
|
||||
env_file = anywhere(values.get(ENV_FILE), home)
|
||||
misplaced = ENV_FILE in values and (env_file is None or lies_in(env_file, top, main, file.parent))
|
||||
waits = values.get(WAIT_FOR_APPROVAL, False)
|
||||
@@ -89,14 +124,22 @@ def load(top: pathlib.Path, main: pathlib.Path, home: pathlib.Path) -> ProjectCo
|
||||
faults = [
|
||||
*(f"`{name}` is missing" for name in SETTINGS if name not in values),
|
||||
*(f"`{name}` is not a setting" for name in values if name not in (*SETTINGS, *OPTIONAL)),
|
||||
*(f"`{name}` is {values[name]!r}, not {SPELLING}" for name, path in paths.items() if path is None),
|
||||
*(f"`{name}` is {values[name]!r}, not {SPELLING}" for name, path in paths.items() if path is None and name != "plan"),
|
||||
*([f"`plan` is {values['plan']!r}, not {SPELLING}, or {EPIC_SPELLING}"] if "plan" in values and paths["plan"] is None and epic is None else []),
|
||||
*([f"`plan` names a Jira epic, so `{ENV_FILE}` must name the file that holds its credentials"] if epic and ENV_FILE not in values else []),
|
||||
*([f"`{ENV_FILE}` is {values[ENV_FILE]!r}, not {OUTSIDE}"] if misplaced else []),
|
||||
*([f"`{WAIT_FOR_APPROVAL}` is {waits!r}, not true or false"] if not isinstance(waits, bool) else []),
|
||||
*(f"`{name}` is {values[name]!r}, {fault}" for name, check in checks.items() if name in values and (fault := check(values[name]))),
|
||||
]
|
||||
if faults:
|
||||
return Failure(f"{file}: {'; '.join(faults)}. The settings are {', '.join(SETTINGS)}, and optionally {', '.join(OPTIONAL[:-1])} and {OPTIONAL[-1]}.")
|
||||
return ProjectConfig(file.parent.name, file.parent, top, {name: path for name, path in paths.items() if path}, env_file, waits is True, **{name.replace("-", "_"): str(values[name]) for name in checks if name in values})
|
||||
return ProjectConfig(file.parent.name, file.parent, top, {name: path for name, path in paths.items() if path}, env_file, waits is True, **{name.replace("-", "_"): str(values[name]) for name in checks if name in values}, epic=epic)
|
||||
|
||||
|
||||
def epic_of(value: object) -> Epic | None:
|
||||
"""The epic a `plan` setting's value spells; None where it spells none. Only https, so the credentials are never sent in the clear."""
|
||||
spelt = isinstance(value, str) and re.fullmatch(rf"{JIRA}(https://[A-Za-z0-9.-]+(?::[0-9]+)?)/browse/([A-Z][A-Z0-9_]*-[0-9]+)", value)
|
||||
return Epic(spelt.group(1), spelt.group(2)) if spelt else None
|
||||
|
||||
|
||||
def language_fault(value: object) -> str | None:
|
||||
|
||||
Reference in New Issue
Block a user