Re-rank Jira's backlog to match an epic's ranking, moving only the issues the loop filed

This commit is contained in:
2026-11-18 18:51:50 +01:00
parent 92bc84ba80
commit 49fe132be2
8 changed files with 186 additions and 13 deletions
+30
View File
@@ -7,6 +7,7 @@ import ast
import dataclasses
import decimal
from do_epic.backend.protocol import EpicStore
from plan.blockers import blockers_first
from plan.todo_table import CODE_HEALTH, EPIC_GOAL, Item, REPO_GOAL, SHAPE
from failure import Failure
@@ -59,6 +60,35 @@ def ranked(items: list[EpicItem], formula: str, epic_goals: str, repos: dict[str
return blockers_first(scored)
def reranked(store: EpicStore, order: list[str]) -> None | Failure:
"""Ranks the epic's child issues named in order, each one the loop filed, so the tracker's backlog lists them in order. Each other child issue, such as one a person filed, keeps its slot; the slots of the named issues take them in order's order. A sub-task holds no slot and follows its parent.
The walk goes from the backlog's end: an issue not just before the one that follows it in the target ranks just before that one, so only the loop's issues move. The target's last issue, where it isn't already last, ranks just after the issue that is. A rank the tracker refuses stops the walk, which fails naming it, since a wrongly ranked team backlog takes hours to restore; the ranks made before it stay."""
read = store.read()
if isinstance(read, Failure):
return read
children = [issue for issue in read if issue.parent == store.epic_key]
if stray := next((key for key in order if key not in {issue.key for issue in children if issue.by_loop}), None):
return Failure(f"{stray} is not one of the child issues of the epic {store.epic_key} that the loop filed, so the backlog was not re-ranked")
if twice := next((key for key in order if order.count(key) > 1), None):
return Failure(f"the order names {twice} twice, so the backlog was not re-ranked")
now = [issue.key for issue in children]
slots = iter(order)
target = [next(slots) if key in order else key for key in now]
for place in reversed(range(len(target))):
key = target[place]
at = now.index(key)
# The slices are empty past the end, so the last issue already in place is skipped too.
if key not in order or now[at + 1:at + 2] == target[place + 1:place + 2]:
continue
other, after = (now[-1], True) if place == len(target) - 1 else (target[place + 1], False)
if refused := store.rank(key, other, after):
return Failure(f"cannot rank {key} just {'after' if after else 'before'} {other}: {refused.reason}; the re-rank stopped there, so rank the loop's issues in {store.epic_key}'s backlog by hand as {', '.join(order)}")
now.remove(key)
now.insert(now.index(other) + after, key)
return None
def chain_scoring(formula: ast.Expression, epic_goals: str, repo: str | None, own: RepoRanking | None) -> ranking.Scoring | Failure:
"""How an item in repo scores: the chain's goals, and what each achieve goal weighs. The bar is 0, since a bar decides what is filed, and the order holds every item that was.