From ffb35e58109e2d98125f4924a1f1224828cf272a Mon Sep 17 00:00:00 2001 From: Omar Selo Date: Tue, 28 Nov 2023 08:09:59 +0000 Subject: [PATCH] Add endpoint to patch artefact status --- .../controllers/artefacts/artefacts.py | 15 ++++++++++++++- .../test_observer/controllers/artefacts/models.py | 6 +++++- backend/test_observer/data_access/models_enums.py | 2 +- .../tests/controllers/artefacts/test_artefacts.py | 15 +++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/backend/test_observer/controllers/artefacts/artefacts.py b/backend/test_observer/controllers/artefacts/artefacts.py index af111285..e11d3af0 100644 --- a/backend/test_observer/controllers/artefacts/artefacts.py +++ b/backend/test_observer/controllers/artefacts/artefacts.py @@ -25,7 +25,7 @@ from test_observer.data_access.repository import get_artefacts_by_family from test_observer.data_access.setup import get_db -from .models import ArtefactBuildDTO, ArtefactDTO +from .models import ArtefactBuildDTO, ArtefactDTO, ArtefactPatch router = APIRouter() @@ -55,6 +55,19 @@ def get_artefact(artefact_id: int, db: Session = Depends(get_db)): return artefact +@router.patch("/{artefact_id}") +def patch_artefact( + artefact_id: int, request: ArtefactPatch, db: Session = Depends(get_db) +): + artefact = db.get(Artefact, artefact_id) + + if artefact is None: + raise HTTPException(status_code=404, detail="Artefact not found") + + artefact.status = request.status + db.commit() + + @router.get("/{artefact_id}/builds", response_model=list[ArtefactBuildDTO]) def get_artefact_builds(artefact_id: int, db: Session = Depends(get_db)): """Get latest artefact builds of an artefact together with their test executions""" diff --git a/backend/test_observer/controllers/artefacts/models.py b/backend/test_observer/controllers/artefacts/models.py index 88757c00..7578334e 100644 --- a/backend/test_observer/controllers/artefacts/models.py +++ b/backend/test_observer/controllers/artefacts/models.py @@ -19,7 +19,7 @@ # Nadzeya Hutsko from pydantic import AliasPath, BaseModel, ConfigDict, Field -from test_observer.data_access.models_enums import TestExecutionStatus +from test_observer.data_access.models_enums import ArtefactStatus, TestExecutionStatus class ArtefactDTO(BaseModel): @@ -60,3 +60,7 @@ class ArtefactBuildDTO(BaseModel): architecture: str revision: int | None test_executions: list[TestExecutionDTO] + + +class ArtefactPatch(BaseModel): + status: ArtefactStatus diff --git a/backend/test_observer/data_access/models_enums.py b/backend/test_observer/data_access/models_enums.py index 1492d1f0..76848d8a 100644 --- a/backend/test_observer/data_access/models_enums.py +++ b/backend/test_observer/data_access/models_enums.py @@ -35,6 +35,6 @@ class TestExecutionStatus(Enum): NOT_TESTED = "NOT_TESTED" -class ArtefactStatus(Enum): +class ArtefactStatus(str, Enum): APPROVED = "APPROVED" MARKED_AS_FAILED = "MARKED_AS_FAILED" diff --git a/backend/tests/controllers/artefacts/test_artefacts.py b/backend/tests/controllers/artefacts/test_artefacts.py index ad7d2a91..c3ea01f9 100644 --- a/backend/tests/controllers/artefacts/test_artefacts.py +++ b/backend/tests/controllers/artefacts/test_artefacts.py @@ -23,6 +23,7 @@ from sqlalchemy.orm import Session from test_observer.data_access.models import ArtefactBuild, Environment, TestExecution +from test_observer.data_access.models_enums import ArtefactStatus from tests.helpers import create_artefact @@ -129,3 +130,17 @@ def test_get_artefact_builds_only_latest(db_session: Session, test_client: TestC "test_executions": [], } ] + + +def test_artefact_signoff(db_session: Session, test_client: TestClient): + artefact = create_artefact(db_session, "candidate") + + response = test_client.patch( + f"/v1/artefacts/{artefact.id}", + json={"status": ArtefactStatus.APPROVED}, + ) + + db_session.refresh(artefact) + + assert response.status_code == 200 + assert artefact.status == ArtefactStatus.APPROVED