-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move environment reviews controllers out of artefacts module
- Loading branch information
Showing
5 changed files
with
302 additions
and
269 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
backend/test_observer/controllers/artefacts/artefact_retriever.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
backend/test_observer/controllers/artefacts/environment_reviews.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.