From d2df8429b79b020e7a371a30f9879961a3d4cea9 Mon Sep 17 00:00:00 2001 From: David Butenhof Date: Wed, 18 Nov 2026 14:01:07 -0500 Subject: [PATCH 1/2] Massive Codex-driven optimization This improves the use of REST and graphql GitHub APIs in order to optimize the API across the three different planner tabs. Signed-off-by: David Butenhof --- backend/src/github_pm/api.py | 566 +++---- backend/src/github_pm/execution.py | 24 + backend/src/github_pm/github_client.py | 311 ++++ backend/src/github_pm/issue_hierarchy.py | 78 +- backend/src/github_pm/planning_reader.py | 162 ++ backend/src/github_pm/report_queries.py | 425 +++++ backend/src/github_pm/repository_catalog.py | 25 + backend/src/github_pm/sdlc_api.py | 28 +- backend/src/github_pm/sdlc_metrics.py | 412 +---- backend/src/github_pm/sdlc_models.py | 8 + backend/src/github_pm/sdlc_report_cli.py | 18 +- backend/src/github_pm/sdlc_service.py | 597 +++---- backend/src/github_pm/status_report_api.py | 8 +- .../src/github_pm/status_report_service.py | 52 +- backend/tests/test_api.py | 1386 +---------------- backend/tests/test_github_client.py | 195 +++ backend/tests/test_planning_optimization.py | 406 +++++ backend/tests/test_report_queries.py | 149 ++ backend/tests/test_sdlc_api.py | 460 +++--- backend/tests/test_sdlc_metrics.py | 10 +- backend/tests/test_sdlc_report_cli.py | 20 +- backend/tests/test_status_report_api.py | 203 +-- .../github-api-optimization-implementation.md | 52 + docs/github-api-optimization.md | 206 +++ frontend/src/App.jsx | 44 +- frontend/src/App.test.jsx | 14 +- frontend/src/components/CommentCard.jsx | 50 +- frontend/src/components/IssueCard.jsx | 106 +- frontend/src/components/IssueCard.test.jsx | 11 +- frontend/src/components/ManageLabels.jsx | 5 +- frontend/src/components/ManageMilestones.jsx | 5 +- frontend/src/components/ManageSort.jsx | 3 +- .../src/components/MarkdownInputModal.jsx | 5 +- frontend/src/components/MilestoneCard.jsx | 104 +- .../src/components/MilestoneCard.test.jsx | 49 +- .../src/components/PlanningDnDContext.jsx | 8 + .../src/components/ProjectStatusPanel.jsx | 23 +- frontend/src/components/SdlcKpisPanel.jsx | 28 +- .../src/components/SdlcKpisPanel.test.jsx | 60 +- frontend/src/services/api.js | 300 ++-- frontend/src/services/api.test.js | 27 + frontend/src/test/setup.js | 25 +- frontend/src/utils/assigneesCache.js | 20 +- frontend/src/utils/issueHierarchy.js | 21 + frontend/src/utils/issueHierarchy.test.js | 22 + frontend/src/utils/labelsCache.js | 20 +- frontend/src/utils/milestonesCache.js | 20 +- frontend/src/utils/queryCache.js | 110 ++ frontend/src/utils/queryCache.test.js | 97 ++ frontend/src/utils/useReactions.js | 35 + frontend/src/utils/useReactions.test.jsx | 53 + 51 files changed, 3738 insertions(+), 3328 deletions(-) create mode 100644 backend/src/github_pm/execution.py create mode 100644 backend/src/github_pm/github_client.py create mode 100644 backend/src/github_pm/planning_reader.py create mode 100644 backend/src/github_pm/report_queries.py create mode 100644 backend/src/github_pm/repository_catalog.py create mode 100644 backend/tests/test_github_client.py create mode 100644 backend/tests/test_planning_optimization.py create mode 100644 backend/tests/test_report_queries.py create mode 100644 docs/github-api-optimization-implementation.md create mode 100644 docs/github-api-optimization.md create mode 100644 frontend/src/utils/queryCache.js create mode 100644 frontend/src/utils/queryCache.test.js create mode 100644 frontend/src/utils/useReactions.js create mode 100644 frontend/src/utils/useReactions.test.jsx diff --git a/backend/src/github_pm/api.py b/backend/src/github_pm/api.py index 82f2be0..1e13d55 100644 --- a/backend/src/github_pm/api.py +++ b/backend/src/github_pm/api.py @@ -1,32 +1,32 @@ +"""HTTP routes for Planning. Assisted-by: Codex.""" + from collections import defaultdict -from collections.abc import Callable from datetime import datetime -import re import time from typing import Annotated, Any, AsyncGenerator, Literal, NoReturn -from urllib.parse import quote_plus +from urllib.parse import quote from fastapi import APIRouter, Body, Depends, HTTPException, Path, Query from pydantic import BaseModel, Field import requests +from github_pm import planning_reader as pr from github_pm.context import context +from github_pm.execution import offload +from github_pm.github_client import Connector from github_pm.issue_hierarchy import ( apply_graphql_hierarchy, apply_graphql_links, build_issue_forest, - collect_descendant_numbers, - is_ancestor, - ISSUE_HIERARCHY_GRAPHQL, - PARENT_ONLY_GRAPHQL, ) from github_pm.logger import logger +from github_pm.report_queries import execute +from github_pm.repository_catalog import catalog, VERSION_MATCH api_router = APIRouter() # We sort "semver" style milestones first, then others alphabetically -VERSION_MATCH = re.compile(r"^v\d+\.\d+\.\d+$") # Assisted-by: openai-code-assist @@ -43,168 +43,10 @@ def _milestone_sort_key(milestone: dict[str, Any]) -> tuple: return (1, (), title) -# Bounded retries for transient GitHub gateway timeouts (504). -_GITHUB_504_MAX_ATTEMPTS = 5 -_GITHUB_504_BACKOFF_SEC = 1.5 - -# full+json returns raw markdown ``body`` and rendered ``body_html``. -# html+json alone omits ``body``, which breaks edit-in-place flows. -# Assisted-by: Cursor +# Raw Markdown and GitHub-rendered HTML are both required by the editors. _GITHUB_BODY_ACCEPT = {"Accept": "application/vnd.github.full+json"} -class Connector: - def __init__(self, github_token: str, *, github_repo: str | None = None): - """Initialize a GitHub connection. - - Args: - github_token: The GitHub Personal Access Token to use - github_repo: ``owner/name``; defaults to ``context.github_repo`` when omitted. - """ - self.github_token = github_token - self.base_url = "https://api.github.com" - repo = github_repo if github_repo is not None else context.github_repo - self.owner, self.repo = repo.split("/", maxsplit=1) - self.github = requests.session() - self.github.headers.update( - { - "Authorization": f"Bearer {self.github_token}", - "Accept": "application/vnd.github+json", - "User-Agent": "Project-Manager", - "X-GitHub-Api-Version": "2022-11-28", - } - ) - logger.info( - "Initializing GitHub Connector service to %s/%s", - self.base_url, - repo, - ) - - def _with_504_retry( - self, request: Callable[[], requests.Response] - ) -> requests.Response: - """Perform one HTTP call, retrying on 504 with a short capped backoff.""" - for attempt in range(_GITHUB_504_MAX_ATTEMPTS): - response = request() - logger.info("GOT %s: %s", response.status_code, response.url) - if response.status_code == 504 and attempt < _GITHUB_504_MAX_ATTEMPTS - 1: - delay = _GITHUB_504_BACKOFF_SEC * (attempt + 1) - logger.warning( - "GitHub API 504 Gateway Timeout; waiting %.1fs before retry %d/%d", - delay, - attempt + 2, - _GITHUB_504_MAX_ATTEMPTS, - ) - time.sleep(delay) - continue - response.raise_for_status() - self.response = response - return response - - def get(self, path: str, headers: dict[str, str] | None = None) -> dict: - response = self._with_504_retry( - lambda: self.github.get(f"{self.base_url}{path}", headers=headers) - ) - return response.json() - - def get_paged(self, path: str, headers: dict[str, str] | None = None) -> list[dict]: - url: str | None = f"{self.base_url}{path}" - results = [] - while url: - response = self._with_504_retry( - lambda u=url: self.github.get(u, headers=headers) - ) - data = response.json() - logger.debug(f"{url}: {len(data)}") - results.extend(data) - url = None - if response.headers.get("link"): - links = response.headers.get("link") - for link in links.split(","): - if 'rel="next"' in link: - url = link.split(";")[0].strip().strip("<>") - logger.debug(f"paging to: {url}") - break - return results - - def search_issue_items( - self, search_query: str, headers: dict[str, str] | None = None - ) -> list[dict]: - """Run ``GET /search/issues`` with pagination; returns the ``items`` array union.""" - q_param = quote_plus(search_query) - url: str | None = f"{self.base_url}/search/issues?q={q_param}&per_page=100" - results: list[dict] = [] - while url: - response = self._with_504_retry( - lambda u=url: self.github.get(u, headers=headers) - ) - data = response.json() - items = data.get("items") - if isinstance(items, list): - results.extend(items) - url = None - link_header = response.headers.get("link") - if link_header: - for link in link_header.split(","): - if 'rel="next"' in link: - url = link.split(";")[0].strip().strip("<>") - logger.debug("search/issues paging to: %s", url) - break - return results - - def patch( - self, path: str, data: dict[str, Any], headers: dict[str, str] | None = None - ) -> dict: - response = self._with_504_retry( - lambda: self.github.patch( - f"{self.base_url}{path}", json=data, headers=headers - ) - ) - return response.json() - - def post( - self, path: str, data: dict[str, Any], headers: dict[str, str] | None = None - ) -> dict: - response = self._with_504_retry( - lambda: self.github.post( - f"{self.base_url}{path}", json=data, headers=headers - ) - ) - return response.json() if response.content else {} - - def post_text( - self, path: str, text: str, headers: dict[str, str] | None = None - ) -> str: - """POST plain text (e.g. ``/markdown/raw``); returns response body as text. - - Generated-by: Cursor - """ - request_headers = {"Content-Type": "text/plain; charset=utf-8"} - if headers: - request_headers.update(headers) - response = self._with_504_retry( - lambda: self.github.post( - f"{self.base_url}{path}", - data=text.encode("utf-8"), - headers=request_headers, - ) - ) - return response.text - - def delete( - self, - path: str, - data: dict[str, Any] | None = None, - headers: dict[str, str] | None = None, - ) -> dict: - response = self._with_504_retry( - lambda: self.github.delete( - f"{self.base_url}{path}", json=data, headers=headers - ) - ) - return response.json() if response.content else {} - - def _github_http_error_detail(exc: requests.HTTPError) -> str: """Best-effort message from a GitHub REST error response. @@ -244,7 +86,9 @@ async def connection() -> AsyncGenerator[Connector]: """FastAPI Dependency to open & close Github connections""" connector = None try: - connector = Connector(github_token=context.github_token) + connector = Connector( + github_token=context.github_token, github_repo=context.github_repo + ) except Exception as e: logger.exception(f"Error opening GitHub service: {e}") raise HTTPException( @@ -264,10 +108,13 @@ async def connection() -> AsyncGenerator[Connector]: raise HTTPException( status_code=400, detail=f"Can't open repository: {str(e)!r}" ) + finally: + connector.close() @api_router.get("/project") -async def get_project(): +@offload +def get_project(): return { "app_name": context.app_name, "github_repo": context.github_repo, @@ -295,7 +142,8 @@ def _sort_items_by_labels(items: list[dict], sort_by: list[str]) -> list[dict]: @api_router.get("/issues/{milestone_number}") -async def get_issues( +@offload +def get_issues( gitctx: Annotated[Connector, Depends(connection)], milestone_number: Annotated[int, Path(title="Milestone")], sort: Annotated[ @@ -319,34 +167,12 @@ async def get_issues( f"/repos/{context.github_repo}/issues?milestone={milestone}&state=open", headers=_GITHUB_BODY_ACCEPT, ) - issues: list[dict] = [] - pull_requests: list[dict] = [] - for i in raw_items: - if "pull_request" in i: - pull_requests.append(i) - continue - try: - response = gitctx.post( - "/graphql", - data={ - "query": ISSUE_HIERARCHY_GRAPHQL, - "variables": { - "owner": gitctx.owner, - "repo": gitctx.repo, - "issue": i["number"], - }, - }, - ) - data = response["data"] - issue_node = data["repository"]["issue"] - apply_graphql_links(i, issue_node) - apply_graphql_hierarchy(i, issue_node) - except Exception as e: - logger.exception( - f"Error enriching hierarchy/PRs for issue {i['number']}: {e!r}" - ) - continue - issues.append(i) + issues = [item for item in raw_items if "pull_request" not in item] + pull_requests = [item for item in raw_items if "pull_request" in item] + enriched = pr.enrich_issues(gitctx, issues) + for issue in issues: + apply_graphql_links(issue, enriched[issue["number"]]) + apply_graphql_hierarchy(issue, enriched[issue["number"]]) forest = build_issue_forest(issues, sort_by, _sort_items_by_labels) sorted_prs = _sort_items_by_labels(pull_requests, sort_by) logger.debug( @@ -360,7 +186,8 @@ async def get_issues( @api_router.get("/issue/{issue_number}") -async def get_issue( +@offload +def get_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], ): @@ -369,30 +196,14 @@ async def get_issue( headers=_GITHUB_BODY_ACCEPT, ) if "pull_request" not in issue: - try: - response = gitctx.post( - "/graphql", - data={ - "query": ISSUE_HIERARCHY_GRAPHQL, - "variables": { - "owner": gitctx.owner, - "repo": gitctx.repo, - "issue": issue["number"], - }, - }, - ) - data = response["data"] - issue_node = data["repository"]["issue"] - apply_graphql_links(issue, issue_node) - except Exception as e: - logger.exception( - f"Error finding linked PRs/dependencies for issue {issue['number']}: {e!r}" - ) + node = pr.enrich_issues(gitctx, [issue])[issue["number"]] + apply_graphql_links(issue, node) return issue @api_router.get("/comments/{issue_number}") -async def get_comments( +@offload +def get_comments( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], ): @@ -404,7 +215,7 @@ async def get_comments( logger.debug( f"{len(comments)} issue {issue_number} comments: {time.time() - start:.3f} seconds" ) - return comments + return pr.comment_reactions(gitctx, comments) class CreateComment(BaseModel): @@ -417,7 +228,8 @@ class CreateComment(BaseModel): @api_router.post("/comments/{issue_number}") -async def create_comment( +@offload +def create_comment( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], comment: Annotated[CreateComment, Body(title="Comment")], @@ -445,7 +257,8 @@ class UpdateComment(BaseModel): @api_router.patch("/comments/{comment_id}/body") -async def update_comment( +@offload +def update_comment( gitctx: Annotated[Connector, Depends(connection)], comment_id: Annotated[int, Path(title="Comment ID")], comment: Annotated[UpdateComment, Body(title="Comment")], @@ -473,7 +286,8 @@ class UpdateIssueBody(BaseModel): @api_router.patch("/issues/{issue_number}/body") -async def update_issue_body( +@offload +def update_issue_body( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], payload: Annotated[UpdateIssueBody, Body(title="Issue Body")], @@ -501,7 +315,8 @@ class UpdateIssueTitle(BaseModel): @api_router.patch("/issues/{issue_number}/title") -async def update_issue_title( +@offload +def update_issue_title( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], payload: Annotated[UpdateIssueTitle, Body(title="Issue Title")], @@ -528,7 +343,8 @@ class UpdateIssueType(BaseModel): # Assisted-by: openai-code-assist @api_router.patch("/issues/{issue_number}/type") -async def update_issue_type( +@offload +def update_issue_type( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], payload: Annotated[UpdateIssueType, Body(title="Issue Type")], @@ -554,7 +370,8 @@ class CloseIssueRequest(BaseModel): @api_router.post("/issues/{issue_number}/close-with-comment") -async def close_issue_with_comment( +@offload +def close_issue_with_comment( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], comment: Annotated[CloseIssueRequest, Body(title="Comment")], @@ -591,7 +408,8 @@ class RenderMarkdown(BaseModel): @api_router.post("/markdown") -async def render_markdown( +@offload +def render_markdown( gitctx: Annotated[Connector, Depends(connection)], payload: Annotated[RenderMarkdown, Body(title="Markdown")], ): @@ -627,7 +445,8 @@ class CreateIssue(BaseModel): @api_router.post("/issues") -async def create_issue( +@offload +def create_issue( gitctx: Annotated[Connector, Depends(connection)], issue: Annotated[CreateIssue, Body(title="Issue")], ): @@ -673,7 +492,8 @@ async def create_issue( @api_router.get("/issues/{issue_number}/reactions") -async def get_issue_reactions( +@offload +def get_issue_reactions( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], ): @@ -689,7 +509,8 @@ async def get_issue_reactions( @api_router.get("/comments/{comment_id}/reactions") -async def get_comment_reactions( +@offload +def get_comment_reactions( gitctx: Annotated[Connector, Depends(connection)], comment_id: Annotated[int, Path(title="Comment")], ): @@ -704,17 +525,15 @@ async def get_comment_reactions( @api_router.get("/milestones") -async def get_milestones( +@offload +def get_milestones( gitctx: Annotated[Connector, Depends(connection)], state: Annotated[ Literal["open", "closed"], Query(title="Milestone State"), ] = "open", ): - milestones = gitctx.get_paged( - f"/repos/{context.github_repo}/milestones?state={state}", - headers=_GITHUB_BODY_ACCEPT, - ) + milestones = catalog(gitctx, "milestones", state=state) milestones = sorted(milestones, key=_milestone_sort_key) if state == "open": milestones.append( @@ -747,7 +566,8 @@ class UpdateMilestoneState(BaseModel): @api_router.post("/milestones") -async def create_milestone( +@offload +def create_milestone( gitctx: Annotated[Connector, Depends(connection)], milestone: Annotated[CreateMilestone, Body(title="Milestone")], ): @@ -766,7 +586,8 @@ async def create_milestone( @api_router.patch("/milestones/{milestone_number}") -async def update_milestone( +@offload +def update_milestone( gitctx: Annotated[Connector, Depends(connection)], milestone_number: Annotated[int, Path(title="Milestone")], milestone: Annotated[UpdateMilestone, Body(title="Milestone")], @@ -783,25 +604,40 @@ async def update_milestone( # Assisted-by: openai-code-assist @api_router.get("/milestones/{milestone_number}/open-counts") -async def get_milestone_open_counts( +@offload +def get_milestone_open_counts( gitctx: Annotated[Connector, Depends(connection)], milestone_number: Annotated[int, Path(title="Milestone")], ): """Return open issue and pull-request counts for a milestone.""" - items = gitctx.get_paged( - f"/repos/{context.github_repo}/issues?milestone={milestone_number}&state=open", - headers=_GITHUB_BODY_ACCEPT, + data = execute( + pr._post(gitctx), + """ +query($owner:String!,$repo:String!,$number:Int!) { + repository(owner:$owner,name:$repo) { + milestone(number:$number) { + issues(states:[OPEN],first:1) { totalCount } + pullRequests(states:[OPEN],first:1) { totalCount } + } + } + rateLimit { cost } +} +""", + {"owner": gitctx.owner, "repo": gitctx.repo, "number": milestone_number}, ) - open_pull_requests = sum("pull_request" in item for item in items) + milestone = (data.get("repository") or {}).get("milestone") + if milestone is None: + raise HTTPException(status_code=404, detail="Milestone not found") return { - "open_issues": len(items) - open_pull_requests, - "open_pull_requests": open_pull_requests, + "open_issues": milestone["issues"]["totalCount"], + "open_pull_requests": milestone["pullRequests"]["totalCount"], } # Assisted-by: openai-code-assist @api_router.patch("/milestones/{milestone_number}/state") -async def update_milestone_state( +@offload +def update_milestone_state( gitctx: Annotated[Connector, Depends(connection)], milestone_number: Annotated[int, Path(title="Milestone")], milestone: Annotated[UpdateMilestoneState, Body(title="Milestone State")], @@ -814,7 +650,8 @@ async def update_milestone_state( @api_router.delete("/milestones/{milestone_number}") -async def delete_milestone( +@offload +def delete_milestone( gitctx: Annotated[Connector, Depends(connection)], milestone_number: Annotated[int, Path(title="Milestone")], ): @@ -823,7 +660,8 @@ async def delete_milestone( @api_router.post("/issues/{issue_number}/milestone/{milestone_number}") -async def add_milestone_to_issue( +@offload +def add_milestone_to_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], milestone_number: Annotated[int, Path(title="Milestone")], @@ -836,7 +674,8 @@ async def add_milestone_to_issue( @api_router.delete("/issues/{issue_number}/milestone/{milestone_number}") -async def remove_milestone_from_issue( +@offload +def remove_milestone_from_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], milestone_number: Annotated[int, Path(title="Milestone")], @@ -848,63 +687,47 @@ async def remove_milestone_from_issue( return issue -def _list_sub_issue_numbers(gitctx: Connector, issue_number: int) -> list[int]: - """List child issue numbers via REST; fall back to empty on failure. - - Generated-by: Cursor - """ - try: - items = gitctx.get_paged( - f"/repos/{context.github_repo}/issues/{issue_number}/sub_issues" - ) - return [item["number"] for item in items if "number" in item] - except Exception as e: - logger.exception("Failed listing sub-issues for #%s: %r", issue_number, e) - return [] - - def _cascade_milestone( gitctx: Connector, root_number: int, milestone_number: int | None, + descendants: list[int] | None = None, ) -> list[int]: """Set milestone on root and all GitHub descendants. Returns updated numbers. Generated-by: Cursor """ - def list_children(n: int) -> list[int]: - return _list_sub_issue_numbers(gitctx, n) - - numbers = [root_number] + collect_descendant_numbers(list_children, root_number) + numbers = [root_number] + ( + pr.descendants(gitctx, root_number) if descendants is None else descendants + ) updated: list[int] = [] for number in numbers: - gitctx.patch( - f"/repos/{context.github_repo}/issues/{number}", - data={"milestone": milestone_number}, - ) + try: + gitctx.patch( + f"/repos/{context.github_repo}/issues/{number}", + data={"milestone": milestone_number}, + ) + except Exception as error: + # GitHub cascades are not transactional; an ambiguous failed write + # can also have applied. Reload both columns rather than roll back. + raise HTTPException( + status_code=502, + detail={ + "message": f"Milestone update stopped at issue #{number}: {error}", + "updated_issue_numbers": updated, + "failed_issue_number": number, + "refresh_required": True, + }, + ) from error updated.append(number) return updated def _graphql_issue_node(gitctx: Connector, issue_number: int) -> dict: - response = gitctx.post( - "/graphql", - data={ - "query": PARENT_ONLY_GRAPHQL, - "variables": { - "owner": gitctx.owner, - "repo": gitctx.repo, - "issue": issue_number, - }, - }, - ) - data = response.get("data") or {} - repo = data.get("repository") or {} - issue_node = repo.get("issue") - if not issue_node: - raise HTTPException(status_code=404, detail=f"Issue #{issue_number} not found") - return issue_node + return pr.issue_nodes( + gitctx, [issue_number], pr.IDENTITY + " " + pr.PARENT, issue_only=True + )[issue_number] class SetIssueParent(BaseModel): @@ -912,7 +735,8 @@ class SetIssueParent(BaseModel): @api_router.put("/issues/{issue_number}/parent") -async def set_issue_parent( +@offload +def set_issue_parent( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], body: Annotated[SetIssueParent, Body(title="Parent")], @@ -925,17 +749,14 @@ async def set_issue_parent( if parent_number == issue_number: raise HTTPException(status_code=422, detail="An issue cannot be its own parent") - def list_children(n: int) -> list[int]: - return _list_sub_issue_numbers(gitctx, n) - - if is_ancestor(list_children, issue_number, parent_number): + descendant_numbers = pr.descendants(gitctx, issue_number) + if parent_number in descendant_numbers: raise HTTPException( - status_code=422, - detail="Cannot link an issue under one of its descendants", + status_code=422, detail="Cannot link an issue under one of its descendants" ) - - child = gitctx.get(f"/repos/{context.github_repo}/issues/{issue_number}") - parent = gitctx.get(f"/repos/{context.github_repo}/issues/{parent_number}") + nodes = pr.issue_nodes(gitctx, [issue_number, parent_number], issue_only=True) + child = pr.rest_identity(nodes[issue_number]) + parent = pr.rest_identity(nodes[parent_number]) gitctx.post( f"/repos/{context.github_repo}/issues/{parent_number}/sub_issues", @@ -947,7 +768,9 @@ def list_children(n: int) -> list[int]: updated_issue_numbers: list[int] = [] if child_ms != parent_ms: # parent_ms may be None (no milestone); still cascade to match parent. - updated_issue_numbers = _cascade_milestone(gitctx, issue_number, parent_ms) + updated_issue_numbers = _cascade_milestone( + gitctx, issue_number, parent_ms, descendant_numbers + ) return { "issue_number": issue_number, @@ -959,7 +782,8 @@ def list_children(n: int) -> list[int]: @api_router.delete("/issues/{issue_number}/parent") -async def clear_issue_parent( +@offload +def clear_issue_parent( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], ): @@ -967,8 +791,8 @@ async def clear_issue_parent( Generated-by: Cursor """ - child = gitctx.get(f"/repos/{context.github_repo}/issues/{issue_number}") issue_node = _graphql_issue_node(gitctx, issue_number) + child = pr.rest_identity(issue_node) parent = issue_node.get("parent") if not parent or parent.get("number") is None: raise HTTPException( @@ -987,7 +811,8 @@ async def clear_issue_parent( @api_router.post("/issues/{issue_number}/adopt-parent-milestone") -async def adopt_parent_milestone( +@offload +def adopt_parent_milestone( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], ): @@ -995,8 +820,8 @@ async def adopt_parent_milestone( Generated-by: Cursor """ - issue = gitctx.get(f"/repos/{context.github_repo}/issues/{issue_number}") issue_node = _graphql_issue_node(gitctx, issue_number) + issue = pr.rest_identity(issue_node) parent = issue_node.get("parent") if not parent or parent.get("number") is None: raise HTTPException( @@ -1075,20 +900,13 @@ def _get_issue_for_dependency(gitctx: Connector, issue_number: int) -> dict: return issue -def _get_issue_or_pull_request(gitctx: Connector, number: int) -> dict: - """Fetch an issue or pull request and translate a missing item to 404. - - Generated-by: openai-code-assist - """ - try: - return gitctx.get(f"/repos/{context.github_repo}/issues/{number}") - except requests.HTTPError as exc: - status = getattr(exc.response, "status_code", None) - if status == 404: - raise HTTPException( - status_code=404, detail=f"Issue #{number} not found" - ) from exc - raise +def _dependency_pair(gitctx, first, second): + nodes = pr.issue_nodes(gitctx, [first, second]) + if any(n["__typename"] != "Issue" for n in nodes.values()): + raise HTTPException( + status_code=422, detail="Dependencies require issues, not pull requests" + ) + return {number: pr.rest_identity(node) for number, node in nodes.items()} def _close_reference_payload(pull_request: dict) -> dict: @@ -1144,7 +962,8 @@ def _add_close_reference(gitctx: Connector, issue: dict, pull_request: dict) -> @api_router.post("/issues/{issue_number}/close-references") -async def add_close_reference( +@offload +def add_close_reference( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], body: Annotated[AddCloseReference, Body(title="Closing Reference")], @@ -1153,7 +972,8 @@ async def add_close_reference( Generated-by: openai-code-assist """ - issue = _get_issue_or_pull_request(gitctx, issue_number) + nodes = pr.issue_nodes(gitctx, [issue_number, body.pull_request_number]) + issue = pr.rest_identity(nodes[issue_number]) if "pull_request" in issue: raise HTTPException( status_code=422, @@ -1162,7 +982,7 @@ async def add_close_reference( "closing references require issues" ), ) - pull_request = _get_issue_or_pull_request(gitctx, body.pull_request_number) + pull_request = pr.rest_identity(nodes[body.pull_request_number]) if "pull_request" not in pull_request: raise HTTPException( status_code=422, @@ -1207,7 +1027,8 @@ def _post_blocked_by_dependency( @api_router.post("/issues/{issue_number}/dependencies/blocked_by") -async def add_blocked_by( +@offload +def add_blocked_by( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], body: Annotated[AddDependency, Body(title="Dependency")], @@ -1220,8 +1041,8 @@ async def add_blocked_by( raise HTTPException( status_code=422, detail="An issue cannot be blocked by itself" ) - _get_issue_for_dependency(gitctx, issue_number) - blocker = _get_issue_for_dependency(gitctx, body.issue_number) + nodes = _dependency_pair(gitctx, issue_number, body.issue_number) + blocker = nodes[body.issue_number] linked = _post_blocked_by_dependency(gitctx, issue_number, blocker["id"]) return { "issue_number": issue_number, @@ -1233,7 +1054,8 @@ async def add_blocked_by( @api_router.delete( "/issues/{issue_number}/dependencies/blocked_by/{blocking_issue_number}" ) -async def remove_blocked_by( +@offload +def remove_blocked_by( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], blocking_issue_number: Annotated[int, Path(title="Blocking Issue Number")], @@ -1259,7 +1081,8 @@ async def remove_blocked_by( @api_router.post("/issues/{issue_number}/dependencies/blocking") -async def add_blocking( +@offload +def add_blocking( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], body: Annotated[AddDependency, Body(title="Dependency")], @@ -1270,8 +1093,8 @@ async def add_blocking( """ if body.issue_number == issue_number: raise HTTPException(status_code=422, detail="An issue cannot block itself") - current = _get_issue_for_dependency(gitctx, issue_number) - blocked = _get_issue_for_dependency(gitctx, body.issue_number) + nodes = _dependency_pair(gitctx, issue_number, body.issue_number) + current, blocked = nodes[issue_number], nodes[body.issue_number] _post_blocked_by_dependency(gitctx, body.issue_number, current["id"]) return { "issue_number": issue_number, @@ -1283,7 +1106,8 @@ async def add_blocking( @api_router.delete( "/issues/{issue_number}/dependencies/blocking/{blocked_issue_number}" ) -async def remove_blocking( +@offload +def remove_blocking( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], blocked_issue_number: Annotated[int, Path(title="Blocked Issue Number")], @@ -1312,12 +1136,9 @@ async def remove_blocking( @api_router.get("/labels") -async def get_labels(gitctx: Annotated[Connector, Depends(connection)]): - labels = gitctx.get_paged( - f"/repos/{context.github_repo}/labels", - headers=_GITHUB_BODY_ACCEPT, - ) - return labels +@offload +def get_labels(gitctx: Annotated[Connector, Depends(connection)]): + return catalog(gitctx, "labels") class CreateLabel(BaseModel): @@ -1338,7 +1159,8 @@ class UpdateLabel(BaseModel): @api_router.post("/labels") -async def create_label( +@offload +def create_label( gitctx: Annotated[Connector, Depends(connection)], label: Annotated[CreateLabel, Body(title="Label")], ): @@ -1353,8 +1175,9 @@ async def create_label( return response -@api_router.patch("/labels/{label_name}") -async def update_label( +@api_router.patch("/labels/{label_name:path}") +@offload +def update_label( gitctx: Annotated[Connector, Depends(connection)], label_name: str, label: Annotated[UpdateLabel, Body(title="Label")], @@ -1365,7 +1188,7 @@ async def update_label( """ response = gitctx.patch( - f"/repos/{context.github_repo}/labels/{label_name}", + f"/repos/{context.github_repo}/labels/{quote(label_name, safe='')}", data={ "new_name": label.name, "color": label.color, @@ -1375,63 +1198,65 @@ async def update_label( return response -@api_router.delete("/labels/{label_name}") -async def delete_label( - gitctx: Annotated[Connector, Depends(connection)], label_name: str -): - gitctx.delete(f"/repos/{context.github_repo}/labels/{label_name}") +@api_router.delete("/labels/{label_name:path}") +@offload +def delete_label(gitctx: Annotated[Connector, Depends(connection)], label_name: str): + gitctx.delete(f"/repos/{context.github_repo}/labels/{quote(label_name, safe='')}") return {"message": f"{label_name} label deleted"} -@api_router.post("/issues/{issue_number}/labels/{label_name}") -async def add_label_to_issue( +@api_router.post("/issues/{issue_number}/labels/{label_name:path}") +@offload +def add_label_to_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], label_name: Annotated[str, Path(title="Label")], ): - issue = gitctx.get(f"/repos/{context.github_repo}/issues/{issue_number}") - labels = set([label["name"] for label in issue["labels"]]) - if label_name not in labels: - labels.add(label_name) - issue = gitctx.patch( - f"/repos/{context.github_repo}/issues/{issue_number}", - data={"labels": list(labels)}, + return { + "labels": gitctx.post( + f"/repos/{context.github_repo}/issues/{issue_number}/labels", + data={"labels": [label_name]}, ) - return issue + } -@api_router.delete("/issues/{issue_number}/labels/{label_name}") -async def remove_label_from_issue( +@api_router.delete("/issues/{issue_number}/labels/{label_name:path}") +@offload +def remove_label_from_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], label_name: Annotated[str, Path(title="Label")], ): - issue = gitctx.get(f"/repos/{context.github_repo}/issues/{issue_number}") - labels = set([label["name"] for label in issue["labels"]]) - if label_name in labels: - labels.remove(label_name) - issue = gitctx.patch( - f"/repos/{context.github_repo}/issues/{issue_number}", - data={"labels": list(labels)}, + try: + labels = gitctx.delete( + f"/repos/{context.github_repo}/issues/{issue_number}/labels/{quote(label_name, safe='')}" ) - return issue + except requests.HTTPError as exc: + if exc.response is None or exc.response.status_code != 404: + raise + # An absent label remains a successful no-op, but an inaccessible issue + # must not be mistaken for one. This exceptional path needs one read. + issue = gitctx.get(f"/repos/{context.github_repo}/issues/{issue_number}") + if any(label["name"] == label_name for label in issue["labels"]): + raise + labels = issue["labels"] + return {"labels": labels} # Assignee Management @api_router.get("/assignees") -async def get_assignees(gitctx: Annotated[Connector, Depends(connection)]): +@offload +def get_assignees(gitctx: Annotated[Connector, Depends(connection)]): """Get all allowed assignees for the repository""" - assignees = gitctx.get_paged( - f"/repos/{context.github_repo}/assignees", - headers=_GITHUB_BODY_ACCEPT, - ) + assignees = catalog(gitctx, "assignees") return sorted(assignees, key=lambda x: x["login"]) @api_router.post("/issues/{issue_number}/assignees") -async def add_assignee_to_issue( +@offload +def add_assignee_to_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], assignees: Annotated[list[str], Body(title="Assignees")], @@ -1448,7 +1273,8 @@ async def add_assignee_to_issue( @api_router.delete("/issues/{issue_number}/assignees") -async def remove_assignee_from_issue( +@offload +def remove_assignee_from_issue( gitctx: Annotated[Connector, Depends(connection)], issue_number: Annotated[int, Path(title="Issue")], assignees: Annotated[list[str], Body(title="Assignees")], diff --git a/backend/src/github_pm/execution.py b/backend/src/github_pm/execution.py new file mode 100644 index 0000000..1e23ea8 --- /dev/null +++ b/backend/src/github_pm/execution.py @@ -0,0 +1,24 @@ +"""Execute complete blocking operations outside the ASGI event loop. + +Generated-by: Codex +""" + +from functools import wraps +import inspect +from typing import get_type_hints + +from starlette.concurrency import run_in_threadpool + + +def offload(function): + """Keep routes awaitable while offloading synchronous HTTP and backoff.""" + + @wraps(function) + async def run(*args, **kwargs): + return await run_in_threadpool(function, *args, **kwargs) + + # FastAPI resolves annotations in the wrapper's module. Resolve postponed + # annotations here, where the original function's globals are available. + run.__annotations__ = get_type_hints(function, include_extras=True) + run.__signature__ = inspect.signature(function, eval_str=True) + return run diff --git a/backend/src/github_pm/github_client.py b/backend/src/github_pm/github_client.py new file mode 100644 index 0000000..0ed6a4f --- /dev/null +++ b/backend/src/github_pm/github_client.py @@ -0,0 +1,311 @@ +"""Bounded, observable GitHub transport with scoped conditional REST caching. + +Generated-by: Codex +""" + +from collections import OrderedDict +from contextlib import contextmanager +from copy import deepcopy +from hashlib import sha256 +import re +from threading import Lock +import time +from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit + +import requests + +from github_pm.context import context +from github_pm.logger import logger + +_TIMEOUT = (5, 45) +_MAX_ATTEMPTS = 5 +_CACHE_LIMIT = 256 +_OPERATION_TIMEOUT = 300 +_cache = OrderedDict() +_state_lock = Lock() +_gates = {} + + +class _Gate: + def __init__(self): + self.lock = Lock() + self.write_at = 0.0 + self.resume_at = 0.0 + self.revision = 0 + + +def _page_url(url): + parts = urlsplit(url) + params = dict(parse_qsl(parts.query, keep_blank_values=True)) + params.setdefault("per_page", "100") + return urlunsplit(parts._replace(query=urlencode(params))) + + +class Connector: + def __init__(self, github_token: str, *, github_repo: str | None = None): + self.github_token = github_token + self.base_url = "https://api.github.com" + self.owner, self.repo = (github_repo or context.github_repo).split("/", 1) + self.scope = (sha256(github_token.encode()).hexdigest(), self.owner, self.repo) + with _state_lock: + self.gate = _gates.setdefault(self.scope[0], _Gate()) + self.github = requests.session() + self.github.headers.update( + { + "Authorization": f"Bearer {github_token}", + "Accept": "application/vnd.github+json", + "User-Agent": "Project-Manager", + "X-GitHub-Api-Version": "2022-11-28", + } + ) + self.metrics = dict( + calls=0, bytes=0, retries=0, elapsed=0.0, graphql_cost=0, not_modified=0 + ) + self.response = None + self.closed = False + self.deadline = time.monotonic() + _OPERATION_TIMEOUT + + def close(self): + if not self.closed: + self.closed = True + self.github.close() + logger.info( + "GitHub operation %s/%s: %s", self.owner, self.repo, self.metrics + ) + + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + + def _invalidate(self): + with _state_lock: + self.gate.revision += 1 + for key in list(_cache): + if key[0] == self.scope: + del _cache[key] + + def _remaining(self): + remaining = self.deadline - time.monotonic() + if remaining <= 0: + raise TimeoutError("GitHub operation exceeded its five-minute deadline") + return remaining + + def _sleep(self, delay): + if delay > self._remaining(): + raise TimeoutError("GitHub retry would exceed the operation deadline") + time.sleep(delay) + + @contextmanager + def _reserved(self): + if not self.gate.lock.acquire(timeout=self._remaining()): + raise TimeoutError("Timed out waiting for the GitHub request queue") + try: + yield + finally: + self.gate.lock.release() + + def _request(self, method, url, *, read=False, **kwargs): + started = time.monotonic() + try: + with self._reserved(): + for attempt in range(_MAX_ATTEMPTS): + wait = self.gate.resume_at - time.time() + if wait > 60: + raise RuntimeError( + "GitHub rate limit active; retry after its reset time" + ) + if wait > 0: + self._sleep(wait) + if not read: + pause = 1.0 - (time.monotonic() - self.gate.write_at) + if pause > 0: + self._sleep(pause) + self.gate.write_at = time.monotonic() + # A failed/ambiguous write may still have changed GitHub. + self._invalidate() + remaining_time = self._remaining() + timeout = tuple( + min(value, remaining_time / 2) for value in _TIMEOUT + ) + response = getattr(self.github, method)( + url, timeout=timeout, **kwargs + ) + self.metrics["calls"] += 1 + content = response.content + if isinstance(content, (bytes, str)): + self.metrics["bytes"] += len(content) + headers = ( + response.headers + if isinstance(response.headers, dict) + else dict(response.headers) + ) + retry_after = headers.get("Retry-After") or headers.get( + "retry-after" + ) + remaining = headers.get("X-RateLimit-Remaining") or headers.get( + "x-ratelimit-remaining" + ) + for name in ("Limit", "Remaining", "Reset", "Resource"): + value = response.headers.get(f"X-RateLimit-{name}") + if isinstance(value, str): + self.metrics["rate_" + name.lower()] = value + graphql_throttled = False + if ( + read + and method == "post" + and url.endswith("/graphql") + and response.status_code == 200 + ): + result = response.json() + graphql_throttled = any( + error.get("type") == "RATE_LIMITED" + for error in result.get("errors", []) + ) + throttled = ( + graphql_throttled + or response.status_code == 429 + or ( + response.status_code == 403 + and ( + retry_after + or remaining == "0" + or "rate limit" in str(response.text).lower() + ) + ) + ) + if throttled: + reset = headers.get("X-RateLimit-Reset") or headers.get( + "x-ratelimit-reset" + ) + delay = ( + float(retry_after) + if retry_after + else ( + max(0, float(reset) - time.time()) + if remaining == "0" and reset + else 60 * (attempt + 1) + ) + ) + self.gate.resume_at = time.time() + delay + else: + delay = 1.5 * (attempt + 1) + if ( + read + and (throttled or response.status_code in (502, 503, 504)) + and attempt < _MAX_ATTEMPTS - 1 + and delay <= 60 + ): + self.metrics["retries"] += 1 + self._sleep(delay) + continue + response.raise_for_status() + self.response = response + return response + finally: + self.metrics["elapsed"] += time.monotonic() - started + + def _get_response(self, url, headers=None): + cacheable = bool(re.search(r"/(milestones|labels|assignees)(?:\?|$)", url)) + key = (self.scope, url, tuple(sorted((headers or {}).items()))) + with _state_lock: + cached = _cache.get(key) if cacheable else None + revision = self.gate.revision + request_headers = dict(headers or {}) + if cached: + for name, value in cached[2].items(): + request_headers[name] = value + response = self._request("get", url, headers=request_headers or None, read=True) + if response.status_code == 304: + if not cached: + raise RuntimeError( + "GitHub returned 304 without a cached representation" + ) + self.metrics["not_modified"] += 1 + return deepcopy(cached[0]), cached[1] + data = response.json() + links = response.headers.get("link") or response.headers.get("Link") or "" + if not isinstance(links, str): + links = "" + validators = {} + for source, target in [ + ("ETag", "If-None-Match"), + ("Last-Modified", "If-Modified-Since"), + ]: + value = response.headers.get(source) + if isinstance(value, str): + validators[target] = value + if cacheable and validators: + with _state_lock: + if revision != self.gate.revision: + return data, links + _cache[key] = (deepcopy(data), links, validators) + _cache.move_to_end(key) + while len(_cache) > _CACHE_LIMIT: + _cache.popitem(last=False) + return data, links + + def get(self, path, headers=None): + return self._get_response(f"{self.base_url}{path}", headers)[0] + + def get_paged(self, path, headers=None): + url = _page_url(f"{self.base_url}{path}") + results, seen = [], set() + while url: + if url in seen or not url.startswith(self.base_url + "/"): + raise RuntimeError("Invalid GitHub pagination link") + seen.add(url) + data, links = self._get_response(url, headers) + if not isinstance(data, list): + raise RuntimeError("Expected a GitHub list response") + results.extend(data) + url = next( + ( + link["url"] + for link in requests.utils.parse_header_links(links) + if link.get("rel") == "next" + ), + None, + ) + return results + + def search_issue_items(self, search_query, headers=None): + from github_pm.report_queries import rest_search + + return rest_search(lambda path: self.get(path, headers=headers), search_query) + + def post(self, path, data, headers=None): + read = path == "/graphql" and bool( + re.match(r"\s*(query\b|\{)", data.get("query", "")) + ) + response = self._request( + "post", f"{self.base_url}{path}", json=data, headers=headers, read=read + ) + result = response.json() if response.content else {} + if path == "/graphql" and isinstance(result, dict): + rate = (result.get("data") or {}).get("rateLimit") or {} + self.metrics["graphql_cost"] += rate.get("cost", 0) + return result + + def patch(self, path, data, headers=None): + response = self._request( + "patch", f"{self.base_url}{path}", json=data, headers=headers + ) + return response.json() + + def delete(self, path, data=None, headers=None): + response = self._request( + "delete", f"{self.base_url}{path}", json=data, headers=headers + ) + return response.json() if response.content else {} + + def post_text(self, path, text, headers=None): + response = self._request( + "post", + f"{self.base_url}{path}", + data=text.encode("utf-8"), + headers={"Content-Type": "text/plain; charset=utf-8", **(headers or {})}, + read=True, + ) + return response.text diff --git a/backend/src/github_pm/issue_hierarchy.py b/backend/src/github_pm/issue_hierarchy.py index 72721af..47ecdfb 100644 --- a/backend/src/github_pm/issue_hierarchy.py +++ b/backend/src/github_pm/issue_hierarchy.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """Build and mutate GitHub sub-issue hierarchies for the planner. Generated-by: Cursor @@ -193,83 +194,6 @@ def apply_graphql_links(issue: dict, issue_node: dict | None) -> None: issue["blocking"] = blocking -ISSUE_HIERARCHY_GRAPHQL = """ -query($owner: String!, $repo: String!, $issue: Int!) { - repository(owner: $owner, name: $repo, followRenames: true) { - issue(number: $issue) { - closedByPullRequestsReferences(first: 100, includeClosedPrs: true) { - nodes { - number - title - url - } - } - blockedBy(first: 50) { - nodes { - databaseId - number - title - url - state - } - } - blocking(first: 50) { - nodes { - databaseId - number - title - url - state - } - } - parent { - number - title - milestone { - number - title - } - } - subIssues(first: 100) { - nodes { - number - } - } - subIssuesSummary { - total - completed - percentCompleted - } - } - } -} -""" - -PARENT_ONLY_GRAPHQL = """ -query($owner: String!, $repo: String!, $issue: Int!) { - repository(owner: $owner, name: $repo, followRenames: true) { - issue(number: $issue) { - id - number - parent { - number - title - milestone { - number - title - } - } - subIssues(first: 100) { - nodes { - number - } - } - } - } -} -""" - - def collect_descendant_numbers( list_sub_issues: Callable[[int], list[int]], root_number: int, diff --git a/backend/src/github_pm/planning_reader.py b/backend/src/github_pm/planning_reader.py new file mode 100644 index 0000000..43ed852 --- /dev/null +++ b/backend/src/github_pm/planning_reader.py @@ -0,0 +1,162 @@ +"""Focused, batched reads for Planning and hierarchy commands. + +Generated-by: Codex +""" + +from github_pm.report_queries import complete_connections, execute, PAGE + +DEPENDENCY = "databaseId number title url state" +CLOSING = "number title url" +PARENT = "parent { number title milestone { number title } }" +IDENTITY = "id databaseId number title url state milestone { number title }" +LINKS = ( + "closedByPullRequestsReferences(first:100,includeClosedPrs:true) { nodes { " + + CLOSING + + " } " + + PAGE + + " } blockedBy(first:100) { nodes { " + + DEPENDENCY + + " } " + + PAGE + + " } blocking(first:100) { nodes { " + + DEPENDENCY + + " } " + + PAGE + + " }" +) +REACTION = "content user { login }" +REACTIONS = "reactions(first:100) { nodes { " + REACTION + " } " + PAGE + " }" +_REACTION_NAMES = { + "THUMBS_UP": "+1", + "THUMBS_DOWN": "-1", + "LAUGH": "laugh", + "HOORAY": "hooray", + "CONFUSED": "confused", + "HEART": "heart", + "ROCKET": "rocket", + "EYES": "eyes", +} + + +def _post(gitctx): + return lambda payload: gitctx.post("/graphql", payload) + + +def issue_nodes(gitctx, numbers, fields=IDENTITY, *, issue_only=False): + """Use independent aliases so callers need only repository-local numbers.""" + numbers = list(dict.fromkeys(numbers)) + result = {} + for offset in range(0, len(numbers), 50): + batch = numbers[offset : offset + 50] + variables = {"owner": gitctx.owner, "repo": gitctx.repo} + definitions, selections = ["$owner:String!", "$repo:String!"], [] + for i, number in enumerate(batch): + definitions.append(f"$n{i}:Int!") + variables[f"n{i}"] = number + if issue_only: + selection = f"issue(number:$n{i}) {{ __typename {fields} }}" + else: + selection = f"issueOrPullRequest(number:$n{i}) {{ __typename ... on Issue {{ {fields} }} ... on PullRequest {{ {IDENTITY} }} }}" + selections.append(f"i{i}:" + selection) + data = execute( + _post(gitctx), + "query(" + + ",".join(definitions) + + ") { repository(owner:$owner,name:$repo) { " + + " ".join(selections) + + " } rateLimit { cost } }", + variables, + ) + repository = data.get("repository") or {} + for i, number in enumerate(batch): + node = repository.get(f"i{i}") + if not node: + raise RuntimeError(f"GitHub issue #{number} is missing or inaccessible") + result[number] = node + return result + + +def enrich_issues(gitctx, issues): + if not issues: + return {} + nodes = issue_nodes( + gitctx, + [i["number"] for i in issues], + "id number " + PARENT + " " + LINKS, + issue_only=True, + ) + complete_connections( + _post(gitctx), + list(nodes.values()), + "Issue", + { + "blockedBy": (DEPENDENCY, ""), + "blocking": (DEPENDENCY, ""), + "closedByPullRequestsReferences": (CLOSING, "includeClosedPrs:true"), + }, + ) + return nodes + + +def descendants(gitctx, root): + """Breadth-first traversal includes closed and cross-milestone descendants.""" + found, frontier, seen = [], [root], {root} + while frontier: + nodes = issue_nodes( + gitctx, + frontier, + "id number subIssues(first:100) { nodes { number } " + PAGE + " }", + issue_only=True, + ) + complete_connections( + _post(gitctx), list(nodes.values()), "Issue", {"subIssues": ("number", "")} + ) + frontier = [] + for node in nodes.values(): + for child in node["subIssues"]["nodes"]: + number = child["number"] + if number not in seen: + seen.add(number) + found.append(number) + frontier.append(number) + return found + + +def rest_identity(node): + result = dict(node, id=node["databaseId"], node_id=node["id"], html_url=node["url"]) + if node["__typename"] == "PullRequest": + result["pull_request"] = {} + return result + + +def comment_reactions(gitctx, comments): + """Batch reaction enrichment; keep REST bodies/IDs for editing fidelity.""" + targets = [ + c for c in comments if (c.get("reactions") or {}).get("total_count", 0) > 0 + ] + for offset in range(0, len(targets), 50): + batch = targets[offset : offset + 50] + if any(not c.get("node_id") for c in batch): + raise RuntimeError("GitHub omitted comment node IDs") + data = execute( + _post(gitctx), + "query($ids:[ID!]!) { nodes(ids:$ids) { ... on IssueComment { id " + + REACTIONS + + " } } rateLimit { cost } }", + {"ids": [c["node_id"] for c in batch]}, + ) + nodes = {n["id"]: n for n in data.get("nodes") or [] if n} + if len(nodes) != len(batch): + raise RuntimeError("GitHub omitted comment reactions") + complete_connections( + _post(gitctx), + list(nodes.values()), + "IssueComment", + {"reactions": (REACTION, "")}, + ) + for comment in batch: + comment["reaction_details"] = [ + dict(content=_REACTION_NAMES[r["content"]], user=r.get("user")) + for r in nodes[comment["node_id"]]["reactions"]["nodes"] + ] + return comments diff --git a/backend/src/github_pm/report_queries.py b/backend/src/github_pm/report_queries.py new file mode 100644 index 0000000..3d2cdf4 --- /dev/null +++ b/backend/src/github_pm/report_queries.py @@ -0,0 +1,425 @@ +"""Small GitHub projections and independent, complete connection pagination. + +Generated-by: Codex +""" + +from dataclasses import dataclass, replace +from datetime import date, datetime, timedelta, UTC +import re +from urllib.parse import urlencode + +from github_pm.context import _parse_sdlc_label_csv + +PAGE = "pageInfo { hasNextPage endCursor }" +AUTHOR = "author { __typename login }" +LABELS = "labels(first:100) { nodes { name } " + PAGE + " }" +REVIEWS = ( + "reviews(first:100) { nodes { submittedAt state " + AUTHOR + " } " + PAGE + " }" +) +SUMMARY = "id number title url createdAt updatedAt state isDraft mergedAt" +DELIVERY = ( + "id number createdAt mergedAt additions deletions milestone { title } " + + AUTHOR + + " " + + LABELS +) +ESCAPE = "id number mergedAt milestone { title } " + AUTHOR + " " + LABELS +ATTENTION = SUMMARY + " mergeable mergeStateStatus " + REVIEWS +ISSUE_DATES = "id number createdAt closedAt milestone { title }" + + +def execute(post, query, variables=None): + result = post({"query": query, "variables": variables or {}}) + if result.get("errors"): + messages = "; ".join(str(e.get("message", e)) for e in result["errors"]) + raise RuntimeError(f"GitHub GraphQL error: {messages}") + data = result.get("data") + if not isinstance(data, dict): + raise RuntimeError("GitHub GraphQL response is missing data") + return data + + +def next_cursor(connection, previous=None): + if not isinstance(connection.get("nodes"), list): + raise RuntimeError("GitHub connection is missing its nodes") + page = connection.get("pageInfo") + if not isinstance(page, dict): + raise RuntimeError("GitHub connection is missing pagination information") + if not page.get("hasNextPage"): + return None + cursor = page.get("endCursor") + if not cursor or cursor == previous: + raise RuntimeError("GitHub pagination failed to advance") + return cursor + + +@dataclass(frozen=True) +class Search: + query: str + fields: str + typename: str = "PullRequest" + + +def _split_query(query): + match = re.search(r"\b(created|merged|closed):([^\s]+)\.\.([^\s]+)", query) + if not match: + raise RuntimeError( + "GitHub search exceeds 1,000 results; a bounded date interval is required" + ) + field, lo, hi = match.groups() + start = datetime.fromisoformat(lo.replace("Z", "+00:00")).replace(tzinfo=UTC) + end = datetime.fromisoformat(hi.replace("Z", "+00:00")).replace(tzinfo=UTC) + if len(hi) == 10: + end += timedelta(days=1, seconds=-1) + seconds = int((end - start).total_seconds()) + if seconds < 1: + raise RuntimeError( + "More than 1,000 GitHub search matches in a single second; results would be incomplete" + ) + midpoint = start + timedelta(seconds=seconds // 2) + + def ranged(a, b): + token = f'{field}:{a.strftime("%Y-%m-%dT%H:%M:%SZ")}..{b.strftime("%Y-%m-%dT%H:%M:%SZ")}' + return query[: match.start()] + token + query[match.end() :] + + return ranged(start, midpoint), ranged(midpoint + timedelta(seconds=1), end) + + +def search_many(post, searches): + """Batch independent searches, splitting large intervals before collecting pages.""" + results = {key: [] for key in searches} + pending = [(key, spec, None) for key, spec in searches.items()] + visited = set() + while pending: + batch, pending = pending[:4], pending[4:] + definitions, fields, variables = [], [], {} + for i, (key, spec, cursor) in enumerate(batch): + marker = (key, spec.query, cursor) + if marker in visited: + raise RuntimeError("GitHub search pagination repeated a cursor") + visited.add(marker) + definitions.extend([f"$q{i}:String!", f"$c{i}:String"]) + variables.update({f"q{i}": spec.query, f"c{i}": cursor}) + fields.append( + f"s{i}:search(query:$q{i}, type:ISSUE, first:100, after:$c{i}) {{ issueCount {PAGE} nodes {{ __typename ... on {spec.typename} {{ {spec.fields} }} }} }}" + ) + data = execute( + post, + "query(" + + ",".join(definitions) + + ") { " + + " ".join(fields) + + " rateLimit { cost } }", + variables, + ) + for i, (key, spec, cursor) in enumerate(batch): + connection = data.get(f"s{i}") + if not isinstance(connection, dict): + raise RuntimeError(f"GitHub search {key} is missing") + if not isinstance(connection.get("issueCount"), int): + raise RuntimeError("GitHub search omitted the result count") + if connection["issueCount"] > 1000: + if cursor: + raise RuntimeError( + "GitHub search changed during pagination; retry the report" + ) + pending.extend( + (key, replace(spec, query=q), None) + for q in _split_query(spec.query) + ) + continue + results[key].extend( + n + for n in connection.get("nodes", []) + if n and n.get("number") is not None + ) + after = next_cursor(connection, cursor) + if after: + pending.append((key, spec, after)) + for key, nodes in results.items(): + results[key] = list( + { + n.get("id") or (n.get("__typename"), n["number"]): n for n in nodes + }.values() + ) + return results + + +def complete_connections(post, nodes, typename, connections): + """Finish nested connections in bounded batches with an independent cursor per node/field. + + connections maps field names to (node selection, additional arguments). + """ + pending = [] + visited = set() + for node in nodes: + for field, (selection, extra) in connections.items(): + connection = node.get(field) + if connection is None: + raise RuntimeError( + f'GitHub omitted {field} for #{node.get("number", "?")}' + ) + cursor = next_cursor(connection) + if cursor: + if not node.get("id"): + raise RuntimeError( + "GitHub omitted the node ID needed for pagination" + ) + pending.append((node, field, selection, extra, cursor)) + while pending: + batch, pending = pending[:25], pending[25:] + definitions, fields, variables = [], [], {} + for i, (node, field, selection, extra, cursor) in enumerate(batch): + marker = (node["id"], field, cursor) + if marker in visited: + raise RuntimeError("GitHub nested pagination repeated a cursor") + visited.add(marker) + definitions.extend([f"$id{i}:ID!", f"$c{i}:String!"]) + variables.update({f"id{i}": node["id"], f"c{i}": cursor}) + args = f"first:100,after:$c{i}" + ("," + extra if extra else "") + fields.append( + f"n{i}:node(id:$id{i}) {{ ... on {typename} {{ {field}({args}) {{ nodes {{ {selection} }} {PAGE} }} }} }}" + ) + data = execute( + post, + "query(" + + ",".join(definitions) + + ") { " + + " ".join(fields) + + " rateLimit { cost } }", + variables, + ) + for i, (node, field, selection, extra, cursor) in enumerate(batch): + connection = (data.get(f"n{i}") or {}).get(field) + if not isinstance(connection, dict): + raise RuntimeError(f"GitHub omitted a continuation for {field}") + node[field]["nodes"].extend(connection.get("nodes") or []) + node[field]["pageInfo"] = connection["pageInfo"] + after = next_cursor(connection, cursor) + if after: + pending.append((node, field, selection, extra, after)) + + +def complete_prs(post, nodes, *, labels=False, reviews=False): + connections = {} + if labels: + connections["labels"] = ("name", "") + if reviews: + connections["reviews"] = ("submittedAt state " + AUTHOR, "") + complete_connections(post, nodes, "PullRequest", connections) + return nodes + + +def open_prs(post, owner, repo): + nodes, cursor, visited = [], None, set() + while True: + if cursor in visited: + raise RuntimeError("GitHub open PR pagination repeated a cursor") + visited.add(cursor) + data = execute( + post, + "query($owner:String!,$repo:String!,$cursor:String) { repository(owner:$owner,name:$repo) { pullRequests(states:[OPEN],first:100,after:$cursor) { nodes { " + + ATTENTION + + " } " + + PAGE + + " } } rateLimit { cost } }", + {"owner": owner, "repo": repo, "cursor": cursor}, + ) + connection = (data.get("repository") or {}).get("pullRequests") + if not isinstance(connection, dict): + raise RuntimeError("GitHub omitted the open pull request collection") + nodes.extend(n for n in connection.get("nodes") or [] if n) + cursor = next_cursor(connection, cursor) + if not cursor: + break + return complete_prs(post, nodes, reviews=True) + + +def rest_search(get, query): + """Complete REST searches, including explicit oversized/incomplete-result handling.""" + items, page = [], 1 + while True: + data = get( + "/search/issues?" + urlencode({"q": query, "per_page": 100, "page": page}) + ) + if data.get("incomplete_results"): + raise RuntimeError( + "GitHub search returned incomplete results; retry the report" + ) + if data.get("total_count", 0) > 1000: + if page != 1: + raise RuntimeError( + "GitHub search changed during pagination; retry the report" + ) + partitions = _split_query(query) + return list( + { + item["id"]: item + for part in partitions + for item in rest_search(get, part) + }.values() + ) + batch = data.get("items") or [] + items.extend(batch) + total = data.get("total_count") + if not isinstance(total, int): + raise RuntimeError("GitHub search omitted the result count") + if len(items) >= total: + return list({item["id"]: item for item in items}.values()) + if not batch: + raise RuntimeError("GitHub search ended before returning all matches") + page += 1 + + +def date_str(d: datetime) -> str: + """GitHub search date (YYYY-MM-DD) in UTC.""" + if d.tzinfo is None: + d = d.replace(tzinfo=UTC) + return d.astimezone(UTC).date().isoformat() + + +def escape_labeled_issues_query(github_repo: str, escape_label: str) -> str: + """GitHub issue search query for issues carrying the escape label.""" + lab = escape_label.strip().lower() + if not lab: + lab = "escape" + label_tok = f'label:"{lab}"' if any(c in lab for c in " /") else f"label:{lab}" + return f"{repo_search_fragment(github_repo)} is:issue {label_tok}" + + +def repo_search_fragment(github_repo: str) -> str: + return f"repo:{github_repo}" + + +def merged_prs_query(github_repo: str, merged_since: datetime) -> str: + return ( + f"{repo_search_fragment(github_repo)} is:pr is:merged " + f"merged:>={date_str(merged_since)}" + ) + + +def merged_prs_query_between(github_repo: str, start_d: date, end_d: date) -> str: + """GraphQL issue search: merged PRs with merge date in ``[start_d, end_d]`` (UTC calendar days). + + REST ``GET /search/issues`` rejects several merged/closed date combinations (422); GraphQL + ``search`` accepts ``merged:`` ranges the same way as the web UI. + """ + a, b = start_d.isoformat(), end_d.isoformat() + if a > b: + a, b = b, a + return f"{repo_search_fragment(github_repo)} is:pr is:merged merged:{a}..{b}" + + +def opened_prs_between_query(github_repo: str, start_d: date, end_d: date) -> str: + """PRs with ``created`` in ``[start_d, end_d]`` (UTC calendar days, inclusive).""" + a, b = start_d.isoformat(), end_d.isoformat() + if a > b: + a, b = b, a + return f"{repo_search_fragment(github_repo)} is:pr created:{a}..{b}" + + +def open_pr_backlog_query(github_repo: str, start_d: date) -> str: + """Open, non-draft PRs last updated strictly before ``start_d`` (UTC calendar day at 00:00). + + Uses ``draft:false`` and ``updated: str: + """All open pull requests in the repo (drafts included) for merge/review heuristics.""" + return f"{repo_search_fragment(github_repo)} is:pr is:open" + + +def open_prs_updated_between_query(github_repo: str, start_d: date, end_d: date) -> str: + """Open, non-draft PRs with ``updated`` in ``[start_d, end_d]`` (UTC calendar days, inclusive).""" + a, b = start_d.isoformat(), end_d.isoformat() + if a > b: + a, b = b, a + return ( + f"{repo_search_fragment(github_repo)} is:pr is:open draft:false " + f"updated:{a}..{b}" + ) + + +def opened_issues_between_query(github_repo: str, start_d: date, end_d: date) -> str: + """Issues (not PRs) with ``created`` in ``[start_d, end_d]`` (UTC calendar days, inclusive).""" + a, b = start_d.isoformat(), end_d.isoformat() + if a > b: + a, b = b, a + return f"{repo_search_fragment(github_repo)} is:issue created:{a}..{b}" + + +def opened_prs_query(github_repo: str, created_since: datetime) -> str: + return ( + f"{repo_search_fragment(github_repo)} is:pr " + f"created:>={date_str(created_since)}" + ) + + +def milestone_merged_prs_query(github_repo: str, milestone_title: str) -> str: + # Quote milestone title for spaces/special chars + safe = milestone_title.replace('"', "\\") + return f'{repo_search_fragment(github_repo)} is:pr is:merged milestone:"{safe}"' + + +def bug_issues_created_query( + github_repo: str, bug_labels_csv: str, since: datetime +) -> str: + """Issues with bug label(s) created on or after `since` (any state).""" + label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) + return ( + f"{repo_search_fragment(github_repo)} is:issue {label_clause} " + f"created:>={date_str(since)}" + ) + + +def bug_issues_closed_query( + github_repo: str, bug_labels_csv: str, since: datetime +) -> str: + label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) + return ( + f"{repo_search_fragment(github_repo)} is:issue is:closed {label_clause} " + f"closed:>={date_str(since)}" + ) + + +def bug_issues_created_query_between( + github_repo: str, bug_labels_csv: str, lo: datetime, hi: datetime +) -> str: + """Bug issues created with ``created`` in ``[date(lo), date(hi)]`` (UTC dates, inclusive).""" + label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) + a, b = date_str(lo), date_str(hi) + if a > b: + a, b = b, a + return ( + f"{repo_search_fragment(github_repo)} is:issue {label_clause} created:{a}..{b}" + ) + + +def bug_issues_closed_query_between( + github_repo: str, bug_labels_csv: str, lo: datetime, hi: datetime +) -> str: + """Closed bug issues with ``closed`` in ``[date(lo), date(hi)]`` (UTC dates, inclusive).""" + label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) + a, b = date_str(lo), date_str(hi) + if a > b: + a, b = b, a + return ( + f"{repo_search_fragment(github_repo)} is:issue is:closed {label_clause} " + f"closed:{a}..{b}" + ) + + +def _label_or_clause(labels: frozenset[str]) -> str: + if not labels: + return "" + parts = [ + f'label:"{lab}"' if any(c in lab for c in " /") else f"label:{lab}" + for lab in sorted(labels) + ] + if len(parts) == 1: + return parts[0] + return "(" + " OR ".join(parts) + ")" diff --git a/backend/src/github_pm/repository_catalog.py b/backend/src/github_pm/repository_catalog.py new file mode 100644 index 0000000..a7360df --- /dev/null +++ b/backend/src/github_pm/repository_catalog.py @@ -0,0 +1,25 @@ +"""Repository metadata, revalidated through the scoped HTTP cache. + +Generated-by: Codex +""" + +import re + +VERSION_MATCH = re.compile(r"^v\d+\.\d+\.\d+$") + + +def catalog(gitctx, resource, *, state=None): + path = f"/repos/{gitctx.owner}/{gitctx.repo}/{resource}" + if state: + path += f"?state={state}" + return gitctx.get_paged( + path, headers={"Accept": "application/vnd.github.full+json"} + ) + + +def milestone_catalog(gitctx): + items = catalog(gitctx, "milestones", state="all") + return ( + [m for m in items if m["state"] == "open"], + [m for m in items if m["state"] == "closed"], + ) diff --git a/backend/src/github_pm/sdlc_api.py b/backend/src/github_pm/sdlc_api.py index 275b6af..b0f94a2 100644 --- a/backend/src/github_pm/sdlc_api.py +++ b/backend/src/github_pm/sdlc_api.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """SDLC KPI REST endpoints (GitHub-backed). Generated-by: Cursor @@ -10,10 +11,13 @@ from fastapi import APIRouter, Depends, Query from github_pm import sdlc_service -from github_pm.api import connection, Connector +from github_pm.api import connection from github_pm.context import context +from github_pm.execution import offload +from github_pm.github_client import Connector from github_pm.sdlc_models import ( BugBacklogSeriesResponse, + DashboardResponse, DeliverySeriesResponse, EscapedDefectSeriesResponse, ) @@ -22,7 +26,8 @@ @sdlc_router.get("/delivery", response_model=DeliverySeriesResponse) -async def get_sdlc_delivery( +@offload +def get_sdlc_delivery( gitctx: Annotated[Connector, Depends(connection)], weeks: Annotated[int, Query(ge=1, le=52)] = 4, week_days: Annotated[int, Query(ge=1, le=90)] = 7, @@ -40,7 +45,8 @@ async def get_sdlc_delivery( @sdlc_router.get("/escaped-defect-rate", response_model=EscapedDefectSeriesResponse) -async def get_escaped_defect_rate( +@offload +def get_escaped_defect_rate( gitctx: Annotated[Connector, Depends(connection)], weeks: Annotated[int, Query(ge=1, le=52)] = 4, week_days: Annotated[int, Query(ge=1, le=90)] = 7, @@ -57,7 +63,8 @@ async def get_escaped_defect_rate( @sdlc_router.get("/bug-backlog-delta", response_model=BugBacklogSeriesResponse) -async def get_bug_backlog_delta( +@offload +def get_bug_backlog_delta( gitctx: Annotated[Connector, Depends(connection)], weeks: Annotated[int, Query(ge=1, le=52)] = 4, week_days: Annotated[int, Query(ge=1, le=90)] = 7, @@ -66,3 +73,16 @@ async def get_bug_backlog_delta( return sdlc_service.compute_bug_backlog_delta_series( gitctx, context, weeks=weeks, week_days=week_days ) + + +@sdlc_router.get("/dashboard", response_model=DashboardResponse) +@offload +def get_sdlc_dashboard( + gitctx: Annotated[Connector, Depends(connection)], + weeks: Annotated[int, Query(ge=1, le=52)] = 4, + week_days: Annotated[int, Query(ge=1, le=90)] = 7, +): + """Collect shared dashboard facts once. Generated-by: Codex.""" + return sdlc_service.compute_sdlc_dashboard( + gitctx, context, weeks=weeks, week_days=week_days + ) diff --git a/backend/src/github_pm/sdlc_metrics.py b/backend/src/github_pm/sdlc_metrics.py index 0aad7b4..fe21d7c 100644 --- a/backend/src/github_pm/sdlc_metrics.py +++ b/backend/src/github_pm/sdlc_metrics.py @@ -1,4 +1,5 @@ -"""SDLC KPI helpers: PR classification, size buckets, medians, GitHub search. +# Assisted-by: Codex +"""Pure SDLC KPI helpers: classification, size buckets, medians and attribution. Generated-by: Cursor """ @@ -6,13 +7,11 @@ from __future__ import annotations from collections.abc import Callable, Iterable, Mapping, Sequence -from datetime import date, datetime, timedelta, UTC +from datetime import datetime, timedelta, UTC import re from typing import Any, Literal -from urllib.parse import quote_plus from github_pm.context import _parse_sdlc_label_csv, Settings -from github_pm.logger import logger PRType = Literal["feature", "bug_fix", "docs", "unclassified"] SizeBucket = Literal["tiny", "small", "medium", "large", "unknown"] @@ -29,13 +28,6 @@ def window_start(days: int, now: datetime | None = None) -> datetime: return now - timedelta(days=days) -def date_str(d: datetime) -> str: - """GitHub search date (YYYY-MM-DD) in UTC.""" - if d.tzinfo is None: - d = d.replace(tzinfo=UTC) - return d.astimezone(UTC).date().isoformat() - - def classify_pr_type( label_names: Iterable[str], settings: Settings, @@ -285,37 +277,6 @@ def build_semver_milestone_previous_map( return {ordered[i]: ordered[i - 1] for i in range(1, len(ordered))} -def escape_labeled_issues_query(github_repo: str, escape_label: str) -> str: - """GitHub issue search query for issues carrying the escape label.""" - lab = escape_label.strip().lower() - if not lab: - lab = "escape" - label_tok = f'label:"{lab}"' if any(c in lab for c in " /") else f"label:{lab}" - return f"{repo_search_fragment(github_repo)} is:issue {label_tok}" - - -def rest_search_issue_items_paginated( - get_fn: Callable[[str], dict[str, Any]], - q: str, - *, - per_page: int = 100, -) -> list[dict[str, Any]]: - """Walk ``/search/issues`` pages (GitHub caps total results at 1000).""" - items: list[dict[str, Any]] = [] - page = 1 - while True: - path = f"/search/issues?q={quote_plus(q)}&per_page={per_page}&page={page}" - data = get_fn(path) - batch = data.get("items") or [] - items.extend(batch) - if len(batch) < per_page: - break - if len(items) >= 1000: - break - page += 1 - return items - - def count_escape_issues_by_prior_milestone( escape_items: Iterable[dict[str, Any]], issue_milestone_to_prior: dict[str, str], @@ -338,373 +299,6 @@ def count_escape_issues_by_prior_milestone( return counts -def graphql_search_pull_requests( - post_graphql: Callable[[dict[str, Any]], dict[str, Any]], - search_query: str, - *, - page_size: int = 100, - filter_bot_authors: bool = True, -) -> list[dict[str, Any]]: - """Paginate GitHub GraphQL search (PullRequest nodes).""" - nodes: list[dict[str, Any]] = [] - cursor: str | None = None - gql = """ - query($q: String!, $first: Int!, $after: String) { - search(query: $q, type: ISSUE, first: $first, after: $after) { - pageInfo { hasNextPage endCursor } - nodes { - ... on PullRequest { - number - title - url - createdAt - updatedAt - state - isDraft - mergedAt - additions - deletions - labels(first: 30) { nodes { name } } - milestone { title } - author { - __typename - ... on User { login } - ... on Bot { login } - ... on Organization { login } - } - } - } - } - } - """ - while True: - payload = { - "query": gql, - "variables": { - "q": search_query, - "first": page_size, - "after": cursor, - }, - } - data = post_graphql(payload) - errors = data.get("errors") - if errors: - logger.error("GraphQL errors: %s", errors) - raise RuntimeError(f"GitHub GraphQL error: {errors!r}") - search = data.get("data", {}).get("search") or {} - batch = search.get("nodes") or [] - if filter_bot_authors: - nodes.extend(filter_out_bot_pr_nodes(batch)) - else: - nodes.extend([n for n in batch if n and n.get("number") is not None]) - page = search.get("pageInfo") or {} - if not page.get("hasNextPage"): - break - cursor = page.get("endCursor") - if not cursor: - break - return nodes - - -def graphql_search_open_pull_requests_attention( - post_graphql: Callable[[dict[str, Any]], dict[str, Any]], - search_query: str, - *, - page_size: int = 100, - filter_bot_authors: bool = False, -) -> list[dict[str, Any]]: - """Paginate GitHub GraphQL search for open PRs including ``mergeable``, - ``mergeStateStatus``, and ``reviews``. - - Used for project status "attention" sections; callers filter on merge state and - review timestamps. - """ - nodes: list[dict[str, Any]] = [] - cursor: str | None = None - gql = """ - query($q: String!, $first: Int!, $after: String) { - search(query: $q, type: ISSUE, first: $first, after: $after) { - pageInfo { hasNextPage endCursor } - nodes { - ... on PullRequest { - number - title - url - createdAt - updatedAt - state - isDraft - mergedAt - mergeable - mergeStateStatus - reviews(first: 100) { - nodes { - submittedAt - state - author { - __typename - ... on User { login } - ... on Bot { login } - ... on Organization { login } - } - } - } - additions - deletions - labels(first: 30) { nodes { name } } - milestone { title } - author { - __typename - ... on User { login } - ... on Bot { login } - ... on Organization { login } - } - } - } - } - } - """ - while True: - payload = { - "query": gql, - "variables": { - "q": search_query, - "first": page_size, - "after": cursor, - }, - } - data = post_graphql(payload) - errors = data.get("errors") - if errors: - logger.error("GraphQL errors: %s", errors) - raise RuntimeError(f"GitHub GraphQL error: {errors!r}") - search = data.get("data", {}).get("search") or {} - batch = search.get("nodes") or [] - if filter_bot_authors: - nodes.extend(filter_out_bot_pr_nodes(batch)) - else: - nodes.extend([n for n in batch if n and n.get("number") is not None]) - page = search.get("pageInfo") or {} - if not page.get("hasNextPage"): - break - cursor = page.get("endCursor") - if not cursor: - break - return nodes - - -def graphql_search_timeline_nodes( - post_graphql: Callable[[dict[str, Any]], dict[str, Any]], - search_query: str, - *, - page_size: int = 100, -) -> list[dict[str, Any]]: - """Paginate GraphQL ``search(type: ISSUE)`` returning Issue and PullRequest nodes. - - Used when REST ``GET /search/issues`` rejects the same query string (422). Request only - basic fields plus PR state and draft status and issue closure dates. - """ - nodes: list[dict[str, Any]] = [] - cursor: str | None = None - gql = """ - query($q: String!, $first: Int!, $after: String) { - search(query: $q, type: ISSUE, first: $first, after: $after) { - pageInfo { hasNextPage endCursor } - nodes { - __typename - ... on PullRequest { - number - title - url - createdAt - state - isDraft - } - ... on Issue { - number - title - url - createdAt - closedAt - } - } - } - } - """ - while True: - payload = { - "query": gql, - "variables": { - "q": search_query, - "first": page_size, - "after": cursor, - }, - } - data = post_graphql(payload) - errors = data.get("errors") - if errors: - logger.error("GraphQL errors: %s", errors) - raise RuntimeError(f"GitHub GraphQL error: {errors!r}") - search = data.get("data", {}).get("search") or {} - batch = search.get("nodes") or [] - nodes.extend([n for n in batch if n and n.get("number") is not None]) - page = search.get("pageInfo") or {} - if not page.get("hasNextPage"): - break - cursor = page.get("endCursor") - if not cursor: - break - return nodes - - -def repo_search_fragment(github_repo: str) -> str: - return f"repo:{github_repo}" - - -def merged_prs_query(github_repo: str, merged_since: datetime) -> str: - return ( - f"{repo_search_fragment(github_repo)} is:pr is:merged " - f"merged:>={date_str(merged_since)}" - ) - - -def merged_prs_query_between(github_repo: str, start_d: date, end_d: date) -> str: - """GraphQL issue search: merged PRs with merge date in ``[start_d, end_d]`` (UTC calendar days). - - REST ``GET /search/issues`` rejects several merged/closed date combinations (422); GraphQL - ``search`` accepts ``merged:`` ranges the same way as the web UI. - """ - a, b = start_d.isoformat(), end_d.isoformat() - if a > b: - a, b = b, a - return f"{repo_search_fragment(github_repo)} is:pr is:merged merged:{a}..{b}" - - -def opened_prs_between_query(github_repo: str, start_d: date, end_d: date) -> str: - """PRs with ``created`` in ``[start_d, end_d]`` (UTC calendar days, inclusive).""" - a, b = start_d.isoformat(), end_d.isoformat() - if a > b: - a, b = b, a - return f"{repo_search_fragment(github_repo)} is:pr created:{a}..{b}" - - -def open_pr_backlog_query(github_repo: str, start_d: date) -> str: - """Open, non-draft PRs last updated strictly before ``start_d`` (UTC calendar day at 00:00). - - Uses ``draft:false`` and ``updated: str: - """All open pull requests in the repo (drafts included) for merge/review heuristics.""" - return f"{repo_search_fragment(github_repo)} is:pr is:open" - - -def open_prs_updated_between_query(github_repo: str, start_d: date, end_d: date) -> str: - """Open, non-draft PRs with ``updated`` in ``[start_d, end_d]`` (UTC calendar days, inclusive).""" - a, b = start_d.isoformat(), end_d.isoformat() - if a > b: - a, b = b, a - return ( - f"{repo_search_fragment(github_repo)} is:pr is:open draft:false " - f"updated:{a}..{b}" - ) - - -def opened_issues_between_query(github_repo: str, start_d: date, end_d: date) -> str: - """Issues (not PRs) with ``created`` in ``[start_d, end_d]`` (UTC calendar days, inclusive).""" - a, b = start_d.isoformat(), end_d.isoformat() - if a > b: - a, b = b, a - return f"{repo_search_fragment(github_repo)} is:issue created:{a}..{b}" - - -def opened_prs_query(github_repo: str, created_since: datetime) -> str: - return ( - f"{repo_search_fragment(github_repo)} is:pr " - f"created:>={date_str(created_since)}" - ) - - -def milestone_merged_prs_query(github_repo: str, milestone_title: str) -> str: - # Quote milestone title for spaces/special chars - safe = milestone_title.replace('"', "\\") - return f'{repo_search_fragment(github_repo)} is:pr is:merged milestone:"{safe}"' - - -def bug_issues_created_query( - github_repo: str, bug_labels_csv: str, since: datetime -) -> str: - """Issues with bug label(s) created on or after `since` (any state).""" - label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) - return ( - f"{repo_search_fragment(github_repo)} is:issue {label_clause} " - f"created:>={date_str(since)}" - ) - - -def bug_issues_closed_query( - github_repo: str, bug_labels_csv: str, since: datetime -) -> str: - label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) - return ( - f"{repo_search_fragment(github_repo)} is:issue is:closed {label_clause} " - f"closed:>={date_str(since)}" - ) - - -def bug_issues_created_query_between( - github_repo: str, bug_labels_csv: str, lo: datetime, hi: datetime -) -> str: - """Bug issues created with ``created`` in ``[date(lo), date(hi)]`` (UTC dates, inclusive).""" - label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) - a, b = date_str(lo), date_str(hi) - if a > b: - a, b = b, a - return ( - f"{repo_search_fragment(github_repo)} is:issue {label_clause} created:{a}..{b}" - ) - - -def bug_issues_closed_query_between( - github_repo: str, bug_labels_csv: str, lo: datetime, hi: datetime -) -> str: - """Closed bug issues with ``closed`` in ``[date(lo), date(hi)]`` (UTC dates, inclusive).""" - label_clause = _label_or_clause(_parse_sdlc_label_csv(bug_labels_csv)) - a, b = date_str(lo), date_str(hi) - if a > b: - a, b = b, a - return ( - f"{repo_search_fragment(github_repo)} is:issue is:closed {label_clause} " - f"closed:{a}..{b}" - ) - - -def _label_or_clause(labels: frozenset[str]) -> str: - if not labels: - return "" - parts = [ - f'label:"{lab}"' if any(c in lab for c in " /") else f"label:{lab}" - for lab in sorted(labels) - ] - if len(parts) == 1: - return parts[0] - return "(" + " OR ".join(parts) + ")" - - -def rest_search_total_count( - get_fn: Callable[[str], dict[str, Any]], - q: str, -) -> int: - """GET /search/issues total_count.""" - path = f"/search/issues?q={quote_plus(q)}" - data = get_fn(path) - return int(data.get("total_count", 0)) - - def pr_row_from_graphql_node( node: dict[str, Any], settings: Settings, diff --git a/backend/src/github_pm/sdlc_models.py b/backend/src/github_pm/sdlc_models.py index d59678f..3d645e2 100644 --- a/backend/src/github_pm/sdlc_models.py +++ b/backend/src/github_pm/sdlc_models.py @@ -99,3 +99,11 @@ class BugBacklogSeriesResponse(BaseModel): weeks: int week_days: int slices: list[BugBacklogResponse] + + +class DashboardResponse(BaseModel): + """One collection context for all dashboard panels. Generated-by: Codex.""" + + delivery: DeliverySeriesResponse + escaped: EscapedDefectSeriesResponse + backlog: BugBacklogSeriesResponse diff --git a/backend/src/github_pm/sdlc_report_cli.py b/backend/src/github_pm/sdlc_report_cli.py index 3ffc192..97bdd1c 100644 --- a/backend/src/github_pm/sdlc_report_cli.py +++ b/backend/src/github_pm/sdlc_report_cli.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """Offline CLI: fetch SDLC KPIs from GitHub and write a static HTML report. Configuration uses the same environment variables as ``github_pm.context.Settings``: @@ -60,8 +61,8 @@ def main( ) -> None: """Generate a static SDLC HTML report (same metrics as the /api/v1/sdlc REST API).""" from github_pm import sdlc_service - from github_pm.api import Connector from github_pm.context import Settings + from github_pm.github_client import Connector from github_pm.sdlc_html_render import render_sdlc_report_html settings = Settings() @@ -79,16 +80,11 @@ def main( days_v = 7 if week_days is None else week_days out = output if output is not None else Path.cwd() / "sdlc-report.html" - gitctx = Connector(token, github_repo=settings.github_repo) - delivery = sdlc_service.compute_sdlc_delivery_series( - gitctx, settings, weeks=weeks_v, week_days=days_v - ) - escaped = sdlc_service.compute_escaped_defect_rate_series( - gitctx, settings, weeks=weeks_v, week_days=days_v - ) - bugs = sdlc_service.compute_bug_backlog_delta_series( - gitctx, settings, weeks=weeks_v, week_days=days_v - ) + with Connector(token, github_repo=settings.github_repo) as gitctx: + dashboard = sdlc_service.compute_sdlc_dashboard( + gitctx, settings, weeks=weeks_v, week_days=days_v + ) + delivery, escaped, bugs = dashboard.delivery, dashboard.escaped, dashboard.backlog html_doc = render_sdlc_report_html( repo=settings.github_repo, diff --git a/backend/src/github_pm/sdlc_service.py b/backend/src/github_pm/sdlc_service.py index d49e76c..49dfc55 100644 --- a/backend/src/github_pm/sdlc_service.py +++ b/backend/src/github_pm/sdlc_service.py @@ -1,393 +1,296 @@ -"""Shared SDLC KPI computation (used by FastAPI routes and the ``sdlc-report`` CLI). +"""Whole-interval SDLC collection with pure weekly aggregation. -Generated-by: Cursor +Generated-by: Codex """ -from __future__ import annotations - from datetime import datetime, timedelta -from typing import Any +from github_pm import report_queries as rq from github_pm import sdlc_metrics as sm -from github_pm.api import Connector, VERSION_MATCH -from github_pm.context import Settings -from github_pm.logger import logger +from github_pm.repository_catalog import milestone_catalog, VERSION_MATCH from github_pm.sdlc_models import ( BugBacklogResponse, BugBacklogSeriesResponse, - CycleTimePayload, + DashboardResponse, DeliveryResponse, DeliverySeriesResponse, EscapedDefectResponse, EscapedDefectRow, EscapedDefectSeriesResponse, - FirstReviewPayload, - ThroughputBreakdown, ) -def _post_graphql(gitctx: Connector): +def _post_graphql(gitctx): return lambda payload: gitctx.post("/graphql", payload) -def _github_repo(gitctx: Connector) -> str: - return f"{gitctx.owner}/{gitctx.repo}" - - -def _filter_merged_in_slice( - rows: list[dict[str, Any]], - window_start: datetime, - window_end: datetime, -) -> list[dict[str, Any]]: - """Half-open on the left: merged in (window_start, window_end].""" - out: list[dict[str, Any]] = [] - for r in rows: - m = r.get("merged_at") - if not m: - continue - if window_start < m <= window_end: - out.append(r) - return out - - -def _filter_created_in_slice( - rows: list[dict[str, Any]], - window_start: datetime, - window_end: datetime, -) -> list[dict[str, Any]]: - """Half-open on the left: created in (window_start, window_end].""" - out: list[dict[str, Any]] = [] - for r in rows: - c = r.get("created_at") - if not c: - continue - if window_start < c <= window_end: - out.append(r) - return out - - -def compute_sdlc_delivery( - gitctx: Connector, - settings: Settings, - *, - days: int = 7, - now: datetime | None = None, -) -> DeliveryResponse: - """Delivery metrics (throughput, cycle time, time to first human review). - - Window is ``(now - days, now]`` (UTC): merged and created timestamps strictly after - ``now - days`` and on or before ``now``. - """ - repo = _github_repo(gitctx) - window_end = now if now is not None else sm.utc_now() - window_start = window_end - timedelta(days=days) - - merged_q = sm.merged_prs_query(repo, window_start) - merged_nodes = sm.graphql_search_pull_requests(_post_graphql(gitctx), merged_q) - merged_rows = [sm.pr_row_from_graphql_node(n, settings) for n in merged_nodes] - merged_rows = _filter_merged_in_slice(merged_rows, window_start, window_end) - - throughput = sm.aggregate_throughput(merged_rows) - cycle = sm.build_median_cycle_payload(merged_rows) - - opened_q = sm.opened_prs_query(repo, window_start) - opened_nodes = sm.graphql_search_pull_requests(_post_graphql(gitctx), opened_q) - opened_rows = [sm.pr_row_from_graphql_node(n, settings) for n in opened_nodes] - opened_rows = _filter_created_in_slice(opened_rows, window_start, window_end) - - rows_with_review: list[tuple[dict[str, Any], float]] = [] - for r in opened_rows: - num = r["number"] - try: - raw = gitctx.get( - f"/repos/{gitctx.owner}/{gitctx.repo}/pulls/{num}/reviews", - headers={"Accept": "application/vnd.github+json"}, - ) - except Exception as e: - logger.warning("Failed to fetch reviews for PR %s: %s", num, e) - continue - if not isinstance(raw, list): - logger.warning( - "Reviews for PR %s: expected JSON array, got %s", num, type(raw) - ) - continue - rev_list: list[dict[str, Any]] = raw - first_at = sm.first_human_review_submitted_at(rev_list) - created = r.get("created_at") - if first_at and created: - secs = max(0.0, (first_at - created).total_seconds()) - rows_with_review.append((r, secs)) - - review_payload = sm.build_first_review_payload( - rows_with_review, eligible_count=len(opened_rows) - ) +def _range(field, start, end): + # Search whole UTC dates, then enforce exact (start, end] timestamps locally. + return f"{field}:{rq.date_str(start)}..{rq.date_str(end)}" + +def _collect(gitctx, settings, start, end, kinds): + post = _post_graphql(gitctx) + repo = f"{gitctx.owner}/{gitctx.repo}" + searches, facts = {}, {} + if "delivery" in kinds: + searches["merged"] = rq.Search( + f'repo:{repo} is:pr is:merged {_range("merged", start, end)}', rq.DELIVERY + ) + searches["opened"] = rq.Search( + f'repo:{repo} is:pr {_range("created", start, end)}', + rq.DELIVERY + " " + rq.REVIEWS, + ) + if "escape" in kinds: + opened_ms, closed_ms = milestone_catalog(gitctx) + facts["targets"] = sm.select_escaped_defect_milestones( + opened_ms, closed_ms, version_match=VERSION_MATCH + ) + facts["previous"] = sm.build_semver_milestone_previous_map( + opened_ms, closed_ms, version_match=VERSION_MATCH + ) + searches["escapes"] = rq.Search( + rq.escape_labeled_issues_query(repo, settings.sdlc_escape_label) + + " " + + _range("created", start, end), + rq.ISSUE_DATES, + "Issue", + ) + if "delivery" not in kinds: + for i, (title, _) in enumerate(facts["targets"]): + searches[f"release{i}"] = rq.Search( + rq.milestone_merged_prs_query(repo, title) + + " " + + _range("merged", start, end), + rq.ESCAPE, + ) + if "bugs" in kinds: + searches["bugs_opened"] = rq.Search( + rq.bug_issues_created_query_between( + repo, settings.sdlc_bug_labels, start, end + ), + rq.ISSUE_DATES, + "Issue", + ) + searches["bugs_closed"] = rq.Search( + rq.bug_issues_closed_query_between( + repo, settings.sdlc_bug_labels, start, end + ), + rq.ISSUE_DATES, + "Issue", + ) + results = rq.search_many(post, searches) + for key, nodes in results.items(): + if key in ("merged", "opened") or key.startswith("release"): + rq.complete_prs(post, nodes, labels=True, reviews=key == "opened") + rows = [] + for node in sm.filter_out_bot_pr_nodes(nodes): + row = sm.pr_row_from_graphql_node(node, settings) + if key == "opened": + reviews = [ + dict( + user=dict( + login=(r.get("author") or {}).get("login"), + type=(r.get("author") or {}).get("__typename"), + ), + submitted_at=r.get("submittedAt"), + ) + for r in node["reviews"]["nodes"] + if r + ] + row["first_review"] = sm.first_human_review_submitted_at(reviews) + rows.append(row) + facts[key] = rows + else: + facts[key] = nodes + if "escape" in kinds and "delivery" not in kinds: + facts["merged"] = [ + row + for key, rows in facts.items() + if key.startswith("release") + for row in rows + ] + return facts + + +def _inside(value, start, end): + return value is not None and start < value <= end + + +def _delivery(facts, start, end, days): + merged = [r for r in facts["merged"] if _inside(r["merged_at"], start, end)] + opened = [r for r in facts["opened"] if _inside(r["created_at"], start, end)] + reviewed = [ + (r, max(0.0, (r["first_review"] - r["created_at"]).total_seconds())) + for r in opened + if r.get("first_review") + ] return DeliveryResponse( window_days=days, - window_start=window_start, - window_end=window_end, - as_of=window_end, - merged_pr_throughput=ThroughputBreakdown(**throughput), - median_pr_cycle_time=CycleTimePayload(**cycle), - median_time_to_first_review=FirstReviewPayload(**review_payload), + window_start=start, + window_end=end, + as_of=end, + merged_pr_throughput=sm.aggregate_throughput(merged), + median_pr_cycle_time=sm.build_median_cycle_payload(merged), + median_time_to_first_review=sm.build_first_review_payload( + reviewed, eligible_count=len(opened) + ), ) -def compute_sdlc_delivery_series( - gitctx: Connector, - settings: Settings, - *, - weeks: int, - week_days: int = 7, -) -> DeliverySeriesResponse: - """One delivery slice per week, oldest slice first.""" - now = sm.utc_now() - slices: list[DeliveryResponse] = [] - for i in range(weeks): - window_end = now - timedelta(days=i * week_days) - slices.append( - compute_sdlc_delivery(gitctx, settings, days=week_days, now=window_end) +def _escape(facts, start, end): + releases = [] + for title, next_open in facts["targets"]: + rows = [ + r + for r in facts["merged"] + if r["milestone_title"] == title + and (start is None or _inside(r["merged_at"], start, end)) + ] + counts = { + kind: sum(r["pr_type"] == kind for r in rows) + for kind in ("feature", "bug_fix", "docs") + } + escapes = sum( + facts["previous"].get((i.get("milestone") or {}).get("title")) == title + and ( + start is None + or _inside(sm.parse_github_ts(i.get("createdAt")), start, end) + ) + for i in facts["escapes"] ) - slices.reverse() - return DeliverySeriesResponse(weeks=weeks, week_days=week_days, slices=slices) - - -def compute_escaped_defect_rate( - gitctx: Connector, - settings: Settings, -) -> EscapedDefectResponse: - now = sm.utc_now() - repo = _github_repo(gitctx) - open_milestones = gitctx.get_paged( - f"/repos/{repo}/milestones?state=open", - headers={"Accept": "application/vnd.github+json"}, - ) - closed_milestones = gitctx.get_paged( - f"/repos/{repo}/milestones?state=closed", - headers={"Accept": "application/vnd.github+json"}, - ) - targets = sm.select_escaped_defect_milestones( - open_milestones, - closed_milestones, - version_match=VERSION_MATCH, - ) - issue_milestone_to_prior = sm.build_semver_milestone_previous_map( - open_milestones, - closed_milestones, - version_match=VERSION_MATCH, - ) - escape_q = sm.escape_labeled_issues_query(repo, settings.sdlc_escape_label) - escape_items = sm.rest_search_issue_items_paginated( - lambda path: gitctx.get(path), - escape_q, - ) - escapes_by_release = sm.count_escape_issues_by_prior_milestone( - escape_items, - issue_milestone_to_prior, - version_match=VERSION_MATCH, - ) - releases: list[EscapedDefectRow] = [] - post = _post_graphql(gitctx) - - for title, is_next_open in targets: - q = sm.milestone_merged_prs_query(repo, title) - nodes = sm.graphql_search_pull_requests(post, q) - rows = [sm.pr_row_from_graphql_node(n, settings) for n in nodes] - feat = sum(1 for r in rows if r["pr_type"] == "feature") - bugs = sum(1 for r in rows if r["pr_type"] == "bug_fix") - docs = sum(1 for r in rows if r["pr_type"] == "docs") - denom = feat + bugs + docs - esc = int(escapes_by_release.get(title, 0)) - rate = (esc / denom) if denom else None + denominator = sum(counts.values()) releases.append( EscapedDefectRow( release=title, - feature_prs=feat, - bug_fix_prs=bugs, - docs_prs=docs, - escape_issues=esc, - rate=rate, - is_next_open=is_next_open, + feature_prs=counts["feature"], + bug_fix_prs=counts["bug_fix"], + docs_prs=counts["docs"], + escape_issues=escapes, + rate=escapes / denominator if denominator else None, + is_next_open=next_open, ) ) + return EscapedDefectResponse( + window_start=start, + window_end=end if start is not None else None, + as_of=end, + releases=releases, + ) - return EscapedDefectResponse(as_of=now, releases=releases) - - -def compute_escaped_defect_rate_series( - gitctx: Connector, - settings: Settings, - *, - weeks: int, - week_days: int = 7, -) -> EscapedDefectSeriesResponse: - """Incremental escaped defect stats per week (oldest slice first). - Milestone selection uses **current** open/closed milestones. Denominators count - non-bot PRs merged into each target milestone with ``merged_at`` in the slice; - numerators count escape-labeled issues **created** in the slice whose milestone - maps to that row via ``issue_milestone_to_prior`` (same attribution as the - cumulative endpoint). - """ - now = sm.utc_now() - repo = _github_repo(gitctx) - open_milestones = gitctx.get_paged( - f"/repos/{repo}/milestones?state=open", - headers={"Accept": "application/vnd.github+json"}, - ) - closed_milestones = gitctx.get_paged( - f"/repos/{repo}/milestones?state=closed", - headers={"Accept": "application/vnd.github+json"}, +def _bugs(facts, start, end, days): + opened = sum( + _inside(sm.parse_github_ts(n.get("createdAt")), start, end) + for n in facts["bugs_opened"] ) - targets = sm.select_escaped_defect_milestones( - open_milestones, - closed_milestones, - version_match=VERSION_MATCH, + closed = sum( + _inside(sm.parse_github_ts(n.get("closedAt")), start, end) + for n in facts["bugs_closed"] ) - issue_milestone_to_prior = sm.build_semver_milestone_previous_map( - open_milestones, - closed_milestones, - version_match=VERSION_MATCH, - ) - escape_q = sm.escape_labeled_issues_query(repo, settings.sdlc_escape_label) - escape_items = sm.rest_search_issue_items_paginated( - lambda path: gitctx.get(path), - escape_q, - ) - post = _post_graphql(gitctx) - rows_by_title: dict[str, list[dict[str, Any]]] = {} - for title, _is_next_open in targets: - q = sm.milestone_merged_prs_query(repo, title) - nodes = sm.graphql_search_pull_requests(post, q) - rows_by_title[title] = [sm.pr_row_from_graphql_node(n, settings) for n in nodes] - - slices: list[EscapedDefectResponse] = [] - for i in range(weeks): - window_end = now - timedelta(days=i * week_days) - window_start = window_end - timedelta(days=week_days) - releases: list[EscapedDefectRow] = [] - for title, is_next_open in targets: - rows = rows_by_title[title] - in_win = [ - r - for r in rows - if r.get("merged_at") and window_start < r["merged_at"] <= window_end - ] - feat = sum(1 for r in in_win if r["pr_type"] == "feature") - bugs = sum(1 for r in in_win if r["pr_type"] == "bug_fix") - docs = sum(1 for r in in_win if r["pr_type"] == "docs") - denom = feat + bugs + docs - esc = 0 - for item in escape_items: - ts = sm.parse_github_ts(item.get("created_at")) - if ts is None or not (window_start < ts <= window_end): - continue - ms = item.get("milestone") - if not isinstance(ms, dict): - continue - mt = str(ms.get("title") or "") - if not VERSION_MATCH.match(mt): - continue - prior = issue_milestone_to_prior.get(mt) - if prior != title: - continue - esc += 1 - rate = (esc / denom) if denom else None - releases.append( - EscapedDefectRow( - release=title, - feature_prs=feat, - bug_fix_prs=bugs, - docs_prs=docs, - escape_issues=esc, - rate=rate, - is_next_open=is_next_open, - ) - ) - slices.append( - EscapedDefectResponse( - window_start=window_start, - window_end=window_end, - as_of=window_end, - releases=releases, - ) - ) - slices.reverse() - return EscapedDefectSeriesResponse(weeks=weeks, week_days=week_days, slices=slices) - - -def _count_bug_issues_in_slice( - gitctx: Connector, - repo: str, - labels: str, - window_start: datetime, - window_end: datetime, - *, - opened: bool, -) -> int: - """Count bug issues opened or closed in ``(window_start, window_end]`` (UTC).""" - q = ( - sm.bug_issues_created_query_between(repo, labels, window_start, window_end) - if opened - else sm.bug_issues_closed_query_between(repo, labels, window_start, window_end) - ) - items = sm.rest_search_issue_items_paginated( - lambda path: gitctx.get(path), - q, - ) - key = "created_at" if opened else "closed_at" - n = 0 - for item in items: - raw = item.get(key) - ts = sm.parse_github_ts(raw) if raw else None - if ts is not None and window_start < ts <= window_end: - n += 1 - return n - - -def compute_bug_backlog_delta( - gitctx: Connector, - settings: Settings, - *, - days: int = 7, - now: datetime | None = None, -) -> BugBacklogResponse: - window_end = now if now is not None else sm.utc_now() - window_start = window_end - timedelta(days=days) - repo = _github_repo(gitctx) - labels = settings.sdlc_bug_labels - - opened = _count_bug_issues_in_slice( - gitctx, repo, labels, window_start, window_end, opened=True - ) - closed = _count_bug_issues_in_slice( - gitctx, repo, labels, window_start, window_end, opened=False - ) - return BugBacklogResponse( window_days=days, - window_start=window_start, - window_end=window_end, - as_of=window_end, + window_start=start, + window_end=end, + as_of=end, bugs_opened=opened, bugs_closed=closed, net=opened - closed, ) -def compute_bug_backlog_delta_series( - gitctx: Connector, - settings: Settings, - *, - weeks: int, - week_days: int = 7, -) -> BugBacklogSeriesResponse: +def _series(facts, now, weeks, week_days, kind): + slices = [] + for i in reversed(range(weeks)): + end = now - timedelta(days=i * week_days) + start = end - timedelta(days=week_days) + if kind == "delivery": + slices.append(_delivery(facts, start, end, week_days)) + elif kind == "escape": + slices.append(_escape(facts, start, end)) + else: + slices.append(_bugs(facts, start, end, week_days)) + model = { + "delivery": DeliverySeriesResponse, + "escape": EscapedDefectSeriesResponse, + "bugs": BugBacklogSeriesResponse, + }[kind] + return model(weeks=weeks, week_days=week_days, slices=slices) + + +def compute_sdlc_dashboard(gitctx, settings, *, weeks=4, week_days=7): now = sm.utc_now() - slices: list[BugBacklogResponse] = [] - for i in range(weeks): - window_end = now - timedelta(days=i * week_days) - slices.append( - compute_bug_backlog_delta(gitctx, settings, days=week_days, now=window_end) + kinds = {"delivery", "escape", "bugs"} + facts = _collect( + gitctx, settings, now - timedelta(days=weeks * week_days), now, kinds + ) + return DashboardResponse( + delivery=_series(facts, now, weeks, week_days, "delivery"), + escaped=_series(facts, now, weeks, week_days, "escape"), + backlog=_series(facts, now, weeks, week_days, "bugs"), + ) + + +def _standalone(gitctx, settings, weeks, week_days, kind, now=None): + now = now or sm.utc_now() + facts = _collect( + gitctx, settings, now - timedelta(days=weeks * week_days), now, {kind} + ) + return _series(facts, now, weeks, week_days, kind) + + +def compute_sdlc_delivery_series(gitctx, settings, *, weeks, week_days=7): + return _standalone(gitctx, settings, weeks, week_days, "delivery") + + +def compute_sdlc_delivery(gitctx, settings, *, days=7, now: datetime | None = None): + return _standalone(gitctx, settings, 1, days, "delivery", now).slices[0] + + +def compute_escaped_defect_rate_series(gitctx, settings, *, weeks, week_days=7): + return _standalone(gitctx, settings, weeks, week_days, "escape") + + +def compute_bug_backlog_delta_series(gitctx, settings, *, weeks, week_days=7): + return _standalone(gitctx, settings, weeks, week_days, "bugs") + + +def compute_bug_backlog_delta(gitctx, settings, *, days=7, now=None): + return _standalone(gitctx, settings, 1, days, "bugs", now).slices[0] + + +def compute_escaped_defect_rate(gitctx, settings): + """Cumulative metrics deliberately retain their unbounded historical scope.""" + repo = f"{gitctx.owner}/{gitctx.repo}" + opened, closed = milestone_catalog(gitctx) + facts = { + "targets": sm.select_escaped_defect_milestones( + opened, closed, version_match=VERSION_MATCH + ), + "previous": sm.build_semver_milestone_previous_map( + opened, closed, version_match=VERSION_MATCH + ), + } + historical_scope = f" created:1970-01-01..{rq.date_str(sm.utc_now())}" + searches = { + "escapes": rq.Search( + rq.escape_labeled_issues_query(repo, settings.sdlc_escape_label) + + historical_scope, + rq.ISSUE_DATES, + "Issue", + ) + } + for i, (title, _) in enumerate(facts["targets"]): + searches[f"release{i}"] = rq.Search( + rq.milestone_merged_prs_query(repo, title) + historical_scope, rq.ESCAPE + ) + post = _post_graphql(gitctx) + results = rq.search_many(post, searches) + facts["escapes"], facts["merged"] = results.pop("escapes"), [] + for nodes in results.values(): + rq.complete_prs(post, nodes, labels=True) + facts["merged"].extend( + sm.pr_row_from_graphql_node(n, settings) + for n in sm.filter_out_bot_pr_nodes(nodes) ) - slices.reverse() - return BugBacklogSeriesResponse(weeks=weeks, week_days=week_days, slices=slices) + return _escape(facts, None, sm.utc_now()) diff --git a/backend/src/github_pm/status_report_api.py b/backend/src/github_pm/status_report_api.py index 60282e8..2987904 100644 --- a/backend/src/github_pm/status_report_api.py +++ b/backend/src/github_pm/status_report_api.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """REST API for the project status report. Generated-by: Cursor @@ -10,7 +11,9 @@ from fastapi import APIRouter, Depends, HTTPException, Query -from github_pm.api import connection, Connector +from github_pm.api import connection +from github_pm.execution import offload +from github_pm.github_client import Connector from github_pm.status_report_models import ProjectStatusReportResponse from github_pm.status_report_service import build_project_status_report @@ -24,7 +27,8 @@ def _default_end_date() -> date: @status_report_router.get("/project-status", response_model=ProjectStatusReportResponse) -async def get_project_status_report( +@offload +def get_project_status_report( gitctx: Annotated[Connector, Depends(connection)], start_date: Annotated[ date | None, diff --git a/backend/src/github_pm/status_report_service.py b/backend/src/github_pm/status_report_service.py index b0c94b7..bb4973e 100644 --- a/backend/src/github_pm/status_report_service.py +++ b/backend/src/github_pm/status_report_service.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """Project status report via GraphQL ``search`` Generated-by: Cursor @@ -8,8 +9,9 @@ from datetime import date, datetime, UTC from typing import Any +from github_pm import report_queries as rq from github_pm import sdlc_metrics as sm -from github_pm.api import Connector +from github_pm.github_client import Connector from github_pm.status_report_models import ( PrBacklogItem, ProjectStatusReportResponse, @@ -194,20 +196,33 @@ def build_project_status_report( def post_gql(payload: dict[str, Any]) -> dict[str, Any]: return gitctx.post("/graphql", payload) - merged_q = sm.merged_prs_query_between(repo, start_date, end_date) - merged_nodes = sm.graphql_search_pull_requests( + fetched = rq.search_many( post_gql, - merged_q, - filter_bot_authors=False, + { + "merged": rq.Search( + rq.merged_prs_query_between(repo, start_date, end_date), + "id number title url mergedAt", + ), + "opened_prs": rq.Search( + rq.opened_prs_between_query(repo, start_date, end_date), + "id number title url createdAt state isDraft", + ), + "opened_issues": rq.Search( + rq.opened_issues_between_query(repo, start_date, end_date), + "id number title url createdAt closedAt", + "Issue", + ), + }, ) + attention_nodes = rq.open_prs(post_gql, gitctx.owner, gitctx.repo) + merged_nodes = fetched["merged"] merged_in_window = [ n for n in merged_nodes if _merged_calendar_in_window(n, start_date, end_date) ] merged_in_window.sort(key=lambda n: int(n["number"])) merged_items = [_item_from_gql_node(n) for n in merged_in_window] - opened_pr_q = sm.opened_prs_between_query(repo, start_date, end_date) + " is:open" - opened_pr_nodes = sm.graphql_search_timeline_nodes(post_gql, opened_pr_q) + opened_pr_nodes = fetched["opened_prs"] opened_pr_filtered = [ n for n in opened_pr_nodes @@ -224,8 +239,7 @@ def post_gql(payload: dict[str, Any]) -> dict[str, Any]: item.title += " (draft)" opened_pr_items.append(item) - opened_issue_q = sm.opened_issues_between_query(repo, start_date, end_date) - opened_issue_nodes = sm.graphql_search_timeline_nodes(post_gql, opened_issue_q) + opened_issue_nodes = fetched["opened_issues"] opened_issue_filtered = [ n for n in opened_issue_nodes @@ -235,12 +249,7 @@ def post_gql(payload: dict[str, Any]) -> dict[str, Any]: ] opened_issue_filtered.sort(key=lambda n: int(n["number"])) - recently_q = sm.open_prs_updated_between_query(repo, start_date, end_date) - recently_nodes = sm.graphql_search_pull_requests( - post_gql, - recently_q, - filter_bot_authors=False, - ) + recently_nodes = attention_nodes recently_filtered = [ n for n in recently_nodes @@ -253,22 +262,11 @@ def post_gql(payload: dict[str, Any]) -> dict[str, Any]: recently_filtered.sort(key=lambda n: int(n["number"])) recently_items = [_item_from_gql_node(n) for n in recently_filtered] - attention_q = sm.open_pull_requests_for_attention_query(repo) - attention_nodes = sm.graphql_search_open_pull_requests_attention( - post_gql, - attention_q, - filter_bot_authors=False, - ) reviewer_attention, second_approval, creator_attention = ( _partition_attention_open_prs(attention_nodes) ) - backlog_q = sm.open_pr_backlog_query(repo, start_date) - backlog_nodes = sm.graphql_search_pull_requests( - post_gql, - backlog_q, - filter_bot_authors=False, - ) + backlog_nodes = attention_nodes backlog_filtered = [ n for n in backlog_nodes diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index f7437e8..95699e6 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """Tests for the api module. Generated-by: Cursor @@ -11,16 +12,8 @@ import requests from github_pm.api import ( - add_blocked_by, - add_blocking, - add_close_reference, - add_label_to_issue, add_milestone_to_issue, - AddCloseReference, - AddDependency, - adopt_parent_milestone, api_router, - clear_issue_parent, close_issue_with_comment, CloseIssueRequest, connection, @@ -38,31 +31,22 @@ get_comment_reactions, get_comments, get_issue_reactions, - get_issues, get_labels, - get_milestone_open_counts, get_milestones, get_project, - remove_blocked_by, - remove_blocking, - remove_label_from_issue, remove_milestone_from_issue, render_markdown, RenderMarkdown, - set_issue_parent, - SetIssueParent, update_comment, update_issue_body, update_issue_title, update_issue_type, update_label, - update_milestone_state, UpdateComment, UpdateIssueBody, UpdateIssueTitle, UpdateIssueType, UpdateLabel, - UpdateMilestoneState, ) from github_pm.app import app @@ -76,7 +60,9 @@ async def test_connection_success(self): # Arrange mock_session = Mock() with ( - patch("github_pm.api.requests.session", return_value=mock_session), + patch( + "github_pm.github_client.requests.session", return_value=mock_session + ), patch("github_pm.api.context") as mock_context, ): mock_context.github_repo = "test/repo" @@ -105,7 +91,8 @@ async def test_connection_github_init_error(self): # Arrange with ( patch( - "github_pm.api.requests.session", side_effect=Exception("Auth failed") + "github_pm.github_client.requests.session", + side_effect=Exception("Auth failed"), ), patch("github_pm.api.context") as mock_context, ): @@ -125,7 +112,9 @@ async def test_connection_get_repo_error(self): mock_session = Mock() # Simulate an error during initialization (e.g., invalid token) with ( - patch("github_pm.api.requests.session", return_value=mock_session), + patch( + "github_pm.github_client.requests.session", return_value=mock_session + ), patch("github_pm.api.context") as mock_context, ): mock_context.github_repo = "test/repo" @@ -151,7 +140,9 @@ async def test_connection_closes_on_exit(self): # Arrange mock_session = Mock() with ( - patch("github_pm.api.requests.session", return_value=mock_session), + patch( + "github_pm.github_client.requests.session", return_value=mock_session + ), patch("github_pm.api.context") as mock_context, ): mock_context.github_repo = "test/repo" @@ -171,6 +162,7 @@ async def test_connection_closes_on_exit(self): # Assert - session should have headers set mock_session.headers.update.assert_called() + mock_session.close.assert_called_once() class TestGetProject: @@ -194,650 +186,6 @@ async def test_get_project(self): } -class TestGetIssues: - """Test the get_issues endpoint.""" - - @pytest.mark.asyncio - async def test_get_issues_with_milestone(self): - """Test getting issues for a specific milestone.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Issue 1", - "pull_request": {}, - "labels": [{"name": "bug"}], - }, - { - "id": 2, - "number": 2, - "title": "Issue 2", - "labels": [{"name": "feature"}], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await get_issues(mock_gitctx, milestone_number=1) - - # Assert - assert len(result["issues"]) == 1 - assert len(result["pull_requests"]) == 1 - assert result["pull_requests"][0]["id"] == 1 - assert result["issues"][0]["id"] == 2 - assert result["issues"][0]["hierarchy_depth"] == 0 - assert result["issues"][0]["children"] == [] - assert result["issues"][0]["child_count"] == 0 - mock_gitctx.get_paged.assert_called_once() - # Issue 1 has pull_request, so no GraphQL call - # Issue 2 doesn't have pull_request, so GraphQL should be called - assert mock_gitctx.post.call_count == 1 - - @pytest.mark.asyncio - async def test_get_issues_builds_hierarchy(self): - """Test nesting when GraphQL reports an in-milestone parent.""" - mock_issues = [ - {"id": 10, "number": 10, "title": "Epic", "labels": []}, - {"id": 20, "number": 20, "title": "Story", "labels": []}, - ] - - def graphql_side_effect(*_args, **kwargs): - issue_num = kwargs["data"]["variables"]["issue"] - parent = None - if issue_num == 20: - parent = { - "number": 10, - "title": "Epic", - "milestone": {"number": 1, "title": "M1"}, - } - return { - "data": { - "repository": { - "issue": { - "closedByPullRequestsReferences": {"nodes": []}, - "parent": parent, - "subIssues": {"nodes": []}, - "subIssuesSummary": { - "total": 0, - "completed": 0, - "percentCompleted": 0, - }, - } - } - } - } - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock(side_effect=graphql_side_effect) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await get_issues(mock_gitctx, milestone_number=1) - - assert len(result["issues"]) == 1 - epic = result["issues"][0] - assert epic["number"] == 10 - assert epic["child_count"] == 1 - assert epic["children"][0]["number"] == 20 - assert epic["children"][0]["hierarchy_depth"] == 1 - assert epic["children"][0]["parent_number"] == 10 - - @pytest.mark.asyncio - async def test_get_issues_with_linked_prs(self): - """Test getting issues with linked PRs from GraphQL.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Issue 1", - "labels": [{"name": "bug"}], - } - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": { - "closedByPullRequestsReferences": { - "nodes": [ - { - "number": 123, - "title": "Fix Issue 1", - "url": "https://github.com/test/repo/pull/123", - }, - { - "number": 456, - "title": "Another fix", - "url": "https://github.com/test/repo/pull/456", - }, - ] - } - } - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await get_issues(mock_gitctx, milestone_number=1) - - # Assert - assert len(result["issues"]) == 1 - assert result["pull_requests"] == [] - assert result["issues"][0]["id"] == 1 - assert "closed_by" in result["issues"][0] - assert len(result["issues"][0]["closed_by"]) == 2 - assert result["issues"][0]["closed_by"][0]["number"] == 123 - assert result["issues"][0]["closed_by"][0]["title"] == "Fix Issue 1" - assert ( - result["issues"][0]["closed_by"][0]["url"] - == "https://github.com/test/repo/pull/123" - ) - assert result["issues"][0]["closed_by"][1]["number"] == 456 - mock_gitctx.post.assert_called_once() - - @pytest.mark.asyncio - async def test_get_issues_with_dependencies(self): - """Test getting issues with blocked_by / blocking from GraphQL.""" - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Issue 1", - "labels": [], - } - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": { - "closedByPullRequestsReferences": {"nodes": []}, - "blockedBy": { - "nodes": [ - { - "databaseId": 17, - "number": 17, - "title": "Blocker", - "url": "https://github.com/test/repo/issues/17", - "state": "OPEN", - } - ] - }, - "blocking": { - "nodes": [ - { - "databaseId": 88, - "number": 88, - "title": "Waiting", - "url": "https://github.com/test/repo/issues/88", - "state": "OPEN", - } - ] - }, - } - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await get_issues(mock_gitctx, milestone_number=1) - - assert len(result["issues"]) == 1 - issue = result["issues"][0] - assert issue["blocked_by"][0]["number"] == 17 - assert issue["blocked_by"][0]["id"] == 17 - assert issue["blocking"][0]["number"] == 88 - assert "closed_by" not in issue - - @pytest.mark.asyncio - async def test_get_issues_with_no_milestone(self): - """Test getting issues with milestone_number=0 (no milestone).""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Issue 1", - "labels": [], - } - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await get_issues(mock_gitctx, milestone_number=0) - - # Assert - assert len(result["issues"]) == 1 - assert result["pull_requests"] == [] - assert result["issues"][0]["id"] == 1 - mock_gitctx.get_paged.assert_called_once() - # GraphQL should be called for issue without pull_request - assert mock_gitctx.post.call_count == 1 - - @pytest.mark.asyncio - async def test_get_issues_with_sort_single_label(self): - """Test getting issues sorted by a single label.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Bug Issue", - "labels": [{"name": "bug"}], - }, - { - "id": 2, - "number": 2, - "title": "Feature Issue", - "labels": [{"name": "feature"}], - }, - { - "id": 3, - "number": 3, - "title": "Other Issue", - "labels": [{"name": "documentation"}], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await get_issues(mock_gitctx, milestone_number=0, sort="bug") - - # Assert - assert len(result["issues"]) == 3 - assert result["pull_requests"] == [] - # First should be bug issue - assert result["issues"][0]["id"] == 1 - assert result["issues"][0]["title"] == "Bug Issue" - # Then other issues - assert result["issues"][1]["id"] == 2 - assert result["issues"][2]["id"] == 3 - - @pytest.mark.asyncio - async def test_get_issues_with_sort_multiple_labels(self): - """Test getting issues sorted by multiple labels in order.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Bug Issue", - "labels": [{"name": "bug"}], - }, - { - "id": 2, - "number": 2, - "title": "Feature Issue", - "labels": [{"name": "feature"}], - }, - { - "id": 3, - "number": 3, - "title": "Enhancement Issue", - "labels": [{"name": "enhancement"}], - }, - { - "id": 4, - "number": 4, - "title": "Unlabeled Issue", - "labels": [], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await get_issues( - mock_gitctx, milestone_number=0, sort="bug,feature,enhancement" - ) - - # Assert - assert len(result["issues"]) == 4 - assert result["pull_requests"] == [] - # Order should be: bug, feature, enhancement, other - assert result["issues"][0]["id"] == 1 # bug - assert result["issues"][1]["id"] == 2 # feature - assert result["issues"][2]["id"] == 3 # enhancement - assert result["issues"][3]["id"] == 4 # other (unlabeled) - - @pytest.mark.asyncio - async def test_get_issues_with_sort_case_insensitive(self): - """Test that label matching is case-insensitive.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Bug Issue", - "labels": [{"name": "BUG"}], # Uppercase label - }, - { - "id": 2, - "number": 2, - "title": "Feature Issue", - "labels": [{"name": "Feature"}], # Mixed case label - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - using lowercase sort labels - result = await get_issues( - mock_gitctx, milestone_number=0, sort="bug,feature" - ) - - # Assert - assert len(result["issues"]) == 2 - assert result["pull_requests"] == [] - # Should match despite case difference - assert result["issues"][0]["id"] == 1 # bug (matched "BUG") - assert result["issues"][1]["id"] == 2 # feature (matched "Feature") - - @pytest.mark.asyncio - async def test_get_issues_with_sort_whitespace_stripped(self): - """Test that whitespace in sort parameter is stripped.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Bug Issue", - "labels": [{"name": "bug"}], - }, - { - "id": 2, - "number": 2, - "title": "Feature Issue", - "labels": [{"name": "feature"}], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - with whitespace in sort parameter - result = await get_issues( - mock_gitctx, milestone_number=0, sort=" bug , feature " - ) - - # Assert - assert len(result["issues"]) == 2 - assert result["pull_requests"] == [] - # Should match despite whitespace - assert result["issues"][0]["id"] == 1 # bug - assert result["issues"][1]["id"] == 2 # feature - - @pytest.mark.asyncio - async def test_get_issues_with_sort_first_match_wins(self): - """Test that issues matching multiple labels go to the first matching label.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Bug/Feature Issue", - "labels": [ - {"name": "bug"}, - {"name": "feature"}, - ], # Issue with both labels - }, - { - "id": 2, - "number": 2, - "title": "Feature Only Issue", - "labels": [{"name": "feature"}], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - bug comes first in sort order - result = await get_issues( - mock_gitctx, milestone_number=0, sort="bug,feature" - ) - - # Assert - assert len(result["issues"]) == 2 - assert result["pull_requests"] == [] - # Issue 1 should be in bug category (first match), not feature - assert result["issues"][0]["id"] == 1 # bug (first match wins) - assert result["issues"][1]["id"] == 2 # feature - - @pytest.mark.asyncio - async def test_get_issues_with_sort_all_other(self): - """Test that issues without sort parameter all go to 'other'.""" - # Arrange - mock_issues = [ - { - "id": 1, - "number": 1, - "title": "Bug Issue", - "labels": [{"name": "bug"}], - }, - { - "id": 2, - "number": 2, - "title": "Feature Issue", - "labels": [{"name": "feature"}], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_issues) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - no sort parameter - result = await get_issues(mock_gitctx, milestone_number=0, sort=None) - - # Assert - assert len(result["issues"]) == 2 - assert result["pull_requests"] == [] - # Both should be in "other" since no sort labels specified - assert result["issues"][0]["id"] == 1 - assert result["issues"][1]["id"] == 2 - - @pytest.mark.asyncio - async def test_get_issues_sorts_issues_and_prs_separately(self): - """Issues and PRs are sorted independently by the same label criteria.""" - # Arrange - mock_items = [ - { - "id": 1, - "number": 1, - "title": "Bug Issue", - "labels": [{"name": "bug"}], - }, - { - "id": 2, - "number": 2, - "title": "Feature Issue", - "labels": [{"name": "feature"}], - }, - { - "id": 10, - "number": 10, - "title": "Feature PR", - "pull_request": {}, - "labels": [{"name": "feature"}], - }, - { - "id": 11, - "number": 11, - "title": "Bug PR", - "pull_request": {}, - "labels": [{"name": "bug"}], - }, - ] - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=mock_items) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": {"closedByPullRequestsReferences": {"nodes": []}} - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await get_issues( - mock_gitctx, milestone_number=1, sort="bug,feature" - ) - - # Assert: each list sorted independently (bug then feature) - assert [i["id"] for i in result["issues"]] == [1, 2] - assert [p["id"] for p in result["pull_requests"]] == [11, 10] - assert mock_gitctx.post.call_count == 2 - - class TestGetComments: """Test the get_comments endpoint.""" @@ -851,6 +199,7 @@ async def test_get_comments(self): ] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_comments) # Act @@ -886,6 +235,7 @@ async def test_get_milestones(self): ] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_milestones) # Act @@ -913,6 +263,7 @@ async def test_get_milestones_sorts_versions_numerically_before_other_milestones {"title": "v0.8.1", "number": 8}, ] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_milestones) result = await get_milestones(mock_gitctx) @@ -929,6 +280,7 @@ async def test_get_milestones_sorts_versions_numerically_before_other_milestones async def test_get_closed_milestones_omits_none_placeholder(self): mock_milestones = [{"title": "Completed", "number": 3}] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_milestones) with patch("github_pm.api.context") as mock_context: @@ -960,6 +312,7 @@ async def test_create_milestone_success(self): ) mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_milestone_response) with patch("github_pm.api.context") as mock_context: @@ -986,6 +339,7 @@ async def test_create_milestone_with_defaults(self): milestone_data = CreateMilestone(title="New Milestone") mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_milestone_response) with patch("github_pm.api.context") as mock_context: @@ -998,57 +352,11 @@ async def test_create_milestone_with_defaults(self): assert result == mock_milestone_response mock_gitctx.post.assert_called_once() call_args = mock_gitctx.post.call_args - assert call_args[0][0] == "/repos/test/repo/milestones" - assert call_args[1]["data"]["title"] == "New Milestone" - assert call_args[1]["data"]["state"] == "open" - assert call_args[1]["data"]["description"] is None - assert "due_on" not in call_args[1]["data"] - - -class TestMilestoneState: - """Test milestone state and open-work endpoints.""" - - @pytest.mark.asyncio - async def test_get_milestone_open_counts(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock( - return_value=[ - {"number": 1}, - {"number": 2, "pull_request": {"url": "https://example.test/pr/2"}}, - {"number": 3}, - ] - ) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - result = await get_milestone_open_counts(mock_gitctx, milestone_number=7) - - assert result == {"open_issues": 2, "open_pull_requests": 1} - mock_gitctx.get_paged.assert_called_once_with( - "/repos/test/repo/issues?milestone=7&state=open", - headers={"Accept": "application/vnd.github.full+json"}, - ) - - @pytest.mark.asyncio - async def test_update_milestone_state(self): - mock_response = {"number": 7, "state": "closed"} - mock_gitctx = Mock(spec=Connector) - mock_gitctx.patch = Mock(return_value=mock_response) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - result = await update_milestone_state( - mock_gitctx, - milestone_number=7, - milestone=UpdateMilestoneState(state="closed"), - ) - - assert result == mock_response - mock_gitctx.patch.assert_called_once_with( - "/repos/test/repo/milestones/7", data={"state": "closed"} - ) + assert call_args[0][0] == "/repos/test/repo/milestones" + assert call_args[1]["data"]["title"] == "New Milestone" + assert call_args[1]["data"]["state"] == "open" + assert call_args[1]["data"]["description"] is None + assert "due_on" not in call_args[1]["data"] class TestDeleteMilestone: @@ -1059,6 +367,7 @@ async def test_delete_milestone_success(self): """Test successfully deleting a milestone.""" # Arrange mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.delete = Mock(return_value={}) with patch("github_pm.api.context") as mock_context: @@ -1076,6 +385,7 @@ async def test_delete_milestone_not_found(self): """Test deleting a milestone that doesn't exist.""" # Arrange mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.delete = Mock(side_effect=Exception("Milestone not found")) with patch("github_pm.api.context") as mock_context: @@ -1096,6 +406,7 @@ async def test_add_milestone_to_issue(self): mock_issue_response = {"id": 123, "milestone": {"number": 1}} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_issue_response) with patch("github_pm.api.context") as mock_context: @@ -1123,6 +434,7 @@ async def test_remove_milestone_from_issue(self): mock_issue_response = {"id": 123, "milestone": None} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_issue_response) with patch("github_pm.api.context") as mock_context: @@ -1140,428 +452,6 @@ async def test_remove_milestone_from_issue(self): ) -class TestSetIssueParent: - """Test PUT /issues/{n}/parent.""" - - @pytest.mark.asyncio - async def test_set_parent_same_milestone(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 200, "number": 20, "milestone": {"number": 1}}, - {"id": 100, "number": 10, "milestone": {"number": 1}}, - ] - ) - mock_gitctx.get_paged = Mock(return_value=[]) - mock_gitctx.post = Mock(return_value={}) - mock_gitctx.patch = Mock() - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await set_issue_parent( - mock_gitctx, 20, SetIssueParent(parent_number=10) - ) - - assert result["parent_number"] == 10 - assert result["updated_issue_numbers"] == [] - mock_gitctx.post.assert_called_once_with( - "/repos/test/repo/issues/10/sub_issues", - data={"sub_issue_id": 200, "replace_parent": True}, - ) - mock_gitctx.patch.assert_not_called() - - @pytest.mark.asyncio - async def test_set_parent_cascades_milestone(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 200, "number": 20, "milestone": {"number": 1}}, - {"id": 100, "number": 10, "milestone": {"number": 2}}, - ] - ) - mock_gitctx.get_paged = Mock( - side_effect=[ - [], # cycle check: children of 20 - [{"number": 30}], # cascade: children of 20 - [], # cascade: children of 30 - ] - ) - mock_gitctx.post = Mock(return_value={}) - mock_gitctx.patch = Mock(return_value={}) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await set_issue_parent( - mock_gitctx, 20, SetIssueParent(parent_number=10) - ) - - assert result["from_milestone"] == 1 - assert result["to_milestone"] == 2 - assert result["updated_issue_numbers"] == [20, 30] - assert mock_gitctx.patch.call_count == 2 - - @pytest.mark.asyncio - async def test_set_parent_rejects_self(self): - mock_gitctx = Mock(spec=Connector) - with pytest.raises(HTTPException) as exc: - await set_issue_parent(mock_gitctx, 5, SetIssueParent(parent_number=5)) - assert exc.value.status_code == 422 - - @pytest.mark.asyncio - async def test_set_parent_rejects_descendant(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get_paged = Mock(return_value=[{"number": 10}]) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - with pytest.raises(HTTPException) as exc: - await set_issue_parent( - mock_gitctx, 20, SetIssueParent(parent_number=10) - ) - assert exc.value.status_code == 422 - - -class TestClearIssueParent: - """Test DELETE /issues/{n}/parent.""" - - @pytest.mark.asyncio - async def test_clear_parent(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value={"id": 200, "number": 20}) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": { - "number": 20, - "parent": {"number": 10, "title": "Epic"}, - "subIssues": {"nodes": []}, - } - } - } - } - ) - mock_gitctx.delete = Mock(return_value={}) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await clear_issue_parent(mock_gitctx, 20) - - assert result["parent_number"] == 10 - mock_gitctx.delete.assert_called_once_with( - "/repos/test/repo/issues/10/sub_issue", - data={"sub_issue_id": 200}, - ) - - @pytest.mark.asyncio - async def test_clear_parent_missing(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value={"id": 200, "number": 20}) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": { - "number": 20, - "parent": None, - "subIssues": {"nodes": []}, - } - } - } - } - ) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with pytest.raises(HTTPException) as exc: - await clear_issue_parent(mock_gitctx, 20) - assert exc.value.status_code == 404 - - -class TestIssueDependencies: - """Test blocked_by / blocking dependency endpoints.""" - - @pytest.mark.asyncio - async def test_add_blocked_by(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 1, "number": 1, "title": "Blocked"}, - { - "id": 17, - "number": 17, - "title": "Blocker", - "html_url": "https://github.com/test/repo/issues/17", - "state": "open", - }, - ] - ) - mock_gitctx.post = Mock( - return_value={ - "id": 17, - "number": 17, - "title": "Blocker", - "html_url": "https://github.com/test/repo/issues/17", - "state": "open", - } - ) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await add_blocked_by( - mock_gitctx, 1, AddDependency(issue_number=17) - ) - - assert result["relationship"] == "blocked_by" - assert result["linked_issue"]["number"] == 17 - assert result["linked_issue"]["state"] == "OPEN" - mock_gitctx.post.assert_called_once_with( - "/repos/test/repo/issues/1/dependencies/blocked_by", - data={"issue_id": 17}, - ) - - @pytest.mark.asyncio - async def test_add_blocked_by_idempotent_when_already_linked(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 1, "number": 1, "title": "Blocked"}, - { - "id": 17, - "number": 17, - "title": "Blocker", - "html_url": "https://github.com/test/repo/issues/17", - "state": "open", - }, - ] - ) - response = Mock() - response.status_code = 422 - response.json.return_value = { - "message": ( - "An error occurred while adding the blocking issue to the issue. " - "Validation failed: Target issue has already been taken" - ) - } - response.text = "Validation failed" - mock_gitctx.post = Mock(side_effect=requests.HTTPError(response=response)) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await add_blocked_by( - mock_gitctx, 1, AddDependency(issue_number=17) - ) - - assert result["linked_issue"]["number"] == 17 - assert result["relationship"] == "blocked_by" - - @pytest.mark.asyncio - async def test_add_blocked_by_rejects_self(self): - mock_gitctx = Mock(spec=Connector) - with pytest.raises(HTTPException) as exc: - await add_blocked_by(mock_gitctx, 5, AddDependency(issue_number=5)) - assert exc.value.status_code == 422 - - @pytest.mark.asyncio - async def test_add_blocked_by_rejects_pull_request(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 1, "number": 1, "title": "Issue"}, - {"id": 9, "number": 9, "title": "PR", "pull_request": {}}, - ] - ) - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - with pytest.raises(HTTPException) as exc: - await add_blocked_by(mock_gitctx, 1, AddDependency(issue_number=9)) - assert exc.value.status_code == 422 - - @pytest.mark.asyncio - async def test_remove_blocked_by(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - return_value={"id": 17, "number": 17, "title": "Blocker"} - ) - mock_gitctx.delete = Mock(return_value={}) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await remove_blocked_by(mock_gitctx, 1, 17) - - assert result["blocking_issue_number"] == 17 - mock_gitctx.delete.assert_called_once_with( - "/repos/test/repo/issues/1/dependencies/blocked_by/17" - ) - - @pytest.mark.asyncio - async def test_add_blocking(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 1, "number": 1, "title": "Blocker"}, - { - "id": 88, - "number": 88, - "title": "Waiting", - "html_url": "https://github.com/test/repo/issues/88", - "state": "open", - }, - ] - ) - mock_gitctx.post = Mock( - return_value={ - "id": 1, - "number": 1, - "title": "Blocker", - "html_url": "https://github.com/test/repo/issues/1", - "state": "open", - } - ) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await add_blocking(mock_gitctx, 1, AddDependency(issue_number=88)) - - assert result["relationship"] == "blocking" - assert result["linked_issue"]["number"] == 88 - assert result["linked_issue"]["title"] == "Waiting" - mock_gitctx.post.assert_called_once_with( - "/repos/test/repo/issues/88/dependencies/blocked_by", - data={"issue_id": 1}, - ) - - @pytest.mark.asyncio - async def test_remove_blocking(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value={"id": 1, "number": 1, "title": "Blocker"}) - mock_gitctx.delete = Mock(return_value={}) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await remove_blocking(mock_gitctx, 1, 88) - - assert result["blocked_issue_number"] == 88 - mock_gitctx.delete.assert_called_once_with( - "/repos/test/repo/issues/88/dependencies/blocked_by/1" - ) - - -class TestCloseReferences: - """Test manually linking a pull request as an issue's closing reference.""" - - @pytest.mark.asyncio - async def test_add_close_reference(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 1, "node_id": "I_issue", "number": 1, "title": "Bug"}, - { - "id": 17, - "node_id": "PR_pull_request", - "number": 17, - "title": "Fix bug", - "html_url": "https://github.com/test/repo/pull/17", - "pull_request": {}, - }, - ] - ) - mock_gitctx.post = Mock( - return_value={"data": {"addCloseIssueReferences": {"issue": {"number": 1}}}} - ) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await add_close_reference( - mock_gitctx, 1, AddCloseReference(pull_request_number=17) - ) - - assert result == { - "issue_number": 1, - "relationship": "closed_by", - "linked_pull_request": { - "number": 17, - "title": "Fix bug", - "url": "https://github.com/test/repo/pull/17", - }, - } - mock_gitctx.post.assert_called_once() - payload = mock_gitctx.post.call_args.kwargs["data"] - assert payload["variables"] == { - "issueId": "I_issue", - "pullRequestIds": ["PR_pull_request"], - } - assert "addCloseIssueReferences" in payload["query"] - - @pytest.mark.asyncio - async def test_add_close_reference_rejects_issue_target(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - side_effect=[ - {"id": 1, "node_id": "I_issue", "number": 1}, - {"id": 17, "node_id": "I_other", "number": 17}, - ] - ) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - with pytest.raises(HTTPException) as exc: - await add_close_reference( - mock_gitctx, 1, AddCloseReference(pull_request_number=17) - ) - - assert exc.value.status_code == 422 - mock_gitctx.post.assert_not_called() - - -class TestAdoptParentMilestone: - """Test POST adopt-parent-milestone.""" - - @pytest.mark.asyncio - async def test_adopt_cascades(self): - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock( - return_value={"id": 200, "number": 20, "milestone": {"number": 1}} - ) - mock_gitctx.post = Mock( - return_value={ - "data": { - "repository": { - "issue": { - "number": 20, - "parent": { - "number": 10, - "title": "Epic", - "milestone": {"number": 2, "title": "M2"}, - }, - "subIssues": {"nodes": [{"number": 30}]}, - } - } - } - } - ) - mock_gitctx.get_paged = Mock( - side_effect=[ - [{"number": 30}], - [], - ] - ) - mock_gitctx.patch = Mock(return_value={}) - mock_gitctx.owner = "test" - mock_gitctx.repo = "repo" - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - result = await adopt_parent_milestone(mock_gitctx, 20) - - assert result["from_milestone"] == 1 - assert result["to_milestone"] == 2 - assert result["updated_issue_numbers"] == [20, 30] - assert mock_gitctx.patch.call_count == 2 - - class TestGetLabels: """Test the get_labels endpoint.""" @@ -1575,6 +465,7 @@ async def test_get_labels(self): ] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_labels) # Act @@ -1601,6 +492,7 @@ async def test_create_label_success(self): ) mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_label_response) with patch("github_pm.api.context") as mock_context: @@ -1627,6 +519,7 @@ async def test_create_label_with_defaults(self): label_data = CreateLabel(name="new-label", color="green") mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_label_response) with patch("github_pm.api.context") as mock_context: @@ -1653,6 +546,7 @@ async def test_delete_label_success(self): """Test successfully deleting a label.""" # Arrange mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.delete = Mock(return_value={}) with patch("github_pm.api.context") as mock_context: @@ -1670,6 +564,7 @@ async def test_delete_label_not_found(self): """Test deleting a label that doesn't exist.""" # Arrange mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.delete = Mock(side_effect=Exception("Label not found")) with patch("github_pm.api.context") as mock_context: @@ -1696,6 +591,7 @@ async def test_update_label_success(self): name="updated-label", color="green", description="Updated label" ) mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_label_response) with patch("github_pm.api.context") as mock_context: @@ -1714,132 +610,6 @@ async def test_update_label_success(self): ) -class TestAddLabelToIssue: - """Test the add_label_to_issue endpoint.""" - - @pytest.mark.asyncio - async def test_add_label_to_issue(self): - """Test adding a label to an issue.""" - # Arrange - mock_issue_get = { - "id": 123, - "labels": [{"name": "existing"}], - } - mock_issue_patch = { - "id": 123, - "labels": [{"name": "existing"}, {"name": "bug"}], - } - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value=mock_issue_get) - mock_gitctx.patch = Mock(return_value=mock_issue_patch) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await add_label_to_issue( - mock_gitctx, issue_number=123, label_name="bug" - ) - - # Assert - assert result == mock_issue_patch - mock_gitctx.get.assert_called_once_with("/repos/test/repo/issues/123") - mock_gitctx.patch.assert_called_once() - # Check that patch was called with correct path and that labels contain both - call_args = mock_gitctx.patch.call_args - assert call_args[0][0] == "/repos/test/repo/issues/123" - assert set(call_args[1]["data"]["labels"]) == {"existing", "bug"} - - @pytest.mark.asyncio - async def test_add_label_to_issue_already_exists(self): - """Test adding a label that already exists on an issue.""" - # Arrange - mock_issue_get = { - "id": 123, - "labels": [{"name": "bug"}], - } - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value=mock_issue_get) - mock_gitctx.patch = Mock() - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await add_label_to_issue( - mock_gitctx, issue_number=123, label_name="bug" - ) - - # Assert - assert result == mock_issue_get - mock_gitctx.get.assert_called_once_with("/repos/test/repo/issues/123") - mock_gitctx.patch.assert_not_called() - - -class TestRemoveLabelFromIssue: - """Test the remove_label_from_issue endpoint.""" - - @pytest.mark.asyncio - async def test_remove_label_from_issue(self): - """Test removing a label from an issue.""" - # Arrange - mock_issue_get = { - "id": 123, - "labels": [{"name": "bug"}, {"name": "feature"}], - } - mock_issue_patch = { - "id": 123, - "labels": [{"name": "feature"}], - } - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value=mock_issue_get) - mock_gitctx.patch = Mock(return_value=mock_issue_patch) - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await remove_label_from_issue( - mock_gitctx, issue_number=123, label_name="bug" - ) - - # Assert - assert result == mock_issue_patch - mock_gitctx.get.assert_called_once_with("/repos/test/repo/issues/123") - mock_gitctx.patch.assert_called_once_with( - "/repos/test/repo/issues/123", data={"labels": ["feature"]} - ) - - @pytest.mark.asyncio - async def test_remove_label_from_issue_not_present(self): - """Test removing a label that doesn't exist on an issue.""" - # Arrange - mock_issue_get = { - "id": 123, - "labels": [{"name": "feature"}], - } - - mock_gitctx = Mock(spec=Connector) - mock_gitctx.get = Mock(return_value=mock_issue_get) - mock_gitctx.patch = Mock() - - with patch("github_pm.api.context") as mock_context: - mock_context.github_repo = "test/repo" - - # Act - result = await remove_label_from_issue( - mock_gitctx, issue_number=123, label_name="bug" - ) - - # Assert - assert result == mock_issue_get - mock_gitctx.get.assert_called_once_with("/repos/test/repo/issues/123") - mock_gitctx.patch.assert_not_called() - - class TestGetIssueReactions: """Test the get_issue_reactions endpoint.""" @@ -1853,6 +623,7 @@ async def test_get_issue_reactions(self): ] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_reactions) with patch("github_pm.api.context") as mock_context: @@ -1874,6 +645,7 @@ async def test_get_issue_reactions_empty(self): """Test getting reactions for an issue with no reactions.""" # Arrange mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=[]) with patch("github_pm.api.context") as mock_context: @@ -1901,6 +673,7 @@ async def test_get_comment_reactions(self): ] mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=mock_reactions) with patch("github_pm.api.context") as mock_context: @@ -1924,6 +697,7 @@ async def test_get_comment_reactions_empty(self): """Test getting reactions for a comment with no reactions.""" # Arrange mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.get_paged = Mock(return_value=[]) with patch("github_pm.api.context") as mock_context: @@ -1974,16 +748,20 @@ def test_get_retries_504_then_succeeds(self): """504 then 200 triggers one backoff sleep and succeeds.""" mock_session = Mock() r504 = Mock() + r504.headers = {} r504.status_code = 504 r200 = Mock() + r200.headers = {} r200.status_code = 200 r200.raise_for_status = Mock() r200.json.return_value = {"ok": True} mock_session.get.side_effect = [r504, r200] with ( - patch("github_pm.api.requests.session", return_value=mock_session), - patch("github_pm.api.time.sleep") as mock_sleep, + patch( + "github_pm.github_client.requests.session", return_value=mock_session + ), + patch("github_pm.github_client.time.sleep") as mock_sleep, patch("github_pm.api.context") as mock_context, ): mock_context.github_repo = "o/r" @@ -2000,6 +778,7 @@ def test_get_exhausts_retries_on_persistent_504(self): """After max attempts, persistent 504 propagates ``HTTPError``.""" mock_session = Mock() r504 = Mock() + r504.headers = {} r504.status_code = 504 err = requests.HTTPError() err.response = r504 @@ -2007,8 +786,10 @@ def test_get_exhausts_retries_on_persistent_504(self): mock_session.get.return_value = r504 with ( - patch("github_pm.api.requests.session", return_value=mock_session), - patch("github_pm.api.time.sleep"), + patch( + "github_pm.github_client.requests.session", return_value=mock_session + ), + patch("github_pm.github_client.time.sleep"), patch("github_pm.api.context") as mock_context, ): mock_context.github_repo = "o/r" @@ -2020,56 +801,6 @@ def test_get_exhausts_retries_on_persistent_504(self): assert mock_session.get.call_count == 5 -class TestSearchIssueItems: - """Tests for ``Connector.search_issue_items`` (GitHub search pagination).""" - - def test_single_page(self): - mock_session = Mock() - resp = Mock() - resp.raise_for_status = Mock() - resp.json.return_value = {"items": [{"number": 7}], "total_count": 1} - resp.headers.get.return_value = None - mock_session.get.return_value = resp - - with ( - patch("github_pm.api.requests.session", return_value=mock_session), - patch("github_pm.api.context") as mock_context, - ): - mock_context.github_repo = "o/r" - mock_context.github_token = "tok" - conn = Connector("tok", github_repo="o/r") - items = conn.search_issue_items("repo:o/r+is:issue") - - assert items == [{"number": 7}] - mock_session.get.assert_called_once() - - def test_follows_next_link(self): - mock_session = Mock() - first = Mock() - first.raise_for_status = Mock() - first.json.return_value = {"items": [{"number": 1}], "total_count": 2} - first.headers.get.return_value = ( - '; rel="next"' - ) - second = Mock() - second.raise_for_status = Mock() - second.json.return_value = {"items": [{"number": 2}], "total_count": 2} - second.headers.get.return_value = None - mock_session.get.side_effect = [first, second] - - with ( - patch("github_pm.api.requests.session", return_value=mock_session), - patch("github_pm.api.context") as mock_context, - ): - mock_context.github_repo = "o/r" - mock_context.github_token = "tok" - conn = Connector("tok", github_repo="o/r") - items = conn.search_issue_items("repo:o/r+is:issue") - - assert items == [{"number": 1}, {"number": 2}] - assert mock_session.get.call_count == 2 - - class TestCreateComment: """Test the create_comment endpoint. @@ -2084,6 +815,7 @@ async def test_create_comment_success(self): "body_html": "

Hello

", } mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_comment) with patch("github_pm.api.context") as mock_context: @@ -2112,6 +844,7 @@ async def test_update_comment_success(self): "body_html": "

Updated

", } mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_comment) with patch("github_pm.api.context") as mock_context: @@ -2142,6 +875,7 @@ async def test_update_issue_body_success(self): "body_html": "

New description

", } mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_issue) with patch("github_pm.api.context") as mock_context: @@ -2161,6 +895,7 @@ async def test_update_issue_body_success(self): async def test_update_issue_body_allows_empty(self): mock_issue = {"number": 42, "body": "", "body_html": ""} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_issue) with patch("github_pm.api.context") as mock_context: @@ -2188,6 +923,7 @@ async def test_update_issue_title_success(self): "title": "Updated title", } mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_issue) with patch("github_pm.api.context") as mock_context: @@ -2217,6 +953,7 @@ async def test_update_issue_type_success(self): "type": {"name": "Task", "color": "green"}, } mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.patch = Mock(return_value=mock_issue) with patch("github_pm.api.context") as mock_context: @@ -2244,6 +981,7 @@ async def test_close_with_comment_success(self): mock_comment = {"id": 1, "body": "Done"} mock_issue = {"number": 42, "state": "closed"} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_comment) mock_gitctx.patch = Mock(return_value=mock_issue) @@ -2270,6 +1008,7 @@ async def test_close_with_obsolete_default_comment(self): mock_comment = {"id": 1, "body": "Obsolete"} mock_issue = {"number": 42, "state": "closed"} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_comment) mock_gitctx.patch = Mock(return_value=mock_issue) @@ -2303,6 +1042,7 @@ class TestRenderMarkdown: @pytest.mark.asyncio async def test_render_markdown_success(self): mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post_text = Mock(return_value="

hi

") result = await render_markdown(mock_gitctx, RenderMarkdown(text="**hi**")) @@ -2313,6 +1053,7 @@ async def test_render_markdown_success(self): @pytest.mark.asyncio async def test_render_markdown_empty(self): mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post_text = Mock(return_value="") result = await render_markdown(mock_gitctx, RenderMarkdown(text="")) @@ -2331,6 +1072,7 @@ class TestCreateIssue: async def test_create_issue_basic(self): mock_created = {"id": 100, "number": 50, "title": "New"} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_created) with patch("github_pm.api.context") as mock_context: @@ -2352,6 +1094,7 @@ async def test_create_issue_basic(self): async def test_create_issue_with_milestone_labels_assignees(self): mock_created = {"id": 100, "number": 50, "title": "Epic"} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_created) with patch("github_pm.api.context") as mock_context: @@ -2378,6 +1121,7 @@ async def test_create_issue_with_milestone_labels_assignees(self): async def test_create_issue_skips_milestone_zero(self): mock_created = {"id": 100, "number": 50, "title": "No MS"} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_created) with patch("github_pm.api.context") as mock_context: @@ -2390,6 +1134,7 @@ async def test_create_issue_skips_milestone_zero(self): async def test_create_issue_with_parent(self): mock_created = {"id": 100, "number": 50, "title": "Child"} mock_gitctx = Mock(spec=Connector) + mock_gitctx.owner, mock_gitctx.repo = "test", "repo" mock_gitctx.post = Mock(return_value=mock_created) with patch("github_pm.api.context") as mock_context: @@ -2419,12 +1164,15 @@ def test_post_text_returns_response_text(self): mock_session = Mock() mock_response = Mock() mock_response.status_code = 200 + mock_response.headers = {} mock_response.raise_for_status = Mock() mock_response.text = "

ok

" mock_session.post.return_value = mock_response with ( - patch("github_pm.api.requests.session", return_value=mock_session), + patch( + "github_pm.github_client.requests.session", return_value=mock_session + ), patch("github_pm.api.context") as mock_context, ): mock_context.github_repo = "o/r" diff --git a/backend/tests/test_github_client.py b/backend/tests/test_github_client.py new file mode 100644 index 0000000..c907607 --- /dev/null +++ b/backend/tests/test_github_client.py @@ -0,0 +1,195 @@ +"""Transport lifecycle, safe retries, scoped validators and responsiveness. + +Generated-by: Codex +""" + +import asyncio +import json +from threading import Event +from unittest.mock import Mock + +from fastapi import FastAPI +import httpx +import pytest +import requests + +from github_pm import github_client as gc +from github_pm.execution import offload + + +def response(status=200, data=None, headers=None): + result = requests.Response() + result.status_code = status + result._content = json.dumps(data if data is not None else []).encode() + result.headers.update(headers or {}) + result.url = "https://api.github.com/test" + return result + + +@pytest.fixture +def session(monkeypatch): + gc._cache.clear() + gc._gates.clear() + session = Mock() + monkeypatch.setattr(gc.requests, "session", lambda: session) + return session + + +def test_sessions_close_on_error_and_requests_have_timeouts(session): + session.get.side_effect = requests.Timeout("timeout") + with pytest.raises(requests.Timeout): + with gc.Connector("token", github_repo="a/b") as client: + client.get("/test") + session.close.assert_called_once() + assert session.get.call_args.kwargs["timeout"] == (5, 45) + assert session.get.call_count == 1 + + +def test_read_retry_but_no_ambiguous_write_replay(session, monkeypatch): + sleep = Mock() + monkeypatch.setattr(gc.time, "sleep", sleep) + session.get.side_effect = [response(504), response(data={"ok": True})] + with gc.Connector("token", github_repo="a/b") as client: + assert client.get("/test") == {"ok": True} + assert client.metrics["retries"] == 1 + assert client.metrics["calls"] == 2 + assert client.metrics["bytes"] > 0 + session.post.return_value = response(504) + with pytest.raises(requests.HTTPError): + client.post("/repos/a/b/issues/1/comments", {"body": "once"}) + assert session.post.call_count == 1 + session.post.side_effect = requests.Timeout("ambiguous") + with pytest.raises(requests.Timeout): + client.post("/repos/a/b/issues", {"title": "once"}) + assert session.post.call_count == 2 + + +def test_conditional_cache_retains_paginated_representation_and_invalidates_on_write( + session, +): + link = '; rel="next"' + session.get.side_effect = [ + response(data=[{"name": "a"}], headers={"ETag": "one", "Link": link}), + response(data=[{"name": "b"}], headers={"ETag": "two"}), + response(304), + response(304), + response(data=[]), + ] + session.post.return_value = response(data={"name": "c"}) + with gc.Connector("token", github_repo="a/b") as client: + assert client.get_paged("/repos/a/b/labels") == [{"name": "a"}, {"name": "b"}] + assert client.get_paged("/repos/a/b/labels") == [{"name": "a"}, {"name": "b"}] + assert session.get.call_args_list[2].kwargs["headers"] == { + "If-None-Match": "one" + } + assert session.get.call_args_list[3].kwargs["headers"] == { + "If-None-Match": "two" + } + assert client.metrics["not_modified"] == 2 + client.post("/repos/a/b/labels", {"name": "c"}) + client.get_paged("/repos/a/b/labels") + assert session.get.call_args.kwargs["headers"] is None + assert session.get.call_args_list[0].args[0].endswith("?per_page=100") + + +def test_cache_is_scoped_by_repository_and_authorization(session): + session.get.return_value = response(data=[], headers={"ETag": "private"}) + for token, repo in [("a", "x/y"), ("b", "x/y"), ("a", "x/z")]: + with gc.Connector(token, github_repo=repo) as client: + client.get("/repos/x/y/labels") + assert session.get.call_args.kwargs["headers"] is None + with gc.Connector("a", github_repo="x/y") as client: + client.get("/repos/x/y/labels") + assert session.get.call_args.kwargs["headers"]["If-None-Match"] == "private" + + +def test_get_started_before_write_cannot_repopulate_cache(session): + with gc.Connector("token", github_repo="a/b") as client: + + def read(*args, **kwargs): + # Simulate a concurrent write after HTTP completion, before cache storage. + client._invalidate() + return response(data=["old"], headers={"ETag": "old"}) + + session.get.side_effect = read + client.get("/repos/a/b/labels") + assert not gc._cache + + +def test_rate_limits_share_pause_and_report_headers(session, monkeypatch): + wall = [1000.0] + sleeps = [] + monkeypatch.setattr(gc.time, "time", lambda: wall[0]) + + def sleep(seconds): + sleeps.append(seconds) + wall[0] += seconds + + monkeypatch.setattr(gc.time, "sleep", sleep) + session.get.side_effect = [ + response(429, headers={"Retry-After": "2"}), + response(data=[], headers={"X-RateLimit-Remaining": "4999"}), + ] + with gc.Connector("token", github_repo="a/b") as client: + client.get("/test") + assert sleeps == [2] + assert client.metrics["retries"] == 1 + assert client.metrics["rate_remaining"] == "4999" + assert client.gate is gc.Connector("token", github_repo="a/c").gate + + +def test_graphql_read_rate_limit_retries_and_records_cost(session, monkeypatch): + monkeypatch.setattr(gc.time, "sleep", Mock()) + monkeypatch.setattr(gc.time, "time", lambda: 1000.0) + session.post.side_effect = [ + response( + data={"errors": [{"type": "RATE_LIMITED"}]}, headers={"Retry-After": "0"} + ), + response(data={"data": {"rateLimit": {"cost": 7}}}), + ] + with gc.Connector("token", github_repo="a/b") as client: + client.post("/graphql", {"query": "query { rateLimit { cost } }"}) + assert client.metrics["graphql_cost"] == 7 + assert client.metrics["retries"] == 1 + + +def test_operation_deadline_prevents_more_network_calls(session, monkeypatch): + with gc.Connector("token", github_repo="a/b") as client: + client.deadline = 0 + with pytest.raises(TimeoutError, match="deadline"): + client.get("/test") + session.get.assert_not_called() + + +@pytest.mark.asyncio +async def test_blocking_route_does_not_block_another_request(): + entered, release = Event(), Event() + app = FastAPI() + + @app.get("/slow") + @offload + def slow(): + entered.set() + assert release.wait(5) + return {"done": True} + + @app.get("/fast") + async def fast(): + return {"ready": True} + + async with httpx.AsyncClient( + transport=httpx.ASGITransport(app), base_url="http://test" + ) as client: + pending = asyncio.create_task(client.get("/slow")) + try: + for _ in range(200): + if entered.is_set(): + break + await asyncio.sleep(0.005) + assert entered.is_set() + result = await asyncio.wait_for(client.get("/fast"), timeout=1) + assert result.json() == {"ready": True} + assert not pending.done() + finally: + release.set() + await pending diff --git a/backend/tests/test_planning_optimization.py b/backend/tests/test_planning_optimization.py new file mode 100644 index 0000000..3693684 --- /dev/null +++ b/backend/tests/test_planning_optimization.py @@ -0,0 +1,406 @@ +"""Planning equivalence, batching, mutation validation, and pagination regressions. + +Generated-by: Codex +""" + +from copy import deepcopy +from unittest.mock import Mock + +from fastapi import HTTPException +import pytest +import requests + +from github_pm import api +from github_pm.planning_reader import comment_reactions, descendants, enrich_issues + + +def page(nodes=(), more=False, cursor=None): + return { + "nodes": list(nodes), + "pageInfo": {"hasNextPage": more, "endCursor": cursor}, + } + + +def issue(number, **overrides): + return { + "id": f"I{number}", + "databaseId": number * 10, + "number": number, + "__typename": "Issue", + "title": f"Issue {number}", + "url": f"https://github.com/test/repo/issues/{number}", + "state": "OPEN", + "milestone": {"number": 1, "title": "M1"}, + "parent": None, + "blockedBy": page(), + "blocking": page(), + "closedByPullRequestsReferences": page(), + "subIssues": page(), + **overrides, + } + + +def connector(nodes=None): + nodes = nodes or {} + git = Mock(spec=api.Connector) + git.owner, git.repo = "test", "repo" + + def post(path, data=None): + if path != "/graphql" or data["query"].lstrip().startswith("mutation"): + return {} + return { + "data": { + "repository": { + f"i{k[1:]}": deepcopy(nodes.get(n, issue(n))) + for k, n in data["variables"].items() + if k.startswith("n") + } + } + } + + git.post.side_effect = post + git.patch.return_value = {} + git.delete.return_value = {} + return git + + +@pytest.fixture(autouse=True) +def repository(monkeypatch): + monkeypatch.setattr(api.context, "github_repo", "test/repo") + + +@pytest.mark.parametrize( + "priorities,expected", + [ + ([], [1, 2, 3, 4]), + (["bug"], [2, 4, 1, 3]), + (["feature", "bug"], [1, 4, 2, 3]), + ([" bug ", "feature"], [2, 4, 1, 3]), + ], +) +async def test_board_preserves_label_sort_and_flat_prs(priorities, expected): + git = connector() + git.get_paged.return_value = [ + dict(number=n, id=n, labels=[{"name": name} for name in labels]) + for n, labels in [ + (4, ["BUG", "feature"]), + (3, []), + (2, ["bug"]), + (1, ["feature"]), + ] + ] + [dict(number=8, id=8, labels=[], pull_request={})] + result = await api.get_issues(git, 0, ",".join(priorities)) + assert [i["number"] for i in result["issues"]] == expected + assert result["pull_requests"][0]["number"] == 8 + assert git.post.call_count == 1 + assert "milestone=none" in git.get_paged.call_args.args[0] + + +async def test_board_batches_100_issues_and_keeps_relationships(): + nodes = { + 2: issue(2, parent={"number": 1, "title": "Root", "milestone": {"number": 1}}), + 3: issue( + 3, parent={"number": 999, "title": "External", "milestone": {"number": 2}} + ), + } + nodes[1] = issue( + 1, + blockedBy=page( + [ + { + "databaseId": 90, + "number": 9, + "title": "Blocker", + "url": "u", + "state": "OPEN", + } + ] + ), + closedByPullRequestsReferences=page( + [{"number": 88, "title": "PR", "url": "p"}] + ), + ) + git = connector(nodes) + git.get_paged.return_value = [ + dict(number=n, id=n, labels=[]) for n in range(1, 101) + ] + result = await api.get_issues(git, 1) + assert git.get_paged.call_count == 1 + assert git.post.call_count == 2 + assert result["issues"][0]["children"][0]["number"] == 2 + assert result["issues"][0]["blocked_by"][0]["id"] == 90 + assert result["issues"][0]["closed_by"][0]["number"] == 88 + assert result["issues"][1]["external_parent"]["number"] == 999 + assert "subIssues(" not in git.post.call_args.args[1]["query"] + + +def test_nested_links_finish_every_page(): + git = connector({1: issue(1, blocking=page([{"number": 2}], True, "c"))}) + first = git.post.side_effect + + def post(path, data): + if "node(id:" in data["query"]: + assert data["variables"]["c0"] == "c" + return {"data": {"n0": {"blocking": page([{"number": 3}])}}} + return first(path, data) + + git.post.side_effect = post + nodes = enrich_issues(git, [{"number": 1}]) + assert [n["number"] for n in nodes[1]["blocking"]["nodes"]] == [2, 3] + assert git.post.call_count == 2 + + +def test_incomplete_enrichment_is_not_silent_issue_loss(): + git = connector() + git.post.return_value = {} + git.post.side_effect = lambda *args: { + "data": {"repository": {"i0": None}}, + "errors": [{"message": "denied"}], + } + with pytest.raises(RuntimeError, match="denied"): + enrich_issues(git, [{"number": 1}]) + + +async def test_milestone_counts_do_not_download_items(): + git = connector() + git.post.side_effect = lambda *args: { + "data": { + "repository": { + "milestone": { + "issues": {"totalCount": 7}, + "pullRequests": {"totalCount": 3}, + } + } + } + } + assert await api.get_milestone_open_counts(git, 5) == { + "open_issues": 7, + "open_pull_requests": 3, + } + git.get_paged.assert_not_called() + assert "totalCount" in git.post.call_args.args[1]["query"] + await api.update_milestone_state(git, 5, api.UpdateMilestoneState(state="closed")) + git.patch.assert_called_once_with( + "/repos/test/repo/milestones/5", data={"state": "closed"} + ) + + +@pytest.mark.parametrize("same", [False, True]) +async def test_reparent_reuses_traversal_and_cascades_closed_descendants(same): + git = connector( + { + 20: issue(20, subIssues=page([{"number": 30}])), + 30: issue(30, state="CLOSED"), + 10: issue(10, milestone={"number": 1 if same else 2}), + } + ) + result = await api.set_issue_parent(git, 20, api.SetIssueParent(parent_number=10)) + assert result["updated_issue_numbers"] == ([] if same else [20, 30]) + queries = [c for c in git.post.call_args_list if c.args[0] == "/graphql"] + assert len(queries) == 3 # root frontier, child frontier, paired identities + assert git.patch.call_count == (0 if same else 2) + git.post.assert_any_call( + "/repos/test/repo/issues/10/sub_issues", + data={"sub_issue_id": 200, "replace_parent": True}, + ) + + +@pytest.mark.parametrize("parent", [20, 30]) +async def test_reparent_rejects_self_and_descendant(parent): + git = connector({20: issue(20, subIssues=page([{"number": 30}]))}) + with pytest.raises(HTTPException) as exc: + await api.set_issue_parent(git, 20, api.SetIssueParent(parent_number=parent)) + assert exc.value.status_code == 422 + git.patch.assert_not_called() + + +def test_failed_subtree_read_aborts_instead_of_becoming_a_leaf(): + git = connector() + git.post.side_effect = requests.ConnectionError("unavailable") + with pytest.raises(requests.ConnectionError): + descendants(git, 20) + + +async def test_clear_and_adopt_parent_use_one_identity_read(): + git = connector( + { + 20: issue( + 20, + parent={"number": 10, "title": "Root", "milestone": {"number": 2}}, + subIssues=page([{"number": 30}]), + ) + } + ) + await api.clear_issue_parent(git, 20) + git.delete.assert_called_once_with( + "/repos/test/repo/issues/10/sub_issue", data={"sub_issue_id": 200} + ) + assert git.post.call_count == 1 + git.post.reset_mock() + result = await api.adopt_parent_milestone(git, 20) + assert result["updated_issue_numbers"] == [20, 30] + assert (result["from_milestone"], result["to_milestone"]) == (1, 2) + git.get.assert_not_called() + + +async def test_missing_parent_is_404(): + with pytest.raises(HTTPException) as exc: + await api.clear_issue_parent(connector(), 20) + assert exc.value.status_code == 404 + + +@pytest.mark.parametrize("relationship", ["blocked_by", "blocking"]) +async def test_dependency_pair_validated_in_one_query(relationship): + git = connector() + function = api.add_blocked_by if relationship == "blocked_by" else api.add_blocking + result = await function(git, 1, api.AddDependency(issue_number=17)) + assert result["linked_issue"]["number"] == 17 + assert result["relationship"] == relationship + assert git.post.call_count == 2 + git.get.assert_not_called() + git = connector({17: issue(17, __typename="PullRequest")}) + with pytest.raises(HTTPException) as exc: + await function(git, 1, api.AddDependency(issue_number=17)) + assert exc.value.status_code == 422 + + +async def test_duplicate_dependency_is_success(): + git = connector() + read = git.post.side_effect + response = Mock(status_code=422) + response.json.return_value = {"message": "already exists"} + + def post(path, data=None): + if path != "/graphql": + raise requests.HTTPError(response=response) + return read(path, data) + + git.post.side_effect = post + assert (await api.add_blocked_by(git, 1, api.AddDependency(issue_number=17)))[ + "linked_issue" + ]["number"] == 17 + + +async def test_close_reference_validates_pair_and_keeps_link_fields(): + git = connector({17: issue(17, __typename="PullRequest", id="PR17")}) + result = await api.add_close_reference( + git, 1, api.AddCloseReference(pull_request_number=17) + ) + assert result["linked_pull_request"]["number"] == 17 + assert git.post.call_count == 2 + assert git.post.call_args.kwargs["data"]["variables"] == { + "issueId": "I1", + "pullRequestIds": ["PR17"], + } + with pytest.raises(HTTPException): + await api.add_close_reference( + connector(), 1, api.AddCloseReference(pull_request_number=17) + ) + + +async def test_label_changes_are_single_scoped_writes(): + git = connector() + git.post.side_effect = None + git.post.return_value = [{"name": "bug"}, {"name": "other"}] + assert (await api.add_label_to_issue(git, 1, "bug"))[ + "labels" + ] == git.post.return_value + git.post.assert_called_once_with( + "/repos/test/repo/issues/1/labels", data={"labels": ["bug"]} + ) + git.delete.return_value = [{"name": "other"}] + assert (await api.remove_label_from_issue(git, 1, "type/bug"))["labels"] == [ + {"name": "other"} + ] + git.delete.assert_called_once_with("/repos/test/repo/issues/1/labels/type%2Fbug") + git.get.assert_not_called() + + +async def test_missing_label_is_noop_but_missing_issue_is_error(): + git = connector() + response = Mock(status_code=404) + git.delete.side_effect = requests.HTTPError(response=response) + git.get.return_value = {"labels": []} + assert await api.remove_label_from_issue(git, 1, "bug") == {"labels": []} + git.get.side_effect = requests.HTTPError(response=response) + with pytest.raises(requests.HTTPError): + await api.remove_label_from_issue(git, 1, "bug") + + +def test_comments_batch_reactor_names_and_paginate(): + git = connector() + git.post.side_effect = [ + { + "data": { + "nodes": [ + { + "id": "C1", + "reactions": page( + [{"content": "THUMBS_UP", "user": {"login": "a"}}], + True, + "next", + ), + } + ] + } + }, + {"data": {"n0": {"reactions": page([{"content": "HEART", "user": None}])}}}, + ] + result = comment_reactions( + git, + [ + { + "node_id": "C1", + "body": "raw", + "body_html": "html", + "reactions": {"total_count": 2}, + } + ], + ) + assert result[0]["reaction_details"] == [ + {"content": "+1", "user": {"login": "a"}}, + {"content": "heart", "user": None}, + ] + assert result[0]["body"] == "raw" + assert git.post.call_count == 2 + + +@pytest.mark.asyncio +async def test_partial_cascade_reports_completed_writes(): + gitctx = connector( + { + 1: issue(1, subIssues=page([{"number": 2}])), + 2: issue(2), + 3: issue(3, milestone={"number": 9}), + } + ) + gitctx.patch.side_effect = [{}, requests.Timeout("ambiguous")] + with pytest.raises(HTTPException) as caught: + await api.set_issue_parent(gitctx, 1, api.SetIssueParent(parent_number=3)) + assert caught.value.status_code == 502 + assert caught.value.detail["updated_issue_numbers"] == [1] + assert caught.value.detail["failed_issue_number"] == 2 + assert caught.value.detail["refresh_required"] is True + assert gitctx.patch.call_count == 2 + + +@pytest.mark.parametrize("relationship", ["blocked_by", "blocking"]) +async def test_dependency_self_link_and_directional_removal(relationship): + git = connector() + add = api.add_blocked_by if relationship == "blocked_by" else api.add_blocking + with pytest.raises(HTTPException) as error: + await add(git, 1, api.AddDependency(issue_number=1)) + assert error.value.status_code == 422 + git.post.assert_not_called() + git.get.return_value = {"id": 170 if relationship == "blocked_by" else 10} + remove = ( + api.remove_blocked_by if relationship == "blocked_by" else api.remove_blocking + ) + result = await remove(git, 1, 17) + expected = ( + "/repos/test/repo/issues/1/dependencies/blocked_by/170" + if relationship == "blocked_by" + else "/repos/test/repo/issues/17/dependencies/blocked_by/10" + ) + git.delete.assert_called_once_with(expected) + assert result["relationship"] == relationship diff --git a/backend/tests/test_report_queries.py b/backend/tests/test_report_queries.py new file mode 100644 index 0000000..82066f1 --- /dev/null +++ b/backend/tests/test_report_queries.py @@ -0,0 +1,149 @@ +"""Complete, bounded collection across independent roots and nested connections. + +Generated-by: Codex +""" + +from unittest.mock import Mock + +import pytest + +from github_pm import report_queries as rq + + +def page(nodes, cursor=None, count=None): + return { + "nodes": nodes, + "pageInfo": {"hasNextPage": bool(cursor), "endCursor": cursor}, + "issueCount": count if count is not None else len(nodes), + } + + +def node(number): + return {"id": f"P{number}", "number": number, "__typename": "PullRequest"} + + +def test_independent_root_cursors_and_stable_deduplication(): + post = Mock( + side_effect=[ + {"data": {"s0": page([node(1)], "a"), "s1": page([node(2)])}}, + {"data": {"s0": page([node(1), node(3)])}}, + ] + ) + result = rq.search_many( + post, {"a": rq.Search("a", "id number"), "b": rq.Search("b", "id number")} + ) + assert [n["number"] for n in result["a"]] == [1, 3] + assert [n["number"] for n in result["b"]] == [2] + assert post.call_args.args[0]["variables"] == {"q0": "a", "c0": "a"} + + +def test_oversized_search_partitions_dates_before_collecting(): + post = Mock( + side_effect=[ + {"data": {"s0": page([node(9999)], count=1501)}}, + {"data": {"s0": page([node(1)]), "s1": page([node(2)])}}, + ] + ) + result = rq.search_many( + post, + {"a": rq.Search("repo:a/b is:pr merged:2025-04-01..2025-04-02", "id number")}, + ) + assert [n["number"] for n in result["a"]] == [1, 2] + variables = post.call_args.args[0]["variables"] + assert "merged:2025-04-01T00:00:00Z..2025-04-01T23:59:59Z" in variables["q0"] + assert "merged:2025-04-02T00:00:00Z..2025-04-02T23:59:59Z" in variables["q1"] + + +def test_single_second_overflow_is_explicit(): + post = Mock(return_value={"data": {"s0": page([], count=1001)}}) + with pytest.raises(RuntimeError, match="single second"): + rq.search_many( + post, + { + "a": rq.Search( + "created:2025-01-01T00:00:00Z..2025-01-01T00:00:00Z", "id number" + ) + }, + ) + + +def test_nested_reviews_and_labels_use_independent_cursors(): + nodes = [ + dict( + node(1), + reviews=page( + [{"state": "COMMENTED", "author": {"__typename": "Bot"}}], "r1" + ), + labels=page([], "l1"), + ), + dict(node(2), reviews=page([], "r2"), labels=page([])), + ] + post = Mock( + return_value={ + "data": { + "n0": {"labels": page([{"name": "bug"}])}, + "n1": { + "reviews": page( + [{"state": "APPROVED", "author": {"login": "alice"}}] + ) + }, + "n2": { + "reviews": page([{"state": "APPROVED", "author": {"login": "bob"}}]) + }, + } + } + ) + rq.complete_prs(post, nodes, labels=True, reviews=True) + assert nodes[0]["labels"]["nodes"] == [{"name": "bug"}] + assert len(nodes[0]["reviews"]["nodes"]) == 2 + assert nodes[1]["reviews"]["nodes"][0]["author"]["login"] == "bob" + assert post.call_args.args[0]["variables"] == { + "id0": "P1", + "c0": "l1", + "id1": "P1", + "c1": "r1", + "id2": "P2", + "c2": "r2", + } + + +def test_repeated_nested_cursor_fails_instead_of_looping(): + nodes = [dict(node(1), reviews=page([], "same"))] + post = Mock(return_value={"data": {"n0": {"reviews": page([], "same")}}}) + with pytest.raises(RuntimeError, match="failed to advance"): + rq.complete_prs(post, nodes, reviews=True) + assert post.call_count == 1 + + +def test_open_repository_connection_has_no_search_cap_and_completes_reviews(): + nodes = [dict(node(i), reviews=page([])) for i in range(1001)] + post = Mock( + side_effect=[ + {"data": {"repository": {"pullRequests": page(nodes[:1000], "more")}}}, + {"data": {"repository": {"pullRequests": page(nodes[1000:])}}}, + ] + ) + assert len(rq.open_prs(post, "a", "b")) == 1001 + assert post.call_args.args[0]["variables"]["cursor"] == "more" + assert "search(" not in post.call_args.args[0]["query"] + + +@pytest.mark.parametrize( + "result", [{"errors": [{"message": "denied"}], "data": {}}, {"data": None}] +) +def test_partial_graphql_data_is_not_a_successful_empty_report(result): + with pytest.raises(RuntimeError): + rq.execute(lambda _: result, "query { viewer { login } }") + + +def test_rest_search_paginates_and_rejects_incomplete_results(): + get = Mock( + side_effect=[ + {"items": [{"id": i} for i in range(100)], "total_count": 101}, + {"items": [{"id": 100}], "total_count": 101}, + ] + ) + assert len(rq.rest_search(get, "repo:a/b")) == 101 + assert "per_page=100&page=2" in get.call_args.args[0] + with pytest.raises(RuntimeError, match="incomplete"): + rq.rest_search(lambda _: {"incomplete_results": True}, "repo:a/b") diff --git a/backend/tests/test_sdlc_api.py b/backend/tests/test_sdlc_api.py index aed4a46..f68cc30 100644 --- a/backend/tests/test_sdlc_api.py +++ b/backend/tests/test_sdlc_api.py @@ -1,9 +1,11 @@ -"""Tests for SDLC KPI API routes (mocked GitHub). +"""SDLC contracts, shared facts, interval boundaries and collection costs. -Generated-by: Cursor +Generated-by: Codex """ +from copy import deepcopy from datetime import datetime, UTC +from types import SimpleNamespace from unittest.mock import MagicMock, patch from fastapi.testclient import TestClient @@ -11,259 +13,235 @@ from github_pm.api import connection from github_pm.app import app +from github_pm.sdlc_service import compute_escaped_defect_rate +NOW = datetime(2025, 4, 10, 12, tzinfo=UTC) +SETTINGS = SimpleNamespace( + sdlc_feature_labels="enhancement", + sdlc_bug_labels="bug", + sdlc_docs_labels="documentation", + sdlc_escape_label="escape", +) -def _graphql_page(nodes: list, has_next: bool = False, cursor: str | None = "c1"): + +def page(nodes): return { - "data": { - "search": { - "pageInfo": { - "hasNextPage": has_next, - "endCursor": cursor if has_next else None, - }, - "nodes": nodes, - } - } + "nodes": deepcopy(nodes), + "issueCount": len(nodes), + "pageInfo": {"hasNextPage": False, "endCursor": None}, } -@pytest.fixture -def client(): - return TestClient(app) - - -@pytest.fixture -def mock_connector(): - gitctx = MagicMock() - gitctx.owner = "test" - gitctx.repo = "repo" - gitctx.base_url = "https://api.github.com" - - _human = {"__typename": "User", "login": "contributor"} - merged_node = { - "number": 10, +def pr(number, label, milestone="v0.1.0", **kwargs): + return { + "id": f"P{number}", + "number": number, + "__typename": "PullRequest", "createdAt": "2025-04-05T10:00:00Z", "mergedAt": "2025-04-06T10:00:00Z", "additions": 5, "deletions": 5, - "labels": {"nodes": [{"name": "enhancement"}]}, - "milestone": None, - "author": _human, - } - opened_node = { - "number": 11, - "createdAt": "2025-04-05T12:00:00Z", - "mergedAt": None, - "additions": 2, - "deletions": 2, - "labels": {"nodes": [{"name": "bug"}]}, - "milestone": None, - "author": _human, + "labels": page([{"name": label}]), + "milestone": {"title": milestone}, + "author": {"__typename": "User", "login": "contributor"}, + **kwargs, } - def post_side(path: str, payload: dict): - q = payload["variables"]["q"] - if "is:merged" in q: - return _graphql_page([merged_node]) - return _graphql_page([opened_node]) - - gitctx.post.side_effect = post_side - - def get_side(path: str, headers=None): - if "/pulls/11/reviews" in path: - return [ - { - "user": {"login": "rev", "type": "User"}, - "submitted_at": "2025-04-05T14:00:00Z", - } - ] - if "/search/issues" in path: - return {"total_count": 3} - raise AssertionError(f"unexpected GET {path}") - - gitctx.get.side_effect = get_side - return gitctx - - -class TestSdlcDelivery: - def test_delivery_ok(self, client, mock_connector): - fixed = datetime(2025, 4, 10, 12, 0, 0, tzinfo=UTC) - - async def override_conn(): - yield mock_connector - - with ( - patch("github_pm.sdlc_metrics.utc_now", return_value=fixed), - patch("github_pm.sdlc_api.context") as ctx, - ): - ctx.github_repo = "test/repo" - ctx.sdlc_feature_labels = "enhancement" - ctx.sdlc_bug_labels = "bug" - ctx.sdlc_docs_labels = "documentation" - app.dependency_overrides[connection] = override_conn - try: - r = client.get("/api/v1/sdlc/delivery?weeks=1&week_days=7") - finally: - app.dependency_overrides.clear() - - assert r.status_code == 200 - data = r.json() - assert data["weeks"] == 1 - assert data["week_days"] == 7 - assert len(data["slices"]) == 1 - s0 = data["slices"][0] - assert s0["window_days"] == 7 - assert s0["merged_pr_throughput"]["total"] == 1 - assert s0["median_pr_cycle_time"]["pr_count"] == 1 - assert s0["median_time_to_first_review"]["eligible_pr_count"] == 1 - assert s0["median_time_to_first_review"]["included_pr_count"] == 1 - -class TestEscapedDefect: - def test_escaped_defect_rate(self, client, mock_connector): - fixed = datetime(2025, 4, 10, 12, 0, 0, tzinfo=UTC) - _human = {"__typename": "User", "login": "contributor"} - feat = { - "number": 1, - "createdAt": "2025-04-01T10:00:00Z", - "mergedAt": "2025-04-08T10:00:00Z", - "additions": 1, - "deletions": 1, - "labels": {"nodes": [{"name": "enhancement"}]}, - "milestone": {"title": "v1.0.0"}, - "author": _human, - } - bug = { - "number": 2, - "createdAt": "2025-04-01T10:00:00Z", - "mergedAt": "2025-04-08T11:00:00Z", - "additions": 1, - "deletions": 1, - "labels": {"nodes": [{"name": "bug"}]}, - "milestone": {"title": "v1.0.0"}, - "author": _human, +@pytest.fixture +def gitctx(): + client = MagicMock(owner="test", repo="repo") + merged = [ + pr(1, "enhancement"), + pr(2, "bug"), + pr(3, "documentation"), + pr(4, "enhancement", author={"__typename": "Bot", "login": "robot"}), + pr(5, "enhancement", mergedAt="2025-04-03T12:00:00Z"), + ] + opened = [ + pr( + 10, + "enhancement", + mergedAt=None, + reviews=page( + [ + { + "submittedAt": "2025-04-05T10:30:00Z", + "state": "COMMENTED", + "author": {"__typename": "Bot", "login": "bot"}, + }, + { + "submittedAt": "2025-04-05T12:00:00Z", + "state": "COMMENTED", + "author": {"__typename": "User", "login": "reviewer"}, + }, + ] + ), + ), + pr(11, "bug", mergedAt=None, reviews=page([])), + ] + escapes = [ + { + "id": "E1", + "number": 50, + "createdAt": "2025-04-06T10:00:00Z", + "milestone": {"title": "v0.2.0"}, } - doc_pr = { - "number": 3, - "createdAt": "2025-04-01T10:00:00Z", - "mergedAt": "2025-04-08T12:00:00Z", - "additions": 1, - "deletions": 1, - "labels": {"nodes": [{"name": "documentation"}]}, - "milestone": {"title": "v1.0.0"}, - "author": _human, + ] + bugs = [ + {"id": "B1", "number": 51, "createdAt": "2025-04-05T10:00:00Z"}, + {"id": "B2", "number": 52, "createdAt": "2025-04-10T12:00:00Z"}, + {"id": "B3", "number": 53, "createdAt": "2025-04-03T12:00:00Z"}, + ] + closed = [ + { + "id": "B4", + "number": 54, + "createdAt": "2020-01-01T00:00:00Z", + "closedAt": "2025-04-06T10:00:00Z", } + ] + client.get_paged.return_value = [ + { + "number": 1, + "title": "v0.1.0", + "state": "closed", + "closed_at": "2025-01-01T00:00:00Z", + }, + {"number": 2, "title": "v0.2.0", "state": "open"}, + ] + + def post(path, payload): + assert path == "/graphql" + data = {} + for key, query in payload["variables"].items(): + if not key.startswith("q"): + continue + if "is:merged" in query: + rows = merged + if "milestone:" in query: + rows = [ + n + for n in rows + if f'milestone:"{n["milestone"]["title"]}"' in query + ] + elif "is:pr" in query: + rows = opened + elif "label:escape" in query: + rows = escapes + elif "is:closed" in query: + rows = closed + else: + rows = bugs + data["s" + key[1:]] = page(rows) + return {"data": data} + + client.post.side_effect = post + return client - mock_connector.post.side_effect = lambda path, payload: _graphql_page( - [feat, bug, doc_pr] - ) - # Next open v0.7.0 → include v0.5.1 + v0.6.0 closed + v0.7.0 open (sorted) - mock_connector.get_paged.side_effect = [ - [{"title": "v0.7.0", "state": "open", "number": 3}], - [ - {"title": "v0.5.0", "closed_at": "2025-01-01T00:00:00Z"}, - {"title": "v0.5.1", "closed_at": "2025-02-01T00:00:00Z"}, - {"title": "v0.6.0", "closed_at": "2025-03-01T00:00:00Z"}, - ], - ] - - def get_side(path: str, headers=None): - if "/search/issues" in path: - # Escape issue on v0.6.0 counts toward previous milestone v0.5.1 - return { - "items": [ - { - "milestone": {"title": "v0.6.0"}, - "created_at": "2025-04-06T10:00:00Z", - } - ], - "total_count": 1, - } - raise AssertionError(path) - - mock_connector.get.side_effect = get_side - - async def override_conn(): - yield mock_connector - - with ( - patch("github_pm.sdlc_metrics.utc_now", return_value=fixed), - patch("github_pm.sdlc_api.context") as ctx, - ): - ctx.github_repo = "test/repo" - ctx.sdlc_feature_labels = "enhancement" - ctx.sdlc_bug_labels = "bug" - ctx.sdlc_docs_labels = "documentation" - ctx.sdlc_escape_label = "escape" - app.dependency_overrides[connection] = override_conn - try: - r = client.get("/api/v1/sdlc/escaped-defect-rate?weeks=1&week_days=7") - finally: - app.dependency_overrides.clear() - - assert r.status_code == 200 - data = r.json() - assert data["weeks"] == 1 - assert len(data["slices"]) == 1 - slice0 = data["slices"][0] - assert "as_of" in slice0 - body = slice0["releases"] - assert len(body) == 3 - assert [r["release"] for r in body] == ["v0.5.1", "v0.6.0", "v0.7.0"] - assert [r["is_next_open"] for r in body] == [False, False, True] - assert body[0]["escape_issues"] == 1 - assert body[0]["docs_prs"] == 1 - assert body[0]["rate"] == pytest.approx(1.0 / 3.0) - assert body[1]["escape_issues"] == 0 - assert body[1]["docs_prs"] == 1 - assert body[1]["rate"] == pytest.approx(0.0) - assert body[2]["feature_prs"] == 1 - assert body[2]["bug_fix_prs"] == 1 - assert body[2]["docs_prs"] == 1 - assert body[2]["escape_issues"] == 0 - assert body[2]["rate"] == pytest.approx(0.0) - - -class TestBugBacklog: - def test_bug_backlog_delta(self, client, mock_connector): - opened_items = [ - {"created_at": "2025-04-05T10:00:00Z", "closed_at": None} for _ in range(4) - ] - closed_items = [ - {"created_at": "2025-03-01T00:00:00Z", "closed_at": "2025-04-06T10:00:00Z"} - ] - - def get_side(path: str, headers=None): - if "/search/issues" in path: - # q= is URL-encoded (e.g. is:closed -> is%3Aclosed) - if "is%3Aclosed" in path or "is:closed" in path: - return {"items": closed_items, "total_count": len(closed_items)} - return {"items": opened_items, "total_count": len(opened_items)} - raise AssertionError(path) - - mock_connector.get.side_effect = get_side - - async def override_conn(): - yield mock_connector - - fixed = datetime(2025, 4, 10, 12, 0, 0, tzinfo=UTC) - with ( - patch("github_pm.sdlc_metrics.utc_now", return_value=fixed), - patch("github_pm.sdlc_api.context") as ctx, - ): - ctx.github_repo = "test/repo" - ctx.sdlc_bug_labels = "bug" - app.dependency_overrides[connection] = override_conn - try: - r = client.get("/api/v1/sdlc/bug-backlog-delta?weeks=1&week_days=7") - finally: - app.dependency_overrides.clear() - assert r.status_code == 200 - d = r.json() - assert d["weeks"] == 1 - assert len(d["slices"]) == 1 - s0 = d["slices"][0] - assert s0["bugs_opened"] == 4 - assert s0["bugs_closed"] == 1 - assert s0["net"] == 3 +@pytest.fixture +def client(gitctx): + async def override(): + yield gitctx + + app.dependency_overrides[connection] = override + with ( + patch("github_pm.sdlc_metrics.utc_now", return_value=NOW), + patch("github_pm.sdlc_api.context", SETTINGS), + ): + yield TestClient(app) + app.dependency_overrides.clear() + + +def test_delivery_preserves_bot_filter_review_selection_and_boundaries(client, gitctx): + response = client.get("/api/v1/sdlc/delivery?weeks=1") + assert response.status_code == 200, response.text + slice0 = response.json()["slices"][0] + assert slice0["merged_pr_throughput"]["total"] == 3 + assert slice0["merged_pr_throughput"]["by_pr_type"] == { + "feature": 1, + "bug_fix": 1, + "docs": 1, + "unclassified": 0, + } + assert slice0["median_pr_cycle_time"]["median_seconds"] == 86400 + assert slice0["median_time_to_first_review"]["median_seconds"] == 7200 + assert slice0["median_time_to_first_review"]["included_pr_count"] == 1 + assert slice0["median_time_to_first_review"]["eligible_pr_count"] == 2 + assert gitctx.post.call_count == 1 + gitctx.get.assert_not_called() + gitctx.get_paged.assert_not_called() + + +def test_escape_preserves_release_attribution(client, gitctx): + response = client.get("/api/v1/sdlc/escaped-defect-rate?weeks=1") + assert response.status_code == 200, response.text + rows = response.json()["slices"][0]["releases"] + assert [r["release"] for r in rows] == ["v0.1.0", "v0.2.0"] + assert rows[0]["escape_issues"] == 1 + assert rows[0]["docs_prs"] == 1 + assert rows[0]["rate"] == pytest.approx(1 / 3) + assert rows[1]["is_next_open"] is True + assert rows[1]["rate"] is None + gitctx.get_paged.assert_called_once() + assert "state=all" in gitctx.get_paged.call_args.args[0] + + +def test_bug_delta_includes_closures_of_older_issues_and_end_boundary(client): + response = client.get("/api/v1/sdlc/bug-backlog-delta?weeks=1") + assert response.status_code == 200, response.text + row = response.json()["slices"][0] + assert (row["bugs_opened"], row["bugs_closed"], row["net"]) == (2, 1, 1) + + +@pytest.mark.parametrize("weeks", [1, 4, 52]) +def test_dashboard_fetches_facts_once_regardless_of_week_count(client, gitctx, weeks): + response = client.get(f"/api/v1/sdlc/dashboard?weeks={weeks}") + assert response.status_code == 200, response.text + result = response.json() + assert all( + len(result[k]["slices"]) == weeks for k in ("delivery", "escaped", "backlog") + ) + assert gitctx.post.call_count == 2 # Five independent searches in bounded batches. + gitctx.get_paged.assert_called_once() + gitctx.get.assert_not_called() + queries = [ + v + for call in gitctx.post.call_args_list + for k, v in call.args[1]["variables"].items() + if k.startswith("q") + ] + assert sum("is:merged" in q for q in queries) == 1 + assert all("milestone:" not in q for q in queries) + assert len({result[k]["slices"][-1]["as_of"] for k in result}) == 1 + assert result["escaped"]["slices"][-1]["releases"][0]["rate"] == pytest.approx( + 1 / 3 + ) + + +def test_weekly_boundary_is_counted_once(client): + response = client.get("/api/v1/sdlc/dashboard?weeks=2") + result = response.json() + assert [ + s["merged_pr_throughput"]["total"] for s in result["delivery"]["slices"] + ] == [1, 3] + assert [s["bugs_opened"] for s in result["backlog"]["slices"]] == [1, 2] + + +def test_cumulative_escape_keeps_historical_scope(gitctx): + with patch("github_pm.sdlc_metrics.utc_now", return_value=NOW): + result = compute_escaped_defect_rate(gitctx, SETTINGS) + assert result.window_start is None + assert result.releases[0].feature_prs == 2 + assert result.releases[0].rate == pytest.approx(1 / 4) + + +def test_dashboard_rejects_partial_github_data(client, gitctx): + gitctx.post.side_effect = None + gitctx.post.return_value = { + "data": {}, + "errors": [{"message": "permission denied"}], + } + with pytest.raises(RuntimeError, match="permission denied"): + client.get("/api/v1/sdlc/dashboard") diff --git a/backend/tests/test_sdlc_metrics.py b/backend/tests/test_sdlc_metrics.py index ab2101e..28e74cb 100644 --- a/backend/tests/test_sdlc_metrics.py +++ b/backend/tests/test_sdlc_metrics.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """Unit tests for sdlc_metrics helpers. Generated-by: Cursor @@ -8,6 +9,7 @@ import pytest +from github_pm import report_queries as rq from github_pm import sdlc_metrics as sm from github_pm.context import Settings @@ -114,12 +116,12 @@ def test_aggregate_throughput_and_cycle(): def test_label_or_clause(): - assert "label:bug" in sm._label_or_clause(frozenset({"bug"})) - assert "OR" in sm._label_or_clause(frozenset({"a", "b"})) + assert "label:bug" in rq._label_or_clause(frozenset({"bug"})) + assert "OR" in rq._label_or_clause(frozenset({"a", "b"})) def test_milestone_query_escapes_quotes(): - q = sm.milestone_merged_prs_query("o/r", 'v1.0.0"') + q = rq.milestone_merged_prs_query("o/r", 'v1.0.0"') assert '\\"' in q or "v1.0.0" in q @@ -170,7 +172,7 @@ def test_build_semver_milestone_previous_map(): def test_escape_labeled_issues_query(): - q = sm.escape_labeled_issues_query("acme/rocket", "escape") + q = rq.escape_labeled_issues_query("acme/rocket", "escape") assert "repo:acme/rocket" in q assert "is:issue" in q assert "label:escape" in q diff --git a/backend/tests/test_sdlc_report_cli.py b/backend/tests/test_sdlc_report_cli.py index 2378e3e..aec4111 100644 --- a/backend/tests/test_sdlc_report_cli.py +++ b/backend/tests/test_sdlc_report_cli.py @@ -1,3 +1,4 @@ +# Assisted-by: Codex """Tests for the offline SDLC HTML report CLI. Generated-by: Cursor @@ -17,6 +18,7 @@ BugBacklogResponse, BugBacklogSeriesResponse, CycleTimePayload, + DashboardResponse, DeliveryResponse, DeliverySeriesResponse, EscapedDefectResponse, @@ -116,21 +118,14 @@ def test_writes_html(self, tmp_path: Path): out = tmp_path / "report.html" with ( - patch("github_pm.api.Connector") as mc, + patch("github_pm.github_client.Connector") as mc, patch( - "github_pm.sdlc_service.compute_sdlc_delivery_series", - return_value=delivery, - ), - patch( - "github_pm.sdlc_service.compute_escaped_defect_rate_series", - return_value=escaped, - ), - patch( - "github_pm.sdlc_service.compute_bug_backlog_delta_series", - return_value=bugs, + "github_pm.sdlc_service.compute_sdlc_dashboard", + return_value=DashboardResponse( + delivery=delivery, escaped=escaped, backlog=bugs + ), ), ): - mc.return_value = object() runner = CliRunner() result = runner.invoke( main, @@ -138,6 +133,7 @@ def test_writes_html(self, tmp_path: Path): env={"GITHUB_TOKEN": "tok", "GITHUB_REPO": "x/y"}, ) + mc.return_value.__exit__.assert_called_once() assert result.exit_code == 0, result.output assert out.is_file() body = out.read_text(encoding="utf-8") diff --git a/backend/tests/test_status_report_api.py b/backend/tests/test_status_report_api.py index f38ac7d..7b098e4 100644 --- a/backend/tests/test_status_report_api.py +++ b/backend/tests/test_status_report_api.py @@ -1,8 +1,10 @@ +# Assisted-by: Codex """Tests for project status report API (mocked GitHub GraphQL). Generated-by: Cursor """ +from copy import deepcopy from unittest.mock import MagicMock from fastapi.testclient import TestClient @@ -12,6 +14,36 @@ from github_pm.app import app +def _page(nodes): + return { + "nodes": deepcopy(nodes), + "issueCount": len(nodes), + "pageInfo": {"hasNextPage": False, "endCursor": None}, + } + + +def _response(body, merged, opened, issues, attention): + result = {} + for key, query in body["variables"].items(): + if not key.startswith("q"): + continue + nodes = ( + merged + if "is:merged" in query + else issues if "is:issue" in query else opened + ) + result["s" + key[1:]] = _page(nodes) + if not result: + nodes = list({n["number"]: deepcopy(n) for n in attention}.values()) + for node in nodes: + node.setdefault("id", f'P{node["number"]}') + node.setdefault("reviews", {"nodes": []})["pageInfo"] = _page([])[ + "pageInfo" + ] + result["repository"] = {"pullRequests": _page(nodes)} + return {"data": result} + + @pytest.fixture def client(): return TestClient(app) @@ -51,6 +83,7 @@ def mock_connector_graphql(): "state": "OPEN", } ] + gitctx.status_opened_pr_nodes = opened_pr_nodes issue_nodes = [ { @@ -63,76 +96,28 @@ def mock_connector_graphql(): ] gitctx.status_opened_issue_nodes = issue_nodes - def post_side(path: str, data=None, **kwargs): - body = data - if path != "/graphql" or not isinstance(body, dict): - raise AssertionError(f"unexpected post {path=!r} body={body!r}") - q = (body.get("variables") or {}).get("q") or "" - if "is:merged" in q and "merged:" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": merged_nodes, - } - } - } - if "is:issue" in q and "created:" in q: - assert "closedAt" in body["query"] - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": list(gitctx.status_opened_issue_nodes), - } - } - } - if q == "repo:test/repo is:pr is:open": - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": list(gitctx.status_attention_open_nodes), - } - } - } - if "is:pr" in q and "is:open" in q and "updated:" in q: - if "updated:<" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": list(gitctx.status_backlog_nodes), - } - } - } - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": list(gitctx.status_recently_updated_nodes), - } - } - } - if "is:pr" in q and "created:" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": opened_pr_nodes, - } - } - } - raise AssertionError(f"unexpected graphql q={q!r}") + def post_side(path, data=None, **kwargs): + return _response( + data, + merged_nodes, + opened_pr_nodes, + issue_nodes, + list(gitctx.status_attention_open_nodes) + + list(gitctx.status_recently_updated_nodes) + + list(gitctx.status_backlog_nodes), + ) gitctx.post.side_effect = post_side return gitctx class TestProjectStatusReport: + @pytest.mark.parametrize("is_draft", [False, True]) def test_report_ok_with_end_date_only_defaults_start( - self, client, mock_connector_graphql + self, client, mock_connector_graphql, is_draft ): + mock_connector_graphql.status_opened_pr_nodes[0]["isDraft"] = is_draft + async def override_conn(): yield mock_connector_graphql @@ -153,7 +138,13 @@ async def override_conn(): "html_url": "https://github.com/test/repo/pull/10", } ] - assert body["opened_pull_requests"][0]["number"] == 11 + assert body["opened_pull_requests"] == [ + { + "number": 11, + "title": "Opened PR (draft)" if is_draft else "Opened PR", + "html_url": "https://github.com/test/repo/pull/11", + } + ] assert body["opened_issues"][0]["number"] == 12 assert body["second_approval_needed"] == [] assert body["recently_updated_pull_requests"] == [] @@ -218,6 +209,7 @@ async def override_conn(): body = r.json() assert body["start_date"] == "2025-04-10" assert body["end_date"] == "2025-04-10" + assert body["opened_pull_requests"] == [] def test_report_explicit_range(self, client, mock_connector_graphql): async def override_conn(): @@ -285,7 +277,9 @@ async def override_conn(): args = getattr(c, "args", ()) body = args[1] if len(args) >= 2 else getattr(c, "kwargs", {}).get("data") if isinstance(body, dict) and body.get("variables"): - gql_qs.append(body["variables"].get("q") or "") + gql_qs.extend( + v for k, v in body["variables"].items() if k.startswith("q") + ) assert any("merged:2025-04-04..2025-04-10" in q for q in gql_qs) assert any("is:merged" in q for q in gql_qs) @@ -296,16 +290,15 @@ async def override_conn(): "repo:test/repo is:issue created:2025-04-04..2025-04-10" in q for q in gql_qs ) - assert any( - "repo:test/repo is:pr is:open draft:false updated:<2025-04-04" in q - for q in gql_qs - ) - assert any( - "repo:test/repo is:pr is:open draft:false updated:2025-04-04..2025-04-10" - in q - for q in gql_qs - ) - assert any(q == "repo:test/repo is:pr is:open" for q in gql_qs) + assert len(gql_qs) == 3 + assert mock_connector_graphql.post.call_count == 2 + search_query = mock_connector_graphql.post.call_args_list[0].args[1]["query"] + assert "isDraft" in search_query + assert "closedAt" in search_query + open_query = mock_connector_graphql.post.call_args.args[1]["query"] + assert "pullRequests(states:[OPEN]" in open_query + assert "additions" not in open_query + assert "labels" not in open_query def test_attention_sections_partition_open_prs( self, client, mock_connector_graphql @@ -738,66 +731,8 @@ def test_opened_prs_exclude_closed_and_merged(self, client): issue_nodes = [] - def post_side(path: str, data=None, **kwargs): - body = data - if path != "/graphql" or not isinstance(body, dict): - raise AssertionError(f"unexpected post {path=!r} body={body!r}") - q = (body.get("variables") or {}).get("q") or "" - if "is:merged" in q and "merged:" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": merged_nodes, - } - } - } - if "is:issue" in q and "created:" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": issue_nodes, - } - } - } - if q == "repo:test/repo is:pr is:open": - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": [], - } - } - } - if "is:pr" in q and "is:open" in q and "updated:" in q: - if "updated:<" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": [], - } - } - } - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": [], - } - } - } - if "is:pr" in q and "created:" in q: - return { - "data": { - "search": { - "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": opened_pr_nodes, - } - } - } - raise AssertionError(f"unexpected graphql q={q!r}") + def post_side(path, data=None, **kwargs): + return _response(data, merged_nodes, opened_pr_nodes, issue_nodes, []) gitctx.post.side_effect = post_side diff --git a/docs/github-api-optimization-implementation.md b/docs/github-api-optimization-implementation.md new file mode 100644 index 0000000..dbf5d39 --- /dev/null +++ b/docs/github-api-optimization-implementation.md @@ -0,0 +1,52 @@ +# GitHub API optimization implementation + +Implemented in the `optimize` workspace against baseline `a49ea37`. The original analysis is in [github-api-optimization.md](github-api-optimization.md). UI views, editing controls, report sections, clipboard output, and their date/review/classification rules are retained. Changes are uncommitted. + +## Collection and mutation changes + +| Consumer | Implementation | Request-count evidence | +| --- | --- | --- | +| Planning board | REST pages of 100 retain raw Markdown and rendered HTML. GraphQL aliases enrich 50 issues at a time with parents, dependencies, and closing PRs. Unused subtree summaries are omitted. | A 100-issue, single-page fixture needs one REST list request plus two enrichment requests, versus four REST list pages plus 100 enrichments before. Additional relationship pages are fetched separately. | +| SDLC dashboard and HTML CLI | One `/sdlc/dashboard` response uses a common timestamp and whole-interval facts. Merged PRs are shared between delivery and release metrics; created PRs include human-review data. Created and closed bugs remain separate populations. | Five logical searches in two GraphQL operations, plus one `state=all` milestone catalog page, for the finite fixture at 1, 4, and 52 weeks. Larger real populations require more pages, not duplicate weekly collection. | +| Standalone SDLC APIs | Existing routes remain available with focused collection plans; escaped-defect collection omits delivery-only size fields and reviews. Cumulative calculations retain all historical data. | No REST review request per PR; no weekly repeated milestone catalog. | +| Project Status | Three slim historical searches share one GraphQL operation. A separate paginated repository open-PR collection supplies attention, recently updated, and backlog sections. | Six logical collections become four, with two initial HTTP requests. The open-PR connection avoids Search's 1,000-result limit. | +| Label edits | Targeted REST add/remove operations return authoritative labels to the edited row and its parent list. Labels containing spaces or slashes are encoded. | One ordinary write instead of GET plus replacement PATCH. An already-absent label removal checks issue existence before treating 404 as success. | +| Milestone close confirmation | One GraphQL query returns separate open issue and PR counts. | No downloading issue/PR bodies for counts. | +| Parent/dependency/closing-reference edits | Identity pairs share GraphQL reads. Hierarchy traversal batches breadth-first frontiers and reuses the descendants found during cycle validation. | No second traversal for cross-milestone reparenting; closed and cross-milestone descendants remain included. | +| Comments and reactions | Comments keep REST raw/rendered bodies; comments with reactions receive batched GraphQL reaction details, including every user's login and the existing Unknown fallback. Issue reactions load when the description opens. | Removes per-comment reaction requests and hidden issue-reaction reads. Empty responses and failures finish an attempt instead of causing render-driven retry loops. | +| Sorting | Recursive sibling and PR ordering happens in the browser. Authoritative label-write results update the sorting inputs. | Changing label priority makes zero requests. | + +These are verified fixture counts and control-flow comparisons, not production latency measurements. + +## Service boundaries and completeness + +- `github_client.py` owns transport, lifecycle, conditional metadata caching, scheduling, timeouts, and metrics. API and CLI consumers use scoped sessions; services no longer import the transport from route implementation. +- `report_queries.py` owns consumer-specific projections and independent root/nested pagination. `sdlc_metrics.py` contains pure calculations. `planning_reader.py` owns batched Planning reads, and `repository_catalog.py` owns metadata collection. +- Searches exceeding 1,000 matches split into disjoint date/time intervals and deduplicate stable IDs. A single-second partition exceeding the cap raises an explicit error. Cumulative searches use the entire possible GitHub history for partitioning, rather than the dashboard's weekly bounds. +- Labels, reviews, relationships, sub-issues, and comment reactions follow their own continuation cursors. Missing pagination data, repeated cursors, partial GraphQL errors, and incomplete REST search results fail explicitly. +- A failed milestone cascade reports completed issue numbers and the failed issue. GitHub writes are not transactional; failed or ambiguous writes are not automatically replayed. Planning reloads affected columns after hierarchy errors and after adopting a parent's milestone. + +## Transport and freshness policy + +Blocking GitHub operations run in the ASGI worker threadpool. A regression test holds a blocking operation pending while a separate lightweight request completes. Sessions close on both success and failure. + +Each transport operation has a five-minute budget, including queue waits, with connect/read timeouts capped at 5/45 seconds. Requests sharing a token serialize within the process, writes are spaced by at least one second, and read retries are bounded at five attempts. REST and GraphQL rate-limit signals respect Retry-After/reset information; long throttles fail visibly rather than holding the queue indefinitely. Mutation timeouts and 5xx responses are not replayed. + +Per-operation logs record calls, response bytes, retries, aggregate request/queue time, GraphQL cost, 304 counts, and rate-limit headers. Conditional metadata representations retain their pagination links and are scoped by token hash, repository, URL, and representation headers. Writes invalidate them; older reads cannot repopulate an invalidated cache. The backend cache retains at most 256 entries. + +The frontend query layer coalesces in-flight reads and scopes entries by repository. Metadata stays fresh for 30 seconds; Planning lists, reactions, comments, and dashboard results for 15 seconds; identical Markdown previews for five minutes. At most 256 completed entries are retained, while pending requests finish without eviction loops. Mutations invalidate before and after the attempt, including ambiguous failures. Superseded reads rejoin the current request instead of publishing old data. Project Status Apply always fetches again; explicit Refresh obtains fresh data, including loaded collapsed milestone cards. Unopened cards and comments remain lazy. Report and Planning request generations prevent obsolete responses from replacing newer selections. + +The synchronous-session/threadpool approach was selected from the analysis's transport alternatives. Full GraphQL board replacement, lifespan-managed async transport, and splitting descriptions from summary payloads were optional alternatives, not additional changes required for the selected design. Raw and rendered Markdown remain available without changing editor behavior. + +## Validation + +- `cd backend && UV_CACHE_DIR=/tmp/optimize-uv-cache uv run tox`: all five environments pass; **169 tests**, **90.88% coverage**. +- `cd frontend && npm run format && npm run format:check && npm test -- --run`: **199 tests** pass. +- `cd frontend && npm run build`: production build passes. +- `git diff --check`: passes. + +The tests cover hierarchy and external parents, closed descendants, partial cascades, one-call label edits, shared report facts, UTC timestamp boundaries, bot filtering, review identity and approval semantics, nested/outer pagination, oversized search splitting, validator reuse and invalidation, stale-response protection, local sorting, lazy reactions, preview caching, and event-loop responsiveness. Existing report-copy and editing tests remain green. Obsolete tests tied to per-issue/per-week request shapes were replaced with equivalent behavioral assertions and request-count checks. + +No authenticated live GitHub benchmark or browser end-to-end run was performed. Production p50/p95 latency, rate cost, and batch-size tuning remain measurements to take from the new instrumentation. GitHub data can change during paginated collection; the API does not provide a transactional snapshot. The queue and caches are per process, so separate server workers do not share throttling state. + +Generated-by: Codex diff --git a/docs/github-api-optimization.md b/docs/github-api-optimization.md new file mode 100644 index 0000000..952d45b --- /dev/null +++ b/docs/github-api-optimization.md @@ -0,0 +1,206 @@ +**GitHub API efficiency investigation — 2026-11-18** + +Implementation is now complete; see [implementation and validation notes](github-api-optimization-implementation.md). The following investigation describes the original `a49ea37` baseline, so its source line references and current-behavior descriptions are historical. + +Reviewed branch `optimize`, commit `a49ea37`. The worktree was clean at the start. This is a static analysis of the frontend, backend, and relevant tests, with GitHub/FastAPI documentation checks. Request counts below are derived from control flow, exclude retries unless stated, and are not measured latency results. No authenticated GitHub requests or application changes were made. + +**Recommendation** + +There is substantial room to reduce GitHub requests while retaining the UI. The largest gains come from batching Planning enrichment, fetching SDLC data once for the whole reporting interval, and reusing the open-PR dataset within Project Status. Keep REST where it already performs a focused operation efficiently. Separate consumer-specific data selection from reusable transport and computation. + +| Priority | Change | Expected benefit | Main preservation concern | +| --- | --- | --- | --- | +| First | Batch Planning issue enrichment | Eliminate one GraphQL round trip per issue | Hierarchy, dependencies, closing PRs, complete pagination | +| First | Load one SDLC interval and partition locally; include reviews in GraphQL | Eliminate repeated overlapping searches and per-PR REST review calls | Exact time windows, bot filters, review semantics, size/type breakdowns | +| First | Move blocking HTTP work out of async route execution | Keep unrelated UI requests responsive during reports | Bound GitHub traffic; preserve errors and cancellation behavior | +| First | Stop reaction retry loops | Prevent repeated requests after failure or an empty response | Preserve reactor names and retry after explicit refresh | +| Next | Reuse Project Status open PRs; tailor its fields | Six logical searches become four; batch independent first pages | Current-state attention versus date-window sections | +| Next | Use focused REST label operations; larger REST pages | Halve ordinary label-change calls; fewer list pages | Duplicate/missing-label behavior and concurrent edits | +| Next | Use GraphQL milestone counts; sort loaded issues locally | Avoid full item downloads for counts or sort changes | Separate issue/PR counts and sibling ordering | +| Later | Consolidate metadata caches and lazy detail loading | Reduce duplicate, hidden, and remount-related work | Explicit freshness, editing, external changes | +| Later | Reuse hierarchy traversal during mutations | Reduce subtree reads during drag/drop and adoption | Closed descendants, cycle checks, partial failure | + +**Current UI-to-GitHub request map** + +All backend paths below are under `/api/v1`. `P30(n) = max(1, ceil(n/30))` describes an ordinary REST list with the default page size; `G(q)` is the number of GraphQL pages for query `q`. + +| UI action | Frontend / backend entry points | Current GitHub work | +| --- | --- | --- | +| Application mount | `App.jsx:88`, `App.jsx:100`; `/project`, `/milestones`, `/labels`, `/assignees` | Project is local configuration. Three independent paginated REST lists preload regardless of active tab. | +| Expand milestone | `MilestoneCard.jsx:158`; `api.py:296` | REST list of open issues and PRs, then one sequential hierarchy GraphQL request per non-PR issue. | +| Change label sort | `MilestoneCard.jsx:183` | Reloads the same milestone data and repeats enrichment for expanded, previously loaded cards. | +| Planning Refresh | `App.jsx:243`, `MilestoneCard.jsx:197` | Reloads every previously loaded card, including collapsed ones; unopened cards remain lazy. | +| Issue reactions | `IssueCard.jsx:337`; `api.py:674` | Paginated REST reactions for each mounted row whose reaction summary is nonzero, even while its description is collapsed. | +| Expand comments | `IssueCard.jsx:241`; `api.py:393` | Paginated REST comments, followed by a separate reaction request per comment with reactions. | +| Close milestone confirmation | `ManageMilestones.jsx:182`; `api.py:784` | Downloads all open items with raw and rendered bodies just to count issues and PRs separately. | +| Project Status Apply | `ProjectStatusPanel.jsx:182`; `status_report_service.py:170` | Six sequential GraphQL searches: merged PRs, opened PRs, opened issues, updated open PRs, all open PRs with reviews, stale open PRs. | +| SDLC tab / week change | `SdlcKpisPanel.jsx:265`; `sdlc_api.py` | Three browser requests via `Promise.all`; separate delivery, escape, and backlog collectors. | +| Label toggle | `IssueCard.jsx:722`; `api.py:1385` | GET issue, then PATCH the complete label set when a change is needed. | +| Reparent across milestones | `api.py:913` | Traverse descendants for cycle check, fetch child and parent, link, traverse the same subtree again, PATCH root and every descendant. | + +The frontend already avoids some unnecessary work: comments are lazy, unopened milestones are lazy, shared metadata promises deduplicate several loads, and many mutations update local state without a full reload. Preserve those advantages. + +**1. Planning: retain the useful REST list and batch its enrichment** + +Evidence: `backend/src/github_pm/api.py:296`, `backend/src/github_pm/issue_hierarchy.py:128`, `frontend/src/components/MilestoneCard.jsx:158`. + +For `I` issues and `R` PRs, initial loading currently costs `P30(I + R) + I` requests before any reactions. All enrichment requests are sequential. A milestone containing 100 issues and no PRs therefore requires 104 GitHub requests. + +A focused first implementation would retain the REST list, request `per_page=100`, collect issue `node_id` values, and enrich them in configurable GraphQL batches. GitHub supports lookup of multiple nodes by ID; aliases of `repository.issue(number:)` are an alternative when numbers are the available identity. Map results by identity, preserving errors and missing nodes per item. [GitHub node lookup](https://docs.github.com/en/graphql/reference/meta#nodes). + +With an illustrative batch size of 50 and no nested overflow, that 100-issue example becomes one REST request plus two GraphQL requests: **104 → 3**. Batch size 50 is a tuning starting point, not a GitHub requirement or a demonstrated optimum. Extra pages for linked collections increase this count. + +The current hierarchy fragment also fetches `subIssues(first:100)`, but neither `apply_graphql_hierarchy` nor `build_issue_forest` consumes those nodes. The forest is built from each fetched issue's parent. `sub_issues_summary` is returned by the backend but has no frontend consumer in this checkout. Remove unnecessary selection from the board projection after recording the current UI contract. The mutation-oriented `PARENT_ONLY_GRAPHQL` also selects unused child numbers. + +Keep parent title/milestone information for parents outside the loaded open set, both dependency directions, closing PR references including closed PRs, and the existing forest cycle protection. Preserve flat PR rows and the synthetic milestone `0` meaning no milestone. Do not use only visible UI descendants when preparing server-side cascades. + +A later GraphQL-only board loader is possible, but it is a larger migration: the UI consumes raw Markdown, rendered HTML, author/avatar information, issue type/color, labels, assignees, comment counts, milestone information, dates, URLs, and issue/PR distinctions. Direct milestone issue and PR connections would each need their own cursor, and the no-milestone bucket needs a separate strategy. The hybrid approach captures most request-count savings without introducing all of that mapping at once. Prefer direct connections or the existing REST list over introducing search-index dependence into Planning. + +**2. SDLC: separate collection from per-window calculation** + +Evidence: `sdlc_service.py:69`, `sdlc_service.py:136`, `sdlc_metrics.py:562`, `sdlc_metrics.py:623`. + +`compute_sdlc_delivery_series` calls the complete single-window collector once per week. Each call queries PRs merged or created since the start of that week, without an upper search bound, and only then filters to the week's exact timestamps. Older slices repeatedly download newer data. With fairly uniform activity, fetched PR rows grow roughly with `W(W+1)/2` rather than `W`. + +After filtering opened PRs into a slice, the code makes one REST request per PR for reviews. The same PR is generally not reviewed again in every slice, because creation windows are disjoint; the repeated work is primarily the searches and returned PR rows. The REST review call uses `get`, not `get_paged`, so it also misses subsequent review pages. + +Recommended delivery flow: + +1. Capture one `as_of` and derive the complete interval and all weekly boundaries. +2. Fetch merged PRs over that whole bounded interval once. +3. Fetch PRs created over that whole interval once, with review submission times and review authors in the GraphQL projection. +4. Finish any review and label pagination, normalize/deduplicate PRs, then calculate every slice locally. + +The GraphQL PullRequest schema exposes a reviews connection, allowing this combination. Fetch all pages needed to determine the first human submission; `reviews(first:1)` is insufficient because the first review may be from a bot or lack a submitted timestamp. [GitHub PR schema](https://docs.github.com/en/graphql/reference/pulls#pullrequest). + +For four weeks and 40 eligible opened PRs, assuming every original search fits one page, delivery alone currently requires **8 search calls + 40 review calls**. The proposed shape has **two paginated search connections carrying the needed review data**, whose first pages can be batched. This is not a promise of two HTTP calls for every repository: nested pagination, search partitioning, and query-size budgets still apply. + +Keep `(start, end]` UTC boundaries, oldest-first slices, all throughput and median breakdowns, eligible/included review counts, and current PR-type precedence. Preserve the distinction between PR-author bot detection and review-author bot detection. The existing review metric uses reviews available now for PRs created in a past slice; it does not restrict review submission to that slice's end. Changing that would change the displayed metric. + +**3. Share data across the three SDLC consumers, with narrower standalone loaders** + +Evidence: `sdlc_service.py:217`, `sdlc_service.py:319`, `sdlc_service.py:378`, `sdlc_report_cli.py:82`. + +Escaped-defect series already fetches once before partitioning, but it loads open and closed milestones separately, all escape-labeled issues, and all merged PRs for each selected milestone, regardless of the requested reporting interval. Backlog repeats two REST searches for every week, downloads full issue objects, then uses only timestamps for counting. + +Introduce a dashboard orchestrator, for example `GET /sdlc/dashboard?weeks=…`, that supplies one anchor time and collects shared facts: + +- Repository milestone catalog: one paginated `state=all` collection, divided locally into open and closed groups, with no synthetic Planning placeholder. +- Merged PR facts over the requested interval, including milestone, classification labels, author, and changed lines. Use these for both delivery and escaped-defect denominators; eliminate the per-target merged-PR searches for this combined workflow. +- Created PR facts with reviews for first-review metrics. +- Escape issues created within the overall interval, carrying milestone and creation time. +- Bug issues created and currently closed bug issues closed within the overall interval, carrying the relevant timestamps. Partition both sets locally rather than querying each week. + +Keep the full semver milestone catalog: previous-release attribution depends on the immediately previous milestone globally, not just the displayed release rows. Preserve current milestone/label attribution, non-bot denominators, exclusion of unclassified PRs from the escape denominator, and `null` rates when the denominator is zero. + +A standalone escape loader should use a slim projection without changed lines, reviews, or other delivery fields. A standalone status loader should likewise avoid delivery fields. Sharing one larger fetched dataset is useful inside the dashboard request; forcing every separate consumer through a universal large query is unnecessary. + +Bug search can initially remain REST with whole-interval collection. A slim GraphQL query can subsequently reduce payload size and batch created/closed searches. Do not replace timestamp filtering with date-only `total_count` results: the UI uses rolling timestamp boundaries, not whole calendar days. Retain separate created and closed populations so an issue opened earlier but closed in the interval is counted. + +The existing cumulative computation functions and the CLI are additional internal consumers. Adapt the CLI to the same dashboard orchestration where useful. Do not accidentally date-bound cumulative escaped-defect calculations while optimizing the weekly UI path. + +**4. Project Status: reuse all open PRs and stop borrowing delivery projections** + +Evidence: `status_report_service.py:184`, `status_report_service.py:217`, `status_report_service.py:235`, `status_report_service.py:245`, `sdlc_metrics.py:341`. + +The all-open-PR attention query already returns the timestamps, state, draft flag, title, and URL used by both Recently Updated and Backlog. Derive those sections from that same collection, applying the existing predicates and sorting. This removes two logical searches and their duplicate rows: **six collections become four**. + +Retain independent merged-in-window, created-PR, and created-issue collections. Created PRs cannot safely be reconstructed from current open PRs plus PRs merged in the reporting window: a PR created during a historical window may have merged after its end and still belongs to the current opened-PR section. + +Use four explicit projections: merged summary, created PR summary, created issue summary, and open-PR attention. Status does not need additions, deletions, labels, milestones, or PR-author classification, currently inherited from the shared SDLC helpers. Keep review author identity for the second-approval suffix and clipboard output. + +The four initial connections can share a GraphQL operation with independent cursors, followed only by unfinished connections. Start conservatively if the attention/reviews selection is expensive. For all-open PRs, a paginated `repository.pullRequests(states:[OPEN])` connection avoids the search result ceiling; retain search for selective historical windows. [GitHub repository PR connection](https://docs.github.com/en/graphql/reference/repos#repository). + +Preserve current semantics: inclusive UTC calendar dates; bot-authored PRs included; opened PRs exclude currently CLOSED/unmerged PRs but include MERGED PRs; current open state for attention and backlog; drafts may need creator attention; approval records are counted, not unique approvers or branch-protection satisfaction. Do not substitute `reviewDecision` or latest-review-per-user data for the current approval heuristic. + +In this checkout, New Issues Opened filters by creation date only and does **not** exclude issues closed during the interval. Preserve that baseline during optimization; behavior from another checkout must not silently enter this refactor. Apply must fetch again even when dates are unchanged (`ProjectStatusPanel.jsx:175`, `:215`). + +**5. Focused REST operations and count queries** + +Evidence: `api.py:109`, `api.py:784`, `api.py:1385`, `api.py:1402`. + +Most non-search REST lists omit `per_page`, including milestone items, comments, milestones, labels, assignees, reactions, and sub-issues. Set an explicit endpoint-supported page size, usually 100, while retaining Link pagination. For example, 100 repository issues currently take four list calls and can take one. GitHub documents the issue-list default as 30 and maximum as 100. [REST issue listing](https://docs.github.com/en/rest/issues/issues#list-repository-issues). + +For label addition, use `POST /repos/{owner}/{repo}/issues/{number}/labels`; for one-label removal, use the corresponding DELETE endpoint. These return the label collection and avoid GET plus replacement PATCH, reducing ordinary changes from two calls to one while avoiding overwriting unrelated concurrent label edits. Update the frontend to consume a label result if useful; it currently ignores the full returned issue. Preserve success for already-present additions and deliberate handling of already-absent removals; distinguish a missing label from a missing/inaccessible issue when handling 404. [REST label operations](https://docs.github.com/en/rest/issues/labels#add-labels-to-an-issue). + +For milestone close counts, query the milestone's `issues(states:[OPEN]) { totalCount }` and `pullRequests(states:[OPEN]) { totalCount }` together, using a minimal connection page size if required. No item bodies are needed. The schema exposes separate state-filtered connections, so the existing two-number warning can be retained. Fetch fresh counts when opening the confirmation. [GitHub milestone schema](https://docs.github.com/en/graphql/reference/issues#milestone). + +Single-field issue edits, milestone CRUD, assignee replacement/removal, issue/comment creation, and Markdown preview are already direct REST operations. Keep them unless a measured combined workflow justifies a change. GraphQL mutation batching would still need authorization/identity validation, sequencing, and partial-failure handling; it is not transactional. + +**6. Frontend orchestration, reactions, and caching** + +Evidence: `MilestoneCard.jsx:183`, `IssueCard.jsx:337`, `CommentCard.jsx:34`, `Reactions.jsx:29`, `MarkdownInputModal.jsx:82`, `App.jsx:387`, `frontend/src/utils/*Cache.js`. + +Sort changes are presentation-only: all required labels and issue numbers are already loaded. Apply the existing first-matching-label bucket order, numeric ordering, and recursive sibling sorting locally, or sort a backend-cached raw dataset. Expected GitHub requests for changing sort: **zero**, while explicit Refresh still reloads authoritative data. + +Reaction effects in IssueCard and CommentCard use an empty result and `!reactionsLoading` as the trigger. On failure, loading becomes false while the result remains empty, triggering another request. A successful empty response with a stale nonzero summary does the same. Add explicit idle/loading/success/error state, cache valid empty results, and retry through a bounded policy or explicit refresh. + +Issue reactions are rendered inside the expanded description, but fetched when the row mounts. Fetch when the description opens, or include them in a focused detail request. For comments, fetch paginated bodies and reactions together in GraphQL, or batch reaction enrichment for the REST comment page. Keep comments lazy. The GraphQL schema supports both raw/rendered comment bodies and reaction connections. [GitHub comment schema](https://docs.github.com/en/graphql/reference/issues#issuecomment). + +Reactions must retain every displayed user's login and the existing Unknown fallback, as well as counts and emoji mapping. Counts alone cannot populate current tooltips. Paginate nested reactions; do not silently replace REST's complete list with `first:100`. Map GraphQL reaction enums to the existing display semantics. + +Centralize metadata caching with explicit loaded state, in-flight deduplication, and keys that include repository and milestone state. Empty lists are valid cached results. Closed milestone lists are currently cached separately in each IssueCard; share them. MarkdownInputModal bypasses the shared in-flight promises when labels/assignees are empty. Manage dialogs revalidate in the background, while tab unmounts discard SDLC and Planning component state. Preserve necessary revalidation but coalesce duplicate requests. + +Use small, documented freshness windows or conditional revalidation for metadata. Key backend caches by repository and authorization scope; invalidate relevant entries after writes, and ensure explicit refresh/Apply can obtain fresh data. Avoid caching reports only by dates indefinitely: their current PR state, labels, milestones, and review data can change. Persisted queries should not allow an earlier response to overwrite a newer selection. Request IDs and AbortController can prevent stale UI updates; browser cancellation alone does not guarantee already-running synchronous GitHub work stops. + +REST ETag/Last-Modified revalidation can reduce transferred bytes; authenticated 304 responses do not consume the primary REST rate limit. Store the representation and pagination metadata, and handle 304 before JSON decoding. This still makes a network round trip. [GitHub REST best practices](https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api#use-conditional-requests). + +Retain GitHub Markdown rendering for fidelity. Cache identical preview text/rendering mode and avoid re-rendering unchanged text on repeated Preview visits. If descriptions are split from row summaries later, preserve raw Markdown for editing and rendered HTML for display; this is a secondary payload optimization after eliminating request fan-out. + +**7. Transport and hierarchy mutations** + +Evidence: `api.py:56`, `api.py:83`, `api.py:242`, `sdlc_api.py:24`, `status_report_api.py:27`, `api.py:850`, `api.py:913`. + +All GitHub-backed routes are async functions that directly call synchronous `requests` methods; retry backoff uses `time.sleep`. Those calls block the event loop in the serving worker. Browser `Promise.all` does not make those services concurrently awaitable. The supplied CLI starts one Uvicorn application without a multiworker configuration. + +An incremental correction is synchronous route/dependency execution in FastAPI's threadpool, or explicit offloading of the complete blocking operation. Longer term, an async HTTP transport owned by application lifespan provides connection reuse and cancellation-aware waits. Changing only a helper to `def` does not offload it when an async route calls it directly. [FastAPI execution model](https://fastapi.tiangolo.com/async/#path-operation-functions). + +Connector creates a Session for each backend request, has no explicit network timeout, and does not close the session despite the dependency docstring. Add deterministic cleanup, connection/read timeouts, and operation-level metrics. A lifespan-managed async client is a clean reuse option; do not share an unsynchronized mutable requests Session between worker threads. The existing `test_connection_closes_on_exit` only asserts header initialization, so add an actual cleanup assertion when implementing this change. + +Reducing requests should precede increasing concurrency. Use a GitHub-aware queue/limiter, respect Retry-After and rate-limit reset headers, and bound read retries. GitHub recommends serial requests to reduce secondary throttling. Treat GraphQL queries sent by POST as reads for retry policy, but do not blindly retry content-creating mutations after an ambiguous timeout. The existing 504 retry wrapper applies to all verbs, including comment creation. [GitHub rate-limit handling guidance](https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api#handle-rate-limit-errors-appropriately). + +For reparenting, retain the descendant collection used for the cycle check and reuse it for milestone cascading. With `D` descendants and one child-list page per visited issue, a cross-milestone reparent currently uses `2(D+1)` child-list calls, two identity reads, one link write, and `D+1` milestone writes: `3D+6` requests. Reusing the traversal removes `D+1` reads before considering batching. + +Batch child/parent identity reads, and batch traversal frontiers if further savings justify it. Include database/node IDs and relevant milestones in the projection. Clear-parent and adopt-parent currently fetch overlapping REST and GraphQL identity data that can be combined. Dependency and closing-reference operations also perform paired identity reads that can share one GraphQL query. Server-side validation must remain authoritative. + +Subtree traversal must cover all actual descendants, including closed issues and other milestones. `_list_sub_issue_numbers` currently converts every failure to an empty list, which can produce incomplete cascades; the refactor should make incomplete reads explicit rather than treating them as leaf nodes. Preserve accurate mutation results, rollback/reconciliation of optimistic UI state, and partial-failure reporting. + +**Pagination and correctness gates** + +The existing code has completeness gaps: review REST calls are unpaged; GraphQL labels stop at 30, attention reviews at 100, dependencies at 50, and closing PR references at 100. None of those nested selections exposes continuation information. The board also skips an issue entirely after an enrichment exception (`api.py:343`). These are risks to address or explicitly characterize, not behaviors to copy silently into larger batches. + +GitHub search returns at most 1,000 results. Whole-interval collection must detect oversized searches and partition them into smaller date/time or other disjoint ranges, deduplicating by stable ID. Use direct repository connections where practical; if a partition still exceeds the cap, continue partitioning or report incomplete data explicitly. REST search helpers also ignore `incomplete_results`; do not interpret an incomplete response as a complete report. [GraphQL search ceiling](https://docs.github.com/en/graphql/reference/search#search). + +Each aliased root and each nested connection needs its own cursor and completion state. Reject or clearly surface partial GraphQL errors; missing data must not become an empty successful report. Retry only affected read batches where safe. Batch size should account for nested connections and execution time. Fewer HTTP calls do not necessarily mean fewer GraphQL points: GitHub has connection-based costs, node limits, and timeouts. Record `rateLimit.cost`, rate-limit headers, bytes, calls, retries, and elapsed time per operation. [GraphQL resource limits](https://docs.github.com/en/graphql/overview/rate-limits-and-query-limits-for-the-graphql-api). + +**Suggested service boundaries** + +These are proposed module responsibilities, not required names or implemented files. + +| Layer | Responsibility | +| --- | --- | +| `github_client` | Transport, connection lifecycle, deadlines, rate-aware scheduling, error normalization, REST/GraphQL pagination primitives, metrics | +| `repository_catalog` | Open/closed milestones, labels, allowed assignees; scoped caches and invalidation | +| `planning_reader` / `issue_commands` | Board summaries, batched relationships, lazy issue/comment details; validated writes and cascades | +| `report_queries` | Explicit minimal PR/issue/review projections; reusable pagination without implicit bot filtering | +| `sdlc_service` | Whole-interval collection plan plus pure per-window aggregation; shared dashboard context | +| `status_report_service` | Status-specific collection and section classification with its own bot/date/review semantics | +| Frontend query layer | Shared loaded/in-flight state, mutation reconciliation, refresh behavior, stale-response protection | + +Extract Connector from `api.py` so services no longer depend on route implementation. Split query construction/collection out of `sdlc_metrics.py`, leaving classification and aggregation reusable and independent of GitHub. This enables shared transport without forcing status, delivery, and escape consumers to request each other's fields. + +**Implementation sequence and acceptance** + +1. Record baseline request counts and complete UI fixtures. Add transport timing/counts; correct blocking execution, session cleanup, timeouts, and reaction retry state. Set explicit REST page sizes and implement focused label mutations. +2. Batch Planning relationships, remove unused selections, calculate close counts directly, and make sorting local. Verify hierarchy and mutation behavior against existing UI tests. +3. Collect delivery and backlog over one interval; include paginated reviews. Add the dashboard orchestrator and reuse merged PR facts for escaped-defect metrics. Keep standalone projections narrow. +4. Reuse open PRs within Project Status, introduce its small projections, and batch independent connections. Preserve Apply refresh and clipboard details. +5. Consolidate metadata caches, improve lazy detail loading, and optimize hierarchy mutation reads based on observed frequency. + +Use the existing tests in `backend/tests/test_api.py`, `test_issue_hierarchy.py`, `test_sdlc_api.py`, `test_sdlc_metrics.py`, `test_status_report_api.py`, and the corresponding frontend component suites as a starting point. Some fixtures assert the old request topology, so preserve behavioral expectations while updating mock shapes. + +Add meaningful acceptance cases for multi-page outer and nested connections, more than 1,000 search matches, bot-only initial review pages, repeated approvals by one user, unknown mergeability, timestamp boundaries, no-milestone items, external/closed parents, closed descendants, reaction failures/empty successes, and partially failed batches. Compare normalized UI-visible data and rich/plain clipboard output, not legacy backend payloads alone. + +For an agreed finite fixture, assert the expected request reductions: 100 issue enrichment reads become two batches; label changes need one ordinary write; sorting needs no GitHub calls; status needs four logical collections; SDLC should not re-download overlapping windows. A responsiveness test should hold a mocked GitHub operation pending while verifying a separate lightweight endpoint remains responsive. Live read-only measurements are still needed to choose batch sizes and quantify p50/p95 latency, bytes, and rate cost. + +When implementation touches backend code, the required acceptance command is `cd backend && tox` (using the project's uv environment, e.g. `uv run tox`). For frontend source changes, run `cd frontend && npm run format && npm run format:check && npm test -- --run`. This investigation changes only this document; application tests were inspected but not executed, and proposed GraphQL operations were not exercised against the configured repository. diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 85dd7aa..d537a26 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor // Assisted-by: Cursor import React, { useState, useEffect } from 'react'; @@ -30,6 +31,7 @@ import ManageSort from './components/ManageSort'; import milestonesCache from './utils/milestonesCache'; import labelsCache, { clearLabelsCache } from './utils/labelsCache'; import assigneesCache from './utils/assigneesCache'; +import { invalidateQueries } from './utils/queryCache'; import iconImage from './assets/icon.png'; import './icon.css'; @@ -39,7 +41,7 @@ const VALID_MAIN_VIEW_TABS = new Set(['planning', 'sdlc', 'project-status']); const App = () => { // Initialize with cached data if available const [milestones, setMilestones] = useState(milestonesCache.data || []); - const [loading, setLoading] = useState(milestonesCache.data.length === 0); + const [loading, setLoading] = useState(!milestonesCache.loaded); const [error, setError] = useState(milestonesCache.error || null); const [project, setProject] = useState(null); const [projectLoading, setProjectLoading] = useState(true); @@ -99,10 +101,18 @@ const App = () => { // Preload milestones and labels in the background useEffect(() => { + if (projectLoading) return; + if ( + activeViewTab !== 'planning' && + !isManageMilestonesOpen && + !isManageLabelsOpen && + !isManageSortOpen + ) + return; // Preload milestones if ( !milestonesCache.loading && - milestonesCache.data.length === 0 && + !milestonesCache.loaded && !milestonesCache.promise ) { milestonesCache.loading = true; @@ -124,7 +134,7 @@ const App = () => { setError(err.message); setLoading(false); }); - } else if (milestonesCache.data.length > 0) { + } else if (milestonesCache.loaded) { // Use cached data immediately setMilestones(milestonesCache.data); setLoading(false); @@ -145,11 +155,7 @@ const App = () => { } // Preload labels - if ( - !labelsCache.loading && - labelsCache.data.length === 0 && - !labelsCache.promise - ) { + if (!labelsCache.loading && !labelsCache.loaded && !labelsCache.promise) { labelsCache.loading = true; labelsCache.error = null; labelsCache.promise = fetchLabels() @@ -169,7 +175,7 @@ const App = () => { // Preload assignees if ( !assigneesCache.loading && - assigneesCache.data.length === 0 && + !assigneesCache.loaded && !assigneesCache.promise ) { assigneesCache.loading = true; @@ -188,7 +194,14 @@ const App = () => { }); } // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + }, [ + projectLoading, + activeViewTab, + isManageMilestonesOpen, + isManageLabelsOpen, + isManageSortOpen, + issueMilestoneRefresh, + ]); // Save sort order to localStorage whenever it changes useEffect(() => { @@ -231,7 +244,7 @@ const App = () => { const handleMilestoneChange = () => { // Use cached data if available, otherwise load - if (milestonesCache.data.length > 0) { + if (milestonesCache.loaded) { setMilestones(milestonesCache.data); setLoading(false); setError(milestonesCache.error); @@ -241,6 +254,7 @@ const App = () => { }; const handleRefresh = () => { + invalidateQueries(); setIssueMilestoneRefresh((key) => key + 1); }; @@ -385,8 +399,12 @@ const App = () => { aria-label="Selected view content" > {activeViewTab === 'planning' && renderPlanningContent()} - {activeViewTab === 'sdlc' && } - {activeViewTab === 'project-status' && } + {!projectLoading && activeViewTab === 'sdlc' && ( + + )} + {!projectLoading && activeViewTab === 'project-status' && ( + + )} { api.fetchLabels.mockResolvedValue([]); // Default mock for fetchAssignees (preloaded in background) api.fetchAssignees.mockResolvedValue([]); + api.fetchSdlcDashboard.mockResolvedValue({ + delivery: { slices: [] }, + escaped: { slices: [] }, + backlog: { slices: [] }, + }); api.fetchProjectStatusReport = vi.fn().mockResolvedValue({ start_date: '2025-01-01', end_date: '2025-01-07', @@ -421,10 +427,10 @@ describe('App', () => { expect(api.fetchIssues).toHaveBeenCalledTimes(4); }); expect(api.fetchIssues.mock.calls).toEqual([ - [6, []], - [10, []], - [6, []], - [10, []], + [6], + [10], + [6, [], { force: true }], + [10, [], { force: true }], ]); }); diff --git a/frontend/src/components/CommentCard.jsx b/frontend/src/components/CommentCard.jsx index ba879f6..fab2690 100644 --- a/frontend/src/components/CommentCard.jsx +++ b/frontend/src/components/CommentCard.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor // Assisted-by: Cursor import React, { useState, useEffect } from 'react'; @@ -6,14 +7,23 @@ import { PencilAltIcon } from '@patternfly/react-icons'; import { getDaysSince, formatDate } from '../utils/dateUtils'; import { fetchCommentReactions, updateComment } from '../services/api'; import Reactions from './Reactions'; +import { useReactions } from '../utils/useReactions'; import UserAvatar from './UserAvatar'; import MarkdownInputModal from './MarkdownInputModal'; const CommentCard = ({ comment, onCommentUpdated }) => { const daysSince = getDaysSince(comment.created_at); - const [reactions, setReactions] = useState([]); - const [reactionsLoading, setReactionsLoading] = useState(false); - const [reactionsError, setReactionsError] = useState(null); + const { + data: reactions, + loading: reactionsLoading, + error: reactionsError, + } = useReactions( + comment.id, + comment.reactions?.total_count, + true, + fetchCommentReactions, + comment.reaction_details + ); const [isEditOpen, setIsEditOpen] = useState(false); const [body, setBody] = useState(comment.body || ''); const [bodyHtml, setBodyHtml] = useState(comment.body_html || ''); @@ -23,40 +33,6 @@ const CommentCard = ({ comment, onCommentUpdated }) => { setBodyHtml(comment.body_html || ''); }, [comment.body, comment.body_html, comment.id]); - // Reset reactions when comment changes - useEffect(() => { - if (comment.id) { - setReactions([]); - setReactionsError(null); - } - }, [comment.id]); - - // Fetch reactions if total_count > 0 - useEffect(() => { - if ( - comment.reactions?.total_count > 0 && - reactions.length === 0 && - !reactionsLoading - ) { - setReactionsLoading(true); - setReactionsError(null); - fetchCommentReactions(comment.id) - .then((data) => { - setReactions(data); - setReactionsLoading(false); - }) - .catch((err) => { - setReactionsError(err.message); - setReactionsLoading(false); - }); - } - }, [ - comment.reactions?.total_count, - comment.id, - reactions.length, - reactionsLoading, - ]); - const handleUpdateComment = async (nextBody) => { const updated = await updateComment(comment.id, nextBody); setBody(updated.body || ''); diff --git a/frontend/src/components/IssueCard.jsx b/frontend/src/components/IssueCard.jsx index 55dcf24..71daaad 100644 --- a/frontend/src/components/IssueCard.jsx +++ b/frontend/src/components/IssueCard.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor // Assisted-by: Cursor // Assisted-by: openai-code-assist @@ -63,6 +64,7 @@ import { } from '../services/api'; import CommentCard from './CommentCard'; import Reactions from './Reactions'; +import { useReactions } from '../utils/useReactions'; import UserAvatar from './UserAvatar'; import MarkdownInputModal from './MarkdownInputModal'; import LabelColorInput from './LabelColorInput'; @@ -166,9 +168,16 @@ const IssueCard = ({ const milestoneToggleRef = useRef(null); const menuRef = useRef(null); const toggleRef = useRef(null); - const [reactions, setReactions] = useState([]); - const [reactionsLoading, setReactionsLoading] = useState(false); - const [reactionsError, setReactionsError] = useState(null); + const { + data: reactions, + loading: reactionsLoading, + error: reactionsError, + } = useReactions( + issue.number, + issue.reactions?.total_count, + isDescriptionExpanded, + fetchIssueReactions + ); const [availableAssignees, setAvailableAssignees] = useState([]); const [isAssigneesMenuOpen, setIsAssigneesMenuOpen] = useState(false); const [assigneesLoading, setAssigneesLoading] = useState(false); @@ -325,44 +334,10 @@ const IssueCard = ({ }); }, [isCloseMenuOpen, closeActiveTab, closeComment]); - // Fetch reactions if total_count > 0 - useEffect(() => { - // Reset reactions when issue changes - if (issue.number) { - setReactions([]); - setReactionsError(null); - } - }, [issue.number]); - - useEffect(() => { - if ( - issue.reactions?.total_count > 0 && - reactions.length === 0 && - !reactionsLoading - ) { - setReactionsLoading(true); - setReactionsError(null); - fetchIssueReactions(issue.number) - .then((data) => { - setReactions(data); - setReactionsLoading(false); - }) - .catch((err) => { - setReactionsError(err.message); - setReactionsLoading(false); - }); - } - }, [ - issue.reactions?.total_count, - issue.number, - reactions.length, - reactionsLoading, - ]); - // Preload assignees when component mounts (shared cache) - non-blocking useEffect(() => { // If assignees are already cached, use them - if (assigneesCache.data.length > 0) { + if (assigneesCache.loaded) { setAvailableAssignees(assigneesCache.data); setAssigneesLoading(false); setAssigneesError(assigneesCache.error); @@ -415,7 +390,7 @@ const IssueCard = ({ useEffect(() => { if (isAssigneesMenuOpen) { // If assignees are already cached, use them immediately - if (assigneesCache.data.length > 0) { + if (assigneesCache.loaded) { setAvailableAssignees(assigneesCache.data); setAssigneesLoading(false); setAssigneesError(assigneesCache.error); @@ -436,7 +411,7 @@ const IssueCard = ({ setAssigneesLoading(false); setAssigneesError(assigneesCache.error); }); - } else if (!assigneesCache.loading && assigneesCache.data.length === 0) { + } else if (!assigneesCache.loading && !assigneesCache.loaded) { // Assignees haven't been fetched yet, start fetching now setAssigneesLoading(true); assigneesCache.loading = true; @@ -467,7 +442,7 @@ const IssueCard = ({ // useEffect runs after render, so this won't block the initial render useEffect(() => { // If labels are already cached, use them - if (labelsCache.data.length > 0) { + if (labelsCache.loaded) { setAvailableLabels(labelsCache.data); setLabelsLoading(false); setLabelsError(labelsCache.error); @@ -521,7 +496,7 @@ const IssueCard = ({ useEffect(() => { if (isLabelMenuOpen) { // If labels are already cached, use them immediately - if (labelsCache.data.length > 0) { + if (labelsCache.loaded) { setAvailableLabels(labelsCache.data); setLabelsLoading(false); setLabelsError(labelsCache.error); @@ -543,7 +518,7 @@ const IssueCard = ({ setLabelsLoading(false); setLabelsError(labelsCache.error); }); - } else if (!labelsCache.loading && labelsCache.data.length === 0) { + } else if (!labelsCache.loading && !labelsCache.loaded) { // Labels haven't been fetched yet, start fetching now setLabelsLoading(true); labelsCache.loading = true; @@ -719,13 +694,19 @@ const IssueCard = ({ onLabelsChange({ milestoneNumber }); }; + const commitLabels = (labels) => { + setCurrentLabels(labels); + onIssueUpdate?.({ id: issue.id, labels }); + notifyLabelsChanged(); + }; + const handleRemoveLabel = async (labelName) => { try { - await removeLabel(issue.number, labelName); - setCurrentLabels( - currentLabels.filter((label) => label.name !== labelName) + const result = await removeLabel(issue.number, labelName); + commitLabels( + result.labels ?? + currentLabels.filter((label) => label.name !== labelName) ); - notifyLabelsChanged(); } catch (err) { console.error('Failed to remove label:', err); setLabelsError(err.message); @@ -734,19 +715,19 @@ const IssueCard = ({ const handleToggleLabel = async (labelName, isChecked) => { try { - if (isChecked) { - await addLabel(issue.number, labelName); - const labelToAdd = availableLabels.find((l) => l.name === labelName); - if (labelToAdd) { - setCurrentLabels([...currentLabels, labelToAdd]); - } - } else { - await removeLabel(issue.number, labelName); - setCurrentLabels( - currentLabels.filter((label) => label.name !== labelName) - ); - } - notifyLabelsChanged(); + const result = await (isChecked + ? addLabel(issue.number, labelName) + : removeLabel(issue.number, labelName)); + const labelToAdd = availableLabels.find( + (label) => label.name === labelName + ); + const fallback = isChecked + ? [ + ...currentLabels.filter((label) => label.name !== labelName), + ...(labelToAdd ? [labelToAdd] : []), + ] + : currentLabels.filter((label) => label.name !== labelName); + commitLabels(result.labels ?? fallback); } catch (err) { console.error('Failed to toggle label:', err); setLabelsError(err.message); @@ -1552,6 +1533,11 @@ const IssueCard = ({ }); } catch (err) { console.error('Failed to adopt parent milestone:', err); + onAdoptParentMilestone?.({ + fromMilestoneNumber: currentMilestone?.number ?? milestoneNumber, + toMilestoneNumber: issue.external_parent?.milestone?.number, + error: err.message || String(err), + }); } }; diff --git a/frontend/src/components/IssueCard.test.jsx b/frontend/src/components/IssueCard.test.jsx index b2b57f3..3b12228 100644 --- a/frontend/src/components/IssueCard.test.jsx +++ b/frontend/src/components/IssueCard.test.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { render, screen, waitFor, act } from '@testing-library/react'; @@ -746,7 +747,10 @@ describe('IssueCard', () => { it('calls onLabelsChange when a label is removed', async () => { const user = userEvent.setup(); const onLabelsChange = vi.fn(); - api.removeLabel.mockResolvedValue({}); + const onIssueUpdate = vi.fn(); + api.removeLabel.mockResolvedValue({ + labels: [{ name: 'concurrently-added', color: 'ffffff' }], + }); const issueWithMilestone = { ...mockIssue, milestone: { number: 6, title: 'Current' }, @@ -759,6 +763,7 @@ describe('IssueCard', () => { @@ -772,6 +777,10 @@ describe('IssueCard', () => { await waitFor(() => { expect(api.removeLabel).toHaveBeenCalledWith(459, 'enhancement'); expect(onLabelsChange).toHaveBeenCalledWith({ milestoneNumber: 6 }); + expect(onIssueUpdate).toHaveBeenCalledWith({ + id: mockIssue.id, + labels: [{ name: 'concurrently-added', color: 'ffffff' }], + }); }); }); diff --git a/frontend/src/components/ManageLabels.jsx b/frontend/src/components/ManageLabels.jsx index 81abea7..a0278b3 100644 --- a/frontend/src/components/ManageLabels.jsx +++ b/frontend/src/components/ManageLabels.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor // Assisted-by: openai-code-assist import React, { useState, useEffect } from 'react'; @@ -44,7 +45,7 @@ const ManageLabels = ({ isOpen, onClose, onLabelChange }) => { const loadLabels = () => { // Use cached data if available - if (labelsCache.data.length > 0) { + if (labelsCache.loaded) { setLabels(labelsCache.data); setLoading(false); setError(labelsCache.error); @@ -90,7 +91,7 @@ const ManageLabels = ({ isOpen, onClose, onLabelChange }) => { const refreshLabelsInBackground = () => { // Refresh in background without showing loading state - fetchLabels() + fetchLabels({ revalidate: true }) .then((data) => { labelsCache.data = data; labelsCache.loading = false; diff --git a/frontend/src/components/ManageMilestones.jsx b/frontend/src/components/ManageMilestones.jsx index 23cbb85..2d5dbf2 100644 --- a/frontend/src/components/ManageMilestones.jsx +++ b/frontend/src/components/ManageMilestones.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import React, { useState, useEffect, useRef } from 'react'; import { @@ -51,7 +52,7 @@ const ManageMilestones = ({ isOpen, onClose, onMilestoneChange }) => { const loadMilestones = (state) => { // The shared cache is intentionally limited to open milestones because it // is also used by Planning and issue milestone selectors. - if (state === 'open' && milestonesCache.data.length > 0) { + if (state === 'open' && milestonesCache.loaded) { setMilestones(milestonesCache.data); setLoading(false); setError(milestonesCache.error); @@ -110,7 +111,7 @@ const ManageMilestones = ({ isOpen, onClose, onMilestoneChange }) => { // Assisted-by: openai-code-assist const refreshMilestonesInBackground = (state) => { // Refresh in background without showing loading state - fetchMilestones(state) + fetchMilestones(state, { revalidate: true }) .then((data) => { if (state === 'open') { milestonesCache.data = data; diff --git a/frontend/src/components/ManageSort.jsx b/frontend/src/components/ManageSort.jsx index 2d6817f..2a5e446 100644 --- a/frontend/src/components/ManageSort.jsx +++ b/frontend/src/components/ManageSort.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import React, { useState, useEffect, useRef } from 'react'; import { Modal, Button, Tooltip, Spinner, Alert } from '@patternfly/react-core'; @@ -20,7 +21,7 @@ const ManageSort = ({ isOpen, onClose, sortOrder, onSortChange }) => { const loadLabels = () => { // Use cached data if available - if (labelsCache.data.length > 0) { + if (labelsCache.loaded) { setAllLabels(labelsCache.data); setLoading(false); setError(labelsCache.error); diff --git a/frontend/src/components/MarkdownInputModal.jsx b/frontend/src/components/MarkdownInputModal.jsx index c28f546..9c400af 100644 --- a/frontend/src/components/MarkdownInputModal.jsx +++ b/frontend/src/components/MarkdownInputModal.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import React, { useState, useEffect, useRef } from 'react'; import { @@ -98,7 +99,7 @@ const MarkdownInputModal = ({ if (mode !== 'issue') return; // Load labels - if (labelsCache.data.length > 0) { + if (labelsCache.loaded) { setAvailableLabels(labelsCache.data); } else { setLabelsLoading(true); @@ -112,7 +113,7 @@ const MarkdownInputModal = ({ } // Load assignees - if (assigneesCache.data.length > 0) { + if (assigneesCache.loaded) { setAvailableAssignees(assigneesCache.data); } else { setAssigneesLoading(true); diff --git a/frontend/src/components/MilestoneCard.jsx b/frontend/src/components/MilestoneCard.jsx index 6c2172a..696dcf4 100644 --- a/frontend/src/components/MilestoneCard.jsx +++ b/frontend/src/components/MilestoneCard.jsx @@ -1,6 +1,13 @@ +// Assisted-by: Codex // Generated-by: Cursor // Assisted-by: Cursor -import React, { useState, useEffect, useRef, useCallback } from 'react'; +import React, { + useState, + useEffect, + useRef, + useCallback, + useMemo, +} from 'react'; import { Card, CardHeader, @@ -23,6 +30,7 @@ import MarkdownInputModal from './MarkdownInputModal'; import { usePlanningDnD } from './PlanningDnDContext'; import { flattenVisibleIssues, + sortIssueForest, collectSubtreeNumbers, removeIssueFromForest, removeClosedIssueFromForest, @@ -81,6 +89,13 @@ const MilestoneCard = ({ const prevMilestoneNumberRef = useRef(milestone.number); const lastAppliedHierarchyKeyRef = useRef(null); const dnd = usePlanningDnD(); + const loadRevision = useRef(0); + useEffect( + () => () => { + loadRevision.current += 1; + }, + [] + ); const openMilestoneEditor = () => { setEditMilestone({ @@ -126,19 +141,25 @@ const MilestoneCard = ({ setPullRequests(data.pull_requests || []); }, []); - const refetchIssues = useCallback(() => { - setLoading(true); - setError(null); - return fetchIssues(milestone.number, sortOrder) - .then((data) => { - applyFetchedData(data); - setLoading(false); - }) - .catch((err) => { - setError(err.message); - setLoading(false); - }); - }, [milestone.number, sortOrder, applyFetchedData]); + const refetchIssues = useCallback( + (force = false) => { + const revision = ++loadRevision.current; + setLoading(true); + setError(null); + return fetchIssues(milestone.number, [], { force }) + .then((data) => { + if (revision !== loadRevision.current) return; + applyFetchedData(data); + setLoading(false); + }) + .catch((err) => { + if (revision !== loadRevision.current) return; + setError(err.message); + setLoading(false); + }); + }, + [milestone.number, applyFetchedData] + ); // Reset loaded state when milestone changes useEffect(() => { @@ -159,13 +180,16 @@ const MilestoneCard = ({ if (isIssuesExpanded && !hasLoadedOnce && !loading) { setLoading(true); setError(null); - fetchIssues(milestone.number, sortOrder) + const revision = ++loadRevision.current; + fetchIssues(milestone.number) .then((data) => { + if (revision !== loadRevision.current) return; applyFetchedData(data); setLoading(false); setHasLoadedOnce(true); }) .catch((err) => { + if (revision !== loadRevision.current) return; setError(err.message); setLoading(false); setHasLoadedOnce(true); @@ -180,26 +204,21 @@ const MilestoneCard = ({ applyFetchedData, ]); - // Re-fetch issues when sort order changes (if already loaded) - const prevSortOrderRef = useRef(sortOrder); - useEffect(() => { - const sortOrderChanged = - JSON.stringify(prevSortOrderRef.current) !== JSON.stringify(sortOrder); - if (isIssuesExpanded && hasLoadedOnce && !loading && sortOrderChanged) { - prevSortOrderRef.current = sortOrder; - refetchIssues(); - } else { - prevSortOrderRef.current = sortOrder; - } - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [sortOrder]); + const sortedIssues = useMemo( + () => sortIssueForest(issues, sortOrder), + [issues, sortOrder] + ); + const sortedPrs = useMemo( + () => sortIssueForest(pullRequests, sortOrder), + [pullRequests, sortOrder] + ); useEffect(() => { // Loaded cards own the visible issue data; unopened cards fetch current // data when expanded instead of doing work that cannot be displayed yet. if (issueMilestoneRefresh === 0) return; if (!hasLoadedOnce) return; - refetchIssues(); + refetchIssues(true); // eslint-disable-next-line react-hooks/exhaustive-deps }, [issueMilestoneRefresh]); @@ -219,6 +238,15 @@ const MilestoneCard = ({ issueSnapshot, } = hierarchyAction; + if (type === 'error' || type === 'adopt') { + if ( + sourceMilestoneNumber === milestone.number || + targetMilestoneNumber === milestone.number + ) + refetchIssues(true); + return; + } + if (type === 'unlink') { if (sourceMilestoneNumber !== milestone.number) return; setIssues((prev) => { @@ -393,7 +421,7 @@ const MilestoneCard = ({ }; const renderIssueTable = () => { - const visible = flattenVisibleIssues(issues, expandedHierarchy); + const visible = flattenVisibleIssues(sortedIssues, expandedHierarchy); return ( { dnd?.finishDrag(); }} + onAdoptParentMilestone={({ + fromMilestoneNumber, + toMilestoneNumber, + error, + }) => { + const action = { + type: error ? 'error' : 'adopt', + message: error, + sourceMilestoneNumber: fromMilestoneNumber, + targetMilestoneNumber: toMilestoneNumber, + }; + if (dnd) dnd.notifyHierarchyChanged(action); + else refetchIssues(true); + }} onIssueUpdate={updateItemInForest(setIssues)} onIssueCreated={handleIssueCreated} onIssueClosed={handleIssueClosed} @@ -454,7 +496,7 @@ const MilestoneCard = ({ > {itemTableHeader(false)} - {pullRequests.map((issue) => ( + {sortedPrs.map((issue) => ( { assigneesCache.promise = null; }); + it('changes sort order locally without fetching the milestone again', async () => { + const user = userEvent.setup(); + api.fetchIssues.mockResolvedValue({ + issues: [mockIssue], + pull_requests: [], + }); + const { rerender } = render( + + ); + await user.click(screen.getByRole('button', { name: /show issues/i })); + await screen.findByText(/Test Issue/); + rerender(); + expect(screen.getByText(/Test Issue/)).toBeInTheDocument(); + expect(api.fetchIssues).toHaveBeenCalledTimes(1); + }); + + it('reloads authoritative data after a partially failed hierarchy edit', async () => { + const user = userEvent.setup(); + api.fetchIssues.mockResolvedValue({ + issues: [mockIssue], + pull_requests: [], + }); + const { rerender } = render(); + await user.click(screen.getByRole('button', { name: /show issues/i })); + await screen.findByText(/Test Issue/); + api.fetchIssues.mockResolvedValue({ issues: [], pull_requests: [] }); + rerender( + + ); + await waitFor(() => expect(api.fetchIssues).toHaveBeenCalledTimes(2)); + expect(api.fetchIssues).toHaveBeenLastCalledWith(6, [], { force: true }); + await waitFor(() => + expect(screen.queryByText(/Test Issue/)).not.toBeInTheDocument() + ); + }); + it('renders milestone title', async () => { await act(async () => { render(); @@ -124,7 +169,7 @@ describe('MilestoneCard', () => { await user.click(expandButton); await waitFor(() => { - expect(api.fetchIssues).toHaveBeenCalledWith(6, []); + expect(api.fetchIssues).toHaveBeenCalledWith(6); }); await waitFor( @@ -275,7 +320,7 @@ describe('MilestoneCard', () => { await waitFor(() => { expect(api.fetchIssues).toHaveBeenCalledTimes(2); }); - expect(api.fetchIssues).toHaveBeenLastCalledWith(6, []); + expect(api.fetchIssues).toHaveBeenLastCalledWith(6, [], { force: true }); }); it('refetches loaded milestones even when issues section is collapsed', async () => { diff --git a/frontend/src/components/PlanningDnDContext.jsx b/frontend/src/components/PlanningDnDContext.jsx index dd27ed8..c08b590 100644 --- a/frontend/src/components/PlanningDnDContext.jsx +++ b/frontend/src/components/PlanningDnDContext.jsx @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import React, { createContext, @@ -217,6 +218,11 @@ export const PlanningDnDProvider = ({ } }, []); + const notifyHierarchyChanged = useCallback((action) => { + if (action.type === 'error') setError(action.message); + onHierarchyChangedRef.current?.(action); + }, []); + const value = useMemo( () => ({ planningBoundsRef, @@ -225,6 +231,7 @@ export const PlanningDnDProvider = ({ isUnlinkZone, error, clearError: () => setError(null), + notifyHierarchyChanged, beginDrag, pointerOverParent, pointerOverPlanning, @@ -241,6 +248,7 @@ export const PlanningDnDProvider = ({ pointerOverPlanning, finishDrag, setHoverParent, + notifyHierarchyChanged, ] ); diff --git a/frontend/src/components/ProjectStatusPanel.jsx b/frontend/src/components/ProjectStatusPanel.jsx index 204e008..6f2d2ee 100644 --- a/frontend/src/components/ProjectStatusPanel.jsx +++ b/frontend/src/components/ProjectStatusPanel.jsx @@ -1,6 +1,13 @@ +// Assisted-by: Codex // Generated-by: Cursor // Assisted-by: openai-code-assist -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { + useCallback, + useEffect, + useMemo, + useRef, + useState, +} from 'react'; import { Alert, Button, @@ -167,7 +174,8 @@ const StatusSection = ({ ); }; -const ProjectStatusPanel = () => { +const ProjectStatusPanel = ({ refreshKey = 0 }) => { + const requestId = useRef(0); const initial = useMemo(() => getInitialDateRange(), []); const [startDate, setStartDate] = useState(null); @@ -180,16 +188,18 @@ const ProjectStatusPanel = () => { const [report, setReport] = useState(null); const load = useCallback(async (startIso, endIso) => { + const id = ++requestId.current; setLoading(true); setError(null); try { const data = await fetchProjectStatusReport(startIso, endIso); - setReport(data); + if (id === requestId.current) setReport(data); } catch (e) { + if (id !== requestId.current) return; setReport(null); setError(e?.message || 'Failed to load report'); } finally { - setLoading(false); + if (id === requestId.current) setLoading(false); } }, []); @@ -198,7 +208,10 @@ const ProjectStatusPanel = () => { return; } load(startDate, endDate); - }, [startDate, endDate, applyRevision, load]); + return () => { + requestId.current += 1; + }; + }, [startDate, endDate, applyRevision, refreshKey, load]); useEffect(() => { if (!startDate || !endDate) { diff --git a/frontend/src/components/SdlcKpisPanel.jsx b/frontend/src/components/SdlcKpisPanel.jsx index ab5a5f9..1b69146 100644 --- a/frontend/src/components/SdlcKpisPanel.jsx +++ b/frontend/src/components/SdlcKpisPanel.jsx @@ -1,5 +1,6 @@ +// Assisted-by: Codex // Generated-by: Cursor -import React, { useState, useCallback, useEffect } from 'react'; +import React, { useState, useCallback, useEffect, useRef } from 'react'; import { Card, CardHeader, @@ -13,11 +14,7 @@ import { TextInput, Tooltip, } from '@patternfly/react-core'; -import { - fetchSdlcDelivery, - fetchEscapedDefectRate, - fetchBugBacklogDelta, -} from '../services/api'; +import { fetchSdlcDashboard } from '../services/api'; /** Format a duration as whole days and hours only (no minutes/seconds). */ export const formatDaysAndHours = (sec) => { @@ -251,7 +248,8 @@ const readWeeksFromStorage = () => { return DEFAULT_WEEKS; }; -const SdlcKpisPanel = () => { +const SdlcKpisPanel = ({ refreshKey = 0 }) => { + const requestId = useRef(0); const [weeksInput, setWeeksInput] = useState(() => String(readWeeksFromStorage()) ); @@ -263,27 +261,29 @@ const SdlcKpisPanel = () => { const [backlog, setBacklog] = useState(null); const load = useCallback(() => { + const id = ++requestId.current; setLoading(true); setError(null); - return Promise.all([ - fetchSdlcDelivery(weeks, 7), - fetchEscapedDefectRate(weeks, 7), - fetchBugBacklogDelta(weeks, 7), - ]) - .then(([d, e, b]) => { + return fetchSdlcDashboard(weeks, 7, { force: refreshKey > 0 }) + .then(({ delivery: d, escaped: e, backlog: b }) => { + if (id !== requestId.current) return; setDelivery(d); setEscaped(e); setBacklog(b); setLoading(false); }) .catch((err) => { + if (id !== requestId.current) return; setError(err.message || String(err)); setLoading(false); }); - }, [weeks]); + }, [weeks, refreshKey]); useEffect(() => { load(); + return () => { + requestId.current += 1; + }; }, [load]); useEffect(() => { diff --git a/frontend/src/components/SdlcKpisPanel.test.jsx b/frontend/src/components/SdlcKpisPanel.test.jsx index 89d6ed3..dd51217 100644 --- a/frontend/src/components/SdlcKpisPanel.test.jsx +++ b/frontend/src/components/SdlcKpisPanel.test.jsx @@ -1,21 +1,13 @@ +// Assisted-by: Codex // Generated-by: Cursor import React from 'react'; import { describe, it, expect, vi, beforeEach } from 'vitest'; -import { render, screen, waitFor } from '@testing-library/react'; +import { render, screen, waitFor, act } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import SdlcKpisPanel, { formatDaysAndHours } from './SdlcKpisPanel'; -vi.mock('../services/api', () => ({ - fetchSdlcDelivery: vi.fn(), - fetchEscapedDefectRate: vi.fn(), - fetchBugBacklogDelta: vi.fn(), -})); - -import { - fetchSdlcDelivery, - fetchEscapedDefectRate, - fetchBugBacklogDelta, -} from '../services/api'; +vi.mock('../services/api', () => ({ fetchSdlcDashboard: vi.fn() })); +import { fetchSdlcDashboard } from '../services/api'; describe('formatDaysAndHours', () => { it('formats days and hours', () => { @@ -70,11 +62,11 @@ describe('SdlcKpisPanel', () => { const slices = Array.from({ length: sliceCount }, () => ({ ...deliverySlice, })); - fetchSdlcDelivery.mockResolvedValue({ + const delivery = { weeks: sliceCount, week_days: 7, slices, - }); + }; const escapeSlice = { window_start: '2025-04-03T12:00:00Z', window_end: '2025-04-10T12:00:00Z', @@ -100,16 +92,17 @@ describe('SdlcKpisPanel', () => { bugs_closed: 0, net: 1, }; - fetchEscapedDefectRate.mockResolvedValue({ + const escaped = { weeks: sliceCount, week_days: 7, slices: Array.from({ length: sliceCount }, () => ({ ...escapeSlice })), - }); - fetchBugBacklogDelta.mockResolvedValue({ + }; + const backlog = { weeks: sliceCount, week_days: 7, slices: Array.from({ length: sliceCount }, () => ({ ...backlogSlice })), - }); + }; + fetchSdlcDashboard.mockResolvedValue({ delivery, escaped, backlog }); }; it('renders title and loads metrics on mount', async () => { @@ -119,9 +112,8 @@ describe('SdlcKpisPanel', () => { expect(screen.getByText('SDLC KPIs')).toBeInTheDocument(); await waitFor(() => { - expect(fetchSdlcDelivery).toHaveBeenCalledWith(4, 7); - expect(fetchEscapedDefectRate).toHaveBeenCalledWith(4, 7); - expect(fetchBugBacklogDelta).toHaveBeenCalledWith(4, 7); + expect(fetchSdlcDashboard).toHaveBeenCalledWith(4, 7, { force: false }); + expect(fetchSdlcDashboard).toHaveBeenCalledTimes(1); }); await waitFor(() => { @@ -129,6 +121,25 @@ describe('SdlcKpisPanel', () => { }); }); + it('ignores older dashboard responses and reloads on explicit Refresh', async () => { + let rejectOld; + fetchSdlcDashboard.mockReturnValueOnce( + new Promise((_, reject) => { + rejectOld = reject; + }) + ); + makeSeriesMocks(4); + const { rerender } = render(); + rerender(); + await screen.findByText(/Merged PR throughput/i); + expect(fetchSdlcDashboard).toHaveBeenLastCalledWith(4, 7, { force: true }); + await act(async () => { + rejectOld(new Error('obsolete failure')); + }); + expect(screen.queryByText(/obsolete failure/)).not.toBeInTheDocument(); + expect(screen.getByText(/Merged PR throughput/i)).toBeInTheDocument(); + }); + it('loads week count from localStorage', async () => { localStorage.setItem('pmStatsSdlcWeeks', '8'); makeSeriesMocks(8); @@ -136,9 +147,8 @@ describe('SdlcKpisPanel', () => { render(); await waitFor(() => { - expect(fetchSdlcDelivery).toHaveBeenCalledWith(8, 7); - expect(fetchEscapedDefectRate).toHaveBeenCalledWith(8, 7); - expect(fetchBugBacklogDelta).toHaveBeenCalledWith(8, 7); + expect(fetchSdlcDashboard).toHaveBeenCalledWith(8, 7, { force: false }); + expect(fetchSdlcDashboard).toHaveBeenCalledTimes(1); }); }); @@ -160,6 +170,6 @@ describe('SdlcKpisPanel', () => { await waitFor(() => { expect(localStorage.getItem('pmStatsSdlcWeeks')).toBe('6'); }); - expect(fetchSdlcDelivery).toHaveBeenLastCalledWith(6, 7); + expect(fetchSdlcDashboard).toHaveBeenLastCalledWith(6, 7, { force: false }); }); }); diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 4429423..2968fd3 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -1,6 +1,31 @@ +// Assisted-by: Codex // Generated-by: Cursor +import { + cachedQuery, + invalidateQueries, + setQueryRepository, +} from '../utils/queryCache'; const API_BASE = '/api/v1'; +// Generated-by: Codex +const apiFetch = async (url, options) => { + const mutation = + options?.method && options.method !== 'GET' && !url.endsWith('/markdown'); + if (mutation) invalidateQueries(); + try { + return await (options ? fetch(url, options) : fetch(url)); + } finally { + // A timeout can follow an accepted write, so invalidate on failure too. + if (mutation) invalidateQueries(); + } +}; + +const readJson = async (path, message) => { + const response = await apiFetch(`${API_BASE}${path}`); + await raiseForResponse(response, message); + return response.json(); +}; + const readErrorDetail = async (response) => { try { const body = await response.json(); @@ -27,18 +52,18 @@ const raiseForResponse = async (response, fallback) => { throw new Error(`${fallback}: ${detail}`); }; -export const fetchMilestones = async (state = 'open') => { - const query = state === 'open' ? '' : `?state=${encodeURIComponent(state)}`; - const response = await fetch(`${API_BASE}/milestones${query}`); - if (!response.ok) { - throw new Error(`Failed to fetch milestones: ${response.statusText}`); - } - return response.json(); +export const fetchMilestones = (state = 'open', options = {}) => { + const path = `/milestones${state === 'open' ? '' : `?state=${encodeURIComponent(state)}`}`; + return cachedQuery( + path, + () => readJson(path, 'Failed to fetch milestones'), + options + ); }; // Assisted-by: openai-code-assist export const fetchMilestoneOpenCounts = async (milestoneNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/milestones/${milestoneNumber}/open-counts` ); if (!response.ok) { @@ -49,29 +74,23 @@ export const fetchMilestoneOpenCounts = async (milestoneNumber) => { return response.json(); }; -export const fetchIssues = async (milestoneNumber, sortOrder = []) => { - let url = `${API_BASE}/issues/${milestoneNumber}`; - if (sortOrder && sortOrder.length > 0) { - const sortParam = sortOrder.join(','); - url += `?sort=${encodeURIComponent(sortParam)}`; - } - const response = await fetch(url); - if (!response.ok) { - throw new Error(`Failed to fetch issues: ${response.statusText}`); - } - return response.json(); +export const fetchIssues = (milestoneNumber, _sortOrder = [], options = {}) => { + const path = `/issues/${milestoneNumber}`; + return cachedQuery(path, () => readJson(path, 'Failed to fetch issues'), { + ttl: 15000, + ...options, + }); }; -export const fetchComments = async (issueNumber) => { - const response = await fetch(`${API_BASE}/comments/${issueNumber}`); - if (!response.ok) { - throw new Error(`Failed to fetch comments: ${response.statusText}`); - } - return response.json(); -}; +export const fetchComments = (issueNumber) => + cachedQuery( + `/comments/${issueNumber}`, + () => readJson(`/comments/${issueNumber}`, 'Failed to fetch comments'), + { ttl: 15000 } + ); export const createComment = async (issueNumber, body) => { - const response = await fetch(`${API_BASE}/comments/${issueNumber}`, { + const response = await apiFetch(`${API_BASE}/comments/${issueNumber}`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -85,7 +104,7 @@ export const createComment = async (issueNumber, body) => { }; export const updateComment = async (commentId, body) => { - const response = await fetch(`${API_BASE}/comments/${commentId}/body`, { + const response = await apiFetch(`${API_BASE}/comments/${commentId}/body`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -99,7 +118,7 @@ export const updateComment = async (commentId, body) => { }; export const updateIssueBody = async (issueNumber, body) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/body`, { + const response = await apiFetch(`${API_BASE}/issues/${issueNumber}/body`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -113,7 +132,7 @@ export const updateIssueBody = async (issueNumber, body) => { }; export const updateIssueTitle = async (issueNumber, title) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/title`, { + const response = await apiFetch(`${API_BASE}/issues/${issueNumber}/title`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -128,7 +147,7 @@ export const updateIssueTitle = async (issueNumber, title) => { // Assisted-by: openai-code-assist export const updateIssueType = async (issueNumber, type) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/type`, { + const response = await apiFetch(`${API_BASE}/issues/${issueNumber}/type`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -142,7 +161,7 @@ export const updateIssueType = async (issueNumber, type) => { }; export const closeIssue = async (issueNumber, { reason, body }) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/close-with-comment`, { method: 'POST', @@ -159,21 +178,26 @@ export const closeIssue = async (issueNumber, { reason, body }) => { }; export const renderMarkdown = async (text) => { - const response = await fetch(`${API_BASE}/markdown`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', + return cachedQuery( + `markdown:${text}`, + async () => { + const response = await apiFetch(`${API_BASE}/markdown`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ text }), + }); + if (!response.ok) { + throw new Error(`Failed to render markdown: ${response.statusText}`); + } + return response.json(); }, - body: JSON.stringify({ text }), - }); - if (!response.ok) { - throw new Error(`Failed to render markdown: ${response.statusText}`); - } - return response.json(); + { ttl: 300000 } + ); }; - export const createIssue = async (issueData) => { - const response = await fetch(`${API_BASE}/issues`, { + const response = await apiFetch(`${API_BASE}/issues`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -187,24 +211,21 @@ export const createIssue = async (issueData) => { }; export const fetchProject = async () => { - const response = await fetch(`${API_BASE}/project`); - if (!response.ok) { - throw new Error(`Failed to fetch project: ${response.statusText}`); - } - return response.json(); + const data = await readJson('/project', 'Failed to fetch project'); + setQueryRepository(data.github_repo); + return data; }; -export const fetchLabels = async () => { - const response = await fetch(`${API_BASE}/labels`); - if (!response.ok) { - throw new Error(`Failed to fetch labels: ${response.statusText}`); - } - return response.json(); -}; +export const fetchLabels = (options = {}) => + cachedQuery( + '/labels', + () => readJson('/labels', 'Failed to fetch labels'), + options + ); export const addLabel = async (issueNumber, labelName) => { - const response = await fetch( - `${API_BASE}/issues/${issueNumber}/labels/${labelName}`, + const response = await apiFetch( + `${API_BASE}/issues/${issueNumber}/labels/${encodeURIComponent(labelName)}`, { method: 'POST', } @@ -216,8 +237,8 @@ export const addLabel = async (issueNumber, labelName) => { }; export const removeLabel = async (issueNumber, labelName) => { - const response = await fetch( - `${API_BASE}/issues/${issueNumber}/labels/${labelName}`, + const response = await apiFetch( + `${API_BASE}/issues/${issueNumber}/labels/${encodeURIComponent(labelName)}`, { method: 'DELETE', } @@ -229,7 +250,7 @@ export const removeLabel = async (issueNumber, labelName) => { }; export const createMilestone = async (milestoneData) => { - const response = await fetch(`${API_BASE}/milestones`, { + const response = await apiFetch(`${API_BASE}/milestones`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -244,7 +265,7 @@ export const createMilestone = async (milestoneData) => { // Assisted-by: openai-code-assist export const updateMilestone = async (milestoneNumber, milestoneData) => { - const response = await fetch(`${API_BASE}/milestones/${milestoneNumber}`, { + const response = await apiFetch(`${API_BASE}/milestones/${milestoneNumber}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json', @@ -259,7 +280,7 @@ export const updateMilestone = async (milestoneNumber, milestoneData) => { // Assisted-by: openai-code-assist export const updateMilestoneState = async (milestoneNumber, state) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/milestones/${milestoneNumber}/state`, { method: 'PATCH', @@ -276,7 +297,7 @@ export const updateMilestoneState = async (milestoneNumber, state) => { }; export const deleteMilestone = async (milestoneNumber) => { - const response = await fetch(`${API_BASE}/milestones/${milestoneNumber}`, { + const response = await apiFetch(`${API_BASE}/milestones/${milestoneNumber}`, { method: 'DELETE', }); if (!response.ok) { @@ -286,7 +307,7 @@ export const deleteMilestone = async (milestoneNumber) => { }; export const createLabel = async (labelData) => { - const response = await fetch(`${API_BASE}/labels`, { + const response = await apiFetch(`${API_BASE}/labels`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -301,13 +322,16 @@ export const createLabel = async (labelData) => { // Assisted-by: openai-code-assist export const updateLabel = async (labelName, labelData) => { - const response = await fetch(`${API_BASE}/labels/${labelName}`, { - method: 'PATCH', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(labelData), - }); + const response = await apiFetch( + `${API_BASE}/labels/${encodeURIComponent(labelName)}`, + { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(labelData), + } + ); if (!response.ok) { throw new Error(`Failed to update label: ${response.statusText}`); } @@ -315,9 +339,12 @@ export const updateLabel = async (labelName, labelData) => { }; export const deleteLabel = async (labelName) => { - const response = await fetch(`${API_BASE}/labels/${labelName}`, { - method: 'DELETE', - }); + const response = await apiFetch( + `${API_BASE}/labels/${encodeURIComponent(labelName)}`, + { + method: 'DELETE', + } + ); if (!response.ok) { throw new Error(`Failed to delete label: ${response.statusText}`); } @@ -325,7 +352,7 @@ export const deleteLabel = async (labelName) => { }; export const setIssueMilestone = async (issueNumber, milestoneNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/milestone/${milestoneNumber}`, { method: 'POST', @@ -338,7 +365,7 @@ export const setIssueMilestone = async (issueNumber, milestoneNumber) => { }; export const removeIssueMilestone = async (issueNumber, milestoneNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/milestone/${milestoneNumber}`, { method: 'DELETE', @@ -351,7 +378,7 @@ export const removeIssueMilestone = async (issueNumber, milestoneNumber) => { }; export const setIssueParent = async (issueNumber, parentNumber) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/parent`, { + const response = await apiFetch(`${API_BASE}/issues/${issueNumber}/parent`, { method: 'PUT', headers: { 'Content-Type': 'application/json', @@ -365,7 +392,7 @@ export const setIssueParent = async (issueNumber, parentNumber) => { }; export const clearIssueParent = async (issueNumber) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/parent`, { + const response = await apiFetch(`${API_BASE}/issues/${issueNumber}/parent`, { method: 'DELETE', }); if (!response.ok) { @@ -375,7 +402,7 @@ export const clearIssueParent = async (issueNumber) => { }; export const adoptParentMilestone = async (issueNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/adopt-parent-milestone`, { method: 'POST', @@ -388,7 +415,7 @@ export const adoptParentMilestone = async (issueNumber) => { }; export const addBlockedBy = async (issueNumber, relatedIssueNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/dependencies/blocked_by`, { method: 'POST', @@ -403,7 +430,7 @@ export const addBlockedBy = async (issueNumber, relatedIssueNumber) => { }; export const removeBlockedBy = async (issueNumber, blockingIssueNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/dependencies/blocked_by/${blockingIssueNumber}`, { method: 'DELETE', @@ -414,7 +441,7 @@ export const removeBlockedBy = async (issueNumber, blockingIssueNumber) => { }; export const addBlocking = async (issueNumber, relatedIssueNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/dependencies/blocking`, { method: 'POST', @@ -429,7 +456,7 @@ export const addBlocking = async (issueNumber, relatedIssueNumber) => { }; export const removeBlocking = async (issueNumber, blockedIssueNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/dependencies/blocking/${blockedIssueNumber}`, { method: 'DELETE', @@ -441,7 +468,7 @@ export const removeBlocking = async (issueNumber, blockedIssueNumber) => { // Assisted-by: openai-code-assist export const addClosedBy = async (issueNumber, pullRequestNumber) => { - const response = await fetch( + const response = await apiFetch( `${API_BASE}/issues/${issueNumber}/close-references`, { method: 'POST', @@ -455,40 +482,46 @@ export const addClosedBy = async (issueNumber, pullRequestNumber) => { return response.json(); }; -export const fetchIssueReactions = async (issueNumber) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/reactions`); - if (!response.ok) { - throw new Error(`Failed to fetch issue reactions: ${response.statusText}`); - } - return response.json(); -}; +export const fetchIssueReactions = (issueNumber) => + cachedQuery( + `/issues/${issueNumber}/reactions`, + () => + readJson( + `/issues/${issueNumber}/reactions`, + 'Failed to fetch issuereactions' + ), + { ttl: 15000 } + ); -export const fetchCommentReactions = async (commentId) => { - const response = await fetch(`${API_BASE}/comments/${commentId}/reactions`); - if (!response.ok) { - throw new Error( - `Failed to fetch comment reactions: ${response.statusText}` - ); - } - return response.json(); -}; +export const fetchCommentReactions = (commentId) => + cachedQuery( + `/comments/${commentId}/reactions`, + () => + readJson( + `/comments/${commentId}/reactions`, + 'Failed to fetch commentreactions' + ), + { ttl: 15000 } + ); -export const fetchAssignees = async () => { - const response = await fetch(`${API_BASE}/assignees`); - if (!response.ok) { - throw new Error(`Failed to fetch assignees: ${response.statusText}`); - } - return response.json(); -}; +export const fetchAssignees = (options = {}) => + cachedQuery( + '/assignees', + () => readJson('/assignees', 'Failed to fetch assignees'), + options + ); export const setIssueAssignees = async (issueNumber, assignees) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/assignees`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(assignees), - }); + const response = await apiFetch( + `${API_BASE}/issues/${issueNumber}/assignees`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(assignees), + } + ); if (!response.ok) { throw new Error(`Failed to set assignees: ${response.statusText}`); } @@ -496,13 +529,16 @@ export const setIssueAssignees = async (issueNumber, assignees) => { }; export const removeIssueAssignees = async (issueNumber, assignees) => { - const response = await fetch(`${API_BASE}/issues/${issueNumber}/assignees`, { - method: 'DELETE', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(assignees), - }); + const response = await apiFetch( + `${API_BASE}/issues/${issueNumber}/assignees`, + { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(assignees), + } + ); if (!response.ok) { throw new Error(`Failed to remove assignees: ${response.statusText}`); } @@ -514,7 +550,7 @@ export const fetchSdlcDelivery = async (weeks = 4, weekDays = 7) => { weeks: String(weeks), week_days: String(weekDays), }); - const response = await fetch(`${API_BASE}/sdlc/delivery?${params}`); + const response = await apiFetch(`${API_BASE}/sdlc/delivery?${params}`); if (!response.ok) { throw new Error( `Failed to fetch SDLC delivery metrics: ${response.statusText}` @@ -528,7 +564,7 @@ export const fetchEscapedDefectRate = async (weeks = 4, weekDays = 7) => { weeks: String(weeks), week_days: String(weekDays), }); - const response = await fetch( + const response = await apiFetch( `${API_BASE}/sdlc/escaped-defect-rate?${params}` ); if (!response.ok) { @@ -544,7 +580,9 @@ export const fetchBugBacklogDelta = async (weeks = 4, weekDays = 7) => { weeks: String(weeks), week_days: String(weekDays), }); - const response = await fetch(`${API_BASE}/sdlc/bug-backlog-delta?${params}`); + const response = await apiFetch( + `${API_BASE}/sdlc/bug-backlog-delta?${params}` + ); if (!response.ok) { throw new Error( `Failed to fetch bug backlog delta: ${response.statusText}` @@ -569,7 +607,7 @@ export const fetchProjectStatusReport = async (startDateISO, endDateISO) => { const url = qs ? `${API_BASE}/project-status?${qs}` : `${API_BASE}/project-status`; - const response = await fetch(url); + const response = await apiFetch(url); if (!response.ok) { throw new Error( `Failed to fetch project status report: ${response.statusText}` @@ -577,3 +615,13 @@ export const fetchProjectStatusReport = async (startDateISO, endDateISO) => { } return response.json(); }; + +// Generated-by: Codex +export const fetchSdlcDashboard = (weeks = 4, weekDays = 7, options = {}) => { + const path = `/sdlc/dashboard?${new URLSearchParams({ weeks: String(weeks), week_days: String(weekDays) })}`; + return cachedQuery( + path, + () => readJson(path, 'Failed to fetch SDLC metrics'), + { ttl: 15000, ...options } + ); +}; diff --git a/frontend/src/services/api.test.js b/frontend/src/services/api.test.js index 593fba8..c668fc9 100644 --- a/frontend/src/services/api.test.js +++ b/frontend/src/services/api.test.js @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import { describe, it, expect, vi, beforeEach } from 'vitest'; import { @@ -35,6 +36,32 @@ describe('api', () => { global.fetch = vi.fn(); }); + it('caches identical Markdown previews without invalidating other reads', async () => { + global.fetch.mockResolvedValue({ + ok: true, + json: async () => ({ html: '

Preview

' }), + }); + await fetchIssues(1); + await renderMarkdown('Preview'); + await renderMarkdown('Preview'); + await fetchIssues(1, ['bug']); + expect(global.fetch).toHaveBeenCalledTimes(2); + }); + + it('invalidates cached reads even when a mutation times out', async () => { + global.fetch.mockResolvedValue({ + ok: true, + json: async () => ({ issues: [], pull_requests: [] }), + }); + await fetchIssues(1); + global.fetch.mockRejectedValueOnce(new Error('ambiguous write')); + await expect(updateIssueTitle(1, 'updated')).rejects.toThrow( + 'ambiguous write' + ); + await fetchIssues(1); + expect(global.fetch).toHaveBeenCalledTimes(3); + }); + describe('fetchMilestones', () => { it('fetches milestones successfully', async () => { const mockMilestones = [ diff --git a/frontend/src/test/setup.js b/frontend/src/test/setup.js index 5b2fa9b..f661492 100644 --- a/frontend/src/test/setup.js +++ b/frontend/src/test/setup.js @@ -1,8 +1,31 @@ // Generated-by: Cursor -import { expect, afterEach } from 'vitest'; +import { expect, afterEach, beforeEach } from 'vitest'; import { cleanup } from '@testing-library/react'; import '@testing-library/jest-dom'; +import { invalidateQueries } from '../utils/queryCache'; +beforeEach(() => invalidateQueries()); afterEach(() => { cleanup(); }); + +// Node 27 may expose an unconfigured global Web Storage object in jsdom. +// Use an isolated Web Storage fallback when jsdom cannot supply one. +// Assisted-by: Codex +if (!globalThis.localStorage) { + const values = new Map(); + Object.defineProperty(globalThis, 'localStorage', { + configurable: true, + writable: true, + value: { + getItem: (key) => values.get(String(key)) ?? null, + setItem: (key, value) => values.set(String(key), String(value)), + removeItem: (key) => values.delete(String(key)), + clear: () => values.clear(), + key: (index) => [...values.keys()][index] ?? null, + get length() { + return values.size; + }, + }, + }); +} diff --git a/frontend/src/utils/assigneesCache.js b/frontend/src/utils/assigneesCache.js index 3778a66..ca7836e 100644 --- a/frontend/src/utils/assigneesCache.js +++ b/frontend/src/utils/assigneesCache.js @@ -1,19 +1,9 @@ -// Generated-by: Cursor -// Shared assignees cache - loaded once and shared across all components -let assigneesCache = { - data: [], - loading: false, - error: null, - promise: null, -}; - +// Generated-by: Codex +import { createMetadataCache, invalidateQueries } from './queryCache'; +const assigneesCache = createMetadataCache(); export const getAssigneesCache = () => assigneesCache; - export const clearAssigneesCache = () => { - assigneesCache.data = []; - assigneesCache.loading = false; - assigneesCache.error = null; - assigneesCache.promise = null; + assigneesCache.clear(); + invalidateQueries('/assignees'); }; - export default assigneesCache; diff --git a/frontend/src/utils/issueHierarchy.js b/frontend/src/utils/issueHierarchy.js index be51719..08c4189 100644 --- a/frontend/src/utils/issueHierarchy.js +++ b/frontend/src/utils/issueHierarchy.js @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor /** * Flatten a nested issue forest into visible rows based on hierarchy expand state. @@ -141,3 +142,23 @@ export const reannotateDepths = (forest, depth = 0) => child_count: children.length, }; }); + +// Generated-by: Codex +export const sortIssueForest = (items, sortOrder = []) => { + const priorities = sortOrder.map((label) => label.trim()); + const bucket = (item) => { + const labels = new Set( + (item.labels || []).map((label) => label.name.toLowerCase()) + ); + const index = priorities.findIndex((label) => labels.has(label)); + return index < 0 ? priorities.length : index; + }; + return (items || []) + .map((item) => ({ + ...item, + ...(item.children + ? { children: sortIssueForest(item.children, sortOrder) } + : {}), + })) + .sort((a, b) => bucket(a) - bucket(b) || a.number - b.number); +}; diff --git a/frontend/src/utils/issueHierarchy.test.js b/frontend/src/utils/issueHierarchy.test.js index f9faae5..070e2ee 100644 --- a/frontend/src/utils/issueHierarchy.test.js +++ b/frontend/src/utils/issueHierarchy.test.js @@ -1,3 +1,4 @@ +// Assisted-by: Codex // Generated-by: Cursor import { describe, it, expect } from 'vitest'; import { @@ -7,6 +8,7 @@ import { removeClosedIssueFromForest, insertIssueInForest, reannotateDepths, + sortIssueForest, } from './issueHierarchy'; describe('issueHierarchy utils', () => { @@ -114,3 +116,23 @@ describe('issueHierarchy utils', () => { expect(annotated[0].children[0].hierarchy_depth).toBe(1); }); }); + +it('sorts siblings by first matching label and number without changing hierarchy or inputs', () => { + const forest = [ + { number: 1, labels: [], children: [] }, + { + number: 3, + labels: [{ name: 'BUG' }, { name: 'feature' }], + children: [ + { number: 4, labels: [] }, + { number: 5, labels: [{ name: 'feature' }] }, + ], + }, + { number: 2, labels: [{ name: 'feature' }], children: [] }, + ]; + const sorted = sortIssueForest(forest, [' feature ', 'bug']); + expect(sorted.map((node) => node.number)).toEqual([2, 3, 1]); + expect(sorted[1].children.map((node) => node.number)).toEqual([5, 4]); + expect(forest[1].children.map((node) => node.number)).toEqual([4, 5]); + expect(sortIssueForest(forest).map((node) => node.number)).toEqual([1, 2, 3]); +}); diff --git a/frontend/src/utils/labelsCache.js b/frontend/src/utils/labelsCache.js index 3dd071e..a6dd502 100644 --- a/frontend/src/utils/labelsCache.js +++ b/frontend/src/utils/labelsCache.js @@ -1,19 +1,9 @@ -// Generated-by: Cursor -// Shared labels cache - loaded once and shared across all components -let labelsCache = { - data: [], - loading: false, - error: null, - promise: null, -}; - +// Generated-by: Codex +import { createMetadataCache, invalidateQueries } from './queryCache'; +const labelsCache = createMetadataCache(); export const getLabelsCache = () => labelsCache; - export const clearLabelsCache = () => { - labelsCache.data = []; - labelsCache.loading = false; - labelsCache.error = null; - labelsCache.promise = null; + labelsCache.clear(); + invalidateQueries('/labels'); }; - export default labelsCache; diff --git a/frontend/src/utils/milestonesCache.js b/frontend/src/utils/milestonesCache.js index ad2e67f..78a38c4 100644 --- a/frontend/src/utils/milestonesCache.js +++ b/frontend/src/utils/milestonesCache.js @@ -1,19 +1,9 @@ -// Generated-by: Cursor -// Shared milestones cache - loaded once and shared across all components -let milestonesCache = { - data: [], - loading: false, - error: null, - promise: null, -}; - +// Generated-by: Codex +import { createMetadataCache, invalidateQueries } from './queryCache'; +const milestonesCache = createMetadataCache(); export const getMilestonesCache = () => milestonesCache; - export const clearMilestonesCache = () => { - milestonesCache.data = []; - milestonesCache.loading = false; - milestonesCache.error = null; - milestonesCache.promise = null; + milestonesCache.clear(); + invalidateQueries('/milestones'); }; - export default milestonesCache; diff --git a/frontend/src/utils/queryCache.js b/frontend/src/utils/queryCache.js new file mode 100644 index 0000000..698451f --- /dev/null +++ b/frontend/src/utils/queryCache.js @@ -0,0 +1,110 @@ +// Generated-by: Codex +// Short-lived, repository-scoped read cache. Mutations invalidate pending writes +// to the cache as well, so an older read cannot repopulate it after a mutation. +const entries = new Map(); +let repository = ''; +let generation = 0; + +const trimCompletedEntries = () => { + for (const [key, entry] of entries) { + if (entries.size <= 256) break; + if (!entry.promise) entries.delete(key); + } +}; + +export const invalidateQueries = (prefix = '') => { + generation += 1; + for (const key of entries.keys()) { + if (key.startsWith(`${repository}:${prefix}`)) entries.delete(key); + } +}; + +export const setQueryRepository = (repo) => { + if (repository !== repo) { + repository = repo; + generation += 1; + entries.clear(); + } +}; + +export const cachedQuery = ( + key, + loader, + { ttl = 30000, force = false, revalidate = false } = {} +) => { + const scopedKey = `${repository}:${key}`; + const previous = entries.get(scopedKey); + // Force bypasses both completed and in-flight reads: Apply/Refresh is an + // explicit request for a new observation, even with unchanged parameters. + if (!force && previous) { + if (previous.promise) return previous.promise; + if (!revalidate && previous.expires > Date.now()) + return Promise.resolve(previous.data); + } + const revision = generation; + const entry = {}; + entry.promise = Promise.resolve() + .then(loader) + .then( + (data) => { + if (generation !== revision || entries.get(scopedKey) !== entry) { + // A write or newer forced read superseded this response. Existing + // consumers rejoin a current read, so local UI caches cannot revive it. + if (entries.get(scopedKey) === entry) entries.delete(scopedKey); + return cachedQuery(key, loader, { ttl }); + } + entry.data = data; + entry.expires = Date.now() + ttl; + entry.promise = null; + trimCompletedEntries(); + return data; + }, + (error) => { + if (generation !== revision || entries.get(scopedKey) !== entry) { + if (entries.get(scopedKey) === entry) entries.delete(scopedKey); + return cachedQuery(key, loader, { ttl }); + } + entries.delete(scopedKey); + throw error; + } + ); + entries.set(scopedKey, entry); + trimCompletedEntries(); + return entry.promise; +}; + +export const createMetadataCache = () => { + let data = []; + let loadedAt = null; + let scope = repository; + let revision = generation; + return { + get data() { + return scope === repository ? data : []; + }, + set data(value) { + data = value; + loadedAt = Date.now(); + scope = repository; + revision = generation; + }, + get loaded() { + return ( + scope === repository && + revision === generation && + loadedAt !== null && + Date.now() - loadedAt < 30000 + ); + }, + loading: false, + error: null, + promise: null, + clear() { + data = []; + loadedAt = null; + this.loading = false; + this.error = null; + this.promise = null; + }, + }; +}; diff --git a/frontend/src/utils/queryCache.test.js b/frontend/src/utils/queryCache.test.js new file mode 100644 index 0000000..797b8c0 --- /dev/null +++ b/frontend/src/utils/queryCache.test.js @@ -0,0 +1,97 @@ +// Generated-by: Codex +import { describe, it, expect, vi, afterEach } from 'vitest'; +import { + cachedQuery, + createMetadataCache, + invalidateQueries, + setQueryRepository, +} from './queryCache'; + +const deferred = () => { + let resolve; + const promise = new Promise((done) => { + resolve = done; + }); + return { promise, resolve }; +}; +afterEach(() => vi.useRealTimers()); + +describe('shared queries', () => { + it('coalesces in-flight reads and caches valid empty responses', async () => { + const loader = vi.fn().mockResolvedValue([]); + const a = cachedQuery('/labels', loader); + expect(cachedQuery('/labels', loader)).toBe(a); + expect(await a).toEqual([]); + expect(await cachedQuery('/labels', loader)).toEqual([]); + expect(loader).toHaveBeenCalledTimes(1); + }); + + it('expires metadata and isolates repositories and milestone state', async () => { + vi.useFakeTimers(); + const loader = vi.fn().mockResolvedValue([]); + setQueryRepository('a/b'); + await cachedQuery('/milestones?state=open', loader); + await cachedQuery('/milestones?state=closed', loader); + await cachedQuery('/milestones?state=open', loader); + expect(loader).toHaveBeenCalledTimes(2); + vi.advanceTimersByTime(30001); + await cachedQuery('/milestones?state=open', loader); + setQueryRepository('a/c'); + await cachedQuery('/milestones?state=open', loader); + expect(loader).toHaveBeenCalledTimes(4); + }); + + it('does not publish an old read after a mutation', async () => { + const old = deferred(); + const loader = vi + .fn() + .mockReturnValueOnce(old.promise) + .mockResolvedValue(['fresh']); + const pending = cachedQuery('/issues/1', loader); + await Promise.resolve(); + invalidateQueries(); + old.resolve(['stale']); + expect(await pending).toEqual(['fresh']); + expect(await cachedQuery('/issues/1', loader)).toEqual(['fresh']); + expect(loader).toHaveBeenCalledTimes(2); + }); + + it('explicit refresh bypasses pending reads and older consumers receive the new result', async () => { + const old = deferred(); + const loader = vi + .fn() + .mockReturnValueOnce(old.promise) + .mockResolvedValue(['new']); + const pending = cachedQuery('/sdlc/dashboard', loader); + await Promise.resolve(); + expect( + await cachedQuery('/sdlc/dashboard', loader, { force: true }) + ).toEqual(['new']); + old.resolve(['old']); + expect(await pending).toEqual(['new']); + expect(loader).toHaveBeenCalledTimes(2); + }); + + it('does not loop or cache failures, and allows an explicit later retry', async () => { + const loader = vi + .fn() + .mockRejectedValueOnce(new Error('failed')) + .mockResolvedValue([]); + await expect(cachedQuery('/comments/1', loader)).rejects.toThrow('failed'); + expect(loader).toHaveBeenCalledTimes(1); + expect(await cachedQuery('/comments/1', loader)).toEqual([]); + }); + + it('tracks valid empty metadata and invalidates it after writes', () => { + const cache = createMetadataCache(); + expect(cache.loaded).toBe(false); + cache.data = []; + expect(cache.loaded).toBe(true); + invalidateQueries(); + expect(cache.loaded).toBe(false); + cache.data = ['private']; + setQueryRepository('another/repo'); + expect(cache.data).toEqual([]); + expect(cache.loaded).toBe(false); + }); +}); diff --git a/frontend/src/utils/useReactions.js b/frontend/src/utils/useReactions.js new file mode 100644 index 0000000..a2aab6e --- /dev/null +++ b/frontend/src/utils/useReactions.js @@ -0,0 +1,35 @@ +// Generated-by: Codex +import { useEffect, useState } from 'react'; + +// A failed or empty result completes an attempt. Only the resource, its server +// summary, or opening the description again can start another attempt. +export const useReactions = (id, count, active, loader, initial) => { + const [state, setState] = useState({ + data: initial || [], + loading: false, + error: null, + }); + useEffect(() => { + let current = true; + if (initial !== undefined) { + setState({ data: initial, loading: false, error: null }); + } else if (active && count > 0) { + setState({ data: [], loading: true, error: null }); + loader(id).then( + (data) => { + if (current) setState({ data, loading: false, error: null }); + }, + (error) => { + if (current) + setState({ data: [], loading: false, error: error.message }); + } + ); + } else { + setState({ data: [], loading: false, error: null }); + } + return () => { + current = false; + }; + }, [id, count, active, loader, initial]); + return state; +}; diff --git a/frontend/src/utils/useReactions.test.jsx b/frontend/src/utils/useReactions.test.jsx new file mode 100644 index 0000000..163f6fc --- /dev/null +++ b/frontend/src/utils/useReactions.test.jsx @@ -0,0 +1,53 @@ +// Generated-by: Codex +import { describe, it, expect, vi } from 'vitest'; +import { renderHook, waitFor } from '@testing-library/react'; +import { useReactions } from './useReactions'; + +describe('reaction loading', () => { + it('loads issue users only when the description opens', async () => { + const data = [ + { content: '+1', user: { login: 'alice' } }, + { content: 'heart', user: null }, + ]; + const loader = vi.fn().mockResolvedValue(data); + const { result, rerender } = renderHook( + ({ active }) => useReactions(1, 2, active, loader), + { initialProps: { active: false } } + ); + expect(loader).not.toHaveBeenCalled(); + rerender({ active: true }); + await waitFor(() => expect(result.current.data).toEqual(data)); + expect(loader).toHaveBeenCalledTimes(1); + }); + + it.each(['empty', 'failure'])( + 'finishes a %s attempt without a render-driven retry loop', + async (outcome) => { + const loader = + outcome === 'empty' + ? vi.fn().mockResolvedValue([]) + : vi.fn().mockRejectedValue(new Error('failed')); + const { result, rerender } = renderHook( + ({ active }) => useReactions(1, 1, active, loader), + { initialProps: { active: true } } + ); + await waitFor(() => expect(result.current.loading).toBe(false)); + rerender({ active: true }); + expect(loader).toHaveBeenCalledTimes(1); + expect(result.current.data).toEqual([]); + if (outcome === 'failure') expect(result.current.error).toBe('failed'); + rerender({ active: false }); + rerender({ active: true }); + await waitFor(() => expect(result.current.loading).toBe(false)); + expect(loader).toHaveBeenCalledTimes(2); + } + ); + + it('uses batched comment reactions without a per-comment request', () => { + const loader = vi.fn(); + const data = [{ content: 'eyes', user: { login: 'reviewer' } }]; + const { result } = renderHook(() => useReactions(1, 1, true, loader, data)); + expect(result.current.data).toEqual(data); + expect(loader).not.toHaveBeenCalled(); + }); +}); From 2f7495b451690fb66901e6627cabbf89f22e88c7 Mon Sep 17 00:00:00 2001 From: David Butenhof Date: Wed, 18 Nov 2026 14:26:45 -0500 Subject: [PATCH 2/2] Fix merge fallout Signed-off-by: David Butenhof --- frontend/package-lock.json | 51 ++++++-------------- frontend/package.json | 1 + frontend/src/components/CommentCard.test.jsx | 4 ++ 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index cb6a3c9..dba6da7 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -17,6 +17,7 @@ "remark-gfm": "^4.0.0" }, "devDependencies": { + "@testing-library/dom": "^9.3.4", "@testing-library/jest-dom": "^6.1.0", "@testing-library/react": "^14.1.0", "@testing-library/user-event": "^14.5.0", @@ -1278,23 +1279,31 @@ "dev": true }, "node_modules/@testing-library/dom": { - "version": "10.4.1", - "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-10.4.1.tgz", - "integrity": "sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg==", + "version": "9.3.4", + "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-9.3.4.tgz", + "integrity": "sha512-FlS4ZWlp97iiNWig0Muq8p+3rVDjRiYE+YKGbAqXOu9nwJFFOdL00kFpz42M+4huzYi86vAK1sOOfyOG45muIQ==", "dev": true, - "peer": true, "dependencies": { "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", "@types/aria-query": "^5.0.1", - "aria-query": "5.3.0", + "aria-query": "5.1.3", + "chalk": "^4.1.0", "dom-accessibility-api": "^0.5.9", "lz-string": "^1.5.0", - "picocolors": "1.1.1", "pretty-format": "^27.0.2" }, "engines": { - "node": ">=18" + "node": ">=14" + } + }, + "node_modules/@testing-library/dom/node_modules/aria-query": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/aria-query/-/aria-query-5.1.3.tgz", + "integrity": "sha512-R5iJ5lkuHybztUfuOAznmboyjWq8O6sqNqtK7CLOqdydi54VNbORp49mb14KbWgG1QD3JFO9hJdZ+y4KutfdOQ==", + "dev": true, + "dependencies": { + "deep-equal": "^2.0.5" } }, "node_modules/@testing-library/jest-dom": { @@ -1340,34 +1349,6 @@ "react-dom": "^18.0.0" } }, - "node_modules/@testing-library/react/node_modules/@testing-library/dom": { - "version": "9.3.4", - "resolved": "https://registry.npmjs.org/@testing-library/dom/-/dom-9.3.4.tgz", - "integrity": "sha512-FlS4ZWlp97iiNWig0Muq8p+3rVDjRiYE+YKGbAqXOu9nwJFFOdL00kFpz42M+4huzYi86vAK1sOOfyOG45muIQ==", - "dev": true, - "dependencies": { - "@babel/code-frame": "^7.10.4", - "@babel/runtime": "^7.12.5", - "@types/aria-query": "^5.0.1", - "aria-query": "5.1.3", - "chalk": "^4.1.0", - "dom-accessibility-api": "^0.5.9", - "lz-string": "^1.5.0", - "pretty-format": "^27.0.2" - }, - "engines": { - "node": ">=14" - } - }, - "node_modules/@testing-library/react/node_modules/aria-query": { - "version": "5.1.3", - "resolved": "https://registry.npmjs.org/aria-query/-/aria-query-5.1.3.tgz", - "integrity": "sha512-R5iJ5lkuHybztUfuOAznmboyjWq8O6sqNqtK7CLOqdydi54VNbORp49mb14KbWgG1QD3JFO9hJdZ+y4KutfdOQ==", - "dev": true, - "dependencies": { - "deep-equal": "^2.0.5" - } - }, "node_modules/@testing-library/user-event": { "version": "14.6.1", "resolved": "https://registry.npmjs.org/@testing-library/user-event/-/user-event-14.6.1.tgz", diff --git a/frontend/package.json b/frontend/package.json index 42845c7..3ad2bad 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -22,6 +22,7 @@ "remark-gfm": "^4.0.0" }, "devDependencies": { + "@testing-library/dom": "^9.3.4", "@testing-library/jest-dom": "^6.1.0", "@testing-library/react": "^14.1.0", "@testing-library/user-event": "^14.5.0", diff --git a/frontend/src/components/CommentCard.test.jsx b/frontend/src/components/CommentCard.test.jsx index 3004667..0daf451 100644 --- a/frontend/src/components/CommentCard.test.jsx +++ b/frontend/src/components/CommentCard.test.jsx @@ -1,5 +1,6 @@ // Generated-by: Cursor // Assisted-by: Cursor +// Assisted-by: Codex import { describe, it, expect, vi, beforeEach } from 'vitest'; import { render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; @@ -98,8 +99,11 @@ describe('CommentCard', () => { await user.click(screen.getByRole('button', { name: 'Edit comment' })); const textarea = screen.getByPlaceholderText('Write markdown…'); + expect(textarea).toHaveValue(mockComment.body); await user.clear(textarea); + expect(textarea).toHaveValue(''); await user.type(textarea, 'Updated body'); + expect(textarea).toHaveValue('Updated body'); await user.click(screen.getByRole('button', { name: 'OK' })); await waitFor(() => {