Rank an issue the loop files in Jira by its score among the issues the loop filed, a security finding first
CI / tests (pull_request) Successful in 2m8s

This commit is contained in:
2026-11-16 02:41:43 +01:00
parent 9d685c52b3
commit 2db4759c2a
15 changed files with 361 additions and 71 deletions
+84 -4
View File
@@ -1,4 +1,5 @@
import base64
import dataclasses
import pathlib
import sys
import tempfile
@@ -7,11 +8,12 @@ import unittest.mock
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from project_config.settings import Epic, Finish
from project_config.settings import DEFAULT_FLOOR, Epic, Finish
from release_plan.todo_table import EXEMPTIONS
from failure import Failure
from plan_store import jira_epic
from plan_store.stores import Plan, Written
from release_plan import ranking
from adf_codec.convert import Refused
from release_plan.release import release_of
@@ -220,6 +222,24 @@ VERSIONS = "https://acme.atlassian.net/rest/api/3/project/ABC/versions"
PERHAPS_FILED = "unless Jira refused the request, the issue may exist: look under ABC-12 before filing the item again"
FIELDS = {"Release": "0.1.0", "Tagline": "Count the runs.", "L": "1", "E": "2", "I": "3", "S": "1", "Moves": "1 by 9"}
PROPERTY = {"L": 1, "E": 2, "I": 3, "S": 1, "Moves": "1 by 9", "Exempt": "", "Filed by": "loop"}
RANK = "https://acme.atlassian.net/rest/agile/1.0/issue/rank"
# Goal 1 weighs 1 and goal 2, a maintain goal, 0.5, so under the formula V / 2**S, FIELDS scores 9 / 2 = 4.5.
README = "# P\n\n## Goals\n\n**Void:** code health, since a test project has no code to read.\n\n1. **Achieve:** Fast.\n2. **Maintain:** Correct.\n\n## Audience\n\nMe.\n"
@dataclasses.dataclass(frozen=True)
class Stated:
"""Stands in for the scoring a project's config states, with the goal weights README gives."""
formula: str = "V / 2**S"
bar: float = 1
def read(self) -> ranking.Scoring | Failure:
return ranking.config_scoring(self.formula, self.bar, README, "", "", DEFAULT_FLOOR)
def filed(key: str, moves: str = "1 by 9", s: int = 1, exempt: str = "") -> dict[str, object]:
"""An open child the loop filed, which scores 9 / 2**s with moves "1 by 9"."""
return issue(key, scores=PROPERTY | {"Moves": moves, "S": s, "Exempt": exempt})
class Filing(unittest.TestCase):
@@ -232,25 +252,27 @@ class Filing(unittest.TestCase):
self.created: object = {"id": "10040", "key": "ABC-40"}
self.linked: object = None
self.versions: object = [{"name": "0.1.0"}, {"name": "v0.2.0"}, "odd", {"name": ["0.1.0"]}, {"name": {"en": "0.1.0"}}]
self.ranked: object = None
self.scoring: Stated | None = Stated()
def http(self, method: str, url: str, authorization: str, body: object) -> object:
"""Stands in for Jira: answers the epic, self.versions for the project's Fix Versions, self.open to the search, self.created to a new issue and self.linked to a new link."""
self.calls.append((method, url, body))
assert authorization == AUTHORIZATION
return {SEARCH: {"issues": self.open}, ISSUES: self.created, LINKS: self.linked, VERSIONS: self.versions}.get(url, {"key": "ABC-12"})
return {SEARCH: {"issues": self.open}, ISSUES: self.created, LINKS: self.linked, VERSIONS: self.versions, RANK: self.ranked}.get(url, {"key": "ABC-12"})
def to_adf(self, markdown: str) -> dict[str, object] | Failure:
return adf(markdown)
def add(self, fields: dict[str, str] = FIELDS, detail: str = ""):
return jira_epic.JiraPlan(EPIC, self.env, self.http, text_of, self.to_adf).add(fields, detail)
return jira_epic.JiraPlan(EPIC, self.env, self.http, text_of, self.to_adf, scoring=self.scoring).add(fields, detail)
def sent(self, url: str) -> list[object]:
return [body for _, sent_to, body in self.calls if sent_to == url]
def test_files_the_item_as_a_task_under_the_epic_marked_as_the_loops(self):
self.assertEqual(self.add(), Written(40))
self.assertEqual(self.calls, [("GET", VERSIONS, None), ("POST", ISSUES, {
self.assertEqual([call for call in self.calls if call[1] != SEARCH and "/issue/ABC-12" not in call[1]], [("GET", VERSIONS, None), ("POST", ISSUES, {
"fields": {"project": {"key": "ABC"}, "parent": {"key": "ABC-12"}, "issuetype": {"name": "Task"}, "summary": "Count the runs.", "fixVersions": [{"name": "0.1.0"}]},
"properties": [{"key": "plan-item", "value": PROPERTY}],
})])
@@ -303,6 +325,7 @@ class Filing(unittest.TestCase):
self.assertEqual(self.add({name: value for name, value in FIELDS.items() if name != "Tagline"}), Failure("the item has no tagline"))
def test_an_item_without_moves_files_with_none(self):
self.scoring = Stated(bar=0)
self.assertEqual(self.add({name: value for name, value in FIELDS.items() if name != "Moves"}), Written(40))
self.assertEqual(self.sent(ISSUES)[0]["properties"][0]["value"]["Moves"], "") # type: ignore[index]
@@ -367,6 +390,63 @@ class Filing(unittest.TestCase):
self.assertIsInstance(self.add(), Failure)
self.assertEqual(self.calls, [])
def test_an_item_under_the_bar_fails_before_any_request(self):
self.assertEqual(self.add(FIELDS | {"Moves": "2 by 1", "S": "3"}), Failure("the item to file scores 0.1, under the bar 1; don't file it, or mark it with a reason the bar doesn't apply"))
self.assertEqual(self.calls, [])
def test_an_exempt_item_under_the_bar_files(self):
self.assertEqual(self.add(FIELDS | {"Moves": "", "Exempt": "owner"}), Written(40))
def test_an_item_that_names_a_goal_the_goals_file_lacks_fails_before_any_request(self):
self.assertEqual(self.add(FIELDS | {"Moves": "3 by 1"}), Failure("the item to file cites goal 3, which the goals file's ## Goals does not number"))
self.assertEqual(self.calls, [])
def test_an_item_without_every_score_fails_before_any_request(self):
self.assertEqual(self.add({name: value for name, value in FIELDS.items() if name != "S"}), Failure("the item to file has no S: score it on L, E, I and S"))
self.assertEqual(self.calls, [])
def test_a_formula_that_does_not_parse_fails_before_any_request(self):
self.scoring = Stated(formula="V +")
self.assertEqual(self.add(), Failure("cannot score the item to file: the formula 'V +' does not parse"))
self.assertEqual(self.calls, [])
def test_a_config_without_a_formula_fails(self):
self.scoring = None
self.assertEqual(self.add(), Failure("the config states no formula and bar to score the item by: set `jira-score-formula` and `jira-score-bar`, as `project-config --help` shows"))
def test_ranks_the_item_just_before_the_first_issue_the_loop_filed_that_scores_lower(self):
# ABC-30 and ABC-33 are a person's, ABC-31 a security finding, and ABC-32 scores as much as the new item.
self.open = [issue("ABC-30"), filed("ABC-31", s=4, exempt="security"), filed("ABC-32"), issue("ABC-33", scores={"L": 1, "E": 1, "I": 1, "S": 4, "Moves": "1 by 1"}), filed("ABC-34", s=2), filed("ABC-35", s=3)]
self.assertEqual(self.add(), Written(40))
self.assertEqual(self.sent(RANK), [{"issues": ["ABC-40"], "rankBeforeIssue": "ABC-34"}])
self.assertEqual([method for method, url, _ in self.calls if url == RANK], ["PUT"])
def test_ranks_a_security_finding_just_before_the_first_issue_the_loop_filed_that_isnt_one(self):
self.open = [issue("ABC-30"), filed("ABC-31", exempt="security"), filed("ABC-32"), filed("ABC-33", s=4)]
self.assertEqual(self.add(FIELDS | {"Exempt": "security", "S": "4"}), Written(40))
self.assertEqual(self.sent(RANK), [{"issues": ["ABC-40"], "rankBeforeIssue": "ABC-32"}])
def test_leaves_the_item_last_where_no_issue_the_loop_filed_scores_lower(self):
self.open = [issue("ABC-30", scores={}), filed("ABC-31"), filed("ABC-32", s=2, exempt="security")]
self.assertEqual(self.add(), Written(40))
self.assertEqual(self.sent(RANK), [])
def test_a_rank_jira_refuses_leaves_the_item_filed_with_a_note(self):
self.open = [filed("ABC-34", s=2)]
refusals = (
(Failure("PUT /rest/agile/1.0/issue/rank answered 403: Forbidden"), "PUT /rest/agile/1.0/issue/rank answered 403: Forbidden"),
({"entries": [{"issueKey": "ABC-40", "status": 400, "errors": ["Rank is not configured"]}]}, "Rank is not configured"),
)
for answer, reason in refusals:
with self.subTest(answer=answer):
self.ranked = answer
self.assertEqual(self.add(), Written(40, (f"filed ABC-40, but cannot rank it before ABC-34: {reason}; rank it in Jira",)))
def test_an_issue_the_loop_filed_that_does_not_score_leaves_the_item_filed_unranked_with_a_note(self):
self.open = [filed("ABC-34", moves="3 by 1", s=2)]
self.assertEqual(self.add(), Written(40, ("filed ABC-40, but cannot rank it: ABC-34 cites goal 3, which the goals file's ## Goals does not number; rank it in Jira",)))
self.assertEqual(self.sent(RANK), [])
TRANSITIONS = "https://acme.atlassian.net/rest/api/3/issue/ABC-34/transitions"