Start an epic's issues in order of score, scoring each that holds none

This commit is contained in:
Lilleman
2026-11-20 14:30:30 +01:00
parent abaf54bc46
commit 5f2a4b6385
19 changed files with 615 additions and 83 deletions
+47 -3
View File
@@ -5,10 +5,11 @@ import typing
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
from plan_store.jira_epic import BLOCKS, FILED_BY, PROPERTY, scores
from plan_store.jira_epic import BLOCKS, FILED_BY, PROPERTY, scores as held_scores
from project_config.settings import DEFAULT_ISSUE_TYPE, Epic, Finish, JIRA_FINDING_TYPE, JIRA_FINISH_STATUS, JIRA_FINISH_TRANSITION, JIRA_REVIEW_TYPE
from atlassian.jira import DONE, entry_of, finishing_transition, ISSUE_KEY, issue_type_refusal, issue_types_of, MAX_PAGES, move_refusal, PERHAPS_FILED, PERHAPS_MOVED, project_of, rank_refusal, searched, SHAPE, status_category, unshaped
from do_epic.backend.protocol import EpicIssue, no_blocker, no_child, no_parent, no_words, unfit, unfit_label
from do_epic.backend.protocol import EpicIssue, no_blocker, no_child, no_parent, no_scores, no_words, unfit, unfit_label
from plan.todo_table import MOVES
from failure import Failure
from do_epic.repo_pick import REVIEW_LABEL
@@ -161,6 +162,38 @@ class JiraEpic:
refused_move = move_refusal(self.epic.site, self.epic_key, transition, self.finish, authorization, self.http)
return Failure(f"posted the summary on {self.epic_key}, but {refused_move}; close the epic in Jira rather than posting the summary again") if refused_move else None
def set_scores(self, key: str, scores: dict[str, int], moves: str, comment: str) -> None | Failure:
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
if refused := no_words(comment):
return refused
if not ISSUE_KEY.fullmatch(key):
return no_child(self.epic_key, key, "scored")
answered = self.http("GET", f"{self.epic.site}/rest/api/3/issue/{key}?fields=parent&properties={PROPERTY}", authorization, None)
if isinstance(answered, Failure):
return Failure(f"cannot read {key} to score it: {answered.reason}")
found = held_of(key, answered)
if isinstance(found, Failure):
return found
above, held = found
# A sub-task is no child of the epic but its parent is, so a sub-task's parent is read too.
grand = self.parent_of(above, authorization, "to score its sub-task") if above not in (None, self.epic_key) else None
if isinstance(grand, Failure):
return grand
if above is None or (above != self.epic_key and grand != self.epic_key):
return no_child(self.epic_key, key, "scored")
read = held_scores(key, held)
fields = read if isinstance(read, Failure) else no_scores(key, scores, moves, read.scores)
body = fields if isinstance(fields, Failure) else self.to_adf(comment)
if isinstance(body, Failure):
return Failure(f"cannot score {key}: {body.reason}")
put = self.http("PUT", f"{self.epic.site}/rest/api/3/issue/{key}", authorization, {"properties": [{"key": PROPERTY, "value": held | scores | {MOVES: moves}}]})
if isinstance(put, Failure):
return Failure(f"cannot score {key}: {put.reason}")
posted = self.http("POST", f"{self.epic.site}/rest/api/3/issue/{key}/comment", authorization, {"body": body})
return Failure(f"{key} is scored, but the comment that tells its team could not be posted: {posted.reason}") if isinstance(posted, Failure) else None
def finish_issue(self, key: str) -> None | Failure:
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
@@ -263,6 +296,17 @@ class JiraEpic:
return Failure(f"{project} has no sub-task issue type; turn sub-tasks on in Jira") if found is None else {"id": found}
def held_of(key: str, answered: object) -> tuple[str | None, dict[str, typing.Any]] | Failure:
"""The key of the parent, None where it has none, and the value of PROPERTY, empty where it holds none, in Jira's answer for the issue named key."""
fields = answered.get("fields") if isinstance(answered, dict) else None
properties = answered.get("properties") if isinstance(answered, dict) else None
above = fields.get("parent") if isinstance(fields, dict) else None
held = properties.get(PROPERTY, {}) if isinstance(properties, dict) else {}
if not isinstance(held, dict):
return Failure(f"{key}'s issue property {PROPERTY} is not a JSON object")
return (above.get("key") if isinstance(above, dict) and isinstance(above.get("key"), str) else None), held
def found_of(raw: object) -> Found | Failure:
"""The issue in one entry of a search's answer, or which issue is not in the shape Jira documents."""
entry = entry_of(raw, PROPERTY)
@@ -281,7 +325,7 @@ def found_of(raw: object) -> Found | Failure:
return Failure(f"{key} is not in the shape Jira Cloud's REST API version 3 documents: its summary, labels, assignee, status, parent, description or a linked issue's key holds another kind of value")
if not isinstance(held, dict):
return Failure(f"{key}'s issue property {PROPERTY} is not a JSON object")
read = scores(key, held)
read = held_scores(key, held)
if isinstance(read, Failure):
return read
return Found(EpicIssue(key, summary.strip(), "", tuple(sorted(labels)), name, state, done, parent, read.scores, read.moves, read.exempt, blockers, held.get(FILED_BY[0]) == FILED_BY[1]), description)