From 9ee462057c8136e1c7b6d15ddff1e7a6f9275609 Mon Sep 17 00:00:00 2001 From: Senko Rasic Date: Tue, 28 May 2024 10:58:16 +0200 Subject: [PATCH] show last project update time in JSON list --- core/cli/helpers.py | 5 +++++ tests/cli/test_cli.py | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/core/cli/helpers.py b/core/cli/helpers.py index a77f4fba..5081fde4 100644 --- a/core/cli/helpers.py +++ b/core/cli/helpers.py @@ -194,6 +194,7 @@ async def list_projects_json(db: SessionManager): data = [] for project in projects: + last_updated = None p = { "name": project.name, "id": project.id.hex, @@ -206,6 +207,8 @@ async def list_projects_json(db: SessionManager): "steps": [], } for state in branch.states: + if not last_updated or state.created_at > last_updated: + last_updated = state.created_at s = { "name": state.action or f"Step #{state.step_index}", "step": state.step_index, @@ -214,7 +217,9 @@ async def list_projects_json(db: SessionManager): if b["steps"]: b["steps"][-1]["name"] = "Latest step" p["branches"].append(b) + p["updated_at"] = last_updated.isoformat() if last_updated else None data.append(p) + print(json.dumps(data, indent=2)) diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py index f743a1c6..8349832b 100644 --- a/tests/cli/test_cli.py +++ b/tests/cli/test_cli.py @@ -1,5 +1,6 @@ import json from argparse import ArgumentParser, ArgumentTypeError +from datetime import datetime from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -167,9 +168,9 @@ async def test_list_projects_json(mock_StateManager, capsys): branch = MagicMock( id=MagicMock(hex="1234"), states=[ - MagicMock(step_index=1, action="foo"), - MagicMock(step_index=2, action=None), - MagicMock(step_index=3, action="baz"), + MagicMock(step_index=1, action="foo", created_at=datetime(2021, 1, 1)), + MagicMock(step_index=2, action=None, created_at=datetime(2021, 1, 2)), + MagicMock(step_index=3, action="baz", created_at=datetime(2021, 1, 3)), ], ) branch.name = "branch1" @@ -191,6 +192,7 @@ async def test_list_projects_json(mock_StateManager, capsys): { "name": "project1", "id": "abcd", + "updated_at": "2021-01-03T00:00:00", "branches": [ { "name": "branch1",