From 619b0d000ed801f61a2806e78ea3502ed387d758 Mon Sep 17 00:00:00 2001 From: nhamza Date: Sun, 8 Nov 2026 15:37:19 +0200 Subject: [PATCH 1/6] edge-scrum: chai-bot report skills for the weekly health report (OCPEDGE-3121) Add the chai-bot skills that produce the weekly sprint and release health report of edge-context decision 0018: - chai/sprint-health: capacity mode in sprint week 1, mid-sprint mode after, team level only; capacity = velocity x current sprint availability, or "not PTO-adjusted" when availability is unknown. - chai/release-health: refinement and test-plan spike gaps in the refinement sprint; capacity vs scope, cut line and features at risk (run-checks.py with velocity x availability to branch cut) from development to pre-branch-cut. - chai/weekly-health-report: orchestrator; 3-line top post, details and owner-mentioned action items in the thread, missing data listed, never estimated. - bin/chai_jira_adapter.py: parses query_jira text (headers, issue blocks, fields by id), checks key-ordered paging completeness per project, matches people to the roster by display name, parses get_jira_issue links, and parses edge-context team-roster.md (Rover email, "Not counted" exclusion). - bin/chai_report.py: mode, capacity, section selection, team-level figures and the Slack text. - scrum_context.py: availability for the current sprint and each later sprint up to branch cut, limited to the calendar window read. - chai/scrum-context: drop "Not counted for sprint capacity" members before counting engineers; document the absence window and new outputs. Bump edge-scrum to 1.6.0. Assisted-by: Claude --- .claude-plugin/marketplace.json | 2 +- plugins/edge-scrum/.claude-plugin/plugin.json | 2 +- plugins/edge-scrum/README.md | 9 +- plugins/edge-scrum/bin/assemble-report.py | 48 +- plugins/edge-scrum/bin/chai_jira_adapter.py | 869 ++++++++++++++++++ plugins/edge-scrum/bin/chai_report.py | 596 ++++++++++++ plugins/edge-scrum/bin/run-checks.py | 69 +- plugins/edge-scrum/bin/scrum_context.py | 85 ++ .../bin/tests/test_assemble_report.py | 74 ++ .../bin/tests/test_chai_jira_adapter.py | 654 +++++++++++++ .../edge-scrum/bin/tests/test_chai_report.py | 520 +++++++++++ .../edge-scrum/chai/release-health/SKILL.md | 231 +++++ .../edge-scrum/chai/scrum-context/SKILL.md | 37 +- .../edge-scrum/chai/sprint-health/SKILL.md | 215 +++++ .../chai/weekly-health-report/SKILL.md | 162 ++++ 15 files changed, 3525 insertions(+), 48 deletions(-) create mode 100644 plugins/edge-scrum/bin/chai_jira_adapter.py create mode 100644 plugins/edge-scrum/bin/chai_report.py create mode 100644 plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py create mode 100644 plugins/edge-scrum/bin/tests/test_chai_report.py create mode 100644 plugins/edge-scrum/chai/release-health/SKILL.md create mode 100644 plugins/edge-scrum/chai/sprint-health/SKILL.md create mode 100644 plugins/edge-scrum/chai/weekly-health-report/SKILL.md diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index 6c44804a..c0bed7a4 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -28,7 +28,7 @@ "name": "edge-scrum", "source": "./plugins/edge-scrum", "description": "Agents, skills, and workflows relevant to scrum process management for the OpenShift Edge Team", - "version": "1.5.0" + "version": "1.6.0" }, { "name": "git-commits", diff --git a/plugins/edge-scrum/.claude-plugin/plugin.json b/plugins/edge-scrum/.claude-plugin/plugin.json index 892d5c0c..239d2ef4 100644 --- a/plugins/edge-scrum/.claude-plugin/plugin.json +++ b/plugins/edge-scrum/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "edge-scrum", "description": "Agents, skills, and workflows relevant to scrum process management for the OpenShift Edge Team", - "version": "1.5.0", + "version": "1.6.0", "author": { "name": "jeff-roche" }, "homepage": "https://github.com/openshift-eng/edge-tooling", "license": "Apache-2.0" diff --git a/plugins/edge-scrum/README.md b/plugins/edge-scrum/README.md index ae0217bf..07ba7c83 100644 --- a/plugins/edge-scrum/README.md +++ b/plugins/edge-scrum/README.md @@ -107,7 +107,14 @@ Output is saved to `.reports/release_planning_{version}_{YYYY-MM-DD}.md`. ### chai-bot Skills -Playbooks under [`chai/`](chai/) are for the chai-bot coordinator, not Claude Code. +Playbooks under [`chai/`](chai/) are for the chai-bot coordinator, not Claude Code. Register them with chai-bot from `plugins/edge-scrum/chai/`. They are read-only, ask no questions, use no sub-agents, and run the [`bin/`](bin/) scripts in `sandbox_exec` on data fetched with chai-bot's Jira tools ([`bin/chai_jira_adapter.py`](bin/chai_jira_adapter.py) converts `query_jira` text into the scripts' input). + +| Skill | What it does | +|-------|--------------| +| [`scrum-context`](chai/scrum-context/SKILL.md) | Current sprint and week, release phases, ceremonies, team availability per sprint | +| [`sprint-health`](chai/sprint-health/SKILL.md) | Team-level sprint health: capacity mode in week 1, mid-sprint mode after | +| [`release-health`](chai/release-health/SKILL.md) | One release: refinement and test-plan spike gaps, or capacity vs scope, cut line and features at risk | +| [`weekly-health-report`](chai/weekly-health-report/SKILL.md) | The weekly report of edge-context decision 0018: 3-line post, details and owner-mentioned actions in the thread | #### `scrum-context` diff --git a/plugins/edge-scrum/bin/assemble-report.py b/plugins/edge-scrum/bin/assemble-report.py index 30da83d0..67a7835c 100644 --- a/plugins/edge-scrum/bin/assemble-report.py +++ b/plugins/edge-scrum/bin/assemble-report.py @@ -92,14 +92,24 @@ def build_blocks(checks, recs, params): risk = meta.get("overall_risk", "LOW") gap = meta.get("gap_sp", 0) + hidden = checks.get("hidden_scope", {}) gap_hidden_low = meta.get("gap_with_hidden_low", gap) + gap_hidden_high = meta.get("gap_with_hidden_high", gap) + # run-checks --no-hidden-scope-estimate emits these keys as None: never format + # them, and leave the hidden-scope figures out of the report. + hidden_estimated = all(v is not None for v in ( + hidden.get("estimate_low", 0), hidden.get("estimate_high", 0), gap_hidden_low, gap_hidden_high)) headline = recs.get("headline") or recs.get("executive_summary") or "" glyph = RISK_GLYPH.get(risk, "") if gap < 0: verdict = f"**{glyph} {risk} — ~{abs(gap)} SP must leave the release.**" - elif gap_hidden_low < 0: + elif hidden_estimated and gap_hidden_low < 0: verdict = (f"**{glyph} {risk} — pointed scope fits with {gap} SP to spare, but not once unpointed work " f"is estimated ({gap_hidden_low:+d} SP at the high estimate).**") + elif not hidden_estimated and hidden.get("unpointed_open", 0) > 0: + # Not estimated: the gap covers pointed work only; count the rest, never size it. + verdict = (f"**{glyph} {risk} — pointed scope fits with ~{gap} SP to spare; " + f"{plural(hidden['unpointed_open'], 'unpointed item')} not counted.**") else: verdict = f"**{glyph} {risk} — scope fits with {gap} SP to spare.**" verdict += (f" {plural(params['remaining_sprints'], 'dev sprint')} left · pencils down S{params['pencils_down']}" @@ -131,20 +141,28 @@ def build_blocks(checks, recs, params): verdict += f"\n\n{headline}" blocks["VERDICT"] = [{"t": "p", "text": verdict}] - hidden = checks.get("hidden_scope", {}) - blocks["SUMMARY_STRIP"] = [{ - "t": "table", - "headers": ["Scope (pointed)", "Hidden scope", "Capacity", "Gap"], - "align": ["r", "r", "r", "r"], - "rows": [[ - f"**{meta['total_remaining_sp']} SP**", - f"{hidden.get('unpointed_open', 0)} stories unpointed ({hidden.get('unpointed_assigned', 0)} assigned)" - f" ≈ {hidden.get('estimate_low', 0)}–{hidden.get('estimate_high', 0)} SP" - + (f"; {hidden.get('unpointed_bugs', 0)} unpointed bugs (excl.)" if hidden.get("unpointed_bugs") else ""), - f"**{meta['total_capacity_sp']} SP**", - f"**{gap:+d} SP** ({meta.get('gap_with_hidden_low', gap):+d} to {meta.get('gap_with_hidden_high', gap):+d} incl. hidden)", - ]], - }] + if hidden_estimated: + blocks["SUMMARY_STRIP"] = [{ + "t": "table", + "headers": ["Scope (pointed)", "Hidden scope", "Capacity", "Gap"], + "align": ["r", "r", "r", "r"], + "rows": [[ + f"**{meta['total_remaining_sp']} SP**", + f"{hidden.get('unpointed_open', 0)} stories unpointed ({hidden.get('unpointed_assigned', 0)} assigned)" + f" ≈ {hidden.get('estimate_low', 0)}–{hidden.get('estimate_high', 0)} SP" + + (f"; {hidden.get('unpointed_bugs', 0)} unpointed bugs (excl.)" if hidden.get("unpointed_bugs") else ""), + f"**{meta['total_capacity_sp']} SP**", + f"**{gap:+d} SP** ({gap_hidden_low:+d} to {gap_hidden_high:+d} incl. hidden)", + ]], + }] + else: + blocks["SUMMARY_STRIP"] = [{ + "t": "table", + "headers": ["Scope (pointed)", "Capacity", "Gap"], + "align": ["r", "r", "r"], + "rows": [[f"**{meta['total_remaining_sp']} SP**", f"**{meta['total_capacity_sp']} SP**", + f"**{gap:+d} SP**"]], + }] blocks["STATS_LINE"] = [{"t": "small", "text": ( f"{plural(meta['active_features'], 'active feature')} · {meta['dormant_features']} dormant · " diff --git a/plugins/edge-scrum/bin/chai_jira_adapter.py b/plugins/edge-scrum/bin/chai_jira_adapter.py new file mode 100644 index 00000000..2653f0a4 --- /dev/null +++ b/plugins/edge-scrum/bin/chai_jira_adapter.py @@ -0,0 +1,869 @@ +#!/usr/bin/env python3 +"""Convert chai-bot ``query_jira`` text output into the JSON the edge-scrum transforms read. + +chai-bot's Jira tool returns formatted **text**, not JSON, with no total count. +This module parses that text, checks that a key-ordered paged fetch is complete, +and emits issues in the mcp-atlassian ``jira_search`` shape that +``transform-*.py`` and ``compute-velocity.py`` already consume, so those scripts +run unchanged inside chai-bot's ``sandbox_exec``. + +Text format (checked against real chai-bot output). An optional preamble +("Create metadata for …", "Invalid extra_fields entries: …") comes first, then +one of two headers:: + + Found N issues (showing N): + Showing the first N matching issues; more match: + +then one indented block per issue. A line may hold several ``Label: value`` +segments separated by `` | ``; a missing line or segment means empty, and an +unset custom field prints no line at all:: + + [KEY-123] Summary text + Type: Story | Status: Code Review | Priority: Major | Assignee: | Reporter: | Resolution: Done + Created: 2030-01-02 | Updated: 2030-01-06 | Resolved: 2030-01-06 + Components: | Labels: a, b | Fix Versions: x | Parent: KEY-100 + customfield_10020: (id N, , board B, start , end ) + customfield_10028: + customfield_: + issuelinks: (set, but query_jira cannot render this field's structure) + resolutiondate: + +A truncated page ends with a "... More issues match than the N shown ..." hint. +A query with no results returns ``No issues found for JQL: ``. Output may +be wrapped as ``{"result": ""}``. + +Fields are requested by id (story points ``customfield_10028``, sprint +``customfield_10020``, QA Contact ``customfield_10470``, SME +``customfield_10475``, Flagged ``customfield_10021``). Unknown lines are ignored +and counted in a warning. A field listed under "Invalid extra_fields entries" +was not returned: the fetch is incomplete, except for Flagged, which is then a +skipped signal (a missing Flagged line otherwise just means "not flagged"). + +``query_jira`` cannot render issue links. Links come from ``get_jira_issue``, +whose "Linked Jira Issues" section has lines like +``- is blocked by: KEY (Blocks) | status: | `` +(:func:`parse_issue_links`, :func:`attach_issue_links`). A feature's spikes come +from ``issue in linkedIssues("", "is blocked by") AND issuetype = Spike`` +(:func:`attach_linked_spikes`). + +People: chai-bot returns display names only (no emails). A display name that +matches the roster (``_jira_transforms.roster_match``) gets the roster username +as its ``email``, so roster-keyed scripts work; an unmatched name is kept as +``name`` so its work still shows up (as roster drift) instead of as unassigned. + +Paging recipe (see the SKILLs): ``() ORDER BY key DESC`` with max_results +50, then ``() AND key < ORDER BY key DESC`` until a page has the +"Found N issues" header or fewer than 50 issues. Jira's ``key <`` compares keys +within one project only, so when a multi-project query needs more than one page, +it is paged one project at a time (``AND project = P``). + +CLI:: + + chai_jira_adapter.py fetch --manifest m.json [--roster roster.json] + [--issue-links links.json] [--linked-spikes spikes.json] --output out.json + chai_jira_adapter.py roster --team-roster team-roster.md --output roster.json +""" + +import argparse +import json +import os +import re +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _jira_transforms import ( # noqa: E402 + build_roster_index, + normalize_name, + roster_match, + write_output, +) + +PAGE_SIZE = 50 +KEY_PATTERN = r"[A-Z][A-Z0-9_]*-\d+" +_HEADER_RE = re.compile(r"^\s*\[(" + KEY_PATTERN + r")\]\s*(.*?)\s*$") +_KEY_RE = re.compile(r"^(?P[A-Z][A-Z0-9_]*)-(?P\d+)$") +_LABEL_RE = re.compile(r"^\s*([A-Za-z][A-Za-z0-9_ ]{0,40}?)\s*:\s?(.*)$") +_SPRINT_RE = re.compile( + r"(?P[^()]*?)\s*\(\s*id\s+(?P\d+)\s*,\s*(?P[^,]+?)\s*,\s*" + r"board\s+(?P[^,]+?)\s*,\s*start\s+(?P[^,]+?)\s*,\s*end\s+(?P[^)]+?)\s*\)") +_FOUND_RE = re.compile(r"^\s*Found\s+(\d+)\s+issues?(?:\s*\(showing\s+(\d+)\))?", re.I) +_MORE_RE = re.compile(r"^\s*Showing the first\s+(\d+)\s+matching issues?.*more match", re.I) +_NO_RESULTS_RE = re.compile(r"^\s*No issues found for JQL:(.*)$", re.S) +_HINT_RE = re.compile(r"^\s*(\.\.\.|…)?\s*More issues match\b", re.I) +_INVALID_FIELDS_RE = re.compile(r"^Invalid extra_fields entries:\s*(.*?)(?:\.\s+Use\b.*)?\.?$", re.I) +_UNKNOWN_FIELDS_RE = re.compile(r"^Unknown fields:\s*(.*?)\.?$", re.I) +_METADATA_RE = re.compile(r"^(Create metadata for\b|Issue types available\b|Requested fields:|\[[^\]]+\]$)", re.I) +_LINKED_HEADER_RE = re.compile(r"linked jira issues", re.I) +_LINKED_LINE_RE = re.compile( + r"^\s*[-*]\s*(?P[^:|]+?)\s*:\s*(?P" + KEY_PATTERN + r")\s*" + r"(?:\((?P[^)]*)\))?\s*(?:\|\s*status\s*:\s*(?P[^|]*?)\s*)?(?:\|.*)?$", re.I) + +EMPTY_VALUES = {"", "none", "null", "n/a", "unassigned", "unresolved", "-"} + +# Labels on the pipe-separated lines, mapped to output keys. +PIPE_FIELDS = { + "type": "issue_type", "issue type": "issue_type", "status": "status", + "priority": "priority", "assignee": "assignee", "reporter": "reporter", + "resolution": "resolution", "created": "created", "updated": "updated", + "resolved": "resolved", +} +USER_FIELDS = {"assignee", "customfield_10470", "customfield_10473", "customfield_10475"} +# Custom fields the transforms read. +CUSTOM_FIELDS = { + "customfield_10020", # Sprint + "customfield_10028", # Story Points + "customfield_10470", # QA Contact + "customfield_10473", # Docs approver + "customfield_10475", # SME + "customfield_10795", # T-shirt size + "customfield_10855", # Target Version + "customfield_10014", # Epic Link (legacy) + "customfield_10018", # Parent Link + "customfield_10021", # Flagged (no line when unset; skipped signal when rejected) +} +IGNORED_FIELDS = {"issuelinks"} # query_jira prints "(set, but ... cannot render ...)" +LIST_FIELDS = {"components": "components", "labels": "labels", "fix versions": "fixVersions", + "fixversions": "fixVersions", "fix version": "fixVersions"} + +# Link wording → (type name, inward wording, outward wording). On issue A, +# "is blocked by: B" means B is A's inward issue; "blocks: B" its outward one. +_LINK_PAIRS = [ + ("Blocks", "is blocked by", "blocks"), + ("Cloners", "is cloned by", "clones"), + ("Duplicate", "is duplicated by", "duplicates"), + ("Causality", "is caused by", "causes"), + ("Related", "is related to", "relates to"), +] +LINK_WORDING = {} +for _name, _inward, _outward in _LINK_PAIRS: + LINK_WORDING[_inward] = (_name, _inward, _outward, "inward_issue") + LINK_WORDING[_outward] = (_name, _inward, _outward, "outward_issue") + +SKIPPED_FLAGGED = "flagged (query_jira rejected customfield_10021)" +FLAGGED_FIELD = "customfield_10021" +SKIPPED_DESCRIPTION = "acceptance criteria (no description in chai-bot's Jira output)" + + +# ---------------------------------------------------------------- values + + +def _clean(value): + """Stripped value, or None for an empty placeholder ("None", "Unassigned", …).""" + if value is None: + return None + text = str(value).strip() + return None if text.casefold() in EMPTY_VALUES else text + + +def _split_list(value): + """Comma-separated list value → list of non-empty items.""" + text = _clean(value) + if not text: + return [] + return [item.strip() for item in text.split(",") if _clean(item)] + + +def _number(value): + """Story points as a float, or None when empty or not a number.""" + text = _clean(value) + if text is None: + return None + try: + return float(text) + except ValueError: + return None + + +def parse_sprints(value): + """``customfield_10020`` text → sprint dicts (one per ``name (id …)`` entry).""" + out = [] + for m in _SPRINT_RE.finditer(value or ""): + name = m.group("name").strip().lstrip(",;").strip() + start, end = _clean(m.group("start")), _clean(m.group("end")) + out.append({"id": int(m.group("id")), "name": name, + "state": m.group("state").strip().lower(), + "boardId": _clean(m.group("board")), + "startDate": start, "endDate": end}) + return out + + +def parse_issue_links(text): + """``get_jira_issue`` output → issuelinks from its "Linked Jira Issues" section. + + Lines look like ``- is blocked by: KEY (Blocks) | status: | ``. + Returns ``(links, found)``; ``found`` is False when the section is missing. + """ + links, found, inside = [], False, False + for line in (text or "").splitlines(): + if _LINKED_HEADER_RE.search(line) and not _LINKED_LINE_RE.match(line): + found, inside = True, True + continue + if not inside: + continue + if not line.strip(): + continue + m = _LINKED_LINE_RE.match(line) + if not m: + if line.lstrip().startswith(("-", "*")): + continue # a list line we cannot read; skip it + break # next section + wording = re.sub(r"\s+", " ", m.group("wording").strip().lower()) + type_name = _clean(m.group("type")) + if wording in LINK_WORDING: + name, inward, outward, side = LINK_WORDING[wording] + else: + name, inward, outward, side = type_name or wording, wording, wording, "outward_issue" + other = {"key": m.group("key")} + status = _clean(m.group("status")) + if status: + other["status"] = {"name": status} + links.append({"type": {"name": type_name or name, "inward": inward, "outward": outward}, + side: other}) + return links, found + + +# ---------------------------------------------------------------- page parsing + + +def _split_blocks(text): + """Split page text into ``(preamble_lines, [(key, summary, lines), ...], hint)``. + + ``hint`` is True when the trailing "... More issues match ..." paging hint is + present; that line is not part of any issue block. + """ + preamble, blocks, hint = [], [], False + for line in (text or "").splitlines(): + if _HINT_RE.match(line): + hint = True + continue + m = _HEADER_RE.match(line) + if m: + blocks.append((m.group(1), m.group(2), [])) + elif blocks: + blocks[-1][2].append(line) + else: + preamble.append(line) + return preamble, blocks, hint + + +def _segments(line): + """``Label: value | Label: value`` → label matches; one segment when any part has no label.""" + parts = re.split(r"\s+\|\s+", line.strip()) + matches = [_LABEL_RE.match(p) for p in parts] + if len(parts) > 1 and all(matches): + return matches + m = _LABEL_RE.match(line) + return [m] if m else [None] + + +def _parse_block(key, summary, lines, unknown): + """One issue block → raw field dict (values still text). Unknown labels go to ``unknown``. + + Any line may hold several ``Label: value`` segments separated by `` | `` + (``Components: X | Labels: a, b | Fix Versions: v | Parent: KEY``). + """ + raw = {"key": key, "summary": summary} + for line in lines: + if not line.strip(): + continue + for m in _segments(line): + if m is None: + unknown.append("(unlabelled)") + continue + label, value = m.group(1).strip(), m.group(2) + low = label.lower() + if low in PIPE_FIELDS: + raw[PIPE_FIELDS[low]] = value + elif low in CUSTOM_FIELDS or low in ("resolutiondate", "parent"): + raw[low] = value + elif low in LIST_FIELDS: + raw[LIST_FIELDS[low]] = value + elif low == "description": + raw["description"] = value + elif low in IGNORED_FIELDS: + pass # e.g. "issuelinks: (set, but query_jira cannot render ...)" + else: + unknown.append(label) + return raw + + +def parse_header(lines): + """Page header → ``("found", total, shown)``, ``("more", shown, shown)`` or None.""" + for line in lines: + m = _FOUND_RE.match(line) + if m: + total = int(m.group(1)) + return ("found", total, int(m.group(2)) if m.group(2) else total) + m = _MORE_RE.match(line) + if m: + return ("more", int(m.group(1)), int(m.group(1))) + return None + + +def parse_preamble(lines): + """chai-bot notes before the results → ``(rejected_fields, unknown_fields, other_lines)``. + + ``Invalid extra_fields entries: A, B. Use Jira field IDs ...`` lists fields + query_jira rejected (not returned at all). ``Unknown fields: ...`` comes from + the create-metadata block and is informational. The rest of that block + ("Create metadata for ...", "Issue types available ...", "[Feature]", + "Requested fields: ...") is recognized and ignored. + """ + rejected, unknown_fields, other = [], [], [] + for line in lines: + text = line.strip() + if not text or parse_header([text]): + continue + m = _INVALID_FIELDS_RE.match(text) + if m: + rejected.extend(f.strip() for f in m.group(1).split(",") if f.strip()) + continue + m = _UNKNOWN_FIELDS_RE.match(text) + if m: + unknown_fields.extend(f.strip() for f in m.group(1).split(",") if f.strip()) + continue + if _METADATA_RE.match(text): + continue + other.append(text) + return rejected, unknown_fields, other + + +def unwrap(text): + """chai-bot tool output → text: ``{"result": ""}`` is unwrapped, bare text kept.""" + if isinstance(text, dict): + return text.get("result") if isinstance(text.get("result"), str) else None + if not isinstance(text, str): + return text + stripped = text.strip() + if stripped.startswith("{"): + try: + data = json.loads(stripped) + except ValueError: + return text + if isinstance(data, dict) and isinstance(data.get("result"), str): + return data["result"] + return text + + +def parse_page(text, error=None, page_size=PAGE_SIZE, jql=None): + """Parse one ``query_jira`` page and check it positively. + + Returns ``{"status": "ok"|"error"|"incomplete", "reason", "final", "blocks", + "unknown", "rejected", "unknown_fields"}``. A page is ``ok`` only when: + + * non-final: header ``Showing the first N matching issues; more match`` with + N = ``page_size``, exactly N parsed issues, and the "... More issues match" + hint; + * final: header ``Found N issues (showing N)`` with N = the number of parsed + issues, and no hint. + + * no results: ``No issues found for JQL: `` where ```` equals the + query that was sent (``jql``, ignoring surrounding whitespace): a complete, + empty final page. + + Every issue must also carry its ``Type: … | Status: …`` line (chai-bot always + prints it), so a page cut inside its last issues is caught even when the + issue count still matches. Anything else is ``incomplete``. ``text`` may be + wrapped as ``{"result": ""}`` (see :func:`unwrap`). + """ + if error: + return {"status": "error", "reason": "query_jira call failed", "final": False, + "blocks": [], "unknown": [], "rejected": [], "unknown_fields": []} + text = unwrap(text) + empty = _NO_RESULTS_RE.match(text or "") + if empty: + out = {"status": "ok", "reason": None, "final": True, "blocks": [], "unknown": [], + "rejected": [], "unknown_fields": []} + if jql is None: + out.update(status="incomplete", final=False, + reason="'No issues found' page without the sent JQL to compare") + elif empty.group(1).strip() != str(jql).strip(): + out.update(status="incomplete", final=False, + reason="'No issues found' names a different JQL than the query sent") + return out + preamble, blocks, hint = _split_blocks(text) + header = parse_header(preamble) + rejected, unknown_fields, other = parse_preamble(preamble) + unknown = ["(preamble)"] * len(other) + parsed = [_parse_block(k, s, lines, unknown) for k, s, lines in blocks] + out = {"status": "ok", "reason": None, "final": False, "blocks": parsed, "unknown": unknown, + "rejected": rejected, "unknown_fields": unknown_fields} + + def incomplete(reason): + out.update(status="incomplete", reason=reason, final=False) + return out + + if header is None: + return incomplete("no 'Found N issues (showing N)' or 'Showing the first N matching issues' header") + kind, total, shown = header + count = len(parsed) + if kind == "more": + if shown != page_size: + return incomplete(f"'more match' page shows {shown} issues, expected {page_size} (max_results)") + if count != shown: + return incomplete(f"page lists {count} issues but its header says {shown}") + if not hint: + return incomplete("full page without the 'More issues match' paging hint") + else: + if total != shown: + return incomplete(f"header says {total} issues but shows {shown}") + if count != shown: + return incomplete(f"page lists {count} issues but its header says {shown}") + if hint: + return incomplete("'Found N issues' page followed by a 'More issues match' hint") + out["final"] = True + cut = [raw["key"] for raw in parsed if "issue_type" not in raw or "status" not in raw] + if cut: + return incomplete(f"issue {cut[0]} has no 'Type: … | Status: …' line (page cut off)") + return out + + +# ---------------------------------------------------------------- conversion + + +class PeopleMatcher: + """Display name → user dict, matched to the roster by display name.""" + + def __init__(self, roster=None): + """Index the roster's contacts (or members) by display name and email.""" + people = (roster or {}).get("contacts") or (roster or {}).get("members") or [] + members = [{"username": p.get("email") or p.get("username"), "display_name": p.get("display_name")} + for p in people] + self.index = build_roster_index({"members": members}) + self.matched = set() + self.unmatched = set() + + def user(self, value): + """Display name → user dict with the roster email when matched, else the name; None if empty.""" + name = _clean(value) + if name is None: + return None + member = roster_match(None, name, self.index) + user = {"display_name": name, "displayName": name} + if member and member.get("username"): + user["email"] = member["username"] + self.matched.add(normalize_name(name)) + else: + user["name"] = name + self.unmatched.add(name) + return user + + +_PARENT_TYPE_RE = re.compile(r"\b(Epic|Feature|Initiative|Outcome|Story|Task)\b") +_CHILD_OF_EPIC = {"story", "task", "spike", "bug"} + + +def to_issue(raw, people): + """Raw text fields → an issue in the mcp-atlassian ``jira_search`` shape.""" + issue = {"key": raw["key"], "summary": raw.get("summary") or ""} + for field in ("issue_type", "status", "priority", "resolution"): + value = _clean(raw.get(field)) + issue[field] = {"name": value} if value else None + if issue["status"] is None: + issue["status"] = {"name": ""} + # Dates are left out when missing: the transforms read them with defaults + # (raw.get("updated", ...)), which a stored None would bypass. + dates = {"created": _clean(raw.get("created")), "updated": _clean(raw.get("updated")), + "resolutiondate": _clean(raw.get("resolutiondate")) or _clean(raw.get("resolved"))} + issue.update({k: v for k, v in dates.items() if v}) + for field in USER_FIELDS: + if field in raw or field == "assignee": + issue[field] = people.user(raw.get(field)) + reporter = _clean(raw.get("reporter")) + if reporter: + issue["reporter"] = {"display_name": reporter, "displayName": reporter} + issue["components"] = [{"name": c} for c in _split_list(raw.get("components"))] + issue["labels"] = _split_list(raw.get("labels")) + if "fixVersions" in raw: + issue["fixVersions"] = [{"name": v} for v in _split_list(raw.get("fixVersions"))] + parent_text = _clean(raw.get("parent")) + if parent_text: + key = re.search(KEY_PATTERN, parent_text) + if key: + parent = {"key": key.group(0)} + ptype = _PARENT_TYPE_RE.search(parent_text[key.end():]) + child = (issue["issue_type"] or {}).get("name", "").lower() + if ptype: + parent["issue_type"] = {"name": ptype.group(1)} + elif child in _CHILD_OF_EPIC: + # A standard issue's native parent is its epic (Jira Cloud hierarchy). + parent["issue_type"] = {"name": "Epic"} + issue["parent"] = parent + if "customfield_10020" in raw: + issue["customfield_10020"] = parse_sprints(raw["customfield_10020"]) + if "customfield_10028" in raw: + issue["customfield_10028"] = _number(raw["customfield_10028"]) + for field in ("customfield_10795",): + if field in raw and _clean(raw[field]): + issue[field] = {"value": _clean(raw[field])} + if "customfield_10855" in raw: + issue["customfield_10855"] = [{"name": v} for v in _split_list(raw["customfield_10855"])] + for field in ("customfield_10014", "customfield_10018"): + if field in raw and _clean(raw[field]): + key = re.search(KEY_PATTERN, raw[field]) + if key: + issue[field] = key.group(0) + if "customfield_10021" in raw: + flag = _clean(raw["customfield_10021"]) + issue["customfield_10021"] = [{"value": flag}] if flag else [] + if "description" in raw: + issue["description"] = raw["description"] or "" + issue["issuelinks"] = [] + return issue + + +def attach_issue_links(adapted, issue_texts): + """Set ``issuelinks`` from ``get_jira_issue`` output on the matching issues. + + ``issue_texts`` maps key → that issue's ``get_jira_issue`` text (None when the + call failed). Returns the problems found; a failed call or a missing "Linked + Jira Issues" section means that issue's blocked signal is incomplete. + """ + by_key = {i["key"]: i for i in adapted.get("issues", [])} + problems = [] + for key, text in (issue_texts or {}).items(): + if key not in by_key: + continue + if text is None: + problems.append(f"{key}: get_jira_issue failed") + continue + links, found = parse_issue_links(text) + if not found: + problems.append(f"{key}: no 'Linked Jira Issues' section") + continue + by_key[key]["issuelinks"] = links + adapted.setdefault("fetch", {}).setdefault("link_problems", []).extend(problems) + return problems + + +def attach_linked_spikes(features, spikes_by_feature): + """Add an "is blocked by" link from each feature to each spike found for it. + + ``spikes_by_feature`` maps feature key → adapted spike issues returned by + ``issue in linkedIssues("", "is blocked by") AND issuetype = Spike``. + The links carry the spike's type, status, summary, assignee and resolution, + which is what ``transform-features.py`` reads for spike candidates. + """ + by_key = {i["key"]: i for i in features.get("issues", [])} + for fkey, spikes in (spikes_by_feature or {}).items(): + feature = by_key.get(fkey) + if feature is None: + continue + have = {(l.get("inward_issue") or {}).get("key") for l in feature["issuelinks"]} + for spike in spikes or []: + if spike["key"] in have: + continue + other = {"key": spike["key"], "issue_type": spike.get("issue_type") or {"name": "Spike"}} + for field in ("status", "summary", "assignee", "resolution"): + if spike.get(field): + other[field] = spike[field] + feature["issuelinks"].append({ + "type": {"name": "Blocks", "inward": "is blocked by", "outward": "blocks"}, + "inward_issue": other}) + have.add(spike["key"]) + + +def attach_linked_spike_fetches(features, fetches_by_feature): + """:func:`attach_linked_spikes` from whole ``adapt_fetch`` results, keeping completeness. + + ``fetches_by_feature`` maps feature key → the ``adapt_fetch`` output of that + feature's ``linkedIssues(...)`` query. An incomplete one is recorded in + ``features["fetch"]["linked_spike_problems"]`` (feature and reason) and makes + ``features["fetch"]["complete"]`` False: a missed spike would report a false + missing-spike gap. Returns the problems. + """ + problems = [] + for fkey, fetch in (fetches_by_feature or {}).items(): + status = (fetch or {}).get("fetch") or {} + if not status.get("complete", False): + why = "; ".join(status.get("problems") or []) or "fetch status missing" + problems.append(f"{fkey}: linked-spike fetch incomplete ({why})") + out = features.setdefault("fetch", {}) + out.setdefault("linked_spike_problems", []).extend(problems) + if problems: + out["complete"] = False + attach_linked_spikes(features, {k: (v or {}).get("issues", []) for k, v in (fetches_by_feature or {}).items()}) + return problems + + +# ---------------------------------------------------------------- paging + + +def _key_sort(key): + """Sort key for an issue key: (project, number), so TEST-9 sorts before TEST-10.""" + m = _KEY_RE.match(key or "") + return (m.group("project"), int(m.group("num"))) if m else (key or "", 0) + + +def strip_order_by(jql): + """JQL without a trailing ORDER BY clause.""" + return re.sub(r"\s+ORDER\s+BY\s+.*$", "", jql.strip(), flags=re.I | re.S).strip() + + +def page_jql(jql, project=None, last_key=None): + """JQL for the next key-ordered page of ``jql`` (one project at a time).""" + out = f"({strip_order_by(jql)})" + if project: + out += f" AND project = {project}" + if last_key: + out += f" AND key < {last_key}" + return out + " ORDER BY key DESC" + + +def _load_page_text(page): + """Text of one manifest page: inline ``text``, else the ``file`` contents, else None.""" + if page.get("text") is not None: + return page["text"] + if page.get("file"): + with open(page["file"], encoding="utf-8") as fh: + return fh.read() + return None + + +def check_part(pages, project=None, order="key"): + """Completeness problems of one fetch part, given parsed pages in fetch order. + + Key order: every page ok; every page but the last is non-final ("more match" + header, or a full page with no header); the last page is final ("Found N + issues" header, or fewer than 50 issues). Across pages, keys must stay in one + project and strictly descend. Rank order: a single final page. + """ + if not pages: + return ["no pages fetched"] + problems = [f"page {i}: {p['reason']}" for i, p in enumerate(pages, 1) if p["status"] != "ok"] + if problems: + return problems + if order == "rank": + if len(pages) != 1 or not pages[0]["final"]: + problems.append("rank-ordered fetch did not fit on one page; rank order not available") + return problems + for i, page in enumerate(pages[:-1], 1): + if page["final"]: + problems.append(f"page {i} was the last page, but more pages were fetched") + if not pages[-1]["final"]: + problems.append("last page says more issues match; fetch stopped early") + keys = [b["key"] for p in pages for b in p["blocks"]] + if len(pages) > 1: + projects = {_key_sort(k)[0] for k in keys} + if project and projects - {project}: + problems.append(f"issues from other projects in a {project}-only fetch") + elif len(projects) > 1: + problems.append("key paging spans several projects (Jira's 'key <' pages within one " + "project only); page each project separately") + nums = [_key_sort(k) for k in keys] + if any(a <= b for a, b in zip(nums, nums[1:])): + problems.append("keys are not strictly descending across pages " + "(duplicate or out-of-order page)") + return problems + + +def adapt_fetch(manifest, roster=None): + """Parse and check one fetch described by ``manifest``; return issues + fetch status. + + ``manifest``:: + + {"name": "sprint_issues", "order": "key" | "rank", "page_size": 50, + "signals": ["flagged", "description"], # optional: signals this fetch should carry + "parts": [{"project": "OCPEDGE", # optional; one part per paged project + "pages": [{"text": "...", "jql": ""} | {"file": "p1.txt", "jql": ...} + | {"error": "..."}]}]} + + ``text`` may be bare or wrapped as ``{"result": ""}``. ``jql`` (the exact + query sent for that page) is needed to accept a ``No issues found for JQL`` + page as complete. + + ``"pages"`` at the top level is shorthand for a single part. The fetch is + ``complete`` only if every page succeeded and every part reached its last + page; callers must not compute figures from an incomplete fetch. + """ + page_size = manifest.get("page_size") or PAGE_SIZE + order = manifest.get("order") or "key" + parts = manifest.get("parts") + if parts is None: + parts = [{"project": manifest.get("project"), "pages": manifest.get("pages") or []}] + people = PeopleMatcher(roster) + problems, unknown, issues, seen = [], [], [], set() + rejected, unknown_fields = set(), set() + page_count = 0 + for part in parts: + parsed = [] + for page in part.get("pages") or []: + try: + text = _load_page_text(page) + except OSError: + parsed.append({"status": "error", "reason": "page file not readable", "final": False, + "blocks": [], "unknown": []}) + continue + parsed.append(parse_page(text, error=page.get("error") or text is None, + page_size=page_size, jql=page.get("jql"))) + rejected.update(parsed[-1].get("rejected", [])) + unknown_fields.update(parsed[-1].get("unknown_fields", [])) + page_count += len(parsed) + label = part.get("project") or "fetch" + problems.extend(f"{label}: {p}" for p in check_part(parsed, part.get("project"), order)) + for page in parsed: + unknown.extend(page["unknown"]) + for raw in page["blocks"]: + if raw["key"] in seen: + continue + seen.add(raw["key"]) + issues.append(to_issue(raw, people)) + + signals = set(manifest.get("signals") or []) + skipped = [] + # Unset custom fields print no line, so a missing Flagged line means "not + # flagged". Flagged is unavailable only when query_jira rejects the field; + # any other rejected field makes the figures wrong, so the fetch is incomplete. + if FLAGGED_FIELD in rejected: + skipped.append(SKIPPED_FLAGGED) + other_rejected = sorted(rejected - {FLAGGED_FIELD}) + if other_rejected: + problems.append(f"query_jira rejected field(s): {', '.join(other_rejected)}") + if "description" in signals and not any("description" in i for i in issues): + skipped.append(SKIPPED_DESCRIPTION) + warnings = [] + if unknown_fields: + warnings.append(f"create metadata lists unknown field(s): {', '.join(sorted(unknown_fields))}") + if unknown: + labels = sorted({u for u in unknown if u not in ("(preamble)", "(unlabelled)")}) + warnings.append(f"ignored {len(unknown)} unrecognized line(s)" + + (f"; labels: {', '.join(labels)}" if labels else "")) + return { + "issues": issues, + "fetch": {"name": manifest.get("name"), "complete": not problems, "pages": page_count, + "fetched": len(issues), "problems": problems}, + "skipped_signals": skipped, + "people": {"matched": len(people.matched), "unmatched": sorted(people.unmatched)}, + "warnings": warnings, + } + + +# ---------------------------------------------------------------- roster + + +ROSTER_ROLES = ("software engineer", "software quality engineer") +_ROVER_RE = re.compile(r"rover\.redhat\.com/people/profile/([A-Za-z0-9._-]+)") +_MD_LINK_RE = re.compile(r"^\[([^\]]+)\]\(([^)]*)\)$") + + +def parse_team_roster(markdown, roles=ROSTER_ROLES): + """Parse edge-context ``people/team-roster.md``. + + Returns ``{"members", "contacts", "not_counted", "engineer_count", "warnings"}``: + + * ``contacts`` — every table row: display name, Rover email (profile id + + ``@redhat.com``, or None), role. Used to map action-item owners to Slack. + * ``members`` — the counted engineers in ``.roster.json`` shape: roles that + contain "Software Engineer" or "Software Quality Engineer" (any level), + minus anyone listed under "Not counted for sprint capacity". + """ + contacts, not_counted, warnings = [], [], [] + section = None + for line in (markdown or "").splitlines(): + heading = re.match(r"^\s*#+\s*(.*?)\s*$", line) + if heading: + section = heading.group(1).strip().casefold() + continue + if section and section.startswith("not counted for sprint capacity"): + item = re.match(r"^\s*[-*]\s+(.*?)\s*$", line) + if item: + not_counted.append(_MD_LINK_RE.sub(r"\1", item.group(1)).strip()) + continue + if not line.strip().startswith("|"): + continue + cells = [c.strip() for c in line.strip().strip("|").split("|")] + if len(cells) < 3 or set("".join(cells)) <= set("-: ") or cells[0].casefold() == "name": + continue + name_cell = cells[0] + link = _MD_LINK_RE.match(name_cell) + name = link.group(1).strip() if link else name_cell + rover = _ROVER_RE.search(link.group(2)) if link else None + contacts.append({ + "display_name": name, + "email": f"{rover.group(1)}@redhat.com" if rover else None, + "github": cells[1] if len(cells) > 1 else None, + "role": cells[2] if len(cells) > 2 else "", + "location": cells[3] if len(cells) > 3 else None, + }) + excluded = {normalize_name(n) for n in not_counted} + known = {normalize_name(c["display_name"]) for c in contacts} + for name in not_counted: + if normalize_name(name) not in known: + warnings.append("a 'Not counted' entry matches no roster row") + members = [] + for c in contacts: + role = (c["role"] or "").casefold() + if not any(r in role for r in roles) or normalize_name(c["display_name"]) in excluded: + continue + members.append({"username": c["email"] or c["display_name"], "display_name": c["display_name"], + "country": c["location"], "sp_target": 8}) + return {"members": members, "contacts": contacts, "not_counted": not_counted, + "engineer_count": len(members), "warnings": warnings} + + +_SCRUM_MASTER_LINE_RE = re.compile( + r"^\s*[-*]?\s*\**\s*(?:current scrum master|current|scrum master|assigned to|held by)\s*\**\s*:\s*\**\s*(.+?)\s*\**\s*$", + re.I) + + +def find_scrum_master(roster_markdown=None, role_markdown=None): + """The current Scrum Master's display name from edge-context, or None. + + Looks for a ``team-roster.md`` row whose role contains "Scrum Master", then for + a ``Current Scrum Master: `` (or ``Current:``, ``Assigned to:``, + ``Held by:``) line in ``roles/scrum-master.md``. None when neither names one. + """ + for c in parse_team_roster(roster_markdown or "")["contacts"]: + if "scrum master" in (c["role"] or "").casefold(): + return c["display_name"] + for line in (role_markdown or "").splitlines(): + m = _SCRUM_MASTER_LINE_RE.match(line) + if m: + name = _MD_LINK_RE.sub(r"\1", m.group(1).strip()).strip() + if name and _clean(name): + return name + return None + + +# ---------------------------------------------------------------- CLI + + +def _load(path): + """Read a JSON file.""" + with open(path, encoding="utf-8") as fh: + return json.load(fh) + + +def main(argv=None): + """CLI entry point: ``fetch`` (adapt one paged fetch) or ``roster`` (parse team-roster.md).""" + parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) + sub = parser.add_subparsers(dest="command", required=True) + f = sub.add_parser("fetch", help="parse and check one paged query_jira fetch") + f.add_argument("--manifest", required=True) + f.add_argument("--roster", default=None, help="roster JSON (from the 'roster' command)") + f.add_argument("--issue-links", default=None, + help='JSON {key: get_jira_issue text or null} to attach issue links from') + f.add_argument("--linked-spikes", default=None, + help='JSON {feature key: adapted spike fetch} from linkedIssues(...) queries; ' + 'an incomplete one marks the output fetch incomplete') + f.add_argument("--output", required=True) + r = sub.add_parser("roster", help="parse edge-context people/team-roster.md") + r.add_argument("--team-roster", required=True) + r.add_argument("--output", required=True) + args = parser.parse_args(argv) + if args.command == "fetch": + roster = _load(args.roster) if args.roster else None + out = adapt_fetch(_load(args.manifest), roster=roster) + if args.issue_links: + attach_issue_links(out, _load(args.issue_links)) + if args.linked_spikes: + attach_linked_spike_fetches(out, _load(args.linked_spikes)) + else: + with open(args.team_roster, encoding="utf-8") as fh: + out = parse_team_roster(fh.read()) + write_output(out, args.output) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/plugins/edge-scrum/bin/chai_report.py b/plugins/edge-scrum/bin/chai_report.py new file mode 100644 index 00000000..29d3a9d7 --- /dev/null +++ b/plugins/edge-scrum/bin/chai_report.py @@ -0,0 +1,596 @@ +#!/usr/bin/env python3 +"""Team-level figures and Slack text for the chai-bot weekly health report. + +Pure functions used by the chai-bot skills ``sprint-health``, ``release-health`` +and ``weekly-health-report`` (edge-context decision 0018) inside +``sandbox_exec``. Inputs are the outputs of the existing edge-scrum scripts +(``scrum_context.py``, ``transform-*.py``, ``compute-velocity.py``, +``run-checks.py``); nothing here fetches data. + +Output is **team-level only**: no per-person load, SP or PTO. A person's name +appears only as the owner of a specific action item (decision 0018). +""" + +import math +import os +import sys + +sys.path.insert(0, os.path.dirname(__file__)) +from _jira_transforms import DONE_STATUSES_SPRINT, IN_PROGRESS_STATUSES, days_between, is_stale # noqa: E402 +from scrum_context import to_date, working_days # noqa: E402 + +NOT_PTO_ADJUSTED = "not PTO-adjusted" +# Active-work statuses for the stale check, per law 07 as written: the transform's +# In Progress / Review, plus OCPBUGS ASSIGNED ("being actively worked, equivalent +# to In Progress"). Chai path only; the shared transform is unchanged. +ACTIVE_STATUSES = set(IN_PROGRESS_STATUSES) | {"ASSIGNED"} +REFINEMENT, RELEASE_HEALTH = "refinement", "release-health" +DEV_PHASES = ("development", "pencils-down sprint", "pre-branch-cut") +SKIP_PHASES = ("not started", "finished") +RED, YELLOW, GREEN = "🔴", "🟡", "🟢" + + +def _num(value): + """A real number (not bool), else None.""" + return value if isinstance(value, (int, float)) and not isinstance(value, bool) else None + + +# ---------------------------------------------------------------- mode and capacity + + +def sprint_mode(week): + """Sprint-health mode for a sprint week: week 1 → capacity, later → mid-sprint.""" + week = _num(week) + if week is None: + return None + return "capacity" if week <= 1 else "mid-sprint" + + +def sprint_capacity(velocity, availability): + """Current sprint capacity = velocity × availability (decision 0018). + + When availability is unknown (a "not available …" string or None), capacity + is the velocity unadjusted and labelled "not PTO-adjusted". + """ + velocity = _num(velocity) + if velocity is None: + return {"capacity_sp": None, "pto_adjusted": False, + "label": "not available (velocity not measured)"} + avail = _num(availability) + if avail is None: + return {"capacity_sp": round(velocity, 1), "pto_adjusted": False, "label": NOT_PTO_ADJUSTED} + avail = max(0.0, min(1.0, avail)) + return {"capacity_sp": round(velocity * avail, 1), "pto_adjusted": True, + "label": f"velocity {velocity:g} × availability {avail:.2f}"} + + +def remaining_share(sprint, today): + """Share of a sprint's working days (Mon–Fri, start day to the day before the + end day) still ahead on ``today``, today included: 1 for a sprint that has not + started, 0 for one that has ended.""" + start, end, day = to_date(sprint.get("start")), to_date(sprint.get("end")), to_date(today) + if day is None or start is None or end is None or day < start: + return 1.0 + if day >= end: + return 0.0 + total = working_days(start, end) + return len(working_days(day, end)) / len(total) if total else 0.0 + + +def release_capacity(velocity, sprints, today=None): + """Capacity over the remaining sprints = velocity × Σ (availability × share left). + + ``sprints`` are scrum-context ``sprint_availability`` entries (current sprint + through the branch-cut sprint). The current sprint counts only for its + remaining working days (velocity × availability × remaining working days ÷ + sprint working days); upcoming sprints count in full. A sprint with unknown + availability counts as 1.0 and is listed in ``not_adjusted``. + ``availability_csv`` is the ``--availability`` value for ``run-checks.py``. + """ + values, not_adjusted, current = [], [], None + for s in sprints or []: + avail = _num(s.get("availability")) + if avail is None: + not_adjusted.append(s.get("number")) + avail = 1.0 + share = remaining_share(s, today) if today else 1.0 + if share < 1.0 and current is None: + start, end = to_date(s.get("start")), to_date(s.get("end")) + current = {"number": s.get("number"), "share": round(share, 4), + "days_left": len(working_days(max(to_date(today), start), end)) if end > to_date(today) else 0, + "days": len(working_days(start, end))} + values.append(round(max(0.0, min(1.0, avail)) * share, 4)) + velocity = _num(velocity) + capacity = round(velocity * sum(values)) if velocity is not None else None + if not values: + label = "no sprints remaining" + elif not not_adjusted: + label = f"PTO-adjusted ({sum(values):.2f} effective sprints)" + elif len(not_adjusted) == len(values): + label = NOT_PTO_ADJUSTED + else: + label = f"{NOT_PTO_ADJUSTED} for sprint(s) {', '.join(str(n) for n in not_adjusted)}" + if current: + label += (f"; current sprint {current['days_left']} of {current['days']} working days left") + return {"capacity_sp": capacity, "remaining_sprints": len(values), "current_sprint": current, + "effective_sprints": round(sum(values), 2), "availability_csv": ",".join(f"{v:g}" for v in values), + "pto_adjusted": bool(values) and not not_adjusted, "not_adjusted": not_adjusted, "label": label} + + +def usable_velocity(velocity_data): + """``(team_velocity, None)`` from compute-velocity output, or ``(None, reason)``. + + Velocity is not used when it was averaged over fewer than 3 closed sprints, or + when done team-owned issues were skipped for missing sprint and resolution + data: either would understate it (decision 0018: never estimate). + """ + data = velocity_data or {} + value = _num(data.get("team_velocity")) + if value is None or value <= 0: + return None, "velocity not measured" + if (data.get("sprint_count") or 0) < 3: + return None, f"only {data.get('sprint_count') or 0} closed sprint(s) measured, 3 needed" + skipped = data.get("skipped_no_sprint_data") or 0 + if skipped: + return None, f"{skipped} done team issue(s) had no sprint or resolution data" + return value, None + + +# ---------------------------------------------------------------- sections + + +def _version_key(version): + """Numeric sort key for "X.Y" (9.10 after 9.9); anything else sorts last.""" + try: + major, minor = str(version).split(".") + return (int(major), int(minor)) + except ValueError: + return (9999, 9999) + + +def select_sections(context, windows=()): + """Release sections for the report, from scrum-context results (decision 0018). + + ``context`` is the no-argument scrum-context result; ``windows`` are + release-mode results (one per release in progress, plus any release Product + Pages lists as planned or in development). Returns ``(sections, missing)``: + + * refinement phase → a ``refinement`` section; + * development, pencils-down sprint, pre-branch-cut → a ``release-health`` + section, with ``pencils_down`` set in the pencils-down sprint; + * not scheduled yet, an anchor that could not be placed on a sprint, or an + unknown phase → listed in ``missing`` with the reason; + * not started / finished → no section. + + When nothing is in progress, ``missing`` says so. + """ + # Accept a full release-mode scrum-context result as well as its "release" window. + windows = [w["release"] if isinstance(w, dict) and isinstance(w.get("release"), dict) else w + for w in windows or []] + windows = [w for w in windows if isinstance(w, dict) and isinstance(w.get("release"), str)] + by_release = {w["release"]: w for w in windows} + in_progress = context.get("release_in_progress") if isinstance(context, dict) else None + for entry in in_progress if isinstance(in_progress, list) else []: + by_release.setdefault(entry.get("release"), entry) + sections, missing = [], [] + for release in sorted(by_release, key=_version_key): + w = by_release[release] + phase = w.get("phase") + label = f"{release} release section" + if phase in SKIP_PHASES: + continue + if phase == "not scheduled yet": + missing.append({"what": label, "why": "no Product Pages schedule yet"}) + elif phase == REFINEMENT: + if not isinstance(w.get("refinement_sprint"), dict): + missing.append({"what": label, "why": "refinement sprint could not be placed on a sprint"}) + continue + sections.append({"release": release, "phase": phase, "kind": REFINEMENT, + "pencils_down": False, "window": w}) + elif phase in DEV_PHASES: + if not isinstance(w.get("branch_cut_sprint"), dict) or not isinstance(w.get("refinement_sprint"), dict): + missing.append({"what": label, + "why": "a release milestone could not be placed on a sprint"}) + continue + sections.append({"release": release, "phase": phase, "kind": RELEASE_HEALTH, + "pencils_down": phase == "pencils-down sprint", "window": w}) + else: + missing.append({"what": label, "why": f"phase unknown ({phase or 'not available'})"}) + if not sections and not missing: + missing.append({"what": "Release section", "why": "no release in progress"}) + return sections, missing + + +# ---------------------------------------------------------------- sprint health + + +def _days(sprint, today): + """(days elapsed, days remaining, total days) of a sprint, calendar days, as transform-sprints counts them.""" + start, end = sprint.get("start"), sprint.get("end") + total = days_between(start, end) + elapsed = max(0, min(days_between(start, today), total)) + return elapsed, total - elapsed, total + + +def _stale(issue, today): + """In an active status (law 07, :data:`ACTIVE_STATUSES`) with no update for more than 5 business days.""" + if issue.get("stale"): + return True + if issue.get("status") not in ACTIVE_STATUSES or not issue.get("updated"): + return False + try: + return is_stale(issue["updated"], today) + except (ValueError, TypeError): + return False + + +def _owner(issue): + """An issue's assignee display name, or None when unassigned.""" + name = issue.get("assignee_display") + return None if not name or name in ("Unassigned", "None") else name + + +def sprint_health(sprint_issues, sprint, today, mode, velocity=None, availability=None, + skipped_signals=(), links_complete=True, goal=None, velocity_reason=None): + """Team-level sprint health for capacity or mid-sprint mode. + + ``sprint_issues`` is ``transform-sprint-issues.py`` output; ``sprint`` is + scrum-context's ``current_sprint``; ``velocity`` is ``team_velocity`` from + ``compute-velocity.py``. Returns figures, ``risks`` and ``actions`` (each + action has an ``owner`` display name or None). No per-person figures. + ``goal`` (mid-sprint) is the coordinator's sprint-goal rating, + ``{"rating": "🟢"|"🟡"|"🔴"|None, "note": "..."}``, carried into the reply. + ``velocity_reason`` is :func:`usable_velocity`'s reason when velocity was + rejected; it is shown as the reason capacity is left out. + """ + issues = sprint_issues.get("issues", []) + skipped = list(skipped_signals) + out = {"mode": mode, "sprint": sprint.get("name"), "week": sprint.get("week"), + "committed_sp": sprint_issues.get("total_sp", 0), "issues": len(issues), + "risks": [], "actions": [], "left_out": [], "goal": goal} + if mode not in ("capacity", "mid-sprint"): + # sprint_mode() returned None (week missing or not numeric): run neither + # analysis; the section is left out with the reason. + out.update(mode=None, skipped=True) + out["left_out"].append({"what": "Sprint health", "why": "sprint week unknown"}) + return out + if mode == "capacity": + by_type = {} + for i in issues: + t = by_type.setdefault(i["type"], {"issues": 0, "sp": 0}) + t["issues"] += 1 + t["sp"] += i["sp"] + cap = sprint_capacity(velocity, availability) + out.update(by_type=by_type, velocity=_num(velocity), capacity=cap) + unpointed = [i for i in issues if not i.get("sp")] + no_epic = [i for i in issues if i.get("epic_key") == "No Epic"] + out["unpointed"] = [i["key"] for i in unpointed] + out["no_epic"] = [i["key"] for i in no_epic] + ac_skipped = any(s.startswith("acceptance criteria") for s in skipped) + out["missing_ac"] = None if ac_skipped else [ + i["key"] for i in issues if i["type"] in ("Story", "Task", "Spike") and not i.get("has_ac")] + if cap["capacity_sp"] is not None and out["committed_sp"] > cap["capacity_sp"]: + over = round(out["committed_sp"] - cap["capacity_sp"], 1) + out["risks"].append({"severity": RED, "text": ( + f"Sprint committed {out['committed_sp']} SP vs capacity {cap['capacity_sp']:g} SP " + f"({cap['label']}): {over:g} SP over")}) + out["actions"].append({"text": f"Re-plan the sprint: {over:g} SP over capacity", "owner": None, + "team": True}) + elif cap["capacity_sp"] is None: + out["left_out"].append({"what": "sprint capacity", "why": velocity_reason or "velocity not measured"}) + for label, items, verb in (("unpointed", unpointed, "Point"), + ("without an epic", no_epic, "Link to an epic")): + if items: + out["risks"].append({"severity": YELLOW, "text": f"{len(items)} sprint issue(s) {label}"}) + out["actions"].extend({"text": f"{verb}: {i['key']}", "owner": _owner(i)} for i in items) + if out["missing_ac"]: + out["risks"].append({"severity": YELLOW, + "text": f"{len(out['missing_ac'])} story/task/spike issue(s) without acceptance criteria"}) + else: + elapsed, remaining, total_days = _days(sprint, today) + total = sprint_issues.get("total_sp", 0) + done = sprint_issues.get("total_done_sp", 0) + left = sprint_issues.get("total_remaining_sp", total - done) + burndown = {"total_sp": total, "done_sp": done, "days_elapsed": elapsed, "total_days": total_days} + if total == 0: + burndown["status"] = "No SP committed" + elif elapsed == 0: + burndown["status"] = "Sprint not yet started" + else: + expected = round(total * elapsed / total_days, 1) + delta = round(done - expected, 1) + status = ("On Track" if delta >= 0 else + "Slightly Behind" if abs(delta) <= total * 0.1 else "Behind") + if done == 0: + finish = "unknown (no completed SP yet)" + else: + need = left / (done / elapsed) + over = math.ceil(need - remaining) if need > remaining else 0 + finish = ("on time" if over == 0 else + f"{over} day{'s' if over != 1 else ''} after the sprint ends") + burndown.update(expected_done_sp=expected, delta_sp=delta, status=status, projected_finish=finish) + if status != "On Track": + out["risks"].append({"severity": RED if status == "Behind" else YELLOW, "text": ( + f"Burndown {status.lower()}: {done} of {total} SP done, {expected:g} expected; " + f"projected to finish {finish}")}) + if status == "Behind": + out["actions"].append({"text": f"Re-scope the sprint: {left} SP left, burndown behind", + "owner": None, "team": True}) + out["burndown"] = burndown + flagged_known = not any(s.startswith("flagged") for s in skipped) + blockers = [] + for i in issues: + if i.get("status") in DONE_STATUSES_SPRINT: + continue # finished work needs no unblocking, whatever its links say + reasons = [] + if flagged_known and i.get("flagged"): + reasons.append("flagged") + if links_complete and i.get("blocked_by"): + reasons.append("blocked by " + ", ".join(i["blocked_by"])) + if "Blocked" in (i.get("labels") or []): + reasons.append("labeled Blocked") + if _stale(i, today): + reasons.append("no update for more than 5 business days") + if reasons: + blockers.append({"key": i["key"], "reasons": reasons, "owner": _owner(i)}) + out["blockers"] = [{"key": b["key"], "reasons": b["reasons"]} for b in blockers] + if not links_complete: + out["left_out"].append({"what": "blocked-by links", + "why": "issue link details incomplete (get_jira_issue)"}) + if blockers: + hard = [b for b in blockers if any(not r.startswith("no update") for r in b["reasons"])] + out["risks"].append({"severity": RED if hard else YELLOW, + "text": f"{len(blockers)} sprint issue(s) blocked or stalled"}) + out["actions"].extend({"text": ("Unblock" if any(not r.startswith("no update") for r in b["reasons"]) + else "Check stalled") + f" {b['key']} ({'; '.join(b['reasons'])})", + "owner": b["owner"]} for b in blockers) + out["left_out"].extend({"what": s.split(" (")[0] + " signal", "why": s[s.find("(") + 1:-1] + if "(" in s else "not available"} for s in skipped) + return out + + +# ---------------------------------------------------------------- release health + + +def release_health(checks, section, capacity=None, rank_available=True, velocity_reason=None): + """Team-level release health from ``run-checks.py`` output (development phases). + + Keeps scope vs capacity, the cut line and features at risk. Drops the + per-person capacity rows (``ABOVE_GUIDELINE``) and reduces roster drift to a + count. ``capacity`` is :func:`release_capacity` output, for its label. + ``rank_available=False`` (features did not fit on one rank-ordered page) + leaves the cut line out, since key order is not PM rank. ``velocity_reason`` + (from :func:`usable_velocity`) is shown when velocity is not available. + """ + meta = checks.get("meta", {}) + scope, cap = meta.get("total_remaining_sp"), meta.get("total_capacity_sp") + out = {"kind": RELEASE_HEALTH, "release": section["release"], "phase": section["phase"], + "pencils_down": section.get("pencils_down", False), + "scope_sp": scope, "capacity_sp": cap, "capacity_basis": meta.get("capacity_basis"), + "capacity_label": (capacity or {}).get("label"), + "effective_sprints": (capacity or {}).get("effective_sprints"), + "remaining_sprints": meta.get("remaining_sprints"), "velocity": meta.get("team_velocity"), + "gap_sp": meta.get("gap_sp"), + # A count, never an SP estimate (decision 0018). + "unpointed_items": (meta.get("total_unpointed_stories") or 0) + (meta.get("unpointed_bugs") or 0), + "risks": [], "actions": [], "left_out": []} + if meta.get("capacity_basis") != "velocity": + # Roster-sum capacity is an estimate (decision 0018: never estimate); the + # timeline's pace depends on velocity too. Report scope only. + out.update(capacity_sp=None, below_cut_line=[], features_at_risk=[], + roster_drift={"people": 0, "sp": 0}) + out["left_out"].append({"what": f"{section['release']} capacity, cut line and features at risk", + "why": velocity_reason or "velocity not available"}) + return out + below = [r for r in checks.get("cut_line", []) if r.get("fits") in ("partial", "over")] + if not rank_available: + below = [] + out["left_out"].append({"what": f"{section['release']} cut line", + "why": "features did not fit on one rank-ordered page, so PM rank order is not available"}) + out["below_cut_line"] = [r["feature_key"] for r in below] + at_risk = {} + for c in checks.get("composite", []): + if c.get("composite_risk") == "HIGH": + at_risk[c["feature_key"]] = c.get("sme") + for t in checks.get("timeline", []): + if t.get("risk") in ("HIGH", "NO_CONTRIBUTORS"): + at_risk.setdefault(t["feature_key"], t.get("sme")) + out["features_at_risk"] = sorted(at_risk) + drift = checks.get("process_gaps", {}).get("roster_drift", []) + out["roster_drift"] = {"people": len(drift), "sp": sum(d.get("assigned_sp", 0) for d in drift)} + tag = f"{section['release']}" + (" (pencils-down sprint)" if out["pencils_down"] else "") + if scope is not None and cap is not None and scope > cap: + out["risks"].append({"severity": RED, "text": ( + f"{tag}: scope {scope} SP vs capacity {cap} SP ({out['capacity_label'] or meta.get('capacity_basis')}): " + f"{scope - cap} SP over" + + (f"; {len(below)} feature(s) below the cut line" if rank_available else ""))}) + for r in below: + out["actions"].append({"text": f"{tag}: decide scope for {r['feature_key']} (below the cut line)", + "owner": _sme(r.get("sme"))}) + if at_risk: + out["risks"].append({"severity": RED, "text": f"{tag}: {len(at_risk)} feature(s) at risk"}) + for key in sorted(at_risk): + if key not in out["below_cut_line"]: + out["actions"].append({"text": f"{tag}: review plan for {key} (at risk)", + "owner": _sme(at_risk[key])}) + if out["roster_drift"]["people"]: + out["left_out"].append({"what": f"{tag}: capacity from {out['roster_drift']['people']} assignee(s) not on the roster", + "why": f"their {out['roster_drift']['sp']} SP counts as scope, not capacity"}) + return out + + +def _sme(name): + """A feature's SME display name, or None when unset.""" + return None if name in (None, "", "None") else name + + +def refinement_gaps(features, spikes, section, refinement_sprint_closed=False): + """Refinement and test-plan spike gaps (release-health ``--refinement`` rules). + + ``features`` is ``transform-features.py`` output for features in Refinement; + ``spikes`` is ``transform-spikes.py`` output. Owners: the SME for the + refinement spike, the QE Lead (QA Contact) for the test plan spike. + """ + sprint = (section.get("window") or {}).get("refinement_sprint") or {} + number = sprint.get("number") if isinstance(sprint, dict) else None + spike_map = spikes.get("spike_map", {}) + tag = section["release"] + out = {"kind": REFINEMENT, "release": tag, "phase": section["phase"], + "features": len(features.get("features", [])), + "missing_refinement_spike": [], "spike_not_in_refinement_sprint": [], + "refinement_spike_overdue": [], "missing_test_plan_spike": [], "test_plan_spike_overdue": [], + "risks": [], "actions": [], "left_out": []} + for f in features.get("features", []): + s = spike_map.get(f["key"]) + if s is None: + continue + sme, qe = _sme(f.get("sme")), _sme(f.get("qa_contact")) + if s["spike_status"] == "Closed" or (s["spike_missing"] and (s["spike_on_epic"] or s["epics_appear_refined"])): + pass + elif s["spike_missing"]: + out["missing_refinement_spike"].append(f["key"]) + out["actions"].append({"text": f"{tag}: create a refinement spike that blocks {f['key']}" + + (f" in sprint {number}" if number and not refinement_sprint_closed else ""), + "owner": sme}) + elif refinement_sprint_closed and s["spike_overdue"]: + out["refinement_spike_overdue"].append(f["key"]) + out["actions"].append({"text": f"{tag}: close refinement spike {s['spike_key']} ({f['key']})", + "owner": sme}) + elif not refinement_sprint_closed and not s["spike_in_ref_sprint"]: + out["spike_not_in_refinement_sprint"].append(f["key"]) + out["actions"].append({"text": f"{tag}: move refinement spike {s['spike_key']} ({f['key']}) " + f"to sprint {number}" if number else + f"{tag}: move refinement spike {s['spike_key']} to the refinement sprint", + "owner": sme}) + if not s["test_plan_check_skipped"]: + if s["test_plan_spike_missing"]: + out["missing_test_plan_spike"].append(f["key"]) + out["actions"].append({"text": f"{tag}: create a test plan spike that blocks {f['key']}", + "owner": qe}) + elif s["test_plan_spike_overdue"]: + out["test_plan_spike_overdue"].append(f["key"]) + out["actions"].append({"text": f"{tag}: close test plan spike {s['test_plan_spike_key']}", + "owner": qe}) + gaps = len(out["missing_refinement_spike"]) + len(out["refinement_spike_overdue"]) + if gaps: + out["risks"].append({"severity": RED, + "text": f"{tag}: {gaps} of {out['features']} feature(s) without a closed or open refinement spike"}) + tp = len(out["missing_test_plan_spike"]) + len(out["test_plan_spike_overdue"]) + if tp: + out["risks"].append({"severity": YELLOW, "text": f"{tag}: {tp} feature(s) missing a test plan spike"}) + if out["spike_not_in_refinement_sprint"]: + out["risks"].append({"severity": YELLOW, + "text": f"{tag}: {len(out['spike_not_in_refinement_sprint'])} refinement spike(s) not in the refinement sprint"}) + return out + + +# ---------------------------------------------------------------- report + + +def mention(owner, mentions): + """Slack mention for an owner when mapped (``<@U…>``), else the plain name.""" + if not owner: + return "no owner set" + return (mentions or {}).get(owner) or owner + + +def _sprint_figures(s): + """One-line sprint figures for the top post and the sprint reply.""" + if s["mode"] == "capacity": + cap = s.get("capacity") or {} + cap_txt = f"{cap['capacity_sp']:g} SP ({cap['label']})" if cap.get("capacity_sp") is not None \ + else "not available" + return f"committed {s['committed_sp']} SP in {s['issues']} issues; capacity {cap_txt}" + b = s.get("burndown", {}) + if "expected_done_sp" in b: + return (f"{b['done_sp']} of {b['total_sp']} SP done, {b['expected_done_sp']:g} expected " + f"({b['status']})") + return f"{s['committed_sp']} SP committed ({b.get('status', 'burndown not available')})" + + +def _section_lines(sec): + """Thread reply lines for one release section (refinement or release health).""" + if sec.get("kind") == REFINEMENT: + return [f"*{sec['release']} refinement* ({sec['features']} feature(s) in Refinement)", + f"• Missing refinement spike: {len(sec['missing_refinement_spike'])}", + f"• Refinement spike not in the refinement sprint: {len(sec['spike_not_in_refinement_sprint'])}", + f"• Missing or open test plan spike: " + f"{len(sec['missing_test_plan_spike']) + len(sec['test_plan_spike_overdue'])}"] + lines = [f"*{sec['release']} release health* (" + + ("pencils-down sprint: the last development sprint" if sec.get("pencils_down") else sec["phase"]) + ")", + (f"• Scope {sec['scope_sp']} SP vs capacity {sec['capacity_sp']} SP to branch cut " + + (f"({sec['velocity']:g} SP velocity × {sec['effective_sprints']:g} sprint(s); {sec['capacity_label']})" + if sec.get("effective_sprints") is not None and sec.get("velocity") is not None + else f"({sec.get('capacity_label') or sec.get('capacity_basis')})") + if sec["capacity_sp"] is not None else + f"• Scope {sec['scope_sp']} SP; capacity not available (velocity not measured)"), + f"• Below the cut line: {', '.join(sec['below_cut_line']) or 'none'}", + f"• Features at risk: {', '.join(sec['features_at_risk']) or 'none'}"] + if sec.get("unpointed_items"): + lines.append(f"• {sec['unpointed_items']} unpointed items, not counted in scope") + return lines + + +def _sprint_lines(s): + """Thread reply lines for the sprint section, with the goal rating when given.""" + head = f"*Sprint health: {s['sprint']}* (week {s['week']}, {s['mode']} mode)" + lines = [head, f"• {_sprint_figures(s)}"] + if s["mode"] == "capacity": + types = ", ".join(f"{t} {v['issues']} ({v['sp']} SP)" for t, v in sorted(s.get("by_type", {}).items())) + lines.append(f"• By type: {types or 'none'}") + lines.append(f"• Unpointed: {len(s['unpointed'])}; without an epic: {len(s['no_epic'])}" + + ("" if s.get("missing_ac") is None else f"; without acceptance criteria: {len(s['missing_ac'])}")) + else: + lines.append(f"• Blocked or stalled: {len(s.get('blockers', []))}") + goal = s.get("goal") + if goal: + lines.append(f"• Sprint goal: {(goal.get('rating') or '') + ' ' if goal.get('rating') else ''}" + f"{goal.get('note') or ''}".rstrip()) + return lines + + +def build_report(sprint, sections, missing=(), mentions=None, team_owner=None): + """Assemble the Slack post: a 3-line top post and thread replies. + + ``sprint`` is :func:`sprint_health` output (or None when it could not be + computed); ``sections`` are :func:`release_health` / :func:`refinement_gaps` + outputs (``kind`` set); ``missing`` lists ``{"what", "why"}`` left out. + ``mentions`` maps an owner display name to a Slack mention. ``team_owner`` + (the Scrum Master) owns team-level actions such as re-planning; without it + they keep "no owner set". Returns + ``{"top_post": [3 lines], "replies": [...], "action_count": N}``. + """ + left_out_sprint = [] + if sprint and sprint.get("skipped"): + left_out_sprint, sprint = sprint.get("left_out", []), None + parts = ([sprint] if sprint else []) + list(sections) + if any("risks" not in p for p in parts): + raise ValueError("build_report needs sprint_health / release_health / refinement_gaps results, " + "not select_sections descriptors") + risks = [r for p in parts for r in p["risks"]] + actions = [dict(a, owner=team_owner) if a.get("team") and not a.get("owner") and team_owner else a + for p in parts for a in p["actions"]] + left_out = list(missing) + left_out_sprint + [x for p in parts for x in p.get("left_out", [])] + status = RED if any(r["severity"] == RED for r in risks) else YELLOW if risks or left_out else GREEN + where = [] + if sprint: + where.append(f"{sprint['sprint']} (week {sprint['week']}, {sprint['mode']})") + where += [f"{s['release']} {s['phase']}" for s in sections] + top = [f"{status} Edge weekly health: " + ("; ".join(where) or "sprint data not available")] + if risks: + biggest = next((r for r in risks if r["severity"] == RED), risks[0]) + top.append(f"Biggest risk: {biggest['text']}") + else: + top.append("No risks this week" + (f": {_sprint_figures(sprint)}" if sprint else "")) + top.append(f"{len(actions)} item{'s' if len(actions) != 1 else ''} need{'s' if len(actions) == 1 else ''} action") + replies = [] + if sprint: + replies.append("\n".join(_sprint_lines(sprint))) + for sec in sections: + replies.append("\n".join(_section_lines(sec))) + if actions: + replies.append("\n".join(["*Action items*"] + [ + f"{n}. {a['text']} (owner: {mention(a['owner'], mentions)})" for n, a in enumerate(actions, 1)])) + if left_out: + replies.append("\n".join(["*Left out*"] + [f"• {x['what']}: {x['why']}" for x in left_out])) + return {"top_post": top, "replies": replies, "action_count": len(actions)} diff --git a/plugins/edge-scrum/bin/run-checks.py b/plugins/edge-scrum/bin/run-checks.py index 9dd241e5..936ef720 100644 --- a/plugins/edge-scrum/bin/run-checks.py +++ b/plugins/edge-scrum/bin/run-checks.py @@ -933,6 +933,34 @@ def build_cut_line(timeline_results, capacity_sp, timeline_risk_by_key=None): return out +def count_unpointed(features, active_keys): + """Unpointed open items under active features: stories/tasks/spikes and bugs, counted. + + No SP estimate — this is what the chai-bot report uses (edge-context decision + 0018: never estimate). :func:`estimate_hidden_scope` builds on it. + """ + active_open_unpointed = [ + s for f in features if f["key"] in active_keys for s in f["all_stories"] + if not is_story_done(s) and s["sp"] <= 0 + ] + unpointed = [s for s in active_open_unpointed if s["type"] != "Bug"] + unpointed_bugs = [s for s in active_open_unpointed if s["type"] == "Bug"] + return { + "unpointed_open": len(unpointed), + "unpointed_assigned": sum(1 for s in unpointed if s["assignee"]), + "unpointed_bugs": len(unpointed_bugs), + "unpointed_bug_keys": [s["key"] for s in unpointed_bugs], + } + + +def unestimated_hidden_scope(features, active_keys): + """:func:`count_unpointed` in the hidden-scope shape, with every estimate None.""" + return {**count_unpointed(features, active_keys), + "sp_per_story_low": None, "sp_per_story_typical": None, "sp_per_story_high": None, + "estimate_low": None, "estimate_typical": None, "estimate_high": None, + "basis": "not estimated (--no-hidden-scope-estimate)"} + + def estimate_hidden_scope(features, active_keys): """Estimate SP for unpointed open stories from closed pointed stories' distribution. @@ -947,14 +975,8 @@ def estimate_hidden_scope(features, active_keys): s["sp"] for f in features for s in f["all_stories"] if is_story_done(s) and s["sp"] > 0 ] - active_open_unpointed = [ - s for f in features if f["key"] in active_keys for s in f["all_stories"] - if not is_story_done(s) and s["sp"] <= 0 - ] - unpointed = [s for s in active_open_unpointed if s["type"] != "Bug"] - unpointed_bugs = [s for s in active_open_unpointed if s["type"] == "Bug"] - n = len(unpointed) - assigned = sum(1 for s in unpointed if s["assignee"]) + counts = count_unpointed(features, active_keys) + n = counts["unpointed_open"] if len(closed_pointed) >= 4: srt = sorted(closed_pointed) q1, q2, q3 = statistics.quantiles(srt, n=4, method="inclusive") @@ -964,10 +986,7 @@ def estimate_hidden_scope(features, active_keys): low, typical, high = FALLBACK_SP_RANGE basis = "fallback range (fewer than 4 closed pointed stories)" return { - "unpointed_open": n, - "unpointed_assigned": assigned, - "unpointed_bugs": len(unpointed_bugs), - "unpointed_bug_keys": [s["key"] for s in unpointed_bugs], + **counts, "sp_per_story_low": low, "sp_per_story_typical": typical, "sp_per_story_high": high, @@ -1005,6 +1024,7 @@ def build_process_gaps(features, active, dormant, capacity, sizing, hidden): def build_method(meta, roster, remaining_sprints, hidden, activity_days, window, version): """Formula + inputs + result for every headline figure. Rendered verbatim by the report.""" + estimated = hidden.get("estimate_low") is not None members = roster.get("members", []) targets = [m.get("sp_target", DEFAULT_SP_TARGET) for m in members] avg_target = round(sum(targets) / len(targets), 1) if targets else DEFAULT_SP_TARGET @@ -1044,7 +1064,7 @@ def build_method(meta, roster, remaining_sprints, hidden, activity_days, window, f"Σ targets {sum(targets)} SP/sprint × {sprints_shown} sprints = {meta['total_capacity_sp']} SP ({len(members)} people, targets {min(targets)}–{max(targets)})") + " — roster fallback; team velocity was unavailable" + pto_note), } - return [ + rows = [ capacity_entry, { "id": "scope", @@ -1061,8 +1081,10 @@ def build_method(meta, roster, remaining_sprints, hidden, activity_days, window, "inputs": {"unpointed_open": hidden["unpointed_open"], "sp_low": hidden["sp_per_story_low"], "sp_typical": hidden["sp_per_story_typical"], "sp_high": hidden["sp_per_story_high"], "unpointed_bugs": hidden["unpointed_bugs"], "basis": hidden["basis"]}, - "result": f"{hidden['estimate_low']}–{hidden['estimate_high']} SP", - "shown_as": f"{hidden['unpointed_open']} × {hidden['sp_per_story_low']}–{hidden['sp_per_story_high']} SP = {hidden['estimate_low']}–{hidden['estimate_high']} SP" + "result": (f"{hidden['estimate_low']}–{hidden['estimate_high']} SP" if estimated + else "not estimated"), + "shown_as": (f"{hidden['unpointed_open']} × {hidden['sp_per_story_low']}–{hidden['sp_per_story_high']} SP = {hidden['estimate_low']}–{hidden['estimate_high']} SP" + if estimated else f"{hidden['unpointed_open']} unpointed open stories (not estimated)") + (f" ({hidden['unpointed_bugs']} unpointed bugs excluded)" if hidden["unpointed_bugs"] else ""), }, { @@ -1072,8 +1094,9 @@ def build_method(meta, roster, remaining_sprints, hidden, activity_days, window, "inputs": {"capacity": meta["total_capacity_sp"], "scope": meta["total_remaining_sp"], "hidden_low": hidden["estimate_low"], "hidden_high": hidden["estimate_high"]}, "result": f"{meta['gap_sp']} SP", - "shown_as": f"{meta['total_capacity_sp']} − {meta['total_remaining_sp']} = {meta['gap_sp']} SP " - f"({meta['gap_with_hidden_low']} to {meta['gap_with_hidden_high']} incl. hidden)", + "shown_as": f"{meta['total_capacity_sp']} − {meta['total_remaining_sp']} = {meta['gap_sp']} SP" + + (f" ({meta['gap_with_hidden_low']} to {meta['gap_with_hidden_high']} incl. hidden)" + if estimated else ""), }, { "id": "priority_focus", @@ -1147,6 +1170,8 @@ def build_method(meta, roster, remaining_sprints, hidden, activity_days, window, "shown_as": f"{meta['excluded_epics_count']} epics ({meta['excluded_open_sp']} open SP) left out", }, ] + # Not estimated (--no-hidden-scope-estimate): leave the hidden-scope row out. + return [row for row in rows if estimated or row["id"] != "hidden_scope"] # --- Main --- @@ -1245,6 +1270,9 @@ def main(): help="CSV of feature priorities to assess (default: Blocker,Critical,Major)") parser.add_argument("--include-lower-when", default=",".join(sorted(LOWER_PRIORITY_INCLUDE_STATUSES)), help="CSV of statuses that pull a lower-priority feature into focus (default: Refinement)") + parser.add_argument("--no-hidden-scope-estimate", action="store_true", + help="Count unpointed open items but do not estimate their SP (chai-bot report, " + "edge-context decision 0018: never estimate).") parser.add_argument("--output", required=True) args = parser.parse_args() @@ -1304,7 +1332,8 @@ def main(): bug_load = run_bug_load_check(unlinked_bugs, all_epic_stories, args.component_filter) sizing = run_sizing_check(active, timeline, roster) composite = run_composite_check(active, gate, capacity, timeline, assignment, sizing) - hidden = estimate_hidden_scope(in_focus, active_keys) + hidden = (unestimated_hidden_scope(in_focus, active_keys) if args.no_hidden_scope_estimate + else estimate_hidden_scope(in_focus, active_keys)) unknown_contributors = [c["person"] for c in capacity if not c["in_roster"]] @@ -1361,8 +1390,8 @@ def main(): "hidden_scope_low": hidden["estimate_low"], "hidden_scope_typical": hidden["estimate_typical"], "hidden_scope_high": hidden["estimate_high"], - "gap_with_hidden_low": gap - hidden["estimate_high"], - "gap_with_hidden_high": gap - hidden["estimate_low"], + "gap_with_hidden_low": gap - hidden["estimate_high"] if hidden["estimate_high"] is not None else None, + "gap_with_hidden_high": gap - hidden["estimate_low"] if hidden["estimate_low"] is not None else None, "total_unpointed_stories": hidden["unpointed_open"], "total_unpointed_assigned": hidden["unpointed_assigned"], # Unpointed open bugs are reported separately, never folded into hidden scope. diff --git a/plugins/edge-scrum/bin/scrum_context.py b/plugins/edge-scrum/bin/scrum_context.py index e829b660..d5268c1b 100644 --- a/plugins/edge-scrum/bin/scrum_context.py +++ b/plugins/edge-scrum/bin/scrum_context.py @@ -529,6 +529,78 @@ def availability(absences, engineer_count, start, end): return round(max(0.0, 1 - len(off) / total), 2) +# Calendar window read for absences when the caller does not pass one: the +# window the SKILL documents for calendar reads (21 days back, 42 ahead). +DEFAULT_ABSENCE_DAYS_BEFORE = 21 +DEFAULT_ABSENCE_DAYS_AFTER = 42 +OUTSIDE_CALENDAR_WINDOW = "not available (outside the calendar window read)" + + +def absence_window(raw, today, warnings): + """The calendar window absences were read for, as ``(start, end)`` dates. + + ``raw`` is the optional ``absence_window`` input (``{"start", "end"}``, + inclusive). Missing or malformed → the default window around ``today``. + """ + default = (today - dt.timedelta(days=DEFAULT_ABSENCE_DAYS_BEFORE), + today + dt.timedelta(days=DEFAULT_ABSENCE_DAYS_AFTER)) + if not raw: + return default + start, start_ok = source_date(raw.get("start")) + end, end_ok = source_date(raw.get("end")) + if not (start_ok and end_ok and start and end) or end < start: + warnings.append("ignored absence_window: bad date; using the default window") + return default + return start, end + + +def sprint_availability(sprint, absences, engineer_count, window): + """Availability of one sprint, or why it is not available. + + Unlike ``next_sprint_availability``, a sprint whose working days are not all + inside the calendar window that was read is "not available": absences after + the window were never seen, so a figure would overstate availability. + """ + if not engineer_count: + return ROSTER_NOT_CONFIGURED + if absences is None: + return NOT_AVAILABLE + days = working_days(sprint["start"], sprint["end"]) + if not days: + return NOT_AVAILABLE + if days[0] < window[0] or days[-1] > window[1]: + return OUTSIDE_CALENDAR_WINDOW + return availability(absences, engineer_count, sprint["start"], sprint["end"]) + + +def _horizon_end(windows, following): + """Last day to report availability for: the latest branch-cut sprint end of + ``windows`` (release windows), else the next sprint's end.""" + ends = [] + for w in windows: + bc = w.get("branch_cut_sprint") + if isinstance(bc, dict): + end = to_date(bc.get("end")) + if end: + ends.append(end) + if ends: + return max(ends) + return following["end"] if following else None + + +def availability_by_sprint(sprints, current, horizon_end, absences, engineer_count, window): + """Availability for the current sprint and each later sprint ending by ``horizon_end``.""" + if current is None or horizon_end is None: + return [] + out = [] + for s in project_sprints(sprints, horizon_end): + if s["start"] < current["start"] or s["end"] > max(horizon_end, current["end"]): + continue + out.append({**_sprint_out(s), + "availability": sprint_availability(s, absences, engineer_count, window)}) + return out + + # ---------------------------------------------------------------- assembly @@ -673,13 +745,16 @@ def build_context(data): result["next_sprint"] = _sprint_out(following) if following else NOT_AVAILABLE release = data.get("release") + horizon_windows = [] if release is not None and release != "": # Non-strings are rejected: a JSON number would turn 5.10 into 5.1. result["release"] = release_window(release, schedules, sprints, today, current) + horizon_windows = [result["release"]] elif not schedules: result["release_in_progress"] = NOT_AVAILABLE else: in_progress = releases_in_progress(schedules, sprints, today, current) + horizon_windows = in_progress result["release_in_progress"] = ( [{"release": w["release"], "phase": w["phase"], "development_sprints_remaining": w["development_sprints_remaining"]} @@ -702,6 +777,16 @@ def build_context(data): else: result["next_sprint_availability"] = availability( absences, data["engineer_count"], following["start"], following["end"]) + + # Current sprint and every later sprint up to the latest branch-cut sprint + # (or the next sprint), for capacity = velocity x availability (decision 0018). + window = absence_window(data.get("absence_window"), today, warnings) + by_sprint = availability_by_sprint( + sprints, current, _horizon_end(horizon_windows, following), + absences, data.get("engineer_count"), window) + result["sprint_availability"] = by_sprint + result["current_sprint_availability"] = ( + by_sprint[0]["availability"] if by_sprint else NOT_AVAILABLE) result["warnings"] = warnings return result diff --git a/plugins/edge-scrum/bin/tests/test_assemble_report.py b/plugins/edge-scrum/bin/tests/test_assemble_report.py index 75b5b340..0390e7a8 100644 --- a/plugins/edge-scrum/bin/tests/test_assemble_report.py +++ b/plugins/edge-scrum/bin/tests/test_assemble_report.py @@ -4,7 +4,10 @@ Both are validation logic and need positive and negative cases. """ +import json +import subprocess import sys +import tempfile import os import unittest @@ -334,5 +337,76 @@ def test_rejects_invalid_versions(self): self.assertIsNone(VERSION_RE.fullmatch(v), f"{v!r} should be rejected") + + +class NoHiddenScopeEstimateEndToEndTest(unittest.TestCase): + """run-checks.py --no-hidden-scope-estimate → assemble-report.py renders without None values.""" + + BIN = os.path.join(os.path.dirname(__file__), "..") + + def _write(self, d, name, obj): + path = os.path.join(d, name) + with open(path, "w") as f: + json.dump(obj, f) + return path + + def _story(self, key, sp, status="In Progress"): + return {"key": key, "summary": "Synthetic story", "type": "Story", "status": status, "sp": sp, + "assignee": "aexample@example.com", "assignee_display": "Alex Example", "epic_key": "EPIC-1", + "priority": "Major", "labels": [], "updated": "2030-01-20", "sprints": [], + "flagged": False, "blocked_by": [], "stale": False} + + def _run(self, d, extra): + files = { + "features": self._write(d, "features.json", {"features": [ + {"key": "OCPSTRAT-9001", "summary": "Synthetic feature", "status": "In Progress", "priority": "Major", + "size": "M", "sme": "Alex Example", "rank": 1, "labels": [], "type": "Feature"}]}), + "epics": self._write(d, "epics.json", {"feature_to_epics": {"OCPSTRAT-9001": ["EPIC-1"]}, "epics": [ + {"key": "EPIC-1", "summary": "Synthetic epic", "status": "In Progress", + "feature_key": "OCPSTRAT-9001", "target_versions": [], "fix_versions": [], "labels": []}]}), + "stories": self._write(d, "stories.json", {"stories": [ + self._story("TEST-1", 5), self._story("TEST-2", 0), self._story("TEST-3", 0), + self._story("TEST-4", 3, "Closed"), self._story("TEST-5", 5, "Closed"), + self._story("TEST-6", 2, "Closed"), self._story("TEST-7", 8, "Closed")]}), + "bugs": self._write(d, "bugs.json", {"bugs": []}), + "roster": self._write(d, "roster.json", {"members": [ + {"username": "aexample@example.com", "display_name": "Alex Example", "sp_target": 8}]}), + } + checks = os.path.join(d, "checks.json") + subprocess.run([sys.executable, os.path.join(self.BIN, "run-checks.py"), + *[x for k, v in files.items() for x in (f"--{k}", v)], + "--remaining-sprints", "2", "--version", "9.1", "--today", "2030-01-21", + "--output", checks, *extra], check=True, capture_output=True) + recs = self._write(d, "recs.json", {"headline": "Synthetic headline.", "decisions": []}) + out = os.path.join(d, "report") + r = subprocess.run([sys.executable, os.path.join(self.BIN, "assemble-report.py"), + "--checks", checks, "--recommendations", recs, + "--template", os.path.join(self.BIN, "..", "references", "release-planning-report-template.md"), + "--version", "9.1", "--today", "2030-01-21", "--first-sprint", "10", + "--last-sprint", "12", "--remaining-sprints", "2", "--total-dev-sprints", "2", + "--output", out], capture_output=True, text=True) + self.assertEqual(r.returncode, 0, r.stderr) + with open(out + ".md") as f: + return json.load(open(checks)), f.read() + + def test_report_renders_without_hidden_scope(self): + with tempfile.TemporaryDirectory() as d: + checks, report = self._run(d, ["--no-hidden-scope-estimate"]) + self.assertIsNone(checks["meta"]["gap_with_hidden_low"]) + self.assertNotIn("None", report) + self.assertNotIn("incl. hidden", report) + self.assertNotIn("Hidden scope", report) + self.assertNotIn("≈", report) + self.assertIn("| Scope (pointed) | Capacity | Gap |", report) + # The gap covers pointed work only: the verdict counts the unpointed stories, never sizes them. + self.assertIn("pointed scope fits with ~11 SP to spare; 2 unpointed items not counted.", report) + + def test_default_run_still_shows_hidden_scope(self): + with tempfile.TemporaryDirectory() as d: + checks, report = self._run(d, []) + self.assertIsNotNone(checks["meta"]["gap_with_hidden_low"]) + self.assertIn("incl. hidden", report) + + if __name__ == "__main__": unittest.main() diff --git a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py new file mode 100644 index 00000000..77932fb5 --- /dev/null +++ b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py @@ -0,0 +1,654 @@ +"""Tests for chai_jira_adapter.py — chai-bot query_jira text → transform input. + +The fixtures below are anonymized copies of real chai-bot output: +same structure, line layout and wording, with synthetic keys, names, summaries, +components, labels and dates. The repository is public: never paste real chai-bot +output, names or Jira keys into fixtures. Error and cut-off pages have not been +seen from chai-bot yet; their fixtures are assumptions. +""" + +import importlib.util +import json +import os +import sys +import tempfile +import unittest + +BIN = os.path.join(os.path.dirname(__file__), "..") +sys.path.insert(0, BIN) +import chai_jira_adapter as ad # noqa: E402 +from _jira_transforms import extract_blocked_by # noqa: E402 + + +def _load(name): + spec = importlib.util.spec_from_file_location(name.replace("-", "_"), os.path.join(BIN, f"{name}.py")) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +transform_sprint_issues = _load("transform-sprint-issues") +transform_features = _load("transform-features") +compute_velocity = _load("compute-velocity") + +ROSTER = {"contacts": [ + {"display_name": "Alex Example", "email": "aexample@redhat.com"}, + {"display_name": "Quinn Quality", "email": "qquality@redhat.com"}, +]} +VELOCITY_ROSTER = {"members": [ + {"username": "aexample@redhat.com", "display_name": "Alex Example", "sp_target": 8}, + {"username": "qquality@redhat.com", "display_name": "Quinn Quality", "sp_target": 8}, +]} + +SPRINT_10 = ("Test Sprint 10 (id 500, closed, board 1000, start 2030-01-07T12:00:00.000Z, " + "end 2030-01-28T12:00:00.000Z)") +SPRINT_11 = ("Test Sprint 11 (id 501, active, board 1000, start 2030-01-28T12:00:00.000Z, " + "end 2030-02-18T12:00:00.000Z)") +HINT = (" ... More issues match than the {n} shown (Jira does not say how many). To see the rest, raise " + "max_results up to 50, or narrow the JQL (status, component, assignee, or a created/updated date " + "range). To page, order by key (ORDER BY key DESC), put the filter in parentheses, and add " + "`AND key < ` after it for each next page.") + +# Sample 1: truncated page with sprint, story points and resolution date. +SAMPLE_TRUNCATED = f"""Showing the first 3 matching issues; more match: + [TEST-304] Synthetic check reports a false pass + Type: Bug | Status: Closed | Priority: Undefined | Assignee: Alex Example | Reporter: Alex Example | Resolution: Done + Created: 2030-02-04 | Updated: 2030-02-06 | Resolved: 2030-02-06 + Components: Component A + customfield_10020: {SPRINT_11} + customfield_10028: 2.0 + resolutiondate: 2030-02-06T17:41:00.327+0000 + [TEST-300] Stretch: synthetic CI coverage + Type: Story | Status: Code Review | Priority: Major | Assignee: Alex Example | Reporter: Alex Example + Created: 2030-02-02 | Updated: 2030-02-06 + Components: Component A | Parent: TEST-215 + customfield_10020: {SPRINT_11} + customfield_10028: 2.0 + parent: TEST-215 + [TEST-297] Investigate a synthetic replacement + Type: Story | Status: Closed | Priority: Undefined | Assignee: Jordan Sample | Reporter: Jordan Sample | Resolution: Done + Created: 2030-01-30 | Updated: 2030-02-03 | Resolved: 2030-02-03 + customfield_10020: {SPRINT_11} + customfield_10028: 3.0 + resolutiondate: 2030-02-03T11:32:36.842+0000 + +{HINT.format(n=3)} +""" + +# Sample 2: create-metadata preamble, a rejected field, Labels / Fix Versions, issuelinks not renderable. +SAMPLE_PREAMBLE = f"""Create metadata for FEAT (Synthetic Strategy Project) +Issue types available (1): Feature + +[Feature] +Requested fields: (none found for this issue type) +Unknown fields: QA Contact, SME + +Invalid extra_fields entries: QA Contact. Use Jira field IDs such as duedate or customfield_12310220, comma-separated. + +Showing the first 2 matching issues; more match: + [FEAT-854] Allow setting a synthetic limit through the API + Type: Feature | Status: Release Pending | Priority: Major | Assignee: Casey Owner | Reporter: Drew Filer + Created: 2030-01-01 | Updated: 2030-01-22 + Components: Component B | Labels: alpha, beta-work, gamma | Fix Versions: example-9.1 + issuelinks: (set, but query_jira cannot render this field's structure) + [FEAT-797] Simplify synthetic upgrades + Type: Feature | Status: Release Pending | Priority: Major | Assignee: Erin Lead | Reporter: Frankie Filer + Created: 2029-12-18 | Updated: 2030-01-19 + Components: Component C | Labels: delta, epsilon | Fix Versions: example-9.1 + issuelinks: (set, but query_jira cannot render this field's structure) + +{HINT.format(n=2)} +""" + +# Sample 3: complete single page, custom fields by id (QA Contact, SME). +SAMPLE_COMPLETE = """Found 1 issues (showing 1): + [FEAT-601] Synthetic docs refactor + Type: Initiative | Status: In Progress | Priority: Major | Assignee: Morgan Manager | Reporter: Pat Product + Created: 2029-11-11 | Updated: 2030-02-06 + Components: Component D | Labels: edge, no_core_payload, plan-label | Parent: INIT-171 + customfield_10470: Quinn Quality + customfield_10475: Alex Example +""" + +# Sample 4: custom fields requested (incl. Flagged) but unset: no lines at all. +SAMPLE_UNSET = f"""Showing the first 3 matching issues; more match: + [TEST-304] Synthetic check reports a false pass + Type: Bug | Status: Closed | Priority: Undefined | Assignee: Alex Example | Reporter: Alex Example | Resolution: Done + Created: 2030-02-04 | Updated: 2030-02-06 | Resolved: 2030-02-06 + Components: Component A + [TEST-300] Stretch: synthetic CI coverage + Type: Story | Status: Code Review | Priority: Major | Assignee: Alex Example | Reporter: Alex Example + Created: 2030-02-02 | Updated: 2030-02-06 + Components: Component A | Parent: TEST-215 + [TEST-297] Investigate a synthetic replacement + Type: Story | Status: Closed | Priority: Undefined | Assignee: Jordan Sample | Reporter: Jordan Sample | Resolution: Done + Created: 2030-01-30 | Updated: 2030-02-03 | Resolved: 2030-02-03 + +{HINT.format(n=3).replace("`", "")} +""" + +# Sample 5: issueLinkType = "is blocked by" candidates (key, summary, status). +SAMPLE_BLOCKED_CANDIDATES = f"""Showing the first 3 matching issues; more match: + [ALPHA-584] [QE] Migrate synthetic tests: remaining cases + Type: Task | Status: Closed | Priority: Undefined | Assignee: Quinn Quality | Reporter: Quinn Quality | Resolution: Done + Created: 2030-01-21 | Updated: 2030-01-29 | Resolved: 2030-01-29 + Parent: ALPHA-690 + [TEST-220] Synthetic calendar with trigger rules + Type: Story | Status: In Progress | Priority: Major | Assignee: Alex Example | Reporter: Alex Example + Created: 2030-01-11 | Updated: 2030-02-03 + Labels: TEST:Scrum | Parent: TEST-205 + [TEST-208] Synthetic foundation: persona and rules + Type: Task | Status: Closed | Priority: Major | Assignee: Alex Example | Reporter: Alex Example | Resolution: Done + Created: 2030-01-11 | Updated: 2030-02-01 | Resolved: 2030-02-01 + Labels: TEST:Scrum | Parent: TEST-205 + +{HINT.format(n=3)} +""" + +# Sample 6: get_jira_issue with links (direction and linked status). +SAMPLE_GET_ISSUE = """Latest Jira State: +[TEST-303] Synthetic docs: final re-assembly +Project: TEST +Assignee: Jordan Sample | Reporter: Jordan Sample +Date Created: 2030-02-04T09:37:39 +Date Modified: 2030-02-04T21:26:44 +Type: Story | Status: To Do | Priority: Major +Resolution: Unresolved +Components: Component D +Parent: TEST-112 | https://example.invalid/browse/TEST-112 +Linked Jira Issues: +- is blocked by: TEST-151 (Blocks) | status: Closed | https://example.invalid/browse/TEST-151 +- is blocked by: TEST-152 (Blocks) | status: To Do | https://example.invalid/browse/TEST-152 +- is blocked by: TEST-154 (Blocks) | status: To Do | https://example.invalid/browse/TEST-154 +- is blocked by: TEST-55 (Blocks) | status: In Progress | https://example.invalid/browse/TEST-55 +- blocks: TEST-26 (Blocks) | status: To Do | https://example.invalid/browse/TEST-26 +Attachments: none +URL: https://example.invalid/browse/TEST-303 + +Description: +Synthetic description text. + +Comments: none +""" + + +# Sample 7: no results, raw tool output as chai-bot returns it (wrapped JSON; no names). +SAMPLE_NO_RESULTS = '{"result":"No issues found for JQL: key = OCPEDGE-1 AND key = OCPEDGE-2"}' +SAMPLE_NO_RESULTS_JQL = "key = OCPEDGE-1 AND key = OCPEDGE-2" + + +def block(key, summary="Synthetic item", type_="Story", status="In Progress", assignee="Alex Example", + sp="3.0", sprints=SPRINT_11, extra=()): + """One issue block in chai-bot's layout (indented, combined lines).""" + resolution = " | Resolution: Done" if status == "Closed" else "" + lines = [f" [{key}] {summary}", + f" Type: {type_} | Status: {status} | Priority: Major | Assignee: {assignee} | " + f"Reporter: Sam Sample{resolution}", + " Created: 2030-01-02 | Updated: 2030-02-01", + " Components: Component A | Parent: TEST-1"] + if sprints: + lines.append(f" customfield_10020: {sprints}") + if sp is not None: + lines.append(f" customfield_10028: {sp}") + lines.extend(f" {e}" for e in extra) + return "\n".join(lines) + + +def page(keys, final=True, shown=None, **kw): + """A query_jira page: complete ("Found N issues (showing N):") or truncated (with the hint).""" + blocks = [block(k, **kw) for k in keys] + n = len(blocks) if shown is None else shown + if final: + return f"Found {n} issues (showing {n}):\n" + "\n".join(blocks) + "\n" + return (f"Showing the first {n} matching issues; more match:\n" + "\n".join(blocks) + + "\n\n" + HINT.format(n=n) + "\n") + + +def keys_desc(project, start, count): + return [f"{project}-{n}" for n in range(start, start - count, -1)] + + +class SampleTest(unittest.TestCase): + """The anonymized chai-bot samples parse field by field.""" + + def test_truncated_page_with_sprint_and_points(self): + p = ad.parse_page(SAMPLE_TRUNCATED, page_size=3) # the sample used max_results 3 + self.assertEqual((p["status"], p["final"], p["unknown"]), ("ok", False, [])) + out = ad.adapt_fetch({"page_size": 3, "pages": [{"text": SAMPLE_TRUNCATED}]}, ROSTER) + bug, story, closed = out["issues"] + self.assertEqual([i["key"] for i in out["issues"]], ["TEST-304", "TEST-300", "TEST-297"]) + self.assertEqual((bug["issue_type"], bug["status"], bug["resolution"]), + ({"name": "Bug"}, {"name": "Closed"}, {"name": "Done"})) + self.assertEqual(bug["components"], [{"name": "Component A"}]) + self.assertEqual(bug["customfield_10028"], 2.0) + self.assertEqual(bug["customfield_10020"][0]["id"], 501) + self.assertEqual(bug["resolutiondate"], "2030-02-06T17:41:00.327+0000") + self.assertEqual(story["components"], [{"name": "Component A"}]) # "| Parent:" not swallowed + self.assertEqual(story["parent"], {"key": "TEST-215", "issue_type": {"name": "Epic"}}) + self.assertIsNone(story["resolution"]) # no Resolution segment + self.assertNotIn("resolutiondate", story) + self.assertEqual(closed["assignee"], {"display_name": "Jordan Sample", "displayName": "Jordan Sample", + "name": "Jordan Sample"}) + # A truncated page on its own is not a complete fetch. + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("stopped early", out["fetch"]["problems"][0]) + + def test_preamble_rejected_field_and_lists(self): + p = ad.parse_page(SAMPLE_PREAMBLE) + self.assertEqual(p["rejected"], ["QA Contact"]) + self.assertEqual(p["unknown_fields"], ["QA Contact", "SME"]) + self.assertEqual(p["unknown"], []) # preamble and issuelinks lines are recognized + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_PREAMBLE}]}) + feat = out["issues"][0] + self.assertEqual(feat["labels"], ["alpha", "beta-work", "gamma"]) + self.assertEqual(feat["fixVersions"], [{"name": "example-9.1"}]) + self.assertEqual(feat["components"], [{"name": "Component B"}]) + self.assertEqual(feat["issuelinks"], []) + # A rejected field would silently zero a figure, so the fetch is incomplete. + self.assertIn("query_jira rejected field(s): QA Contact", out["fetch"]["problems"]) + self.assertIn("create metadata lists unknown field(s): QA Contact, SME", out["warnings"]) + + def test_complete_page_with_custom_fields_by_id(self): + out = ad.adapt_fetch({"order": "rank", "pages": [{"text": SAMPLE_COMPLETE}]}, ROSTER) + self.assertTrue(out["fetch"]["complete"]) + feat = out["issues"][0] + self.assertEqual(feat["labels"], ["edge", "no_core_payload", "plan-label"]) + self.assertEqual(feat["parent"], {"key": "INIT-171"}) # an initiative's parent is not an epic + self.assertEqual(feat["customfield_10470"]["email"], "qquality@redhat.com") + self.assertEqual(feat["customfield_10475"]["email"], "aexample@redhat.com") + row = transform_features.transform_feature(feat, rank=1) + self.assertEqual((row["qa_contact"], row["sme"], row["type"]), + ("Quinn Quality", "Alex Example", "Initiative")) + + def test_unset_custom_fields_produce_no_lines(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_UNSET}], "signals": ["flagged"]}, ROSTER) + self.assertEqual(out["skipped_signals"], []) # unset Flagged = not flagged, not unavailable + self.assertEqual(out["warnings"], []) + for issue in out["issues"]: + for field in ("customfield_10470", "customfield_10475", "customfield_10021", "customfield_10028"): + self.assertNotIn(field, issue) + row = transform_sprint_issues.transform_issue(issue, "2030-02-08") + self.assertEqual((row["flagged"], row["qa_contact"], row["sp"]), (False, None, 0)) + + def test_rejected_flagged_is_a_skipped_signal_only(self): + text = ("Invalid extra_fields entries: customfield_10021. Use Jira field IDs such as duedate or " + "customfield_12310220, comma-separated.\n\n" + page(["TEST-9", "TEST-8"])) + out = ad.adapt_fetch({"pages": [{"text": text}], "signals": ["flagged"]}, ROSTER) + self.assertEqual(out["skipped_signals"], [ad.SKIPPED_FLAGGED]) + self.assertTrue(out["fetch"]["complete"]) + + def test_blocked_candidates_page(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_BLOCKED_CANDIDATES}]}, ROSTER) + keys = [i["key"] for i in out["issues"]] + self.assertEqual(keys, ["ALPHA-584", "TEST-220", "TEST-208"]) + self.assertEqual(out["issues"][0]["summary"], "[QE] Migrate synthetic tests: remaining cases") + self.assertEqual(out["issues"][1]["labels"], ["TEST:Scrum"]) + self.assertEqual(out["issues"][1]["parent"]["key"], "TEST-205") + + def test_get_jira_issue_links(self): + links, found = ad.parse_issue_links(SAMPLE_GET_ISSUE) + self.assertTrue(found) + self.assertEqual([(l.get("inward_issue") or l.get("outward_issue"))["key"] for l in links], + ["TEST-151", "TEST-152", "TEST-154", "TEST-55", "TEST-26"]) + self.assertEqual(links[0]["inward_issue"]["status"], {"name": "Closed"}) + self.assertEqual(links[0]["type"], {"name": "Blocks", "inward": "is blocked by", "outward": "blocks"}) + self.assertIn("outward_issue", links[4]) + # The single pipeline rule reads these links: TEST-151 is Closed, the rest are open. + self.assertEqual(extract_blocked_by({"issuelinks": links}), ["TEST-152", "TEST-154", "TEST-55"]) + + +class PagingTest(unittest.TestCase): + def test_complete_header_single_page(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-9", "TEST-8"])}]}, ROSTER) + self.assertTrue(out["fetch"]["complete"]) + self.assertEqual([i["key"] for i in out["issues"]], ["TEST-9", "TEST-8"]) + + def test_truncated_page_then_complete_page(self): + first = page(keys_desc("TEST", 120, 50), final=False) + second = page(keys_desc("TEST", 70, 3)) + out = ad.adapt_fetch({"pages": [{"text": first}, {"text": second}]}) + self.assertTrue(out["fetch"]["complete"], out["fetch"]["problems"]) + self.assertEqual(out["fetch"]["fetched"], 53) + self.assertEqual(out["warnings"], []) + + def test_found_with_more_than_shown_is_incomplete(self): + text = "Found 80 issues (showing 2):\n" + "\n".join(block(k) for k in ["TEST-9", "TEST-8"]) + "\n" + p = ad.parse_page(text) + self.assertEqual((p["status"], p["final"]), ("incomplete", False)) + + def test_final_page_count_must_match_found_header(self): + text = "Found 5 issues (showing 5):\n" + "\n".join(block(k) for k in ["TEST-9", "TEST-8", "TEST-7"]) + "\n" + out = ad.adapt_fetch({"pages": [{"text": text}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("page lists 3 issues but its header says 5", out["fetch"]["problems"][0]) + + def test_page_truncated_inside_the_last_two_issues(self): + full = page(["TEST-9", "TEST-8", "TEST-7"]) + # Cut right after the last two issues' key lines: both field groups lost, count still 3. + lines = full.splitlines() # header, then 6 lines per issue: keys at lines 1, 7 and 13 + cut = "\n".join(lines[:7] + [lines[7], lines[13]]) + "\n" + self.assertEqual(len(ad._split_blocks(cut)[1]), 3) + out = ad.adapt_fetch({"pages": [{"text": cut}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("has no 'Type: … | Status: …' line", out["fetch"]["problems"][0]) + # Cut inside the second-to-last issue: the last issue is lost entirely. + cut2 = "\n".join(lines[:9]) + "\n" + out2 = ad.adapt_fetch({"pages": [{"text": cut2}]}) + self.assertFalse(out2["fetch"]["complete"]) + self.assertIn("page lists 2 issues but its header says 3", out2["fetch"]["problems"][0]) + + def test_type_status_line_must_be_present_not_filled(self): + ok = "Found 1 issues (showing 1):\n [TEST-9] Synthetic\n Type: | Status: To Do\n" + self.assertEqual(ad.parse_page(ok)["status"], "ok") + cut = "Found 1 issues (showing 1):\n [TEST-9] Synthetic\n Created: 2030-01-02\n" + self.assertEqual(ad.parse_page(cut)["status"], "incomplete") + + def test_full_page_without_hint_is_incomplete(self): + text = page(keys_desc("TEST", 120, 50), final=False).split("\n\n ... More issues match")[0] + "\n" + out = ad.adapt_fetch({"pages": [{"text": text}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("without the 'More issues match' paging hint", out["fetch"]["problems"][0]) + + def test_stopping_after_a_truncated_page_is_incomplete(self): + out = ad.adapt_fetch({"pages": [{"text": page(keys_desc("TEST", 120, 50), final=False)}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("stopped early", " ".join(out["fetch"]["problems"])) + + def test_failed_page_makes_fetch_incomplete(self): + out = ad.adapt_fetch({"pages": [{"text": page(keys_desc("TEST", 120, 50), final=False)}, + {"error": "tool error"}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("query_jira call failed", " ".join(out["fetch"]["problems"])) + + # Not observed from chai-bot yet (assumptions): a cut-off page and an error page. + def test_cut_off_page_lists_fewer_issues_than_header(self): + text = page(["TEST-9", "TEST-8"], shown=3) + out = ad.adapt_fetch({"pages": [{"text": text}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("header says 3", " ".join(out["fetch"]["problems"])) + + def test_complete_header_with_more_hint_is_incomplete(self): + text = page(["TEST-9", "TEST-8"]) + "\n" + HINT.format(n=2) + "\n" + self.assertEqual(ad.parse_page(text)["status"], "incomplete") + + def test_page_without_header_or_issues_is_an_error(self): + out = ad.adapt_fetch({"pages": [{"text": "Something went wrong"}]}) + self.assertFalse(out["fetch"]["complete"]) + + def test_found_zero_issues_is_complete_and_empty(self): + # The real empty form is "No issues found for JQL: ..." (see NoResultsTest); a + # "Found 0" header would also pass the header rule. Anything else is incomplete. + out = ad.adapt_fetch({"pages": [{"text": "Found 0 issues (showing 0):\n"}]}) + self.assertTrue(out["fetch"]["complete"]) + self.assertEqual(out["issues"], []) + self.assertFalse(ad.adapt_fetch({"pages": [{"text": "No issues found.\n"}]})["fetch"]["complete"]) + + def test_headerless_page_is_incomplete(self): + text = "\n".join(block(k) for k in ["TEST-3", "TEST-2"]) + out = ad.adapt_fetch({"pages": [{"text": text}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("no 'Found N issues", out["fetch"]["problems"][0]) + + def test_multi_project_paging_must_be_split_by_project(self): + first = page(keys_desc("BETA", 80, 50), final=False) + second = page(keys_desc("ALPHA", 20, 5)) + out = ad.adapt_fetch({"pages": [{"text": first}, {"text": second}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("several projects", " ".join(out["fetch"]["problems"])) + + def test_per_project_parts_are_complete(self): + parts = [{"project": "BETA", "pages": [{"text": page(keys_desc("BETA", 80, 50), final=False)}, + {"text": page(keys_desc("BETA", 30, 2))}]}, + {"project": "ALPHA", "pages": [{"text": page(["ALPHA-4", "ALPHA-2"])}]}] + out = ad.adapt_fetch({"parts": parts}) + self.assertTrue(out["fetch"]["complete"], out["fetch"]["problems"]) + self.assertEqual(out["fetch"]["fetched"], 54) + + def test_overlapping_pages_are_incomplete(self): + first = page(keys_desc("TEST", 120, 50), final=False) + second = page(["TEST-71", "TEST-60"]) # TEST-71 was already on page 1 + out = ad.adapt_fetch({"pages": [{"text": first}, {"text": second}]}) + self.assertFalse(out["fetch"]["complete"]) + + def test_rank_order_complete_only_on_one_final_page(self): + ok = ad.adapt_fetch({"order": "rank", "pages": [{"text": page(["TEST-3", "TEST-7"])}]}) + self.assertTrue(ok["fetch"]["complete"]) + self.assertEqual([i["key"] for i in ok["issues"]], ["TEST-3", "TEST-7"]) # fetch order kept + cut = ad.adapt_fetch({"order": "rank", + "pages": [{"text": page(keys_desc("TEST", 120, 50), final=False)}]}) + self.assertFalse(cut["fetch"]["complete"]) + self.assertIn("rank order not available", cut["fetch"]["problems"][0]) + + def test_page_jql(self): + self.assertEqual(ad.page_jql("project = TEST AND sprint = 5 ORDER BY priority ASC"), + "(project = TEST AND sprint = 5) ORDER BY key DESC") + self.assertEqual(ad.page_jql("sprint = 5", project="TEST", last_key="TEST-40"), + "(sprint = 5) AND project = TEST AND key < TEST-40 ORDER BY key DESC") + + +class NoResultsTest(unittest.TestCase): + """chai-bot's no-results output, wrapped as {"result": ...} or bare.""" + + def test_wrapped_no_results_with_matching_jql_is_complete_and_empty(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_NO_RESULTS, "jql": SAMPLE_NO_RESULTS_JQL}]}) + self.assertTrue(out["fetch"]["complete"], out["fetch"]["problems"]) + self.assertEqual((out["issues"], out["fetch"]["fetched"], out["warnings"]), ([], 0, [])) + + def test_bare_no_results_with_matching_jql_is_complete(self): + bare = json.loads(SAMPLE_NO_RESULTS)["result"] + p = ad.parse_page(bare, jql=" " + SAMPLE_NO_RESULTS_JQL + "\n") # surrounding whitespace ignored + self.assertEqual((p["status"], p["final"], p["blocks"]), ("ok", True, [])) + + def test_no_results_for_a_different_jql_is_incomplete(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_NO_RESULTS, "jql": "key = OCPEDGE-1"}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertIn("different JQL", out["fetch"]["problems"][0]) + + def test_no_results_without_the_sent_jql_is_incomplete(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_NO_RESULTS}]}) + self.assertFalse(out["fetch"]["complete"]) + + def test_wrapped_and_bare_pages_parse_the_same(self): + wrapped = json.dumps({"result": SAMPLE_COMPLETE}) + a = ad.adapt_fetch({"pages": [{"text": wrapped}]}, ROSTER) + b = ad.adapt_fetch({"pages": [{"text": SAMPLE_COMPLETE}]}, ROSTER) + self.assertTrue(a["fetch"]["complete"]) + self.assertEqual(a["issues"], b["issues"]) + self.assertEqual(ad.unwrap({"result": "x"}), "x") + self.assertEqual(ad.unwrap("{not json"), "{not json") + + +class LinkedSpikesCliTest(unittest.TestCase): + """CLI --linked-spikes: an incomplete per-feature fetch marks the output incomplete.""" + + def _write(self, d, name, obj): + path = os.path.join(d, name) + with open(path, "w") as f: + json.dump(obj, f) + return path + + def test_incomplete_linked_spike_fetch_is_recorded(self): + complete_spikes = ad.adapt_fetch({"pages": [{"text": page(["TEST-50"], type_="Spike", status="Closed", + summary="Refine FEAT-601")}]}) + cut_spikes = ad.adapt_fetch({"pages": [{"text": page(keys_desc("TEST", 120, 50), final=False)}]}) + self.assertFalse(cut_spikes["fetch"]["complete"]) + with tempfile.TemporaryDirectory() as d: + manifest = self._write(d, "m.json", {"order": "rank", "pages": [{"text": SAMPLE_COMPLETE}]}) + linked = self._write(d, "linked.json", {"FEAT-601": complete_spikes, "FEAT-602": cut_spikes}) + out_path = os.path.join(d, "out.json") + self.assertEqual(ad.main(["fetch", "--manifest", manifest, "--linked-spikes", linked, + "--output", out_path]), 0) + with open(out_path) as f: + out = json.load(f) + self.assertFalse(out["fetch"]["complete"]) + self.assertEqual(len(out["fetch"]["linked_spike_problems"]), 1) + self.assertTrue(out["fetch"]["linked_spike_problems"][0].startswith( + "FEAT-602: linked-spike fetch incomplete (fetch: last page says more issues match")) + # Spikes from complete fetches are still attached. + links = out["issues"][0]["issuelinks"] + self.assertEqual([link["inward_issue"]["key"] for link in links], ["TEST-50"]) + + def test_complete_linked_spike_fetches_keep_the_fetch_complete(self): + features = ad.adapt_fetch({"order": "rank", "pages": [{"text": SAMPLE_COMPLETE}]}) + empty = ad.adapt_fetch({"pages": [{"text": SAMPLE_NO_RESULTS, "jql": SAMPLE_NO_RESULTS_JQL}]}) + self.assertEqual(ad.attach_linked_spike_fetches(features, {"FEAT-601": empty}), []) + self.assertTrue(features["fetch"]["complete"]) + + +class FieldTest(unittest.TestCase): + def test_missing_lines_are_empty(self): + out = ad.adapt_fetch({"pages": [{"text": "Found 1 issues (showing 1):\n [TEST-6] Bare issue\n"}]}) + issue = out["issues"][0] + self.assertIsNone(issue["assignee"]) + self.assertEqual(issue["status"], {"name": ""}) + self.assertNotIn("created", issue) # left out, so transforms apply their defaults + self.assertNotIn("customfield_10028", issue) + self.assertEqual((issue["components"], issue["labels"], issue["issuelinks"]), ([], [], [])) + row = transform_sprint_issues.transform_issue(issue, "2030-02-05") + self.assertEqual((row["sp"], row["type"], row["assignee"]), (0, "Task", None)) + + def test_unknown_lines_are_counted_in_a_warning(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], extra=["Mystery: one | Another: two", + "free text"])}]}) + self.assertIn("ignored 3 unrecognized line(s); labels: Another, Mystery", out["warnings"][0]) + self.assertTrue(out["fetch"]["complete"]) + + def test_flagged_is_parsed_when_set(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], extra=["customfield_10021: Impediment"])}], + "signals": ["flagged"]}) + self.assertEqual(out["skipped_signals"], []) + self.assertTrue(transform_sprint_issues.transform_issue(out["issues"][0], "2030-02-05")["flagged"]) + + def test_multiple_sprints(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], sprints=f"{SPRINT_10}, {SPRINT_11}")}]}) + sprints = out["issues"][0]["customfield_10020"] + self.assertEqual([(s["id"], s["state"], s["endDate"]) for s in sprints], + [(500, "closed", "2030-01-28T12:00:00.000Z"), (501, "active", "2030-02-18T12:00:00.000Z")]) + + +class PeopleTest(unittest.TestCase): + def test_display_name_matches_roster_case_and_space_insensitively(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], assignee="alex EXAMPLE")}]}, ROSTER) + self.assertEqual(out["issues"][0]["assignee"]["email"], "aexample@redhat.com") + self.assertEqual(out["people"], {"matched": 1, "unmatched": []}) + + def test_unmatched_name_is_kept_not_unassigned(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], assignee="Robin Outsider")}]}, ROSTER) + assignee = out["issues"][0]["assignee"] + self.assertNotIn("email", assignee) + self.assertEqual(assignee["name"], "Robin Outsider") + self.assertEqual(out["people"]["unmatched"], ["Robin Outsider"]) + + def test_velocity_counts_team_work_matched_by_display_name(self): + text = "Found 3 issues (showing 3):\n" + "\n".join([ + block("TEST-9", status="Closed", sp="5", sprints=SPRINT_10), + block("TEST-8", status="Closed", sp="3", assignee="Robin Outsider", sprints=SPRINT_10, + extra=["customfield_10470: Quinn Quality"]), + block("TEST-7", status="Closed", sp="8", assignee="Robin Outsider", sprints=SPRINT_10), + ]) + "\n" + adapted = ad.adapt_fetch({"pages": [{"text": text}]}, ROSTER) + rows = [transform_sprint_issues.transform_issue(i, "2030-02-05") for i in adapted["issues"]] + vel = compute_velocity.compute_velocity( + [{"sprint_id": 500, "sprint_name": "Test Sprint 10", "issues": rows}], VELOCITY_ROSTER) + self.assertEqual(vel["team_velocity"], 8.0) # TEST-9 (assignee) + TEST-8 (QA Contact) + self.assertEqual(vel["unmatched_done_sp"], 8) # TEST-7 is not team-owned + + +class LinkTest(unittest.TestCase): + def test_blocked_only_when_a_blocker_is_open(self): + def blocked_by(text): + return extract_blocked_by({"issuelinks": ad.parse_issue_links(text)[0]}) + for status in ("Closed", "Verified", "Done", "Won't Fix"): + text = f"Linked Jira Issues:\n- is blocked by: TEST-20 (Blocks) | status: {status} | u\n" + self.assertEqual(blocked_by(text), [], status) + self.assertEqual(blocked_by("Linked Jira Issues:\n- is blocked by: TEST-20 (Blocks) | status: To Do | u\n"), + ["TEST-20"]) + self.assertEqual(blocked_by("Linked Jira Issues:\n- blocks: TEST-30 (Blocks) | status: New | u\n"), []) + + def test_wont_fix_only_blocker_is_not_blocked(self): + text = page(["TEST-41"]) + adapted = ad.adapt_fetch({"pages": [{"text": text}]}) + ad.attach_issue_links(adapted, {"TEST-41": "Linked Jira Issues:\n" + "- is blocked by: TEST-42 (Blocks) | status: Won't Fix | u\n"}) + row = transform_sprint_issues.transform_issue(adapted["issues"][0], "2030-02-05") + self.assertEqual(row["blocked_by"], []) + + def test_section_ends_at_next_field(self): + links, _ = ad.parse_issue_links(SAMPLE_GET_ISSUE + "\n- is blocked by: TEST-99 (Blocks) | status: New | u\n") + self.assertNotIn("TEST-99", [(l.get("inward_issue") or {}).get("key") for l in links]) + + def test_attached_links_feed_blocked_by(self): + adapted = ad.adapt_fetch({"pages": [{"text": page(["TEST-304", "TEST-303"])}]}, ROSTER) + problems = ad.attach_issue_links(adapted, {"TEST-303": SAMPLE_GET_ISSUE, "TEST-304": None}) + self.assertEqual(problems, ["TEST-304: get_jira_issue failed"]) + rows = {i["key"]: transform_sprint_issues.transform_issue(i, "2030-02-05") for i in adapted["issues"]} + self.assertEqual(rows["TEST-303"]["blocked_by"], ["TEST-152", "TEST-154", "TEST-55"]) + self.assertEqual(rows["TEST-304"]["blocked_by"], []) + + def test_missing_linked_section_is_a_problem(self): + adapted = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"])}]}) + self.assertEqual(ad.attach_issue_links(adapted, {"TEST-5": "Latest Jira State:\n[TEST-5] x\n"}), + ["TEST-5: no 'Linked Jira Issues' section"]) + + def test_linked_spikes_become_feature_spike_candidates(self): + features = ad.adapt_fetch({"order": "rank", "pages": [{"text": SAMPLE_COMPLETE}]}, ROSTER) + spikes = ad.adapt_fetch({"pages": [{"text": page(["TEST-50"], type_="Spike", status="Closed", + summary="Test plan for FEAT-601", + assignee="Quinn Quality")}]}, ROSTER) + ad.attach_linked_spikes(features, {"FEAT-601": spikes["issues"]}) + ad.attach_linked_spikes(features, {"FEAT-601": spikes["issues"]}) # no duplicate link + row = transform_features.transform_feature(features["issues"][0], rank=1) + self.assertEqual(len(row["spike_candidates"]), 1) + cand = row["spike_candidates"][0] + self.assertEqual((cand["key"], cand["status"], cand["assignee"], cand["resolution"]), + ("TEST-50", "Closed", "Quinn Quality", "Done")) + + +ROSTER_MD = """# Team Roster + +## Example Team + +| Name | GitHub | Role | Location | +|------|--------|------|----------| +| [Morgan Manager](https://rover.redhat.com/people/profile/mmanager) | mm | Manager, Engineering | Nowhere | +| [Alex Example](https://rover.redhat.com/people/profile/aexample) | ae | Senior Software Engineer | Nowhere | +| [Quinn Quality](https://rover.redhat.com/people/profile/qquality) | qq | Principal Software Quality Engineer | Nowhere | +| [Riley Reserved](https://rover.redhat.com/people/profile/rreserved) | rr | Principal Software Engineer | Nowhere | +| [Pat Product](https://rover.redhat.com/people/profile/pproduct) | pp | Principal Product Manager - Technical | Nowhere | +| [Sec Person](https://rover.redhat.com/people/profile/sperson) | sp | Principal Product Security Engineer | Nowhere | +| Nolink Person | np | Software Engineer | Nowhere | + +## Not counted for sprint capacity + +Team members who don't take work from the sprint board. + +- Riley Reserved +""" + + +class RosterTest(unittest.TestCase): + def test_not_counted_members_are_excluded(self): + r = ad.parse_team_roster(ROSTER_MD) + names = [m["display_name"] for m in r["members"]] + self.assertEqual(names, ["Alex Example", "Quinn Quality", "Nolink Person"]) + self.assertEqual(r["engineer_count"], 3) + self.assertEqual(r["not_counted"], ["Riley Reserved"]) + self.assertEqual(r["warnings"], []) + + def test_contacts_carry_rover_email(self): + r = ad.parse_team_roster(ROSTER_MD) + by_name = {c["display_name"]: c for c in r["contacts"]} + self.assertEqual(len(by_name), 7) + self.assertEqual(by_name["Morgan Manager"]["email"], "mmanager@redhat.com") + self.assertIsNone(by_name["Nolink Person"]["email"]) + self.assertEqual(r["members"][0]["username"], "aexample@redhat.com") + self.assertEqual(r["members"][2]["username"], "Nolink Person") + + def test_unknown_not_counted_entry_warns(self): + r = ad.parse_team_roster(ROSTER_MD + "- Nobody Here\n") + self.assertEqual(len(r["warnings"]), 1) + self.assertEqual(r["engineer_count"], 3) + + +if __name__ == "__main__": + unittest.main() diff --git a/plugins/edge-scrum/bin/tests/test_chai_report.py b/plugins/edge-scrum/bin/tests/test_chai_report.py new file mode 100644 index 00000000..1f1310a6 --- /dev/null +++ b/plugins/edge-scrum/bin/tests/test_chai_report.py @@ -0,0 +1,520 @@ +"""Tests for chai_report.py and the per-sprint availability in scrum_context.py. + +All releases, sprints, dates, keys and names are synthetic: the repository is +public. Do not add real Product Pages dates, names or Jira keys. +""" + +import datetime as dt +import os +import sys +import unittest +from importlib import import_module + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +import chai_jira_adapter as ad # noqa: E402 +import chai_report as cr # noqa: E402 +import scrum_context as sc # noqa: E402 +from tests.test_chai_jira_adapter import ROSTER_MD # noqa: E402 + +run_checks = import_module("run-checks") + +D = dt.date +BOARD = 1000 +FIRST_START = D(2030, 1, 7) # Monday; synthetic 3-week sprints, Test Sprint 10 onwards + + +def _sprints(count=6): + out = [] + for i in range(count): + start = FIRST_START + dt.timedelta(weeks=3 * i) + end = start + dt.timedelta(weeks=3) + out.append({"id": str(500 + i), "name": f"Test Sprint {10 + i}", "state": "closed", + "originBoardId": BOARD, "start_date": start.isoformat(), "end_date": end.isoformat()}) + return out + + +def _sprint(number): + start = FIRST_START + dt.timedelta(weeks=3 * (number - 10)) + return {"name": f"Test Sprint {number}", "number": number, "id": str(490 + number), + "start": start.isoformat(), "end": (start + dt.timedelta(weeks=3)).isoformat(), + "projected": False} + + +def _window(release, phase, refinement=True, branch_cut=True): + return {"release": release, "phase": phase, + "refinement_sprint": _sprint(10) if refinement else sc.NOT_SCHEDULED, + "branch_cut_sprint": _sprint(15) if branch_cut else sc.NOT_SCHEDULED, + "pencils_down_sprint": _sprint(14), "development_sprints_remaining": 2} + + +class ModeAndCapacityTest(unittest.TestCase): + def test_mode_by_sprint_week(self): + self.assertEqual(cr.sprint_mode(1), "capacity") + self.assertEqual(cr.sprint_mode(2), "mid-sprint") + self.assertEqual(cr.sprint_mode(3), "mid-sprint") + self.assertIsNone(cr.sprint_mode(None)) + + def test_capacity_with_availability(self): + cap = cr.sprint_capacity(80, 0.9) + self.assertEqual(cap["capacity_sp"], 72.0) + self.assertTrue(cap["pto_adjusted"]) + self.assertIn("availability 0.90", cap["label"]) + + def test_capacity_without_availability_is_not_pto_adjusted(self): + for avail in (None, sc.ROSTER_NOT_CONFIGURED, sc.NOT_AVAILABLE, sc.OUTSIDE_CALENDAR_WINDOW): + cap = cr.sprint_capacity(80, avail) + self.assertEqual(cap["capacity_sp"], 80) + self.assertFalse(cap["pto_adjusted"]) + self.assertEqual(cap["label"], "not PTO-adjusted") + + def test_capacity_without_velocity_is_not_estimated(self): + cap = cr.sprint_capacity(None, 0.9) + self.assertIsNone(cap["capacity_sp"]) + + def test_release_capacity_mixed_availability(self): + sprints = [{"number": 13, "availability": 0.8}, {"number": 14, "availability": 1.0}, + {"number": 15, "availability": sc.OUTSIDE_CALENDAR_WINDOW}] + cap = cr.release_capacity(50, sprints) + self.assertEqual(cap["capacity_sp"], 140) # 50 × (0.8 + 1 + 1) + self.assertEqual(cap["availability_csv"], "0.8,1,1") + self.assertEqual(cap["not_adjusted"], [15]) + self.assertIn("not PTO-adjusted for sprint(s) 15", cap["label"]) + full = cr.release_capacity(50, [{"number": 13, "availability": 0.5}]) + self.assertEqual((full["capacity_sp"], full["pto_adjusted"]), (25, True)) + none = cr.release_capacity(50, [{"number": 13, "availability": None}]) + self.assertEqual(none["label"], "not PTO-adjusted") + + +class SectionSelectionTest(unittest.TestCase): + def test_phase_selects_section_kind(self): + cases = {"refinement": "refinement", "development": "release-health", + "pencils-down sprint": "release-health", "pre-branch-cut": "release-health"} + for phase, kind in cases.items(): + sections, missing = cr.select_sections({}, [_window("9.1", phase)]) + self.assertEqual([s["kind"] for s in sections], [kind], phase) + self.assertEqual(missing, []) + self.assertEqual(sections[0]["pencils_down"], phase == "pencils-down sprint") + + def test_two_releases_in_progress_get_their_own_sections(self): + context = {"release_in_progress": [{"release": "9.1", "phase": "pre-branch-cut"}, + {"release": "9.2", "phase": "refinement"}]} + sections, missing = cr.select_sections( + context, [_window("9.2", "refinement"), _window("9.1", "pre-branch-cut")]) + self.assertEqual([(s["release"], s["kind"]) for s in sections], + [("9.1", "release-health"), ("9.2", "refinement")]) + self.assertEqual(missing, []) + + def test_no_release_in_progress(self): + sections, missing = cr.select_sections({"release_in_progress": sc.NOT_AVAILABLE}, []) + self.assertEqual(sections, []) + self.assertEqual(missing, [{"what": "Release section", "why": "no release in progress"}]) + + def test_not_started_and_finished_releases_are_skipped(self): + sections, missing = cr.select_sections( + {}, [_window("9.0", "finished"), _window("9.3", "not started")]) + self.assertEqual(sections, []) + self.assertEqual(missing[0]["why"], "no release in progress") + + def test_unscheduled_unknown_and_unplaceable_releases_are_missing(self): + sections, missing = cr.select_sections({}, [ + _window("9.1", "development"), + {"release": "9.2", "phase": sc.NOT_SCHEDULED}, + {"release": "9.3", "phase": "not available"}, + _window("9.4", "development", branch_cut=False)]) + self.assertEqual([s["release"] for s in sections], ["9.1"]) + self.assertEqual([(m["what"], m["why"]) for m in missing], [ + ("9.2 release section", "no Product Pages schedule yet"), + ("9.3 release section", "phase unknown (not available)"), + ("9.4 release section", "a release milestone could not be placed on a sprint")]) + + def test_in_progress_entry_without_window_uses_context(self): + context = {"release_in_progress": [{"release": "9.1", "phase": "development"}]} + sections, missing = cr.select_sections(context, []) + self.assertEqual(sections, []) + self.assertIn("could not be placed", missing[0]["why"]) + + +class ScrumContextAvailabilityTest(unittest.TestCase): + def _data(self, today, **extra): + data = {"today": today, "board_id": BOARD, "sprints": _sprints(), + "absences": [{"person_id": "p1", "start": "2030-01-28", "end": "2030-02-01"}], + "engineer_count": 2} + data.update(extra) + return data + + def test_current_sprint_availability(self): + # Test Sprint 11 runs 2030-01-28 .. 2030-02-18: 15 working days x 2 people, 5 off. + out = sc.build_context(self._data("2030-01-29")) + self.assertEqual(out["current_sprint"]["number"], 11) + self.assertEqual(out["current_sprint_availability"], round(1 - 5 / 30, 2)) + self.assertEqual(out["sprint_availability"][0]["number"], 11) + + def test_sprints_up_to_branch_cut(self): + schedules = {"9.1": [{"name": "9.1 Development Branch opens", "date": "2030-01-07"}, + {"name": "Pencils Down / Stabilization", "date": "2030-03-11"}, + {"name": "Master Branch Open for 9.2", "date": "2030-03-25"}]} + out = sc.build_context(self._data("2030-01-29", release="9.1", schedules=schedules, + absence_window={"start": "2030-01-08", "end": "2030-03-10"})) + self.assertEqual(out["release"]["branch_cut_sprint"]["number"], 13) + by_number = {s["number"]: s["availability"] for s in out["sprint_availability"]} + self.assertEqual(sorted(by_number), [11, 12, 13]) + self.assertEqual(by_number[12], 1.0) + # A sprint running past the calendar window read is not estimated. + self.assertEqual(by_number[13], sc.OUTSIDE_CALENDAR_WINDOW) + + def test_without_roster_availability_is_not_available(self): + out = sc.build_context(self._data("2030-01-29", engineer_count=None)) + self.assertEqual(out["current_sprint_availability"], sc.ROSTER_NOT_CONFIGURED) + self.assertEqual(cr.sprint_capacity(60, out["current_sprint_availability"])["label"], + "not PTO-adjusted") + + def test_not_counted_member_is_excluded_from_engineer_count(self): + roster = ad.parse_team_roster(ROSTER_MD) + # 3 counted engineers (a "Not counted" principal engineer is excluded), one off 5 days. + out = sc.build_context(self._data("2030-01-29", engineer_count=roster["engineer_count"])) + self.assertEqual(out["current_sprint_availability"], round(1 - 5 / 45, 2)) + + +ISSUES = { + "total_sp": 30, "total_done_sp": 4, "total_remaining_sp": 26, "total_issues": 4, + "issues": [ + {"key": "TEST-1", "type": "Story", "sp": 13, "assignee_display": "Alex Example", "epic_key": "TEST-100", + "has_ac": True, "blocked_by": ["TEST-9"], "labels": [], "stale": False, "flagged": False}, + {"key": "TEST-2", "type": "Story", "sp": 13, "assignee_display": "Alex Example", "epic_key": "No Epic", + "has_ac": True, "blocked_by": [], "labels": [], "stale": True, "flagged": False}, + {"key": "TEST-3", "type": "Bug", "sp": 0, "assignee_display": "Quinn Quality", "epic_key": "TEST-100", + "has_ac": False, "blocked_by": [], "labels": [], "stale": False, "flagged": False}, + {"key": "TEST-4", "type": "Task", "sp": 4, "assignee_display": "Unassigned", "epic_key": "TEST-100", + "has_ac": True, "blocked_by": [], "labels": [], "stale": False, "flagged": False}, + ]} +SPRINT = {"name": "Test Sprint 11", "number": 11, "start": "2030-01-28", "end": "2030-02-18", "week": 1} +CHECKS = { + "meta": {"total_remaining_sp": 120, "total_capacity_sp": 90, "capacity_basis": "velocity", + "team_velocity": 45.0, "remaining_sprints": 2, "gap_sp": -30, + "hidden_scope_low": 10, "hidden_scope_high": 25, "total_unpointed_stories": 4, "unpointed_bugs": 2}, + "capacity": [{"person": "aexample@redhat.com", "display_name": "Alex Example", "assigned_sp": 70, + "status": "ABOVE_GUIDELINE", "in_roster": True}], + "cut_line": [{"feature_key": "FEAT-1", "sme": "Morgan Manager", "fits": "fits"}, + {"feature_key": "FEAT-2", "sme": "Alex Example", "fits": "partial"}, + {"feature_key": "FEAT-3", "sme": "None", "fits": "over"}], + "composite": [{"feature_key": "FEAT-1", "sme": "Morgan Manager", "composite_risk": "HIGH"}], + "timeline": [{"feature_key": "FEAT-2", "sme": "Alex Example", "risk": "HIGH"}], + "process_gaps": {"roster_drift": [{"person": "Robin Outsider", "display_name": "Robin Outsider", + "assigned_sp": 12}]}, +} + + +class TeamLevelOutputTest(unittest.TestCase): + def _report(self, mentions=None): + sprint = cr.sprint_health(ISSUES, SPRINT, "2030-01-29", "capacity", velocity=24, availability=0.9, + skipped_signals=[ad.SKIPPED_FLAGGED]) + section = {"release": "9.1", "phase": "pencils-down sprint", "pencils_down": True} + release = cr.release_health(CHECKS, section, cr.release_capacity(45, [{"number": 14, "availability": 1}, + {"number": 15, "availability": 1}])) + return sprint, release, cr.build_report(sprint, [release], [{"what": "9.2 release section", + "why": "no Product Pages schedule yet"}], + mentions) + + def test_capacity_mode_figures(self): + sprint, _, _ = self._report() + self.assertEqual(sprint["capacity"]["capacity_sp"], 21.6) + self.assertEqual(sprint["unpointed"], ["TEST-3"]) + self.assertEqual(sprint["no_epic"], ["TEST-2"]) + self.assertEqual(sprint["missing_ac"], []) # bugs are exempt from AC + self.assertEqual(sprint["by_type"]["Story"], {"issues": 2, "sp": 26}) + self.assertIn("8.4 SP over", sprint["risks"][0]["text"]) + + def test_report_has_no_per_person_data(self): + _, release, report = self._report() + text = "\n".join(report["top_post"] + report["replies"]) + self.assertNotIn("ABOVE_GUIDELINE", text) + self.assertNotIn("Robin Outsider", text) # roster drift is a count + self.assertNotIn("70", text) # a person's assigned SP + self.assertNotIn("Load by Person", text) + for name in ("Alex Example", "Quinn Quality", "Morgan Manager"): + for line in text.splitlines(): + if name in line: + self.assertRegex(line, r"^\d+\. .*\(owner: " + name + r"\)$") + self.assertNotIn("capacity", release) # no per-person rows carried through + self.assertEqual(release["roster_drift"], {"people": 1, "sp": 12}) + + def test_top_post_is_three_lines_with_action_count(self): + _, release, report = self._report() + top = report["top_post"] + self.assertEqual(len(top), 3) + self.assertTrue(top[0].startswith("🔴 Edge weekly health: Test Sprint 11 (week 1, capacity)")) + self.assertIn("9.1 pencils-down sprint", top[0]) + self.assertTrue(top[1].startswith("Biggest risk: Sprint committed 30 SP vs capacity 21.6 SP")) + self.assertEqual(top[2], f"{report['action_count']} items need action") + self.assertEqual(release["below_cut_line"], ["FEAT-2", "FEAT-3"]) + self.assertEqual(release["features_at_risk"], ["FEAT-1", "FEAT-2"]) + + def test_owner_mentions_and_fallbacks(self): + _, _, report = self._report(mentions={"Alex Example": "<@U000TEST1>"}) + actions = next(r for r in report["replies"] if r.startswith("*Action items*")) + self.assertIn("Point: TEST-3 (owner: Quinn Quality)", actions) + self.assertIn("Link to an epic: TEST-2 (owner: <@U000TEST1>)", actions) + self.assertIn("decide scope for FEAT-3 (below the cut line) (owner: no owner set)", actions) + self.assertIn("review plan for FEAT-1 (at risk) (owner: Morgan Manager)", actions) + + def test_left_out_lists_missing_sections_and_signals(self): + _, _, report = self._report() + left = next(r for r in report["replies"] if r.startswith("*Left out*")) + self.assertIn("9.2 release section: no Product Pages schedule yet", left) + self.assertIn("flagged signal: query_jira rejected customfield_10021", left) + self.assertIn("capacity from 1 assignee(s) not on the roster", left) + + def test_rank_unavailable_leaves_cut_line_out(self): + release = cr.release_health(CHECKS, {"release": "9.1", "phase": "development"}, rank_available=False) + self.assertEqual(release["below_cut_line"], []) + self.assertIn("cut line", release["left_out"][0]["what"]) + + def test_roster_fallback_capacity_is_not_reported(self): + checks = dict(CHECKS, meta=dict(CHECKS["meta"], capacity_basis="roster_fallback")) + release = cr.release_health(checks, {"release": "9.1", "phase": "development"}) + self.assertIsNone(release["capacity_sp"]) + self.assertEqual((release["below_cut_line"], release["features_at_risk"], release["risks"]), ([], [], [])) + self.assertEqual(release["left_out"][0]["why"], "velocity not available") + lines = "\n".join(cr._section_lines(dict(release, kind="release-health"))) + self.assertIn("capacity not available", lines) + + def test_nothing_to_flag(self): + clean = {"total_sp": 20, "total_issues": 1, "issues": [ + {"key": "TEST-1", "type": "Story", "sp": 20, "assignee_display": "Alex Example", + "epic_key": "TEST-100", "has_ac": True}]} + sprint = cr.sprint_health(clean, SPRINT, "2030-01-29", "capacity", velocity=24, availability=1.0) + report = cr.build_report(sprint, []) + self.assertEqual(report["top_post"][1], + "No risks this week: committed 20 SP in 1 issues; capacity 24 SP " + "(velocity 24 × availability 1.00)") + self.assertEqual(report["top_post"][2], "0 items need action") + self.assertTrue(report["top_post"][0].startswith("🟢")) + + +class MidSprintTest(unittest.TestCase): + def test_burndown_and_blockers(self): + sprint = dict(SPRINT, week=2) + out = cr.sprint_health(ISSUES, sprint, "2030-02-07", "mid-sprint", + skipped_signals=[ad.SKIPPED_FLAGGED]) + b = out["burndown"] + self.assertEqual((b["days_elapsed"], b["total_days"]), (11, 22)) + self.assertEqual(b["expected_done_sp"], 15.0) + self.assertEqual(b["status"], "Behind") + self.assertEqual([x["key"] for x in out["blockers"]], ["TEST-1", "TEST-2"]) + self.assertNotIn("owner", out["blockers"][0]) + + def test_done_issues_are_not_listed_as_blocked(self): + issues = dict(ISSUES, issues=[dict(i, status="Closed") if i["key"] == "TEST-1" else i + for i in ISSUES["issues"]]) + out = cr.sprint_health(issues, dict(SPRINT, week=2), "2030-02-07", "mid-sprint") + self.assertEqual([x["key"] for x in out["blockers"]], ["TEST-2"]) + self.assertNotIn("Unblock TEST-1", " ".join(a["text"] for a in out["actions"])) + + def test_incomplete_links_leave_blocked_signal_out(self): + out = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", + links_complete=False) + self.assertEqual([x["key"] for x in out["blockers"]], ["TEST-2"]) # stale only + self.assertIn("blocked-by links", [x["what"] for x in out["left_out"]]) + + +class RefinementGapsTest(unittest.TestCase): + def test_spike_gaps_and_owners(self): + features = {"features": [ + {"key": "FEAT-1", "sme": "Alex Example", "qa_contact": "Quinn Quality"}, + {"key": "FEAT-2", "sme": "None", "qa_contact": "unassigned_jira"}, + {"key": "FEAT-3", "sme": "Alex Example", "qa_contact": "Quinn Quality"}]} + base = {"spike_key": None, "spike_status": "Missing", "spike_missing": True, "spike_on_epic": False, + "epics_appear_refined": False, "spike_in_ref_sprint": False, "spike_overdue": False, + "test_plan_check_skipped": False, "test_plan_spike_missing": True, + "test_plan_spike_overdue": False, "test_plan_spike_key": None} + spikes = {"spike_map": { + "FEAT-1": base, + "FEAT-2": dict(base, test_plan_check_skipped=True), + "FEAT-3": dict(base, spike_key="TEST-60", spike_status="In Progress", spike_missing=False, + spike_in_ref_sprint=False, test_plan_spike_missing=False, + test_plan_spike_key="TEST-61", test_plan_spike_status="Closed")}} + section = {"release": "9.2", "phase": "refinement", "window": {"refinement_sprint": _sprint(16)}} + out = cr.refinement_gaps(features, spikes, section) + self.assertEqual(out["missing_refinement_spike"], ["FEAT-1", "FEAT-2"]) + self.assertEqual(out["missing_test_plan_spike"], ["FEAT-1"]) + self.assertEqual(out["spike_not_in_refinement_sprint"], ["FEAT-3"]) + owners = {a["text"]: a["owner"] for a in out["actions"]} + self.assertEqual(owners["9.2: create a refinement spike that blocks FEAT-1 in sprint 16"], "Alex Example") + self.assertIsNone(owners["9.2: create a refinement spike that blocks FEAT-2 in sprint 16"]) + self.assertEqual(owners["9.2: create a test plan spike that blocks FEAT-1"], "Quinn Quality") + self.assertEqual(owners["9.2: move refinement spike TEST-60 (FEAT-3) to sprint 16"], "Alex Example") + + +class NoEstimateTest(unittest.TestCase): + """Decision 0018: the report counts unpointed work and never estimates its SP.""" + + def test_report_has_unpointed_count_and_no_sp_range(self): + release = cr.release_health(CHECKS, {"release": "9.1", "phase": "development"}, + cr.release_capacity(45, [{"number": 14, "availability": 1}])) + sprint = cr.sprint_health(ISSUES, SPRINT, "2030-01-29", "capacity", velocity=24, availability=0.9) + report = cr.build_report(sprint, [release]) + text = "\n".join(report["top_post"] + report["replies"]) + self.assertIn("6 unpointed items, not counted in scope", text) + self.assertNotRegex(text, r"\d+(\.\d+)?\s*[–-]\s*\d+(\.\d+)?\s*SP") + self.assertNotIn("about", text) + self.assertNotIn("hidden_scope", release) + + def test_run_checks_can_skip_the_estimator(self): + features = [{"key": "F-1", "all_stories": [ + {"key": "S-1", "type": "Story", "status": "New", "sp": 0, "assignee": "a"}, + {"key": "S-2", "type": "Bug", "status": "New", "sp": 0, "assignee": None}, + {"key": "S-3", "type": "Story", "status": "Closed", "sp": 5, "assignee": "a"}]}] + hidden = run_checks.unestimated_hidden_scope(features, {"F-1"}) + self.assertEqual((hidden["unpointed_open"], hidden["unpointed_bugs"], hidden["estimate_low"]), (1, 1, None)) + estimated = run_checks.estimate_hidden_scope(features, {"F-1"}) + self.assertEqual(estimated["unpointed_open"], 1) + self.assertIsNotNone(estimated["estimate_low"]) # Claude Code path unchanged + + +class CurrentSprintCapacityTest(unittest.TestCase): + """Release capacity counts the current sprint for its remaining working days only.""" + + CURRENT = {"number": 11, "start": "2030-01-28", "end": "2030-02-18", "availability": 0.8} # 15 working days + NEXT = {"number": 12, "start": "2030-02-18", "end": "2030-03-11", "availability": 1.0} + + def _cap(self, today): + return cr.release_capacity(60, [self.CURRENT, self.NEXT], today=today) + + def test_week_1_monday_counts_the_whole_sprint(self): + cap = self._cap("2030-01-28") + self.assertEqual(cap["availability_csv"], "0.8,1") + self.assertEqual(cap["capacity_sp"], 108) # 60 × (0.8 × 15/15 + 1) + self.assertIsNone(cap["current_sprint"]) + + def test_week_3_counts_the_remaining_days(self): + cap = self._cap("2030-02-13") # Wednesday of week 3: Wed, Thu, Fri left + self.assertEqual(cap["current_sprint"], {"number": 11, "share": 0.2, "days_left": 3, "days": 15}) + self.assertEqual(cap["availability_csv"], "0.16,1") + self.assertEqual(cap["capacity_sp"], 70) # 60 × (0.8 × 3/15 + 1) = 69.6 + self.assertIn("current sprint 3 of 15 working days left", cap["label"]) + + def test_last_day_counts_one_working_day(self): + cap = self._cap("2030-02-15") # Friday before the Monday end day + self.assertEqual((cap["current_sprint"]["days_left"], cap["availability_csv"]), (1, "0.0533,1")) + self.assertEqual(cap["capacity_sp"], 63) # 60 × (0.8 × 1/15 + 1) = 63.2 + + def test_weekend_before_the_end_day_counts_nothing(self): + cap = self._cap("2030-02-17") # Sunday: no working day left in the current sprint + self.assertEqual((cap["current_sprint"]["days_left"], cap["capacity_sp"]), (0, 60)) + + +class BurndownWordingTest(unittest.TestCase): + def _risk(self, today, done): + issues = dict(ISSUES, total_done_sp=done, total_remaining_sp=30 - done) + out = cr.sprint_health(issues, dict(SPRINT, week=3), today, "mid-sprint") + return out["risks"][0]["text"] if out["risks"] else None + + def test_exact_biggest_risk_string(self): + self.assertEqual(self._risk("2030-02-07", 4), + "Burndown behind: 4 of 30 SP done, 15 expected; " + "projected to finish 61 days after the sprint ends") + + def test_small_overrun_is_rounded_up_and_singular(self): + # 21 of 22 days elapsed, 1 left; 20 done → 20/21 SP/day, 10 SP left need 10.5 days → 10 late. + self.assertIn("projected to finish 10 days after", self._risk("2030-02-17", 20)) + # 11 elapsed, 11 left; 14 done → 16 SP left at 14/11 SP/day need 12.6 days → 2 days late. + self.assertIn("projected to finish 2 days after", self._risk("2030-02-07", 14)) + # 11 elapsed; 13 done → 17 SP left need 14.4 days, 3.4 over → 4 days (never "3.4" or "0 days"). + self.assertIn("projected to finish 4 days after", self._risk("2030-02-07", 13)) + + +class CodeRabbitFixesTest(unittest.TestCase): + def test_usable_velocity(self): + ok = {"team_velocity": 80.3, "sprint_count": 3, "skipped_no_sprint_data": 0} + self.assertEqual(cr.usable_velocity(ok), (80.3, None)) + self.assertEqual(cr.usable_velocity(dict(ok, skipped_no_sprint_data=2)), + (None, "2 done team issue(s) had no sprint or resolution data")) + self.assertEqual(cr.usable_velocity(dict(ok, sprint_count=2))[0], None) + self.assertEqual(cr.usable_velocity(dict(ok, team_velocity=0)), (None, "velocity not measured")) + self.assertEqual(cr.usable_velocity(None), (None, "velocity not measured")) + + def test_velocity_reason_is_carried_into_left_out(self): + _, reason = cr.usable_velocity({"team_velocity": 70.0, "sprint_count": 2}) + sprint = cr.sprint_health(ISSUES, SPRINT, "2030-01-29", "capacity", velocity=None, + velocity_reason=reason) + self.assertIn({"what": "sprint capacity", "why": "only 2 closed sprint(s) measured, 3 needed"}, + sprint["left_out"]) + checks = dict(CHECKS, meta=dict(CHECKS["meta"], capacity_basis="roster_fallback")) + release = cr.release_health(checks, {"release": "9.1", "phase": "development"}, velocity_reason=reason) + self.assertEqual(release["left_out"][0]["why"], "only 2 closed sprint(s) measured, 3 needed") + # Without a reason, the previous text is kept. + plain = cr.sprint_health(ISSUES, SPRINT, "2030-01-29", "capacity", velocity=None) + self.assertIn({"what": "sprint capacity", "why": "velocity not measured"}, plain["left_out"]) + self.assertEqual(cr.release_health(checks, {"release": "9.1", "phase": "development"})["left_out"][0]["why"], + "velocity not available") + + def test_select_sections_accepts_full_scrum_context_results(self): + sections, missing = cr.select_sections({}, [{"today": "2030-02-01", "release": _window("9.1", "development")}]) + self.assertEqual([s["release"] for s in sections], ["9.1"]) + self.assertEqual(missing, []) + + def test_assigned_is_active_work_per_law_07(self): + base = {"type": "Bug", "sp": 1, "assignee_display": "Alex Example", "epic_key": "TEST-100", + "blocked_by": [], "labels": [], "flagged": False, "stale": False, "updated": "2030-01-29"} + issues = {"total_sp": 3, "total_done_sp": 0, "total_remaining_sp": 3, "issues": [ + dict(base, key="BUG-1", status="ASSIGNED"), + dict(base, key="BUG-2", status="Code Review"), # not a law 07 state: not counted + dict(base, key="BUG-3", status="ASSIGNED", updated="2030-02-06")]} + out = cr.sprint_health(issues, dict(SPRINT, week=2), "2030-02-07", "mid-sprint") + self.assertEqual(out["blockers"], [{"key": "BUG-1", "reasons": ["no update for more than 5 business days"]}]) + + def test_goal_rating_is_carried_into_the_reply(self): + sprint = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", + goal={"rating": "🟡", "note": "goal work stalled on TEST-2"}) + reply = cr.build_report(sprint, [])["replies"][0] + self.assertIn("• Sprint goal: 🟡 goal work stalled on TEST-2", reply) + none = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", + goal={"rating": None, "note": "no sprint goal set"}) + self.assertIn("• Sprint goal: no sprint goal set", cr.build_report(none, [])["replies"][0]) + + def test_build_report_rejects_section_descriptors(self): + sections, _ = cr.select_sections({}, [_window("9.1", "development")]) + with self.assertRaises(ValueError): + cr.build_report(None, sections) + + +class UnknownSprintModeTest(unittest.TestCase): + def test_unknown_week_leaves_sprint_health_out(self): + for week in (None, "three"): + mode = cr.sprint_mode(week) + self.assertIsNone(mode) + out = cr.sprint_health(ISSUES, dict(SPRINT, week=week), "2030-02-07", mode) + self.assertNotIn("burndown", out) + self.assertNotIn("capacity", out) + self.assertEqual((out["risks"], out["actions"]), ([], [])) + report = cr.build_report(out, []) + text = "\n".join(report["top_post"] + report["replies"]) + self.assertIn("• Sprint health: sprint week unknown", text) + self.assertNotIn("None mode", text) + self.assertNotIn("Sprint health: Test Sprint", text) + self.assertTrue(report["top_post"][0].endswith("sprint data not available")) + + +class TeamOwnerTest(unittest.TestCase): + ROLE_DOC = "# Scrum Master Responsibilities\n\n## Assignment\n\nAssigned by management.\n" + + def test_team_actions_go_to_the_scrum_master(self): + sprint = cr.sprint_health(ISSUES, SPRINT, "2030-01-29", "capacity", velocity=24, availability=0.9) + with_owner = cr.build_report(sprint, [], team_owner="Sam Scrum") + actions = next(r for r in with_owner["replies"] if r.startswith("*Action items*")) + self.assertIn("Re-plan the sprint: 8.4 SP over capacity (owner: Sam Scrum)", actions) + self.assertIn("Point: TEST-3 (owner: Quinn Quality)", actions) # issue owners unchanged + self.assertIn("Link to an epic: TEST-2 (owner: Alex Example)", actions) + without = next(r for r in cr.build_report(sprint, [])["replies"] if r.startswith("*Action items*")) + self.assertIn("Re-plan the sprint: 8.4 SP over capacity (owner: no owner set)", without) + + def test_find_scrum_master(self): + self.assertIsNone(ad.find_scrum_master(ROSTER_MD, self.ROLE_DOC)) # edge-context names no one today + roster = ROSTER_MD.replace("| Senior Software Engineer |", "| Senior Software Engineer, Scrum Master |") + self.assertEqual(ad.find_scrum_master(roster, self.ROLE_DOC), "Alex Example") + doc = self.ROLE_DOC + "\n**Current Scrum Master**: [Quinn Quality](https://example.invalid/q)\n" + self.assertEqual(ad.find_scrum_master(ROSTER_MD, doc), "Quinn Quality") + + +if __name__ == "__main__": + unittest.main() diff --git a/plugins/edge-scrum/chai/release-health/SKILL.md b/plugins/edge-scrum/chai/release-health/SKILL.md new file mode 100644 index 00000000..ea1178e1 --- /dev/null +++ b/plugins/edge-scrum/chai/release-health/SKILL.md @@ -0,0 +1,231 @@ +--- +name: release-health +description: Read-only coordinator skill that reports team-level health for one release in its current phase — refinement and test-plan spike gaps in the refinement sprint; capacity vs scope, cut line and features at risk from development through pre-branch-cut +--- + +# Release Health (chai-bot coordinator) + +This playbook is for the **chai-bot coordinator**, not for Claude Code. For one +release (`X.Y`) it reports the release section of the weekly health report +(edge-context decision 0018), chosen by the release's phase. It is **read-only** +and asks no questions; there are no sub-agents. Output is **team level only**: +the per-person "above the guideline" flags of release-planning are dropped. + +## Tools + +| Tool | Used for | +|------|----------| +| `scrum-context` skill | The release's window, phase and per-sprint availability | +| `list_jira_board_sprints` | Closed sprints on board 11479 (velocity) | +| `query_jira` | Features, epics, stories, bugs, spikes (paged; see Jira paging) | +| `get_jira_issue` | Issue links of refinement-sprint spikes | +| `sandbox_exec` (Python) | `plugins/edge-scrum/bin/` scripts on the fetched data | + +Jira credentials are not available in the sandbox: fetch with the tools above, +then pass the text into `sandbox_exec`. + +## Configuration + +```yaml +board_id: 11479 +ROSTER_SOURCE: "openshift-eng/edge-context: people/team-roster.md" +focus_priorities: [Blocker, Critical, Major] # run-checks defaults +include_lower_when: [Refinement] +fields: # request fields by id only + story_points: customfield_10028 + sprint: customfield_10020 + qa_contact: customfield_10470 # QE Lead on features + sme: customfield_10475 + docs_approver: customfield_10473 + tshirt_size: customfield_10795 + target_version: customfield_10855 + parent_link: customfield_10018 +``` + +## Jira paging + +`query_jira` returns text with no total count. Fetch every query this way: + +1. First page: `() ORDER BY key DESC` with `max_results: 50`. +2. While the results header — the first such line, after any "Create metadata" + block — is `Showing the first 50 matching issues; more match:` (the page then + ends with the "... More issues match" hint), fetch the next page: + `() AND key < ORDER BY key DESC`. +3. Stop when a page has the header `Found N issues (showing N):` or fewer than + 50 issues. +4. **More than one project:** Jira's `key <` compares keys within one project + only. When a query over several projects does not fit on one page, page each + project separately by adding `AND project =

`, and pass each project as its + own manifest part. +5. The fetch is complete only if every page succeeded. Pass all pages to + `chai_jira_adapter.adapt_fetch`; it rechecks headers, page sizes and key order + and returns `fetch.complete`. If a call failed or a page was cut off, the fetch + is **incomplete**: compute nothing from it and list it as left out, with the + reason. +6. A "Create metadata …" block may come before the results. If it says + `Invalid extra_fields entries: …`, those fields were not returned: the fetch + counts as incomplete, except for Flagged (`customfield_10021`), which is then + reported as an unavailable signal. Unset custom fields print no line, so a + missing Flagged line means "not flagged". +7. Output may be wrapped as `{"result": ""}`; the adapter unwraps it. A + query with no results returns `No issues found for JQL: `: pass the JQL + sent with each page as `"jql"` in the manifest page + (`{"text": ..., "jql": ""}`). The page counts as complete and + empty only when that JQL matches the query sent; otherwise the fetch is + incomplete. + +**Features keep PM rank.** Fetch features once with `() ORDER BY Rank ASC`, +`max_results: 50`, and adapt with `"order": "rank"`. If that page has the +`Found N issues (showing N):` header, the order is the PM rank. If it shows only +the first 50 ("more match"), fetch the +features again with the key recipe above: scope and risk can still be computed, +but the **cut line is left out** (`release_health(..., rank_available=False)`), +since key order is not rank. + +## Steps + +### 1. Window and phase + +Run the **scrum-context** skill with the release (`X.Y`). Use `phase`, +`refinement_sprint`, `pencils_down_sprint`, `branch_cut_sprint`, the current +sprint, and `sprint_availability`. + +| Phase | Mode | +|-------|------| +| `refinement` | Refinement (step 3) | +| `development`, `pencils-down sprint`, `pre-branch-cut` | Release health (step 4); in the pencils-down sprint, flag it, nothing else changes | +| anything else | No section: return it as missing with the phase as the reason | + +If an anchor the mode needs is "not scheduled yet" (the refinement sprint for +refinement; the refinement and branch-cut sprints for release health), return +the section as missing ("a release milestone could not be placed on a sprint"). + +### 2. Roster + +Read `ROSTER_SOURCE` and run `chai_jira_adapter.parse_team_roster(markdown)` +in the sandbox; write `{"members", "contacts"}` to `roster.json`. Pass it to every +`adapt_fetch` call so display names match the roster. + +### 3. Refinement mode + +The release-health `--refinement` checks for refinement and test-plan spikes. + +1. **Features in Refinement**, paged: + `project = OCPSTRAT AND issuetype in (Feature, Initiative) AND labels in ("ocpedge-plan", "microshift") AND "Target Version" = "openshift-X.Y" AND status = "Refinement" AND (resolution is EMPTY OR resolution not in (Duplicate, Obsolete))` + — fields `summary, status, issuetype, priority, assignee, fixVersions, labels, + customfield_10795, customfield_10470, customfield_10473, customfield_10475`. + None → the section says "no features in Refinement" and has no gaps. +2. **Epics**, paged, in batches of 50 feature keys: + `project in (OCPEDGE, USHIFT) AND "Parent Link" in ()` — fields + `summary, status, assignee, labels, parent, updated, fixVersions, + customfield_10028, customfield_10795, customfield_10855, customfield_10018, + customfield_10470`. Run `transform-epics.py`. +3. **Refinement-sprint spikes**, paged: + `project in (OCPEDGE, USHIFT) AND issuetype = Spike AND sprint = ` + — fields `summary, status, assignee, resolution`. `query_jira` cannot return + issue links, so call `get_jira_issue` on each spike and attach its links with + `attach_issue_links` (needed to see spikes that block a child epic instead of + the feature). If it returns any problem (a failed call, or no readable + "Linked Jira Issues" section), the refinement section is left out ("spike + links incomplete"): a missed link would report a false missing spike. A + projected refinement sprint (no id) → refinement section missing + ("refinement sprint is not on the board yet"). +4. **Spikes that block each feature**, one paged query per feature: + `issue in linkedIssues("", "is blocked by") AND issuetype = Spike` + — fields `summary, status, assignee, resolution`. Attach them with + `chai_jira_adapter.attach_linked_spike_fetches(features, {feature_key: adapted_fetch})` + (the whole `adapt_fetch` result per feature; an incomplete one marks the + features fetch incomplete, so the refinement section is left out) + **before** `transform-features.py`; they merge with the refinement-sprint + spikes in `transform-spikes.py`. +5. Write `{"refinement_sprint_closed": }` as + `sprints.json`, then run `transform-features.py`, and `transform-spikes.py` + with `--input --linked-spikes + --features-file --epics-file --sprints-file`. +6. `chai_report.refinement_gaps(features, spikes, section, refinement_sprint_closed)` + applies the release-health refinement-spike rules (first match wins: Closed → + fine; missing but refined via epics → fine; missing → 🔴, owner SME; open but + not in the refinement sprint → 🟡 move it, owner SME; open in the refinement + sprint → in progress) and the test-plan spike rules (skipped when the QA + Contact is empty or `unassigned_jira`; missing or not Closed → 🟡, owner the + QE Lead). A Closed test plan spike satisfies the check whatever its resolution. + +### 4. Release-health mode (development, pencils-down sprint, pre-branch-cut) + +Capacity vs scope, cut line and features at risk, with release-planning's +`run-checks.py`. + +1. **Features**, rank-ordered (see Jira paging): + `project = OCPSTRAT AND issuetype in (Feature, Initiative) AND labels in ("ocpedge-plan", "microshift") AND "Target Version" = "openshift-X.Y" AND (resolution is EMPTY OR resolution not in (Duplicate, Obsolete))` + — fields `summary, status, issuetype, priority, assignee, fixVersions, labels, + customfield_10795, customfield_10855, customfield_10470, customfield_10473, + customfield_10475`. Run `transform-features.py`. No features → section missing + ("no features target X.Y"). +2. **Epics** as in refinement step 2; `transform-epics.py`. +3. **Stories**, paged, in batches of 20 epic keys: + `project in (OCPEDGE, USHIFT, OCPBUGS) AND parent in ()` — fields + `summary, status, issuetype, priority, assignee, labels, updated, parent, + customfield_10028, customfield_10020`. `transform-stories.py --today`. +4. **Unlinked bugs**, paged: + `project = OCPBUGS AND component in ("Installer / Single Node OpenShift", "Two Node with Arbiter", "Two Node Fencing", "Logical Volume Manager Storage", "MicroShift", "MicroShift / Networking", "MicroShift / Storage") AND fixVersion in ("X.Y", "X.Y.0") AND "Epic Link" is EMPTY` + — fields `summary, status, priority, assignee, components, labels, updated`. + `transform-bugs.py --today`. +5. **Velocity**: `compute-velocity.py` over the last 3 closed board sprints, as in + the sprint-health skill (step 4). The weekly report computes it once and + passes it in. Incomplete, or `chai_report.usable_velocity` returns a reason + → velocity not available: run `run-checks.py` + without `--velocity-file`; the section then reports scope only and lists + capacity, cut line and features at risk as left out (its roster-sum fallback + would be an estimate). +6. **Remaining sprints and availability**: the `sprint_availability` entries from + the current sprint through the **branch-cut sprint** (projected sprints + included). `chai_report.release_capacity(team_velocity, entries, today)` gives + the count, `availability_csv` and the label. The current sprint counts only + for its remaining working days (velocity × availability × remaining working + days ÷ sprint working days, today included); upcoming sprints count in full. + A sprint with unknown availability counts as 1.0 and is named as "not + PTO-adjusted". +7. Run: + + ```bash + python3 run-checks.py --features features.json --epics epics.json \ + --stories stories.json --bugs bugs.json --roster roster.json \ + --remaining-sprints --velocity-file velocity.json \ + --availability --version X.Y --today \ + --first-sprint --pencils-down \ + --focus-priorities Blocker,Critical,Major --include-lower-when Refinement \ + --no-hidden-scope-estimate --output checks.json + ``` + + `--no-hidden-scope-estimate` keeps run-checks from estimating the SP of + unpointed work (decision 0018: never estimate); the section reports "N + unpointed items, not counted in scope" instead. `--pencils-down` is the + branch-cut sprint: work can still land until branch + cut, so the pencils-down sprint is flagged but not treated differently. + Omit `--velocity-file` when velocity is not available. +8. `chai_report.release_health(checks, section, capacity, rank_available, + velocity_reason)` (`velocity_reason` from `usable_velocity`, None when usable): scope + vs capacity, features below the cut line (owner: SME), features at risk + (composite HIGH or timeline HIGH; owner: SME). Per-person capacity rows and + `ABOVE_GUIDELINE` flags are dropped; roster drift becomes a count with its SP, + listed as left out. No prompt about roster drift. + +### 5. Return the result + +Return the section dict (with `kind`: `refinement` or `release-health`) to the +caller. Run on its own (for example in a DM), reply with +`chai_report.build_report(None, [section], left_out, mentions)`. + +Release testing (EC/RC validation after branch cut, decision 0017) is out of scope. + +## Missing data + +| Missing | Result | +|---------|--------| +| Release not scheduled, an anchor not placeable, or phase not in progress | No section; returned as missing with the reason | +| Any fetch of the section incomplete | Section left out (` incomplete: `); nothing computed from it | +| Features did not fit on one rank-ordered page | Cut line left out; scope and risk still reported | +| Velocity | Scope only; capacity, cut line and features at risk left out | +| A remaining sprint's availability | That sprint not adjusted, labelled "not PTO-adjusted" | + +Never fill a gap from memory or by estimating. diff --git a/plugins/edge-scrum/chai/scrum-context/SKILL.md b/plugins/edge-scrum/chai/scrum-context/SKILL.md index aef837c3..f9d6dba9 100644 --- a/plugins/edge-scrum/chai/scrum-context/SKILL.md +++ b/plugins/edge-scrum/chai/scrum-context/SKILL.md @@ -1,6 +1,6 @@ --- name: scrum-context -description: Read-only coordinator skill that answers "where are we in the sprint and release cycle?" for other chai-bot skills — current sprint, release window and phase, next ceremonies, and next sprint's team availability +description: Read-only coordinator skill that answers "where are we in the sprint and release cycle?" for other chai-bot skills — current sprint, release window and phase, next ceremonies, and team availability per sprint --- # Scrum Context (chai-bot coordinator) @@ -33,7 +33,8 @@ ceremonies: # matched by event title, case-insensitive # The only roster setting. Default: edge-context people/team-roster.md, # keeping roles containing "Software Engineer" or "Software Quality Engineer", -# at any level (for example Senior or Principal). +# at any level (for example Senior or Principal), minus anyone listed under +# "Not counted for sprint capacity". # Unset or unreadable → availability is "not available (roster not configured)"; # every other part of the result still works. ROSTER_SOURCE: "openshift-eng/edge-context: people/team-roster.md" @@ -47,8 +48,10 @@ ROSTER_ROLES: ["Software Engineer", "Software Quality Engineer"] # "contains" m | No argument | Current sprint (name, id, start, end, week 1–3); the release in progress and its phase | | A release, e.g. `5.1` | Its window: refinement sprint, development sprints, pencils-down sprint, branch-cut sprint, development sprints remaining, and phase | -Both modes also return the **next ceremony dates** and the **next sprint's team -availability**. +Both modes also return the **next ceremony dates**, the **next sprint's team +availability**, and the availability of the **current sprint and each later +sprint** up to the latest branch-cut sprint (of the release asked for, or of the +releases in progress). Phases: `refinement`, `development`, `pencils-down sprint`, `pre-branch-cut`, `not started`, `finished`, `not scheduled yet`. @@ -117,9 +120,14 @@ whose title matches a ceremony. ignores them, so a cancelled ceremony comes back as `null`. The helper relies on `status` for cancellation, because it never sees the title. (It still accepts a legacy `title` field and checks that for the prefix.) -- **Absences and holidays:** read `ROSTER_SOURCE` and keep only engineers whose - role contains "Software Engineer" or "Software Quality Engineer", at any - level. Map each roster engineer to an anonymized id (`p1`, `p2`, …) **outside** +- **Absences and holidays:** read absences from 21 days before today through + 126 days after today, and pass that window as `absence_window`. Read + `ROSTER_SOURCE` and keep only engineers whose role contains "Software + Engineer" or "Software Quality Engineer", at any level. Then drop anyone listed + under "Not counted for sprint capacity" in the roster (people who don't take + sprint work) **before counting engineers**. `chai_jira_adapter.parse_team_roster` + applies both rules if you run it on the roster file alone (never with calendar + data). Map each roster engineer to an anonymized id (`p1`, `p2`, …) **outside** the sandbox. For each absence entry that belongs to a roster engineer, pass `{"person_id", "start", "end"}` (inclusive dates). For a **team-wide or regional holiday** that names no single person, add one entry per affected roster @@ -162,6 +170,7 @@ keys that are not `X.Y`. "schedules": {"X.Y": [{"name": "...", "date": "YYYY-MM-DD"}]}, "events": [{"ceremony": "Sprint Planning", "start": "YYYY-MM-DD", "status": "confirmed"}], "absences": [{"person_id": "p1", "start": "YYYY-MM-DD", "end": "YYYY-MM-DD"}], + "absence_window": {"start": "YYYY-MM-DD", "end": "YYYY-MM-DD"}, "engineer_count": 0 } ``` @@ -174,7 +183,11 @@ Omit a key when its source was not available. The helper then returns Return the helper output as is. Its keys are `current_sprint` (with `week`), `next_sprint`, `release_in_progress` (no argument) or `release` (with an argument), `next_ceremonies`, `current_sprint_ceremonies`, -`next_sprint_availability`, and `warnings`. +`next_sprint_availability`, `current_sprint_availability`, +`sprint_availability` (one entry per sprint from the current sprint through the +latest branch-cut sprint, or the next sprint when no branch cut is known; each +with `name`, `number`, `start`, `end`, `projected`, `availability`), and +`warnings`. `warnings` lists source items skipped for a malformed date, for example "skipped sprint 'Sprint 12': bad end date". It never contains the raw value or @@ -206,9 +219,13 @@ an event title. A malformed `today` is an error, not a warning. projects at most **12 sprints** (about 36 weeks) past the last board sprint. Beyond that, the current sprint is "not available" and the milestones "not scheduled yet"; that usually means the board is stale. -- **Availability:** `1 − person-days off ÷ person-days` over the next sprint's +- **Availability:** `1 − person-days off ÷ person-days` over a sprint's working days (Mon–Fri, start day through the day before the end day), rounded to - 2 decimals. Overlapping entries count once per person-day. Team-level only. + 2 decimals. Overlapping entries count once per person-day. Team-level only. In + `sprint_availability`, a sprint whose working days are not all inside + `absence_window` is "not available (outside the calendar window read)", + since later absences were never seen. Without `absence_window`, the window is + 21 days before through 42 days after today. ## Missing sources diff --git a/plugins/edge-scrum/chai/sprint-health/SKILL.md b/plugins/edge-scrum/chai/sprint-health/SKILL.md new file mode 100644 index 00000000..b1121cfc --- /dev/null +++ b/plugins/edge-scrum/chai/sprint-health/SKILL.md @@ -0,0 +1,215 @@ +--- +name: sprint-health +description: Read-only coordinator skill that reports team-level sprint health for the current sprint — capacity mode in sprint week 1, mid-sprint mode after — with no prompts and no per-person sections +--- + +# Sprint Health (chai-bot coordinator) + +This playbook is for the **chai-bot coordinator**, not for Claude Code. It reports +the health of the current OpenShift Edge sprint at **team level**, as the +sprint-health part of the weekly health report (edge-context decision 0018). It is +**read-only**: it edits nothing in Jira and asks no questions. The analysis runs +inline in the coordinator; there are no sub-agents. + +## Tools + +| Tool | Used for | +|------|----------| +| `scrum-context` skill | Current sprint, its week, and its availability | +| `list_jira_board_sprints` | Closed sprints on board 11479 (velocity) | +| `query_jira` | Sprint issues (paged; see Jira paging) | +| `get_jira_issue` | Issue links of blocked candidates (mid-sprint mode) | +| `sandbox_exec` (Python) | `plugins/edge-scrum/bin/` scripts on the fetched data | + +Jira credentials are not available in the sandbox: fetch everything with the +tools above, then pass the text into `sandbox_exec`. + +## Configuration + +```yaml +board_id: 11479 +projects: [OCPEDGE, USHIFT, OCPBUGS] +ROSTER_SOURCE: "openshift-eng/edge-context: people/team-roster.md" +fields: # request fields by id only + story_points: customfield_10028 + sprint: customfield_10020 + qa_contact: customfield_10470 + flagged: customfield_10021 # unset → no line (not flagged); rejected → unavailable + epic_link: customfield_10014 +``` + +## Jira paging + +`query_jira` returns text with no total count. Fetch every query this way: + +1. First page: `() ORDER BY key DESC` with `max_results: 50`. +2. While the results header — the first such line, after any "Create metadata" + block — is `Showing the first 50 matching issues; more match:` (the page then + ends with the "... More issues match" hint), fetch the next page: + `() AND key < ORDER BY key DESC`. +3. Stop when a page has the header `Found N issues (showing N):` or fewer than + 50 issues. +4. **More than one project:** Jira's `key <` compares keys within one project + only. When a query over several projects does not fit on one page, page each + project separately by adding `AND project =

`, and pass each project as its + own manifest part. +5. The fetch is complete only if every page succeeded. Pass all pages to + `chai_jira_adapter.adapt_fetch` (below); it rechecks headers, page sizes and + key order and returns `fetch.complete`. If a call failed or a page was cut + off, the fetch is **incomplete**: compute nothing from it and list it as left + out, with the reason. +6. A "Create metadata …" block may come before the results. If it says + `Invalid extra_fields entries: …`, those fields were not returned: the fetch + counts as incomplete, except for Flagged (`customfield_10021`), which is then + reported as an unavailable signal. Unset custom fields print no line, so a + missing Flagged line means "not flagged". +7. Output may be wrapped as `{"result": ""}`; the adapter unwraps it. A + query with no results returns `No issues found for JQL: `: pass the JQL + sent with each page as `"jql"` in the manifest page + (`{"text": ..., "jql": ""}`). The page counts as complete and + empty only when that JQL matches the query sent; otherwise the fetch is + incomplete. + +```python +import chai_jira_adapter as ad +adapted = ad.adapt_fetch({ + "name": "sprint_issues", "signals": ["flagged", "description"], + "parts": [{"project": "OCPEDGE", "pages": [{"text": page_1, "jql": ""}, + {"text": page_2, "jql": ""}]}, + {"project": "USHIFT", "pages": [{"text": page_1, "jql": ""}]}]}, + # or "pages": [...] for one part; "jql" is the exact query sent for that page (step 7) + roster=roster) +# adapted["issues"] is jira_search-shaped; write {"issues": adapted["issues"]} to a file +# and pass it to the transform scripts as --input. +``` + +A failed tool call is passed as `{"error": ""}` in its page slot. + +## Steps + +### 1. Context and mode + +Run the **scrum-context** skill with no argument. Use `current_sprint` (name, id, +start, end, `week`) and `current_sprint_availability`. + +- `current_sprint` "not available" → stop; sprint health is left out with that + reason. +- Mode: `chai_report.sprint_mode(week)`: week 1 → **capacity**, week 2 or later → + **mid-sprint**. No prompt. A missing or non-numeric week gives no mode: run + neither analysis; sprint health is left out ("sprint week unknown"). +- A projected current sprint (no id) → stop; sprint health is left out + ("current sprint is not on the board yet"). + +### 2. Roster + +Read `ROSTER_SOURCE` and parse it in the sandbox with +`chai_jira_adapter.parse_team_roster(markdown)`. It keeps roles containing +"Software Engineer" or "Software Quality Engineer" (any level), drops anyone under +"Not counted for sprint capacity", and maps each person to their Rover email +(profile id + `@redhat.com`). Write `{"members": ..., "contacts": ...}` to +`roster.json`. If the roster is unreadable, velocity is not available. + +### 3. Sprint issues + +`query_jira`, paged: + +- **JQL:** `project in (OCPEDGE, USHIFT, OCPBUGS) AND sprint = ` +- **Fields:** `summary, status, issuetype, priority, assignee, created, updated, + labels, parent, customfield_10028, customfield_10014, customfield_10021, + customfield_10470` + +Adapt (`signals: ["flagged", "description"]`), then run +`transform-sprint-issues.py` with a metadata file +(`{"sprint_id", "sprint_name", "sprint_start", "sprint_end"}`, JSON-encoded) and +`--today`. Acceptance criteria are left out (no description is requested from +`query_jira`); the adapter reports that as a skipped signal, and Flagged too if +`query_jira` rejects the field. + +**Mid-sprint mode only — blocked items.** `query_jira`, paged: +`(project in (OCPEDGE, USHIFT, OCPBUGS) AND sprint = ) AND issueLinkType = "is blocked by" AND statusCategory != Done`. +For each returned key, call `get_jira_issue` and pass the texts to +`chai_jira_adapter.attach_issue_links(adapted, {key: text_or_None})` **before** +the transform. The adapter turns the "Linked Jira Issues" lines into the +`issuelinks` shape, and `transform-sprint-issues.py` applies the single +pipeline rule, `_jira_transforms.extract_blocked_by`: an issue is blocked when +at least one "is blocked by" issue is not in a done state (Closed, Verified, +Done or Won't Fix). If `attach_issue_links` returns any problem (a failed +`get_jira_issue` call, or a response without a readable "Linked Jira Issues" +section), or the candidate fetch is incomplete, set `links_complete = False`: +the blocked signal is left out, not guessed. + +### 4. Velocity (capacity mode) + +Velocity is `compute-velocity.py`: the plain average of completed, team-owned SP +over the **last 3 closed sprints** (decision 0015). The current sprint is never +included. + +1. `list_jira_board_sprints` for board 11479 (closed). Keep only the board's own + sprints; skip any marked with another origin board. Take the three closed + sprints with the highest number below the current sprint. +2. For each, `query_jira`, paged: `project in (OCPEDGE, USHIFT, OCPBUGS) AND + sprint = `, fields `summary, status, issuetype, assignee, resolutiondate, + customfield_10028, customfield_10470, customfield_10020`. Adapt with the + roster, then `transform-sprint-issues.py` with that sprint's metadata. +3. `compute-velocity.py --sprint-issues --roster roster.json`. +4. `team_velocity, reason = chai_report.usable_velocity(velocity_json)`. + +If any of the three fetches is incomplete, or `usable_velocity` returns a reason +(fewer than 3 sprints, or done team issues skipped for missing sprint and +resolution data, which would understate it), velocity is **not available** +(left out with the reason); never average fewer sprints or estimate. + +Capacity: `chai_report.sprint_capacity(team_velocity, current_sprint_availability)` += velocity × the current sprint's availability. When availability is not a +number, capacity is the velocity unadjusted, labelled **"not PTO-adjusted"**. + +### 5. Analysis (inline) + +Run `chai_report.sprint_health(sprint_issues, current_sprint, today, mode, +velocity, availability, skipped_signals, links_complete, goal, velocity_reason)`, +where `velocity_reason` is the reason `usable_velocity` returned (None when +velocity is usable); it becomes the reason capacity is left out. It applies the +sprint-health analyzers at team level: + +- **Capacity mode** (capacity analyzer): committed SP and issues, by type, + velocity, capacity, committed vs capacity, unpointed issues, issues without an + epic. Over capacity is a 🔴 risk with a re-plan action (a team-level item, + owned by the Scrum Master when the weekly report can determine one); unpointed and + epic-less issues are 🟡 with one action per issue for its assignee. +- **Mid-sprint mode** (midpoint analyzer): burndown (expected = total SP × + elapsed ÷ total days; On Track / Slightly Behind (≤ 10% of total) / Behind), + projected finish, and blocked or stalled open issues (blocked by an open issue, + "Blocked" label, Flagged (unless the field was rejected), and issues in an + active status per law 07 (In Progress, Review, or OCPBUGS ASSIGNED) with no + update for more than 5 business days). +- **Sprint goal (mid-sprint, inline judgement):** take the goal text from the + current sprint's `goal` in the `list_jira_board_sprints` result (it is not in + scrum-context's `current_sprint`). List the issues whose summary clearly + matches the goal's terms, and rate the goal 🟢 (none blocked and burndown on + track), 🟡 (stalled or slightly behind) or 🔴 (goal work blocked, or behind). + Pass `goal={"rating": , "note": ""}` to `sprint_health`; it + is shown in the sprint reply. No goal on the sprint → + `goal={"rating": None, "note": "no sprint goal set"}`; sprint data not readable + → leave `goal` out and list "sprint goal" as left out. + +**Team level only.** There is no per-person load table, no per-person SP, and no +PTO detail. A name appears only as the owner of one action item. + +### 6. Return the result + +Return the `sprint_health` dict to the caller (the weekly report). Run on its own +(for example in a DM), reply with +`chai_report.build_report(result, [], left_out, mentions)`: the three top lines, +then the replies. + +## Missing data + +| Missing | Result | +|---------|--------| +| Current sprint | Sprint health left out, with the reason | +| Sprint issue fetch incomplete | Sprint health left out (`sprint issue fetch incomplete: `) | +| A velocity sprint's fetch incomplete, or no roster | Velocity and capacity "not available"; other figures still reported | +| Current sprint availability | Capacity not adjusted, labelled "not PTO-adjusted" | +| Flagged field / issue links | That signal left out and listed | + +Never fill a gap from memory or by estimating. diff --git a/plugins/edge-scrum/chai/weekly-health-report/SKILL.md b/plugins/edge-scrum/chai/weekly-health-report/SKILL.md new file mode 100644 index 00000000..9c8e4f05 --- /dev/null +++ b/plugins/edge-scrum/chai/weekly-health-report/SKILL.md @@ -0,0 +1,162 @@ +--- +name: weekly-health-report +description: Read-only coordinator skill that posts the weekly OpenShift Edge sprint and release health report (edge-context decision 0018) — a 3-line top post with details and owner-mentioned action items in its thread +--- + +# Weekly Health Report (chai-bot coordinator) + +This playbook is for the **chai-bot coordinator**, not for Claude Code. It +implements edge-context decision 0018 end to end: sprint health for the current +sprint plus one release section per release in progress, posted as one message +with details in its thread. It is **read-only** in Jira, asks no questions, and +uses no sub-agents. + +## Tools + +| Tool | Used for | +|------|----------| +| `scrum-context` skill | Current sprint and week, releases in progress, phases, availability | +| `sprint-health` skill | Sprint section (capacity or mid-sprint mode) | +| `release-health` skill | One release section per release in progress | +| Slack user lookup by email (as the persona does) | Owner mentions | +| Slack post | The top post and its thread replies | +| `sandbox_exec` (Python) | `plugins/edge-scrum/bin/chai_report.py` and the scripts the skills use | + +All Jira reads follow the **Jira paging** recipe below, through the skills. + +## Configuration + +```yaml +schedule: "Mondays 10:00 UTC" # set on the chai-bot task, not here +channel: "#team-ocp-edge-notifications" +ROSTER_SOURCE: "openshift-eng/edge-context: people/team-roster.md" +``` + +## Jira paging + +`query_jira` returns text with no total count. Every fetch in this report: + +1. First page: `() ORDER BY key DESC` with `max_results: 50`. +2. While the results header — the first such line, after any "Create metadata" + block — is `Showing the first 50 matching issues; more match:` (the page then + ends with the "... More issues match" hint), fetch the next page: + `() AND key < ORDER BY key DESC`. +3. Stop when a page has the header `Found N issues (showing N):` or fewer than + 50 issues. +4. **More than one project:** Jira's `key <` compares keys within one project + only. When a query over several projects does not fit on one page, page each + project separately by adding `AND project =

`. +5. The fetch is complete only if every page succeeded + (`chai_jira_adapter.adapt_fetch` → `fetch.complete`). If a call failed or a + page was cut off, the fetch is **incomplete**: compute nothing from it, post + what is complete, and list what was left out and why. +6. A "Create metadata …" block may come before the results. If it says + `Invalid extra_fields entries: …`, those fields were not returned: the fetch + counts as incomplete, except for Flagged (`customfield_10021`), which is then + reported as an unavailable signal. Unset custom fields print no line, so a + missing Flagged line means "not flagged". +7. Output may be wrapped as `{"result": ""}`; the adapter unwraps it. A + query with no results returns `No issues found for JQL: `: pass the JQL + sent with each page as `"jql"` in the manifest page + (`{"text": ..., "jql": ""}`). The page counts as complete and + empty only when that JQL matches the query sent; otherwise the fetch is + incomplete. + +## Steps + +### 1. Where are we + +Run **scrum-context** with no argument: `current_sprint` (with `week`), +`current_sprint_availability`, `release_in_progress`. + +Then run **scrum-context** once per release with that release as the argument, +for every release in `release_in_progress` **and** every release Product Pages +lists as Planning or in development. Collect each result's `release` value +(the window, not the whole result) as `windows`: +`windows = [result["release"] for result in release_results]`. + +### 2. Choose the sections + +In the sandbox: `sections, missing = chai_report.select_sections(context, windows)`. + +| Release phase | Section | +|---------------|---------| +| `refinement` | release-health, refinement mode: refinement and test-plan spike gaps | +| `development` | release-health: capacity vs scope, cut line, features at risk | +| `pencils-down sprint` | the same, flagged as the pencils-down sprint | +| `pre-branch-cut` | the same (work can still land before branch cut) | +| `not scheduled yet`, an anchor not placeable on a sprint, or phase unknown | listed in `missing`, with the reason | +| `not started`, `finished` | no section | + +Two releases in progress each get their own section with their own phase. No +release in progress → `missing` says "no release in progress"; sprint health is +still reported. + +### 3. Sprint health + +Run **sprint-health** (week 1 → capacity mode, later → mid-sprint mode; no +prompt). Compute velocity once (`compute-velocity.py`, last 3 closed sprints), +check it with `chai_report.usable_velocity`, and reuse the value **and its +reason** for every release section (`velocity_reason=` in `sprint_health` and +`release_health`), so the "Left out" reply says why capacity is missing. In capacity mode, the current sprint's +capacity = velocity × `current_sprint_availability`; when that is not known, +capacity is not adjusted and is labelled "not PTO-adjusted". Mid-sprint mode +applies no availability adjustment. + +### 4. Release sections + +For each selected section, run **release-health** for that release (it uses the +window and phase already read). Collect the **computed** results +(`release_health` or `refinement_gaps` output) in a new list, `computed`; add +any section it returns as missing to `missing`. `select_sections` only chooses +sections: never pass its descriptors to `build_report`. + +### 5. Owner mentions + +Each action item names its owner: the SME (feature-level items), the QE Lead +(test plan spikes), or the assignee (sprint issues). Team-level items (re-plan +or re-scope the sprint) belong to the **Scrum Master**: +`chai_jira_adapter.find_scrum_master(team_roster_md, scrum_master_md)` reads it +from a roster row whose role contains "Scrum Master", or a +`Current Scrum Master: ` line in edge-context `roles/scrum-master.md`. +When neither names one, those items keep "no owner set". For each distinct owner: + +1. Find the owner in `ROSTER_SOURCE` by display name + (`parse_team_roster(...)["contacts"]`, matched case- and space-insensitively). +2. Their Rover ID is the last part of the profile link; email = `@redhat.com`. +3. Look up the Slack user by that email; the mention is `<@USER_ID>`. +4. No roster row, no Rover link, or no Slack user → the name, without a mention. + +Pass the result as `mentions = {display name: "<@USER_ID>"}`. Whether a mention +pings follows the platform's **Allow Mentions** setting for that person; the +report does not decide it. Do this lookup outside the sandbox. + +### 6. Build and post + +```python +report = chai_report.build_report(sprint, computed, missing, mentions, team_owner=scrum_master) +``` + +- **Top post** = `report["top_post"]`, exactly three lines: status and where we + are; the biggest risk (or "No risks this week" with the sprint figures); + "N items need action". +- **Thread replies** = `report["replies"]`, in order: sprint health, one reply per + release section, action items (each with its owner), and "Left out" (missing + sections and signals, with reasons). + +Post the top post to the channel, then each reply in its thread. Run from a DM +(validation or an extra run requested by a task manager), reply in the DM +instead of the channel. + +## Rules + +- **Team level only.** No per-person load, SP or PTO. A name appears only as the + owner of one action item. +- **Never estimate.** If a source is missing or a fetch is incomplete, post what is + complete and list what was left out and why (for example + "9.2 release section: no Product Pages schedule yet"). No figure is computed + from an incomplete fetch. +- **Nothing to flag** → the top post says "No risks this week" with the sprint + figures, and "0 items need action". +- **Read-only** in Jira. Release testing (EC/RC validation, decision 0017) is out + of scope. From e6de1c38bfdae67653324e41d86b14814ca10c93 Mon Sep 17 00:00:00 2001 From: nhamza Date: Thu, 12 Nov 2026 16:49:29 +0200 Subject: [PATCH 2/6] edge-scrum: chai sprint values after CHAI-1254, address review comments (OCPEDGE-3121) - chai_jira_adapter: parse customfield_10020 entries tolerantly (name, id, state; extra keys like board/start/end/completed in any order, "; " or ", " separated). A value that still looks shortened ("+N more", trailing "...", stored for result_page) or an entry without an id marks that issue's sprint data incomplete and the fetch incomplete; nothing is guessed. - chai SKILLs: read shortened or stored field values whole with result_page before passing results to the scripts. - chai_jira_adapter: roster contacts without a Rover email match by display name. - chai_report: a green sprint-goal rating is withheld when blocked-by links are incomplete or Flagged was rejected. - release-health SKILL: read the refinement sprint state from the board. - sprint-health SKILL: read the goal from the active board sprint. Assisted-by: Claude --- plugins/edge-scrum/bin/chai_jira_adapter.py | 77 +++++++++++++++---- plugins/edge-scrum/bin/chai_report.py | 6 ++ .../bin/tests/test_chai_jira_adapter.py | 70 +++++++++++++++++ .../edge-scrum/bin/tests/test_chai_report.py | 16 ++++ .../edge-scrum/chai/release-health/SKILL.md | 6 +- .../edge-scrum/chai/sprint-health/SKILL.md | 13 +++- .../chai/weekly-health-report/SKILL.md | 2 + 7 files changed, 168 insertions(+), 22 deletions(-) diff --git a/plugins/edge-scrum/bin/chai_jira_adapter.py b/plugins/edge-scrum/bin/chai_jira_adapter.py index 2653f0a4..422d689e 100644 --- a/plugins/edge-scrum/bin/chai_jira_adapter.py +++ b/plugins/edge-scrum/bin/chai_jira_adapter.py @@ -22,7 +22,7 @@ Type: Story | Status: Code Review | Priority: Major | Assignee: | Reporter: | Resolution: Done Created: 2030-01-02 | Updated: 2030-01-06 | Resolved: 2030-01-06 Components: | Labels: a, b | Fix Versions: x | Parent: KEY-100 - customfield_10020: (id N, , board B, start , end ) + customfield_10020: (id N, , board B, start , end , completed ); (...) customfield_10028: customfield_: issuelinks: (set, but query_jira cannot render this field's structure) @@ -83,9 +83,10 @@ _HEADER_RE = re.compile(r"^\s*\[(" + KEY_PATTERN + r")\]\s*(.*?)\s*$") _KEY_RE = re.compile(r"^(?P[A-Z][A-Z0-9_]*)-(?P\d+)$") _LABEL_RE = re.compile(r"^\s*([A-Za-z][A-Za-z0-9_ ]{0,40}?)\s*:\s?(.*)$") -_SPRINT_RE = re.compile( - r"(?P[^()]*?)\s*\(\s*id\s+(?P\d+)\s*,\s*(?P[^,]+?)\s*,\s*" - r"board\s+(?P[^,]+?)\s*,\s*start\s+(?P[^,]+?)\s*,\s*end\s+(?P[^)]+?)\s*\)") +_SPRINT_ENTRY_RE = re.compile(r"([^;()]*?)\s*\(([^()]*)\)") +_SPRINT_STATES = {"active", "closed", "future"} +# Signs that a field value is not whole (chai-bot shortened it or stored it for result_page). +_SHORTENED_RE = re.compile(r"\+\s*\d+\s+more\b|(\.\.\.|…)\s*$|\b(stored|shortened|truncated|result_page)\b", re.I) _FOUND_RE = re.compile(r"^\s*Found\s+(\d+)\s+issues?(?:\s*\(showing\s+(\d+)\))?", re.I) _MORE_RE = re.compile(r"^\s*Showing the first\s+(\d+)\s+matching issues?.*more match", re.I) _NO_RESULTS_RE = re.compile(r"^\s*No issues found for JQL:(.*)$", re.S) @@ -174,17 +175,48 @@ def _number(value): return None +def parse_sprint_value(value): + """``customfield_10020`` text → ``(sprint dicts, problem or None)``. + + Entries look like `` (id N, , board B, start , end , + completed )``, separated by ``; `` (or ``, ``). Only the name, ``id N`` + and the state (active / closed / future) are required; other ``key value`` + parts are kept when known (board, start, end, completed) and ignored + otherwise, in any order. An empty value means no sprint. A value that still + looks shortened ("+N more", a trailing "...", a note that it was stored), an + entry without an id, or text with no entry at all returns a ``problem``: the + caller must not guess the missing sprints (decision 0018: never estimate). + """ + text = _clean(value) + if text is None: + return [], None + out, problem = [], None + for m in _SPRINT_ENTRY_RE.finditer(text): + entry = {"name": m.group(1).strip(" ,;"), "id": None, "state": None} + for part in (p.strip() for p in m.group(2).split(",")): + idm = re.fullmatch(r"id\s+(\d+)", part, re.I) + key, _, val = part.partition(" ") + if idm: + entry["id"] = int(idm.group(1)) + elif part.lower() in _SPRINT_STATES: + entry["state"] = part.lower() + elif key.lower() in ("board", "start", "end", "completed"): + entry[{"board": "boardId", "start": "startDate", "end": "endDate", + "completed": "completeDate"}[key.lower()]] = _clean(val) + if entry["id"] is None: + problem = problem or f"sprint entry '{entry['name']}' has no id" + continue + out.append(entry) + if _SHORTENED_RE.search(text): + problem = "sprint value was shortened; read it whole with result_page" + elif not out and problem is None: + problem = "sprint value not recognized" + return out, problem + + def parse_sprints(value): - """``customfield_10020`` text → sprint dicts (one per ``name (id …)`` entry).""" - out = [] - for m in _SPRINT_RE.finditer(value or ""): - name = m.group("name").strip().lstrip(",;").strip() - start, end = _clean(m.group("start")), _clean(m.group("end")) - out.append({"id": int(m.group("id")), "name": name, - "state": m.group("state").strip().lower(), - "boardId": _clean(m.group("board")), - "startDate": start, "endDate": end}) - return out + """``customfield_10020`` text → sprint dicts (see :func:`parse_sprint_value`).""" + return parse_sprint_value(value)[0] def parse_issue_links(text): @@ -427,7 +459,10 @@ class PeopleMatcher: def __init__(self, roster=None): """Index the roster's contacts (or members) by display name and email.""" people = (roster or {}).get("contacts") or (roster or {}).get("members") or [] - members = [{"username": p.get("email") or p.get("username"), "display_name": p.get("display_name")} + # A contact without a Rover email falls back to its display name, as + # parse_team_roster does for members, so it still matches. + members = [{"username": p.get("email") or p.get("username") or p.get("display_name"), + "display_name": p.get("display_name")} for p in people] self.index = build_roster_index({"members": members}) self.matched = set() @@ -490,7 +525,10 @@ def to_issue(raw, people): parent["issue_type"] = {"name": "Epic"} issue["parent"] = parent if "customfield_10020" in raw: - issue["customfield_10020"] = parse_sprints(raw["customfield_10020"]) + sprints, problem = parse_sprint_value(raw["customfield_10020"]) + issue["customfield_10020"] = sprints + if problem: + issue["sprint_data_incomplete"] = problem if "customfield_10028" in raw: issue["customfield_10028"] = _number(raw["customfield_10028"]) for field in ("customfield_10795",): @@ -709,6 +747,11 @@ def adapt_fetch(manifest, roster=None): seen.add(raw["key"]) issues.append(to_issue(raw, people)) + # An issue whose sprint value is not whole: its sprint data is incomplete, so + # nothing that depends on sprints (velocity, activity) is computed from it. + sprint_incomplete = [i["key"] for i in issues if i.get("sprint_data_incomplete")] + for key in sprint_incomplete: + problems.append(f"{key}: {next(i for i in issues if i['key'] == key)['sprint_data_incomplete']}") signals = set(manifest.get("signals") or []) skipped = [] # Unset custom fields print no line, so a missing Flagged line means "not @@ -731,7 +774,7 @@ def adapt_fetch(manifest, roster=None): return { "issues": issues, "fetch": {"name": manifest.get("name"), "complete": not problems, "pages": page_count, - "fetched": len(issues), "problems": problems}, + "fetched": len(issues), "problems": problems, "sprint_incomplete": sprint_incomplete}, "skipped_signals": skipped, "people": {"matched": len(people.matched), "unmatched": sorted(people.unmatched)}, "warnings": warnings, diff --git a/plugins/edge-scrum/bin/chai_report.py b/plugins/edge-scrum/bin/chai_report.py index 29d3a9d7..7935eaef 100644 --- a/plugins/edge-scrum/bin/chai_report.py +++ b/plugins/edge-scrum/bin/chai_report.py @@ -337,6 +337,12 @@ def sprint_health(sprint_issues, sprint, today, mode, velocity=None, availabilit if not links_complete: out["left_out"].append({"what": "blocked-by links", "why": "issue link details incomplete (get_jira_issue)"}) + if goal and goal.get("rating") == GREEN and (not links_complete or not flagged_known): + # No visible blocker is not proof the goal work is unblocked when a + # blocked signal is missing: do not report green. + missing = "blocked-by links" if not links_complete else "Flagged" + out["goal"] = {"rating": None, + "note": f"{goal.get('note') or 'goal on track'} (not rated: {missing} not available)"} if blockers: hard = [b for b in blockers if any(not r.startswith("no update") for r in b["reasons"])] out["risks"].append({"severity": RED if hard else YELLOW, diff --git a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py index 77932fb5..b4d0bb28 100644 --- a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py +++ b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py @@ -177,6 +177,21 @@ def _load(name): SAMPLE_NO_RESULTS_JQL = "key = OCPEDGE-1 AND key = OCPEDGE-2" +# Sample 8: full Sprint value after CHAI-1254 (no "+N more" cut; long values read whole +# with result_page). Entries are separated by "; ". Generic summary, anonymized assignee. +SPRINT_FULL = ("OCPEDGE Sprint 250 (id 12577, closed, board 8557, start 2024-02-26T10:32:56.158Z, " + "end 2024-03-18T04:01:00.000Z, completed 2024-03-18T10:02:55.043Z); " + "OCPEDGE Sprint 249 (id 14536, closed, board 8557, start 2024-02-05T16:21:59.538Z, " + "end 2024-02-26T05:01:00.000Z, completed 2024-02-26T10:08:48.181Z)") +SAMPLE_SPRINTS = f"""Found 1 issues (showing 1): + [TEST-250] Synthetic story carried across two sprints + Type: Story | Status: Closed | Priority: Major | Assignee: Engineer A | Reporter: Engineer A | Resolution: Done + Created: 2024-02-20 | Updated: 2024-03-18 | Resolved: 2024-03-18 + customfield_10020: {SPRINT_FULL} + customfield_10028: 3.0 +""" + + def block(key, summary="Synthetic item", type_="Story", status="In Progress", assignee="Alex Example", sp="3.0", sprints=SPRINT_11, extra=()): """One issue block in chai-bot's layout (indented, combined lines).""" @@ -458,6 +473,55 @@ def test_wrapped_and_bare_pages_parse_the_same(self): self.assertEqual(ad.unwrap("{not json"), "{not json") +class SprintValueTest(unittest.TestCase): + """customfield_10020 after CHAI-1254: whole values, tolerant entries, never guessed.""" + + def test_full_multi_sprint_value(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_SPRINTS}]}) + self.assertTrue(out["fetch"]["complete"], out["fetch"]["problems"]) + issue = out["issues"][0] + self.assertEqual(issue["assignee"]["name"], "Engineer A") + sprints = issue["customfield_10020"] + self.assertEqual([(s["id"], s["name"], s["state"]) for s in sprints], + [(12577, "OCPEDGE Sprint 250", "closed"), (14536, "OCPEDGE Sprint 249", "closed")]) + self.assertEqual(sprints[0]["endDate"], "2024-03-18T04:01:00.000Z") # "completed" not glued on + self.assertEqual(sprints[0]["completeDate"], "2024-03-18T10:02:55.043Z") + row = transform_sprint_issues.transform_issue(issue, "2024-03-20") + self.assertEqual(row["sprint_details"], [{"id": 12577, "end": "2024-03-18"}, {"id": 14536, "end": "2024-02-26"}]) + self.assertNotIn("sprint_data_incomplete", issue) + + def test_parts_in_any_order_and_unknown_keys(self): + sprints, problem = ad.parse_sprint_value("Sprint X (future, foo bar, id 7, end 2030-01-02T00:00:00.000Z)") + self.assertIsNone(problem) + self.assertEqual((sprints[0]["id"], sprints[0]["state"], sprints[0]["endDate"]), + (7, "future", "2030-01-02T00:00:00.000Z")) + + def test_empty_sprint(self): + self.assertEqual(ad.parse_sprint_value(""), ([], None)) + text = SAMPLE_SPRINTS.replace(SPRINT_FULL, "") + out = ad.adapt_fetch({"pages": [{"text": text}]}) + self.assertTrue(out["fetch"]["complete"]) + self.assertEqual(out["issues"][0]["customfield_10020"], []) + unset = ad.adapt_fetch({"pages": [{"text": SAMPLE_SPRINTS.replace(f" customfield_10020: {SPRINT_FULL}\n", "")}]}) + self.assertTrue(unset["fetch"]["complete"]) + self.assertNotIn("customfield_10020", unset["issues"][0]) + + def test_shortened_value_is_marked_incomplete(self): + first = SPRINT_FULL.split("; ")[0] + for shortened in (first + " +1 more", first + "; OCPEDGE Sprint 249 (id 14536, clo...", + first + " (value stored; read the rest with result_page)"): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_SPRINTS.replace(SPRINT_FULL, shortened)}]}) + self.assertFalse(out["fetch"]["complete"], shortened) + self.assertEqual(out["fetch"]["sprint_incomplete"], ["TEST-250"]) + self.assertIn("TEST-250: sprint value was shortened", out["fetch"]["problems"][0]) + self.assertIn("result_page", out["issues"][0]["sprint_data_incomplete"]) + + def test_entry_without_id_is_incomplete(self): + sprints, problem = ad.parse_sprint_value("Sprint X (closed, board 1)") + self.assertEqual(sprints, []) + self.assertIn("has no id", problem) + + class LinkedSpikesCliTest(unittest.TestCase): """CLI --linked-spikes: an incomplete per-feature fetch marks the output incomplete.""" @@ -532,6 +596,12 @@ def test_display_name_matches_roster_case_and_space_insensitively(self): self.assertEqual(out["issues"][0]["assignee"]["email"], "aexample@redhat.com") self.assertEqual(out["people"], {"matched": 1, "unmatched": []}) + def test_contact_without_rover_email_matches_by_display_name(self): + roster = {"contacts": [{"display_name": "Nolink Person", "email": None}]} + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], assignee="Nolink Person")}]}, roster) + self.assertEqual(out["people"], {"matched": 1, "unmatched": []}) + self.assertEqual(out["issues"][0]["assignee"]["email"], "Nolink Person") + def test_unmatched_name_is_kept_not_unassigned(self): out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], assignee="Robin Outsider")}]}, ROSTER) assignee = out["issues"][0]["assignee"] diff --git a/plugins/edge-scrum/bin/tests/test_chai_report.py b/plugins/edge-scrum/bin/tests/test_chai_report.py index 1f1310a6..61b541c2 100644 --- a/plugins/edge-scrum/bin/tests/test_chai_report.py +++ b/plugins/edge-scrum/bin/tests/test_chai_report.py @@ -472,6 +472,22 @@ def test_goal_rating_is_carried_into_the_reply(self): goal={"rating": None, "note": "no sprint goal set"}) self.assertIn("• Sprint goal: no sprint goal set", cr.build_report(none, [])["replies"][0]) + def test_goal_is_not_green_without_blocked_signals(self): + green = {"rating": "🟢", "note": "goal work on track"} + ok = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", goal=green) + self.assertEqual(ok["goal"], green) + no_links = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", goal=green, + links_complete=False) + self.assertEqual(no_links["goal"], {"rating": None, + "note": "goal work on track (not rated: blocked-by links not available)"}) + no_flag = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", goal=green, + skipped_signals=[ad.SKIPPED_FLAGGED]) + self.assertIsNone(no_flag["goal"]["rating"]) + self.assertIn("Flagged not available", no_flag["goal"]["note"]) + yellow = cr.sprint_health(ISSUES, dict(SPRINT, week=2), "2030-02-07", "mid-sprint", + goal={"rating": "🟡", "note": "stalled"}, links_complete=False) + self.assertEqual(yellow["goal"]["rating"], "🟡") # only green is withheld + def test_build_report_rejects_section_descriptors(self): sections, _ = cr.select_sections({}, [_window("9.1", "development")]) with self.assertRaises(ValueError): diff --git a/plugins/edge-scrum/chai/release-health/SKILL.md b/plugins/edge-scrum/chai/release-health/SKILL.md index ea1178e1..88c194d0 100644 --- a/plugins/edge-scrum/chai/release-health/SKILL.md +++ b/plugins/edge-scrum/chai/release-health/SKILL.md @@ -73,6 +73,8 @@ fields: # request fields by id only (`{"text": ..., "jql": ""}`). The page counts as complete and empty only when that JQL matches the query sent; otherwise the fetch is incomplete. +8. When `query_jira` says a field value was shortened or stored, read it whole + with `result_page` before passing the results to the script. **Features keep PM rank.** Fetch features once with `() ORDER BY Rank ASC`, `max_results: 50`, and adapt with `"order": "rank"`. If that page has the @@ -138,7 +140,9 @@ The release-health `--refinement` checks for refinement and test-plan spikes. features fetch incomplete, so the refinement section is left out) **before** `transform-features.py`; they merge with the refinement-sprint spikes in `transform-spikes.py`. -5. Write `{"refinement_sprint_closed": }` as +5. Read the refinement sprint's state from `list_jira_board_sprints` for board + 11479 (all states; match the refinement sprint's id): scrum-context's window + has no state. Write `{"refinement_sprint_closed": }` as `sprints.json`, then run `transform-features.py`, and `transform-spikes.py` with `--input --linked-spikes --features-file --epics-file --sprints-file`. diff --git a/plugins/edge-scrum/chai/sprint-health/SKILL.md b/plugins/edge-scrum/chai/sprint-health/SKILL.md index b1121cfc..71534351 100644 --- a/plugins/edge-scrum/chai/sprint-health/SKILL.md +++ b/plugins/edge-scrum/chai/sprint-health/SKILL.md @@ -69,6 +69,8 @@ fields: # request fields by id only (`{"text": ..., "jql": ""}`). The page counts as complete and empty only when that JQL matches the query sent; otherwise the fetch is incomplete. +8. When `query_jira` says a field value was shortened or stored, read it whole + with `result_page` before passing the results to the script. ```python import chai_jira_adapter as ad @@ -182,11 +184,14 @@ sprint-health analyzers at team level: "Blocked" label, Flagged (unless the field was rejected), and issues in an active status per law 07 (In Progress, Review, or OCPBUGS ASSIGNED) with no update for more than 5 business days). -- **Sprint goal (mid-sprint, inline judgement):** take the goal text from the - current sprint's `goal` in the `list_jira_board_sprints` result (it is not in - scrum-context's `current_sprint`). List the issues whose summary clearly +- **Sprint goal (mid-sprint, inline judgement):** call `list_jira_board_sprints` + for board 11479 with state `active`, take the sprint whose id matches + scrum-context's `current_sprint` id, and read its `goal` (scrum-context does + not return the goal, and the velocity call above lists closed sprints only). List the issues whose summary clearly matches the goal's terms, and rate the goal 🟢 (none blocked and burndown on - track), 🟡 (stalled or slightly behind) or 🔴 (goal work blocked, or behind). + track; never green when blocked-by links are incomplete or Flagged was + rejected, since `sprint_health` then clears the rating), 🟡 (stalled or + slightly behind) or 🔴 (goal work blocked, or behind). Pass `goal={"rating": , "note": ""}` to `sprint_health`; it is shown in the sprint reply. No goal on the sprint → `goal={"rating": None, "note": "no sprint goal set"}`; sprint data not readable diff --git a/plugins/edge-scrum/chai/weekly-health-report/SKILL.md b/plugins/edge-scrum/chai/weekly-health-report/SKILL.md index 9c8e4f05..2451ec0c 100644 --- a/plugins/edge-scrum/chai/weekly-health-report/SKILL.md +++ b/plugins/edge-scrum/chai/weekly-health-report/SKILL.md @@ -61,6 +61,8 @@ ROSTER_SOURCE: "openshift-eng/edge-context: people/team-roster.md" (`{"text": ..., "jql": ""}`). The page counts as complete and empty only when that JQL matches the query sent; otherwise the fetch is incomplete. +8. When `query_jira` says a field value was shortened or stored, read it whole + with `result_page` before passing the results to the script. ## Steps From 5a8acae34ea8e0239573617b0f432e26909da6c0 Mon Sep 17 00:00:00 2001 From: nhamza Date: Thu, 12 Nov 2026 17:10:24 +0200 Subject: [PATCH 3/6] edge-scrum: mark a cut-off sprint entry incomplete (OCPEDGE-3121) A customfield_10020 value cut inside its last entry, without "..." or "+N more", silently dropped that sprint. Text left over besides the entry separators now marks the issue's sprint data, and the fetch, incomplete. Assisted-by: Claude --- plugins/edge-scrum/bin/chai_jira_adapter.py | 5 +++++ .../edge-scrum/bin/tests/test_chai_jira_adapter.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/plugins/edge-scrum/bin/chai_jira_adapter.py b/plugins/edge-scrum/bin/chai_jira_adapter.py index 422d689e..e15c40d3 100644 --- a/plugins/edge-scrum/bin/chai_jira_adapter.py +++ b/plugins/edge-scrum/bin/chai_jira_adapter.py @@ -207,8 +207,13 @@ def parse_sprint_value(value): problem = problem or f"sprint entry '{entry['name']}' has no id" continue out.append(entry) + # Every part of the value must belong to an entry: text left over besides the + # "; " / ", " separators means an entry was cut off (e.g. a missing ")"). + remainder = _SPRINT_ENTRY_RE.sub("", text).strip(" \t\n;,") if _SHORTENED_RE.search(text): problem = "sprint value was shortened; read it whole with result_page" + elif remainder: + problem = problem or "sprint value has unparsed text after the last entry (cut off?)" elif not out and problem is None: problem = "sprint value not recognized" return out, problem diff --git a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py index b4d0bb28..5b3354d9 100644 --- a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py +++ b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py @@ -516,6 +516,18 @@ def test_shortened_value_is_marked_incomplete(self): self.assertIn("TEST-250: sprint value was shortened", out["fetch"]["problems"][0]) self.assertIn("result_page", out["issues"][0]["sprint_data_incomplete"]) + def test_cut_off_entry_without_ellipsis_is_incomplete(self): + sprints, problem = ad.parse_sprint_value("Sprint A (id 1, closed); Sprint B (id 2, closed") + self.assertEqual([s["id"] for s in sprints], [1]) + self.assertIn("unparsed text", problem) + cut = SPRINT_FULL[:SPRINT_FULL.rindex(", completed")] # second entry loses its ")" + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_SPRINTS.replace(SPRINT_FULL, cut)}]}) + self.assertFalse(out["fetch"]["complete"]) + self.assertEqual(out["fetch"]["sprint_incomplete"], ["TEST-250"]) + # Whole values with either separator still parse cleanly. + self.assertIsNone(ad.parse_sprint_value(SPRINT_FULL)[1]) + self.assertIsNone(ad.parse_sprint_value(SPRINT_FULL.replace("; ", ", "))[1]) + def test_entry_without_id_is_incomplete(self): sprints, problem = ad.parse_sprint_value("Sprint X (closed, board 1)") self.assertEqual(sprints, []) From c9ae4f381dbe12bd179c04d22be73af1780bc24c Mon Sep 17 00:00:00 2001 From: nhamza Date: Sun, 15 Nov 2026 12:02:59 +0200 Subject: [PATCH 4/6] edge-scrum: address copejon's review of the chai report skills (OCPEDGE-3121) - scrum_context: a non-object absence_window falls back to the default window with a warning instead of crashing; the default-window comment now says why it is narrower than the documented 126-day read. - chai_jira_adapter: the "Linked Jira Issues" section starts only at a standalone header line; " | " splits a line only when every label is a known field; no skipped description signal on an empty fetch; sprint-incomplete problems built in one pass. - chai_report: the capacity line uses the velocity reason; mentions match case- and space-insensitively; refinement risk reads "without a closed refinement spike"; "1 issue" in week-1 figures. - assemble-report: the not-estimated verdict counts unpointed bugs too. Assisted-by: Claude --- plugins/edge-scrum/bin/assemble-report.py | 2 +- plugins/edge-scrum/bin/chai_jira_adapter.py | 26 ++++++++++----- plugins/edge-scrum/bin/chai_report.py | 14 +++++--- plugins/edge-scrum/bin/scrum_context.py | 8 +++-- .../bin/tests/test_assemble_report.py | 15 ++++++--- .../bin/tests/test_chai_jira_adapter.py | 32 ++++++++++++++++++- .../edge-scrum/bin/tests/test_chai_report.py | 26 ++++++++++++++- 7 files changed, 101 insertions(+), 22 deletions(-) diff --git a/plugins/edge-scrum/bin/assemble-report.py b/plugins/edge-scrum/bin/assemble-report.py index 67a7835c..49684543 100644 --- a/plugins/edge-scrum/bin/assemble-report.py +++ b/plugins/edge-scrum/bin/assemble-report.py @@ -109,7 +109,7 @@ def build_blocks(checks, recs, params): elif not hidden_estimated and hidden.get("unpointed_open", 0) > 0: # Not estimated: the gap covers pointed work only; count the rest, never size it. verdict = (f"**{glyph} {risk} — pointed scope fits with ~{gap} SP to spare; " - f"{plural(hidden['unpointed_open'], 'unpointed item')} not counted.**") + f"{plural(hidden['unpointed_open'] + hidden.get('unpointed_bugs', 0), 'unpointed item')} not counted.**") else: verdict = f"**{glyph} {risk} — scope fits with {gap} SP to spare.**" verdict += (f" {plural(params['remaining_sprints'], 'dev sprint')} left · pencils down S{params['pencils_down']}" diff --git a/plugins/edge-scrum/bin/chai_jira_adapter.py b/plugins/edge-scrum/bin/chai_jira_adapter.py index e15c40d3..c59f8aed 100644 --- a/plugins/edge-scrum/bin/chai_jira_adapter.py +++ b/plugins/edge-scrum/bin/chai_jira_adapter.py @@ -94,7 +94,6 @@ _INVALID_FIELDS_RE = re.compile(r"^Invalid extra_fields entries:\s*(.*?)(?:\.\s+Use\b.*)?\.?$", re.I) _UNKNOWN_FIELDS_RE = re.compile(r"^Unknown fields:\s*(.*?)\.?$", re.I) _METADATA_RE = re.compile(r"^(Create metadata for\b|Issue types available\b|Requested fields:|\[[^\]]+\]$)", re.I) -_LINKED_HEADER_RE = re.compile(r"linked jira issues", re.I) _LINKED_LINE_RE = re.compile( r"^\s*[-*]\s*(?P[^:|]+?)\s*:\s*(?P" + KEY_PATTERN + r")\s*" r"(?:\((?P[^)]*)\))?\s*(?:\|\s*status\s*:\s*(?P[^|]*?)\s*)?(?:\|.*)?$", re.I) @@ -125,6 +124,9 @@ IGNORED_FIELDS = {"issuelinks"} # query_jira prints "(set, but ... cannot render ...)" LIST_FIELDS = {"components": "components", "labels": "labels", "fix versions": "fixVersions", "fixversions": "fixVersions", "fix version": "fixVersions"} +# Labels that may share a line with others (`` | ``-separated); see _segments. +KNOWN_LABELS = (set(PIPE_FIELDS) | CUSTOM_FIELDS | set(LIST_FIELDS) + | {"description", "parent", "resolutiondate"} | IGNORED_FIELDS) # Link wording → (type name, inward wording, outward wording). On issue A, # "is blocked by: B" means B is A's inward issue; "blocks: B" its outward one. @@ -232,7 +234,9 @@ def parse_issue_links(text): """ links, found, inside = [], False, False for line in (text or "").splitlines(): - if _LINKED_HEADER_RE.search(line) and not _LINKED_LINE_RE.match(line): + # The section starts only at a standalone "Linked Jira Issues:" header line + # (chai-bot prints it on its own line), never at prose containing the phrase. + if re.fullmatch(r"[*#>\s]*linked jira issues:?\s*", line, re.I): found, inside = True, True continue if not inside: @@ -284,10 +288,15 @@ def _split_blocks(text): def _segments(line): - """``Label: value | Label: value`` → label matches; one segment when any part has no label.""" + """``Label: value | Label: value`` → label matches. + + The line is split on `` | `` only when every part's label is a known field + label; otherwise it stays one value, so a value containing `` | Note: x`` + is not cut at the pipe. + """ parts = re.split(r"\s+\|\s+", line.strip()) matches = [_LABEL_RE.match(p) for p in parts] - if len(parts) > 1 and all(matches): + if len(parts) > 1 and all(m and m.group(1).strip().lower() in KNOWN_LABELS for m in matches): return matches m = _LABEL_RE.match(line) return [m] if m else [None] @@ -754,9 +763,10 @@ def adapt_fetch(manifest, roster=None): # An issue whose sprint value is not whole: its sprint data is incomplete, so # nothing that depends on sprints (velocity, activity) is computed from it. - sprint_incomplete = [i["key"] for i in issues if i.get("sprint_data_incomplete")] - for key in sprint_incomplete: - problems.append(f"{key}: {next(i for i in issues if i['key'] == key)['sprint_data_incomplete']}") + incomplete_why = {i["key"]: i["sprint_data_incomplete"] + for i in issues if i.get("sprint_data_incomplete")} + sprint_incomplete = list(incomplete_why) + problems.extend(f"{key}: {why}" for key, why in incomplete_why.items()) signals = set(manifest.get("signals") or []) skipped = [] # Unset custom fields print no line, so a missing Flagged line means "not @@ -767,7 +777,7 @@ def adapt_fetch(manifest, roster=None): other_rejected = sorted(rejected - {FLAGGED_FIELD}) if other_rejected: problems.append(f"query_jira rejected field(s): {', '.join(other_rejected)}") - if "description" in signals and not any("description" in i for i in issues): + if "description" in signals and issues and not any("description" in i for i in issues): skipped.append(SKIPPED_DESCRIPTION) warnings = [] if unknown_fields: diff --git a/plugins/edge-scrum/bin/chai_report.py b/plugins/edge-scrum/bin/chai_report.py index 7935eaef..2bef3b7f 100644 --- a/plugins/edge-scrum/bin/chai_report.py +++ b/plugins/edge-scrum/bin/chai_report.py @@ -16,7 +16,8 @@ import sys sys.path.insert(0, os.path.dirname(__file__)) -from _jira_transforms import DONE_STATUSES_SPRINT, IN_PROGRESS_STATUSES, days_between, is_stale # noqa: E402 +from _jira_transforms import ( # noqa: E402 + DONE_STATUSES_SPRINT, IN_PROGRESS_STATUSES, days_between, is_stale, normalize_name) from scrum_context import to_date, working_days # noqa: E402 NOT_PTO_ADJUSTED = "not PTO-adjusted" @@ -375,6 +376,7 @@ def release_health(checks, section, capacity=None, rank_available=True, velocity "scope_sp": scope, "capacity_sp": cap, "capacity_basis": meta.get("capacity_basis"), "capacity_label": (capacity or {}).get("label"), "effective_sprints": (capacity or {}).get("effective_sprints"), + "velocity_reason": velocity_reason, "remaining_sprints": meta.get("remaining_sprints"), "velocity": meta.get("team_velocity"), "gap_sp": meta.get("gap_sp"), # A count, never an SP estimate (decision 0018). @@ -480,7 +482,7 @@ def refinement_gaps(features, spikes, section, refinement_sprint_closed=False): gaps = len(out["missing_refinement_spike"]) + len(out["refinement_spike_overdue"]) if gaps: out["risks"].append({"severity": RED, - "text": f"{tag}: {gaps} of {out['features']} feature(s) without a closed or open refinement spike"}) + "text": f"{tag}: {gaps} of {out['features']} feature(s) without a closed refinement spike"}) tp = len(out["missing_test_plan_spike"]) + len(out["test_plan_spike_overdue"]) if tp: out["risks"].append({"severity": YELLOW, "text": f"{tag}: {tp} feature(s) missing a test plan spike"}) @@ -497,7 +499,8 @@ def mention(owner, mentions): """Slack mention for an owner when mapped (``<@U…>``), else the plain name.""" if not owner: return "no owner set" - return (mentions or {}).get(owner) or owner + key = normalize_name(owner) + return next((v for k, v in (mentions or {}).items() if normalize_name(k) == key), None) or owner def _sprint_figures(s): @@ -506,7 +509,7 @@ def _sprint_figures(s): cap = s.get("capacity") or {} cap_txt = f"{cap['capacity_sp']:g} SP ({cap['label']})" if cap.get("capacity_sp") is not None \ else "not available" - return f"committed {s['committed_sp']} SP in {s['issues']} issues; capacity {cap_txt}" + return f"committed {s['committed_sp']} SP in {s['issues']} issue{'s' if s['issues'] != 1 else ''}; capacity {cap_txt}" b = s.get("burndown", {}) if "expected_done_sp" in b: return (f"{b['done_sp']} of {b['total_sp']} SP done, {b['expected_done_sp']:g} expected " @@ -529,7 +532,8 @@ def _section_lines(sec): if sec.get("effective_sprints") is not None and sec.get("velocity") is not None else f"({sec.get('capacity_label') or sec.get('capacity_basis')})") if sec["capacity_sp"] is not None else - f"• Scope {sec['scope_sp']} SP; capacity not available (velocity not measured)"), + f"• Scope {sec['scope_sp']} SP; capacity not available " + f"({sec.get('velocity_reason') or 'velocity not measured'})"), f"• Below the cut line: {', '.join(sec['below_cut_line']) or 'none'}", f"• Features at risk: {', '.join(sec['features_at_risk']) or 'none'}"] if sec.get("unpointed_items"): diff --git a/plugins/edge-scrum/bin/scrum_context.py b/plugins/edge-scrum/bin/scrum_context.py index d5268c1b..d8864faa 100644 --- a/plugins/edge-scrum/bin/scrum_context.py +++ b/plugins/edge-scrum/bin/scrum_context.py @@ -529,8 +529,9 @@ def availability(absences, engineer_count, start, end): return round(max(0.0, 1 - len(off) / total), 2) -# Calendar window read for absences when the caller does not pass one: the -# window the SKILL documents for calendar reads (21 days back, 42 ahead). +# Fallback when the caller passes no window: 21 days back, 42 ahead — narrower +# than the 126-day-ahead read the SKILL documents, so an omitted window can +# only under-report availability, never compute it from absences never read. DEFAULT_ABSENCE_DAYS_BEFORE = 21 DEFAULT_ABSENCE_DAYS_AFTER = 42 OUTSIDE_CALENDAR_WINDOW = "not available (outside the calendar window read)" @@ -546,6 +547,9 @@ def absence_window(raw, today, warnings): today + dt.timedelta(days=DEFAULT_ABSENCE_DAYS_AFTER)) if not raw: return default + if not isinstance(raw, dict): + warnings.append("ignored absence_window: not an object; using the default window") + return default start, start_ok = source_date(raw.get("start")) end, end_ok = source_date(raw.get("end")) if not (start_ok and end_ok and start and end) or end < start: diff --git a/plugins/edge-scrum/bin/tests/test_assemble_report.py b/plugins/edge-scrum/bin/tests/test_assemble_report.py index 0390e7a8..e7c57d3f 100644 --- a/plugins/edge-scrum/bin/tests/test_assemble_report.py +++ b/plugins/edge-scrum/bin/tests/test_assemble_report.py @@ -350,13 +350,13 @@ def _write(self, d, name, obj): json.dump(obj, f) return path - def _story(self, key, sp, status="In Progress"): - return {"key": key, "summary": "Synthetic story", "type": "Story", "status": status, "sp": sp, + def _story(self, key, sp, status="In Progress", type_="Story"): + return {"key": key, "summary": "Synthetic story", "type": type_, "status": status, "sp": sp, "assignee": "aexample@example.com", "assignee_display": "Alex Example", "epic_key": "EPIC-1", "priority": "Major", "labels": [], "updated": "2030-01-20", "sprints": [], "flagged": False, "blocked_by": [], "stale": False} - def _run(self, d, extra): + def _run(self, d, extra, extra_stories=()): files = { "features": self._write(d, "features.json", {"features": [ {"key": "OCPSTRAT-9001", "summary": "Synthetic feature", "status": "In Progress", "priority": "Major", @@ -367,7 +367,7 @@ def _run(self, d, extra): "stories": self._write(d, "stories.json", {"stories": [ self._story("TEST-1", 5), self._story("TEST-2", 0), self._story("TEST-3", 0), self._story("TEST-4", 3, "Closed"), self._story("TEST-5", 5, "Closed"), - self._story("TEST-6", 2, "Closed"), self._story("TEST-7", 8, "Closed")]}), + self._story("TEST-6", 2, "Closed"), self._story("TEST-7", 8, "Closed"), *extra_stories]}), "bugs": self._write(d, "bugs.json", {"bugs": []}), "roster": self._write(d, "roster.json", {"members": [ {"username": "aexample@example.com", "display_name": "Alex Example", "sp_target": 8}]}), @@ -401,6 +401,13 @@ def test_report_renders_without_hidden_scope(self): # The gap covers pointed work only: the verdict counts the unpointed stories, never sizes them. self.assertIn("pointed scope fits with ~11 SP to spare; 2 unpointed items not counted.", report) + def test_not_estimated_verdict_counts_unpointed_bugs(self): + with tempfile.TemporaryDirectory() as d: + checks, report = self._run(d, ["--no-hidden-scope-estimate"], + extra_stories=[self._story("OCPBUGS-9", 0, type_="Bug")]) + self.assertEqual(checks["hidden_scope"]["unpointed_bugs"], 1) + self.assertIn("pointed scope fits with ~11 SP to spare; 3 unpointed items not counted.", report) + def test_default_run_still_shows_hidden_scope(self): with tempfile.TemporaryDirectory() as d: checks, report = self._run(d, []) diff --git a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py index 5b3354d9..0cc9106e 100644 --- a/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py +++ b/plugins/edge-scrum/bin/tests/test_chai_jira_adapter.py @@ -586,9 +586,25 @@ def test_missing_lines_are_empty(self): def test_unknown_lines_are_counted_in_a_warning(self): out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], extra=["Mystery: one | Another: two", "free text"])}]}) - self.assertIn("ignored 3 unrecognized line(s); labels: Another, Mystery", out["warnings"][0]) + # Unknown labels are not split on " | " (see test_pipe_inside_a_value_is_not_split). + self.assertIn("ignored 2 unrecognized line(s); labels: Mystery", out["warnings"][0]) self.assertTrue(out["fetch"]["complete"]) + def test_pipe_inside_a_value_is_not_split(self): + out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], extra=["Description: step one | Note: see docs"])}]}) + self.assertEqual(out["issues"][0]["description"], "step one | Note: see docs") + self.assertEqual(out["warnings"], []) + # Known labels still split. + self.assertEqual(out["issues"][0]["components"], [{"name": "Component A"}]) + self.assertEqual(out["issues"][0]["parent"]["key"], "TEST-1") + + def test_empty_fetch_reports_no_skipped_description(self): + out = ad.adapt_fetch({"pages": [{"text": SAMPLE_NO_RESULTS, "jql": SAMPLE_NO_RESULTS_JQL}], + "signals": ["description"]}) + self.assertEqual(out["skipped_signals"], []) + some = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"])}], "signals": ["description"]}) + self.assertEqual(some["skipped_signals"], [ad.SKIPPED_DESCRIPTION]) + def test_flagged_is_parsed_when_set(self): out = ad.adapt_fetch({"pages": [{"text": page(["TEST-5"], extra=["customfield_10021: Impediment"])}], "signals": ["flagged"]}) @@ -655,6 +671,20 @@ def test_wont_fix_only_blocker_is_not_blocked(self): row = transform_sprint_issues.transform_issue(adapted["issues"][0], "2030-02-05") self.assertEqual(row["blocked_by"], []) + def test_phrase_in_summary_does_not_hide_the_real_section(self): + text = SAMPLE_GET_ISSUE.replace("[TEST-303] Synthetic docs: final re-assembly", + "[TEST-303] Clean up linked Jira issues for feature X") + links, found = ad.parse_issue_links(text) + self.assertTrue(found) + self.assertEqual(extract_blocked_by({"issuelinks": links}), ["TEST-152", "TEST-154", "TEST-55"]) + + def test_phrase_in_description_with_bullets_adds_no_links(self): + text = ("Latest Jira State:\n[TEST-7] Synthetic\nType: Story | Status: To Do | Priority: Major\n" + "Description:\nWe track Linked Jira Issues: manually\n" + "- is blocked by: TEST-8 (Blocks) | status: To Do | u\n") + links, found = ad.parse_issue_links(text) + self.assertEqual((links, found), ([], False)) + def test_section_ends_at_next_field(self): links, _ = ad.parse_issue_links(SAMPLE_GET_ISSUE + "\n- is blocked by: TEST-99 (Blocks) | status: New | u\n") self.assertNotIn("TEST-99", [(l.get("inward_issue") or {}).get("key") for l in links]) diff --git a/plugins/edge-scrum/bin/tests/test_chai_report.py b/plugins/edge-scrum/bin/tests/test_chai_report.py index 61b541c2..8b63c5af 100644 --- a/plugins/edge-scrum/bin/tests/test_chai_report.py +++ b/plugins/edge-scrum/bin/tests/test_chai_report.py @@ -285,7 +285,7 @@ def test_nothing_to_flag(self): sprint = cr.sprint_health(clean, SPRINT, "2030-01-29", "capacity", velocity=24, availability=1.0) report = cr.build_report(sprint, []) self.assertEqual(report["top_post"][1], - "No risks this week: committed 20 SP in 1 issues; capacity 24 SP " + "No risks this week: committed 20 SP in 1 issue; capacity 24 SP " "(velocity 24 × availability 1.00)") self.assertEqual(report["top_post"][2], "0 items need action") self.assertTrue(report["top_post"][0].startswith("🟢")) @@ -343,6 +343,7 @@ def test_spike_gaps_and_owners(self): self.assertIsNone(owners["9.2: create a refinement spike that blocks FEAT-2 in sprint 16"]) self.assertEqual(owners["9.2: create a test plan spike that blocks FEAT-1"], "Quinn Quality") self.assertEqual(owners["9.2: move refinement spike TEST-60 (FEAT-3) to sprint 16"], "Alex Example") + self.assertIn("9.2: 2 of 3 feature(s) without a closed refinement spike", [r["text"] for r in out["risks"]]) class NoEstimateTest(unittest.TestCase): @@ -371,6 +372,15 @@ def test_run_checks_can_skip_the_estimator(self): self.assertIsNotNone(estimated["estimate_low"]) # Claude Code path unchanged +class AbsenceWindowTest(unittest.TestCase): + def test_non_object_window_falls_back_with_a_warning(self): + for bad in ("126 days", ["2030-01-01", "2030-03-01"]): + out = sc.build_context({"today": "2030-01-29", "board_id": BOARD, "sprints": _sprints(), + "absences": [], "engineer_count": 2, "absence_window": bad}) + self.assertIn("ignored absence_window: not an object; using the default window", out["warnings"]) + self.assertEqual(out["current_sprint_availability"], 1.0) + + class CurrentSprintCapacityTest(unittest.TestCase): """Release capacity counts the current sprint for its remaining working days only.""" @@ -448,6 +458,20 @@ def test_velocity_reason_is_carried_into_left_out(self): self.assertEqual(cr.release_health(checks, {"release": "9.1", "phase": "development"})["left_out"][0]["why"], "velocity not available") + def test_capacity_line_uses_the_velocity_reason(self): + checks = dict(CHECKS, meta=dict(CHECKS["meta"], capacity_basis="roster_fallback")) + release = cr.release_health(checks, {"release": "9.1", "phase": "development"}, + velocity_reason="only 2 closed sprint(s) measured, 3 needed") + lines = "\n".join(cr._section_lines(dict(release, kind="release-health"))) + self.assertIn("capacity not available (only 2 closed sprint(s) measured, 3 needed)", lines) + self.assertNotIn("velocity not measured", lines) + + def test_mention_matches_case_and_space_insensitively(self): + mentions = {"Alex Example": "<@U000TEST1>"} + self.assertEqual(cr.mention("alex EXAMPLE", mentions), "<@U000TEST1>") + self.assertEqual(cr.mention("Quinn Quality", mentions), "Quinn Quality") + self.assertEqual(cr.mention(None, mentions), "no owner set") + def test_select_sections_accepts_full_scrum_context_results(self): sections, missing = cr.select_sections({}, [{"today": "2030-02-01", "release": _window("9.1", "development")}]) self.assertEqual([s["release"] for s in sections], ["9.1"]) From 3c1e5990c3cd7c1ec0be0a64df953c6e49df1a28 Mon Sep 17 00:00:00 2001 From: nhamza Date: Sun, 15 Nov 2026 12:05:31 +0200 Subject: [PATCH 5/6] edge-scrum: one definition of sprint days remaining (OCPEDGE-3121) transform-sprints counted today in both days_elapsed and days_remaining, so the two summed to total + 1, while chai_report used total - elapsed. Both now use _jira_transforms.sprint_day_counts: elapsed counts today, remaining = total - elapsed. Claude Code's days_remaining is one lower than before; chai's numbers are unchanged. Assisted-by: Claude --- plugins/edge-scrum/bin/_jira_transforms.py | 13 +++++++ plugins/edge-scrum/bin/chai_report.py | 9 ++--- .../bin/tests/test_transform_sprints.py | 36 +++++++++++++++++++ plugins/edge-scrum/bin/transform-sprints.py | 6 ++-- 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 plugins/edge-scrum/bin/tests/test_transform_sprints.py diff --git a/plugins/edge-scrum/bin/_jira_transforms.py b/plugins/edge-scrum/bin/_jira_transforms.py index 12acb0e3..89b1ded7 100755 --- a/plugins/edge-scrum/bin/_jira_transforms.py +++ b/plugins/edge-scrum/bin/_jira_transforms.py @@ -365,6 +365,19 @@ def days_between(start, end, inclusive=True): return delta + 1 if inclusive else delta +def sprint_day_counts(start, end, today): + """``(days elapsed, days remaining, total days)`` of a sprint, in calendar days. + + The single definition shared by ``transform-sprints.py`` (Claude Code) and + ``chai_report.py`` (chai-bot). ``total`` counts the start and end days; + ``elapsed`` counts today, clamped to ``[0, total]``; ``remaining`` is + ``total − elapsed``, so ``elapsed + remaining == total``. + """ + total = days_between(start, end) + elapsed = max(0, min(days_between(start, today), total)) + return elapsed, total - elapsed, total + + def business_days_between(start, end): """Business days (Mon-Fri) between start (exclusive) and end (inclusive).""" s = parse_date(start) diff --git a/plugins/edge-scrum/bin/chai_report.py b/plugins/edge-scrum/bin/chai_report.py index 2bef3b7f..65f6cca5 100644 --- a/plugins/edge-scrum/bin/chai_report.py +++ b/plugins/edge-scrum/bin/chai_report.py @@ -17,7 +17,7 @@ sys.path.insert(0, os.path.dirname(__file__)) from _jira_transforms import ( # noqa: E402 - DONE_STATUSES_SPRINT, IN_PROGRESS_STATUSES, days_between, is_stale, normalize_name) + DONE_STATUSES_SPRINT, IN_PROGRESS_STATUSES, is_stale, normalize_name, sprint_day_counts) from scrum_context import to_date, working_days # noqa: E402 NOT_PTO_ADJUSTED = "not PTO-adjusted" @@ -206,11 +206,8 @@ def select_sections(context, windows=()): def _days(sprint, today): - """(days elapsed, days remaining, total days) of a sprint, calendar days, as transform-sprints counts them.""" - start, end = sprint.get("start"), sprint.get("end") - total = days_between(start, end) - elapsed = max(0, min(days_between(start, today), total)) - return elapsed, total - elapsed, total + """(days elapsed, days remaining, total days): the shared ``sprint_day_counts`` definition.""" + return sprint_day_counts(sprint.get("start"), sprint.get("end"), today) def _stale(issue, today): diff --git a/plugins/edge-scrum/bin/tests/test_transform_sprints.py b/plugins/edge-scrum/bin/tests/test_transform_sprints.py new file mode 100644 index 00000000..60139506 --- /dev/null +++ b/plugins/edge-scrum/bin/tests/test_transform_sprints.py @@ -0,0 +1,36 @@ +"""Tests for the sprint day counts shared by transform-sprints.py and chai_report.py. + +Synthetic dates only. +""" + +import os +import sys +import unittest +from importlib import import_module + +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) +from _jira_transforms import sprint_day_counts # noqa: E402 + +transform_sprints = import_module("transform-sprints") +SPRINT = {"id": 501, "name": "Test Sprint 11", "goal": None, "start": "2030-01-28", "end": "2030-02-18", + "state": "active"} + + +class SprintDayCountsTest(unittest.TestCase): + def test_remaining_is_total_minus_elapsed(self): + self.assertEqual(sprint_day_counts("2030-01-28", "2030-02-18", "2030-02-07"), (11, 11, 22)) + self.assertEqual(sprint_day_counts("2030-01-28", "2030-02-18", "2030-01-28"), (1, 21, 22)) + self.assertEqual(sprint_day_counts("2030-01-28", "2030-02-18", "2030-02-18"), (22, 0, 22)) + + def test_clamped_outside_the_sprint(self): + self.assertEqual(sprint_day_counts("2030-01-28", "2030-02-18", "2030-01-01"), (0, 22, 22)) + self.assertEqual(sprint_day_counts("2030-01-28", "2030-02-18", "2030-03-30"), (22, 0, 22)) + + def test_build_target_sprint_uses_the_shared_definition(self): + target = transform_sprints.build_target_sprint(SPRINT, "2030-02-07") + self.assertEqual((target["days_elapsed"], target["days_remaining"], target["total_days"]), (11, 11, 22)) + self.assertEqual(target["days_elapsed"] + target["days_remaining"], target["total_days"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/plugins/edge-scrum/bin/transform-sprints.py b/plugins/edge-scrum/bin/transform-sprints.py index f01e3617..19a23757 100755 --- a/plugins/edge-scrum/bin/transform-sprints.py +++ b/plugins/edge-scrum/bin/transform-sprints.py @@ -10,7 +10,7 @@ from _jira_transforms import ( extract_sprint_number, format_date, - days_between, + sprint_day_counts, load_sprints, write_output, ) @@ -36,9 +36,7 @@ def build_target_sprint(sprint, today): if sprint["start"] is None or sprint["end"] is None: return None - total = days_between(sprint["start"], sprint["end"]) - elapsed = max(0, min(days_between(sprint["start"], today), total)) - remaining = max(0, min(days_between(today, sprint["end"]), total)) + elapsed, remaining, total = sprint_day_counts(sprint["start"], sprint["end"], today) return { "id": sprint["id"], From c537567ad2dab72a23538a984159a344cd36b7fb Mon Sep 17 00:00:00 2001 From: nhamza Date: Sun, 15 Nov 2026 17:03:53 +0200 Subject: [PATCH 6/6] edge-scrum: refinement-spike wording, scrum-context horizon, docstring (OCPEDGE-3121) - chai_report: the refinement risk counts missing or overdue spikes, so it now says "with a missing or overdue refinement spike". - scrum_context: project sprints through max(horizon end, current sprint end), so the current sprint is generated even when the branch-cut sprint ends before it. - _jira_transforms: ASCII "-" in the sprint_day_counts docstring (RUF002). Assisted-by: Claude --- plugins/edge-scrum/bin/_jira_transforms.py | 2 +- plugins/edge-scrum/bin/chai_report.py | 2 +- plugins/edge-scrum/bin/scrum_context.py | 5 +++-- .../edge-scrum/bin/tests/test_chai_report.py | 18 +++++++++++++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/plugins/edge-scrum/bin/_jira_transforms.py b/plugins/edge-scrum/bin/_jira_transforms.py index 89b1ded7..1ab536a0 100755 --- a/plugins/edge-scrum/bin/_jira_transforms.py +++ b/plugins/edge-scrum/bin/_jira_transforms.py @@ -371,7 +371,7 @@ def sprint_day_counts(start, end, today): The single definition shared by ``transform-sprints.py`` (Claude Code) and ``chai_report.py`` (chai-bot). ``total`` counts the start and end days; ``elapsed`` counts today, clamped to ``[0, total]``; ``remaining`` is - ``total − elapsed``, so ``elapsed + remaining == total``. + ``total - elapsed``, so ``elapsed + remaining == total``. """ total = days_between(start, end) elapsed = max(0, min(days_between(start, today), total)) diff --git a/plugins/edge-scrum/bin/chai_report.py b/plugins/edge-scrum/bin/chai_report.py index 65f6cca5..98121382 100644 --- a/plugins/edge-scrum/bin/chai_report.py +++ b/plugins/edge-scrum/bin/chai_report.py @@ -479,7 +479,7 @@ def refinement_gaps(features, spikes, section, refinement_sprint_closed=False): gaps = len(out["missing_refinement_spike"]) + len(out["refinement_spike_overdue"]) if gaps: out["risks"].append({"severity": RED, - "text": f"{tag}: {gaps} of {out['features']} feature(s) without a closed refinement spike"}) + "text": f"{tag}: {gaps} of {out['features']} feature(s) with a missing or overdue refinement spike"}) tp = len(out["missing_test_plan_spike"]) + len(out["test_plan_spike_overdue"]) if tp: out["risks"].append({"severity": YELLOW, "text": f"{tag}: {tp} feature(s) missing a test plan spike"}) diff --git a/plugins/edge-scrum/bin/scrum_context.py b/plugins/edge-scrum/bin/scrum_context.py index d8864faa..6a168614 100644 --- a/plugins/edge-scrum/bin/scrum_context.py +++ b/plugins/edge-scrum/bin/scrum_context.py @@ -597,8 +597,9 @@ def availability_by_sprint(sprints, current, horizon_end, absences, engineer_cou if current is None or horizon_end is None: return [] out = [] - for s in project_sprints(sprints, horizon_end): - if s["start"] < current["start"] or s["end"] > max(horizon_end, current["end"]): + until = max(horizon_end, current["end"]) # always reach the current sprint + for s in project_sprints(sprints, until): + if s["start"] < current["start"] or s["end"] > until: continue out.append({**_sprint_out(s), "availability": sprint_availability(s, absences, engineer_count, window)}) diff --git a/plugins/edge-scrum/bin/tests/test_chai_report.py b/plugins/edge-scrum/bin/tests/test_chai_report.py index 8b63c5af..db2eaff2 100644 --- a/plugins/edge-scrum/bin/tests/test_chai_report.py +++ b/plugins/edge-scrum/bin/tests/test_chai_report.py @@ -343,7 +343,8 @@ def test_spike_gaps_and_owners(self): self.assertIsNone(owners["9.2: create a refinement spike that blocks FEAT-2 in sprint 16"]) self.assertEqual(owners["9.2: create a test plan spike that blocks FEAT-1"], "Quinn Quality") self.assertEqual(owners["9.2: move refinement spike TEST-60 (FEAT-3) to sprint 16"], "Alex Example") - self.assertIn("9.2: 2 of 3 feature(s) without a closed refinement spike", [r["text"] for r in out["risks"]]) + self.assertIn("9.2: 2 of 3 feature(s) with a missing or overdue refinement spike", + [r["text"] for r in out["risks"]]) class NoEstimateTest(unittest.TestCase): @@ -372,6 +373,21 @@ def test_run_checks_can_skip_the_estimator(self): self.assertIsNotNone(estimated["estimate_low"]) # Claude Code path unchanged +class ProjectionHorizonTest(unittest.TestCase): + def test_current_sprint_after_the_branch_cut_sprint_is_still_generated(self): + # One board sprint ending 2030-01-28; today falls in a projected sprint + # (2030-03-11 .. 2030-04-01) after a release horizon ending 2030-02-18. + sprints = sc.board_sprints([{"id": "500", "name": "Test Sprint 10", "state": "closed", + "originBoardId": BOARD, "start_date": "2030-01-07", + "end_date": "2030-01-28"}], BOARD) + today = D(2030, 3, 12) + current = sc.current_sprint(sprints, today) + self.assertEqual((current["number"], current["projected"]), (13, True)) + window = sc.absence_window(None, today, []) + out = sc.availability_by_sprint(sprints, current, D(2030, 2, 18), [], 2, window) + self.assertEqual([(s["number"], s["availability"]) for s in out], [(13, 1.0)]) + + class AbsenceWindowTest(unittest.TestCase): def test_non_object_window_falls_back_with_a_warning(self): for bad in ("126 days", ["2030-01-01", "2030-03-01"]):