diff --git a/skills/kvb-git-hotfix/scripts/hotfix.py b/skills/kvb-git-hotfix/scripts/hotfix.py index 0273232..fd2b455 100755 --- a/skills/kvb-git-hotfix/scripts/hotfix.py +++ b/skills/kvb-git-hotfix/scripts/hotfix.py @@ -20,7 +20,12 @@ Args: repeat around another unbounded repeat or around an alternation whose branches can start alike; bounded repeats such as {3} pass. Each --target is a make target or `deploy` the fix turns green, none listed - means every target; --pr is the pull request number. + means every target; --pr is the pull request number. A register + task of that name without hotfix fields, as `rasci.py set` leaves + it (for example for a `queue.py hold`), takes them and keeps its + other fields and roles, with the adding session as R and A and its + status set from --status; one that already is a hotfix, records + another branch or works in another repository is refused. update --status staged|pr|merged [--sha ] [--pr ] [--repo ] [--default ] [--session ]: move a hotfix on. --sha resolves in the recorded git common dir or in @@ -563,12 +568,45 @@ def pr_cell(row: dict) -> str: return f"#{row['pr']}" if row.get("pr") else "-" +def adoptable(task: str, row: dict | None, branch: str, repository: tuple[str, str]) -> dict: + if row is None: + return {} + if "stage" in row: + fail(f"{task} is already in the register; move it on with update") + if row.get("branch") not in (None, "", branch): + fail(f"{task} works on {row['branch']}, --branch says {branch}") + there = identity(row.get("workdir") or "") + if there is not None and not same(there, repository): + fail(f"{task} works in {row['workdir']}, another repository than --repo") + return row + + +def authored(roles: dict, author: str) -> dict: + """Args: + roles: the adopted task's roles. + author: the adding session. + + Returns: + The roles with the author as R and A besides its own letters, and A + taken from every other party. + """ + others = {party: "".join(letter for letter in letters if letter != "A") for party, letters in roles.items() if party != author} + mine = "".join(letter for letter in "RASCI" if letter in roles.get(author, "") + "RA") + return {**{party: letters for party, letters in others.items() if letters}, author: mine} + + +def set_status(rasci: ModuleType, row: dict, status: str) -> None: + final = "stopped" if status == "done" and rasci.open_results(row) else status + if row.get("status") != final: + row.pop("end", None) + rasci.apply_status(row, final) + + def cmd_add(rasci: ModuleType, args: argparse.Namespace) -> int: task = task_of(rasci, args.slug) - if task in rasci.load(): - fail(f"{task} is already in the register; move it on with update") repo = clone(args.repo) common, origin = identity(repo) + adoptable(task, rasci.load().get(task), args.branch, (common, origin)) entry = { "common_dir": common, "origin": origin, @@ -583,11 +621,11 @@ def cmd_add(rasci: ModuleType, args: argparse.Namespace) -> int: entry["sha"] = landed(repo, args.sha, args.default) if args.status == "merged" else commit(repo, args.sha) council = module("council", rasci.COUNCIL) sid, _ = council.caller(args.session) - entry["roles"] = {council.live_sessions().get(sid) or sid: "RA"} - rasci.apply_status(entry, REGISTER_STATUS[args.status]) + author = council.live_sessions().get(sid) or sid with rasci.database() as tasks: - if task in tasks: - fail(f"{task} is already in the register; move it on with update") + row = adoptable(task, tasks.get(task), entry["branch"], (common, origin)) + entry = {**row, **entry, "roles": authored(row.get("roles") or {}, author)} + set_status(rasci, entry, REGISTER_STATUS[args.status]) tasks[task] = entry print(f"added {task}") if args.status == "merged": @@ -656,7 +694,7 @@ def cmd_update(rasci: ModuleType, args: argparse.Namespace) -> int: fail(f"{task}: no hotfix in the register; record it with add") entry.update(fields) entry["stage"] = args.status - rasci.apply_status(entry, REGISTER_STATUS[args.status]) + set_status(rasci, entry, REGISTER_STATUS[args.status]) print(f"updated {task}") if args.status == "merged": announce(task, entry, args.session) diff --git a/tests/test_hotfix.py b/tests/test_hotfix.py index 5d95ac8..5a83b6d 100644 --- a/tests/test_hotfix.py +++ b/tests/test_hotfix.py @@ -289,6 +289,57 @@ class TestHotfix(unittest.TestCase): self.assertIn("| hotfix/lint | running | - | One |", shown) self.assertNotIn("hotfix/db", shown) + def test_add_adopts_a_register_task_without_hotfix_fields_and_keeps_its_own(self): + self.rasci("set", "hotfix/queued", "--title", "held for a gate", "--branch", "fix/q", "--workdir", str(self.work), "--status", "running") + self.rasci("role", "hotfix/queued", "Two", "RA") + before = self.rows()["hotfix/queued"] + self.assertEqual(self.out("add", "queued", *self.flags({"--branch": "fix/q", "--status": "pr", "--pr": "1950"})), + ["added hotfix/queued"]) + row = self.rows()["hotfix/queued"] + self.assertEqual((row["title"], row["workdir"], row["start"]), ("held for a gate", str(self.work), before["start"])) + self.assertEqual((row["roles"]["One"], row["roles"]["Two"]), ("RA", "R")) + self.assertEqual([party for party, letters in row["roles"].items() if "A" in letters], ["One"]) + self.assertEqual((row["branch"], row["stage"], row["pr"], row["sha"], row["status"]), + ("fix/q", "pr", 1950, self.lint, "running")) + self.assertIn("hotfix/queued", "\n".join(self.out("pending", "--repo", str(self.seed)))) + self.assertEqual(self.out("update", "queued", "--status", "staged"), ["updated hotfix/queued"]) + self.rasci("set", "hotfix/elsewhere", "--branch", "fix/other") + foreign = self.base / "foreign" + self.git(self.base, "init", "-q", str(foreign)) + self.rasci("set", "hotfix/foreign", "--workdir", str(foreign)) + register = self.root / "rasci.json" + kept = register.read_text(encoding="utf-8") + for slug, branch, message in (("queued", "fix/q", "already in the register; move it on with update"), + ("elsewhere", "fix/x", "works on fix/other, --branch says fix/x"), + ("foreign", "fix/f", f"works in {foreign}, another repository than --repo")): + with self.subTest(slug=slug): + result = self.hotfix("add", slug, *self.flags({"--branch": branch})) + self.assertEqual(result.returncode, 2) + self.assertIn(message, result.stderr) + self.assertEqual(register.read_text(encoding="utf-8"), kept) + + def test_an_adopted_task_takes_its_status_from_the_hotfix_and_keeps_open_key_results_open(self): + self.rasci("set", "hotfix/ended", "--status", "done") + self.assertIn("end", self.rows()["hotfix/ended"]) + self.out("add", "ended", *self.flags({"--branch": "fix/e", "--status": "pr"})) + row = self.rows()["hotfix/ended"] + self.assertEqual((row["status"], "end" in row), ("running", False)) + self.rasci("set", "hotfix/landed", "--status", "running") + self.rasci("kr", "hotfix/landed", "add", "the deploy is green") + self.out("add", "landed", *self.flags({"--branch": "fix/db", "--sha": self.fix, "--status": "merged"})) + row = self.rows()["hotfix/landed"] + self.assertEqual((row["stage"], row["status"], "end" in row), ("merged", "stopped", True)) + old = "2000-01-01T00:00:00+00:00" + self.rasci("set", "hotfix/landed", "--end", old) + self.rasci("kr", "hotfix/landed", "met", "1") + self.assertEqual(self.out("update", "landed", "--status", "merged", "--sha", self.fix), ["updated hotfix/landed"]) + row = self.rows()["hotfix/landed"] + self.assertEqual(row["status"], "done") + self.assertNotEqual(row["end"], old) + self.rasci("set", "hotfix/closed", "--status", "done", "--end", old) + self.out("add", "closed", *self.flags({"--branch": "fix/db", "--sha": self.fix, "--status": "merged"})) + self.assertEqual(self.rows()["hotfix/closed"]["end"], old) + def test_pending_reports_missing_and_open_hotfixes_of_the_repository(self): expected = [ f"missing (merge main)\thotfix/db\tmerged\tfix/db\t{self.fix}\t#12",