diff --git a/backend/src/github_pm/api.py b/backend/src/github_pm/api.py index 4bb5771..82f2be0 100644 --- a/backend/src/github_pm/api.py +++ b/backend/src/github_pm/api.py @@ -86,6 +86,7 @@ def _with_504_retry( """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( diff --git a/backend/src/github_pm/sdlc_metrics.py b/backend/src/github_pm/sdlc_metrics.py index b93fc98..0aad7b4 100644 --- a/backend/src/github_pm/sdlc_metrics.py +++ b/backend/src/github_pm/sdlc_metrics.py @@ -502,7 +502,7 @@ def graphql_search_timeline_nodes( """Paginate GraphQL ``search(type: ISSUE)`` returning Issue and PullRequest nodes. Used when REST ``GET /search/issues`` rejects the same query string (422). Request only - fields common to both types. + basic fields plus PR state and draft status and issue closure dates. """ nodes: list[dict[str, Any]] = [] cursor: str | None = None @@ -518,12 +518,14 @@ def graphql_search_timeline_nodes( url createdAt state + isDraft } ... on Issue { number title url createdAt + closedAt } } } diff --git a/backend/src/github_pm/status_report_service.py b/backend/src/github_pm/status_report_service.py index 295d033..b0c94b7 100644 --- a/backend/src/github_pm/status_report_service.py +++ b/backend/src/github_pm/status_report_service.py @@ -56,6 +56,19 @@ def _updated_calendar_in_window( return start_d <= ud <= end_d +def _closed_calendar_in_window( + node: dict[str, Any], start_d: date, end_d: date +) -> bool: + """Check the issue closure date against the inclusive UTC reporting window. + + Generated-by: Codex + """ + closed = sm.parse_github_ts(node.get("closedAt")) + if not closed: + return False + return start_d <= closed.astimezone(UTC).date() <= end_d + + def _updated_strictly_before_start_date(node: dict[str, Any], start_d: date) -> bool: """True if ``updatedAt`` exists and its UTC calendar date is before ``start_d``.""" u = sm.parse_github_ts(node.get("updatedAt")) @@ -193,16 +206,23 @@ def post_gql(payload: dict[str, Any]) -> dict[str, Any]: 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) + 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_filtered = [ n for n in opened_pr_nodes if n.get("__typename") == "PullRequest" and _created_calendar_in_window(n, start_date, end_date) - and n.get("state") != "CLOSED" + and n.get("state") == "OPEN" ] opened_pr_filtered.sort(key=lambda n: int(n["number"])) + opened_pr_items = [] + for n in opened_pr_filtered: + item = _item_from_gql_node(n) + # Keep display and clipboard titles identical for newly opened drafts. + if n.get("isDraft"): + 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) @@ -211,6 +231,7 @@ def post_gql(payload: dict[str, Any]) -> dict[str, Any]: for n in opened_issue_nodes if n.get("__typename") == "Issue" and _created_calendar_in_window(n, start_date, end_date) + and not _closed_calendar_in_window(n, start_date, end_date) ] opened_issue_filtered.sort(key=lambda n: int(n["number"])) @@ -268,7 +289,7 @@ def post_gql(payload: dict[str, Any]) -> dict[str, Any]: start_date=start_date, end_date=end_date, merged_pull_requests=merged_items, - opened_pull_requests=[_item_from_gql_node(n) for n in opened_pr_filtered], + opened_pull_requests=opened_pr_items, opened_issues=[_item_from_gql_node(n) for n in opened_issue_filtered], second_approval_needed=second_approval, recently_updated_pull_requests=recently_items, diff --git a/backend/tests/test_status_report_api.py b/backend/tests/test_status_report_api.py index 0e9cfbe..f38ac7d 100644 --- a/backend/tests/test_status_report_api.py +++ b/backend/tests/test_status_report_api.py @@ -61,6 +61,7 @@ def mock_connector_graphql(): "createdAt": "2025-04-04T12:00:00Z", } ] + gitctx.status_opened_issue_nodes = issue_nodes def post_side(path: str, data=None, **kwargs): body = data @@ -77,11 +78,12 @@ def post_side(path: str, data=None, **kwargs): } } if "is:issue" in q and "created:" in q: + assert "closedAt" in body["query"] return { "data": { "search": { "pageInfo": {"hasNextPage": False, "endCursor": None}, - "nodes": issue_nodes, + "nodes": list(gitctx.status_opened_issue_nodes), } } } @@ -159,6 +161,46 @@ async def override_conn(): assert body["creator_attention_needed"] == [] assert body["pr_backlog"] == [] + @pytest.mark.parametrize( + ("created_at", "closed_at", "included"), + [ + ("2025-04-03T00:00:00Z", None, True), + ("2025-04-10T23:59:59Z", None, True), + ("2025-04-03T00:00:00Z", "2025-04-03T00:00:00Z", False), + ("2025-04-04T12:00:00Z", "2025-04-06T12:00:00Z", False), + ("2025-04-04T12:00:00Z", "2025-04-10T23:59:59Z", False), + ("2025-04-04T12:00:00Z", "2025-04-11T00:00:00Z", True), + ("2025-04-04T12:00:00Z", "2025-04-11T01:00:00+02:00", False), + ("2025-04-02T23:59:59Z", None, False), + ("2025-04-11T00:00:00Z", None, False), + ], + ) + def test_opened_issues_exclude_closures_during_report_period( + self, client, mock_connector_graphql, created_at, closed_at, included + ): + """Keep new issues unless their closure falls in the reporting interval. + + Generated-by: Codex + """ + issue = mock_connector_graphql.status_opened_issue_nodes[0] + issue.update(createdAt=created_at, closedAt=closed_at) + + async def override_conn(): + yield mock_connector_graphql + + app.dependency_overrides[connection] = override_conn + try: + response = client.get( + "/api/v1/project-status", + params={"start_date": "2025-04-03", "end_date": "2025-04-10"}, + ) + finally: + app.dependency_overrides.clear() + + assert response.status_code == 200 + numbers = [item["number"] for item in response.json()["opened_issues"]] + assert numbers == ([12] if included else []) + def test_report_single_calendar_day(self, client, mock_connector_graphql): async def override_conn(): yield mock_connector_graphql @@ -659,8 +701,8 @@ async def override_conn(): assert [row["number"] for row in backlog] == [100, 50] assert [row["days_since_update"] for row in backlog] == [9, 7] - def test_opened_prs_exclude_closed_without_merge(self, client): - """PRs with GitHub state CLOSED (not merged) must not appear in opened_pull_requests.""" + def test_opened_prs_exclude_closed_and_merged(self, client): + """Only PRs with GitHub state OPEN appear in opened_pull_requests.""" gitctx = MagicMock() gitctx.owner = "test" gitctx.repo = "repo" @@ -774,7 +816,7 @@ async def override_conn(): assert r.status_code == 200 opened = r.json()["opened_pull_requests"] numbers = {p["number"] for p in opened} - assert numbers == {20, 21} + assert numbers == {20} assert r.json()["recently_updated_pull_requests"] == [] assert r.json()["reviewer_attention_needed"] == [] assert r.json()["creator_attention_needed"] == []