Skip to content

Commit

Permalink
Move environment reviews controllers out of artefacts module
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Oct 16, 2024
1 parent f984874 commit 776e5c8
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 269 deletions.
23 changes: 23 additions & 0 deletions backend/test_observer/controllers/artefacts/artefact_retriever.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from fastapi import Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlalchemy.sql.base import ExecutableOption

from test_observer.data_access.models import (
Artefact,
)
from test_observer.data_access.setup import get_db


class ArtefactRetriever:
def __init__(self, *options: ExecutableOption):
self._options = options

def __call__(self, artefact_id: int, db: Session = Depends(get_db)):
artefact = db.scalar(
select(Artefact).where(Artefact.id == artefact_id).options(*self._options)
)
if artefact is None:
msg = f"Artefact with id {artefact_id} not found"
raise HTTPException(status_code=404, detail=msg)
return artefact
73 changes: 3 additions & 70 deletions backend/test_observer/controllers/artefacts/artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,31 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import Session, selectinload
from sqlalchemy.sql.base import ExecutableOption

from test_observer.controllers.artefacts.artefact_retriever import ArtefactRetriever
from test_observer.data_access.models import (
Artefact,
ArtefactBuild,
ArtefactBuildEnvironmentReview,
TestExecution,
)
from test_observer.data_access.models_enums import ArtefactStatus, FamilyName
from test_observer.data_access.repository import get_artefacts_by_family
from test_observer.data_access.setup import get_db

from . import environment_reviews
from .logic import (
are_all_environments_approved,
is_there_a_rejected_environment,
)
from .models import (
ArtefactBuildDTO,
ArtefactBuildEnvironmentReviewDTO,
ArtefactDTO,
ArtefactPatch,
ArtefactVersionDTO,
EnvironmentReviewPatch,
)

router = APIRouter(tags=["artefacts"])


class ArtefactRetriever:
def __init__(self, *options: ExecutableOption):
self._options = options

def __call__(self, artefact_id: int, db: Session = Depends(get_db)):
artefact = db.scalar(
select(Artefact).where(Artefact.id == artefact_id).options(*self._options)
)
if artefact is None:
msg = f"Artefact with id {artefact_id} not found"
raise HTTPException(status_code=404, detail=msg)
return artefact
router.include_router(environment_reviews.router)


@router.get("", response_model=list[ArtefactDTO])
Expand Down Expand Up @@ -175,55 +160,3 @@ def get_artefact_versions(
.where(Artefact.repo == artefact.repo)
.order_by(Artefact.id.desc())
)


@router.get(
"/{artefact_id}/environment-reviews",
response_model=list[ArtefactBuildEnvironmentReviewDTO],
)
def get_environment_reviews(
artefact: Artefact = Depends(
ArtefactRetriever(
selectinload(Artefact.builds).selectinload(
ArtefactBuild.environment_reviews
)
)
),
):
return [
review
for build in artefact.latest_builds
for review in build.environment_reviews
]


@router.patch(
"/{artefact_id}/environment-reviews/{review_id}",
response_model=ArtefactBuildEnvironmentReviewDTO,
)
def update_environment_review(
artefact_id: int,
request: EnvironmentReviewPatch,
review_id: int,
db: Session = Depends(get_db),
):
review = db.scalar(
select(ArtefactBuildEnvironmentReview)
.where(ArtefactBuildEnvironmentReview.id == review_id)
.options(selectinload(ArtefactBuildEnvironmentReview.artefact_build))
)

if not review:
raise HTTPException(404, f"Environment review {review_id} doesn't exist")

if review.artefact_build.artefact_id != artefact_id:
msg = f"Environment review {review_id} doesn't belong to artefact {artefact_id}"
raise HTTPException(422, msg)

for field in request.model_fields_set:
value = getattr(request, field)
if value is not None:
setattr(review, field, value)

db.commit()
return review
70 changes: 70 additions & 0 deletions backend/test_observer/controllers/artefacts/environment_reviews.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import Session, selectinload

from test_observer.controllers.artefacts.artefact_retriever import ArtefactRetriever
from test_observer.data_access.models import (
Artefact,
ArtefactBuild,
ArtefactBuildEnvironmentReview,
)
from test_observer.data_access.setup import get_db

from .models import (
ArtefactBuildEnvironmentReviewDTO,
EnvironmentReviewPatch,
)

router = APIRouter(tags=["environment-reviews"])


@router.get(
"/{artefact_id}/environment-reviews",
response_model=list[ArtefactBuildEnvironmentReviewDTO],
)
def get_environment_reviews(
artefact: Artefact = Depends(
ArtefactRetriever(
selectinload(Artefact.builds).selectinload(
ArtefactBuild.environment_reviews
)
)
),
):
return [
review
for build in artefact.latest_builds
for review in build.environment_reviews
]


@router.patch(
"/{artefact_id}/environment-reviews/{review_id}",
response_model=ArtefactBuildEnvironmentReviewDTO,
)
def update_environment_review(
artefact_id: int,
request: EnvironmentReviewPatch,
review_id: int,
db: Session = Depends(get_db),
):
review = db.scalar(
select(ArtefactBuildEnvironmentReview)
.where(ArtefactBuildEnvironmentReview.id == review_id)
.options(selectinload(ArtefactBuildEnvironmentReview.artefact_build))
)

if not review:
raise HTTPException(404, f"Environment review {review_id} doesn't exist")

if review.artefact_build.artefact_id != artefact_id:
msg = f"Environment review {review_id} doesn't belong to artefact {artefact_id}"
raise HTTPException(422, msg)

for field in request.model_fields_set:
value = getattr(request, field)
if value is not None:
setattr(review, field, value)

db.commit()
return review
Loading

0 comments on commit 776e5c8

Please sign in to comment.