diff --git a/backend/kernelCI_app/typeModels/issueDetails.py b/backend/kernelCI_app/typeModels/issueDetails.py index 1a5083b4..fd487fa3 100644 --- a/backend/kernelCI_app/typeModels/issueDetails.py +++ b/backend/kernelCI_app/typeModels/issueDetails.py @@ -11,6 +11,12 @@ Build__Duration, Build__Compiler, Build__LogUrl, + Test__Id, + Test__Status, + Test__Duration, + Test__Path, + Test__StartTime, + Test__EnvironmentCompatible, ) @@ -25,5 +31,18 @@ class IssueBuildItem(BaseModel): log_url: Build__LogUrl +class IssueTestItem(BaseModel): + id: Test__Id + status: Test__Status + duration: Test__Duration + path: Test__Path + start_time: Test__StartTime + environment_compatible: Test__EnvironmentCompatible + + +class IssuesTestsResponse(RootModel): + root: List[IssueTestItem] + + class IssuesBuildResponse(RootModel): root: List[IssueBuildItem] diff --git a/backend/kernelCI_app/views/issueDetailsTestsView.py b/backend/kernelCI_app/views/issueDetailsTestsView.py index 06c2e697..9fb8f81c 100644 --- a/backend/kernelCI_app/views/issueDetailsTestsView.py +++ b/backend/kernelCI_app/views/issueDetailsTestsView.py @@ -1,14 +1,17 @@ from http import HTTPStatus from typing import Dict, Optional -from django.http import JsonResponse -from django.views import View -from kernelCI_app.helpers.errorHandling import create_error_response from kernelCI_app.models import Incidents -from kernelCI_app.typeModels.issues import IssueDetailsPathParameters +from kernelCI_app.typeModels.issues import ( + IssueDetailsPathParameters, +) +from kernelCI_app.typeModels.issueDetails import IssuesTestsResponse +from drf_spectacular.utils import extend_schema +from rest_framework.views import APIView +from rest_framework.response import Response from pydantic import ValidationError -class IssueDetailsTests(View): +class IssueDetailsTests(APIView): def _fetch_incidents(self, *, issue_id: str, version: int) -> Optional[Dict]: fields = [ "test__id", @@ -35,23 +38,35 @@ def _fetch_incidents(self, *, issue_id: str, version: int) -> Optional[Dict]: for test in tests ] + @extend_schema(responses=IssuesTestsResponse) def get( self, _request, issue_id: Optional[str], version: Optional[str] - ) -> JsonResponse: + ) -> Response: try: parsed_params = IssueDetailsPathParameters( issue_id=issue_id, version=version ) except ValidationError as e: - return create_error_response(e.json()) + return Response( + data={"error": e.json()}, + status=HTTPStatus.BAD_REQUEST, + ) tests_data = self._fetch_incidents( issue_id=parsed_params.issue_id, version=parsed_params.version ) if not tests_data: - return create_error_response( - error_message="No tests found for this issue", status_code=HTTPStatus.OK + return Response( + data={"error": "No tests found for this issue"}, status=HTTPStatus.OK + ) + + try: + valid_response = IssuesTestsResponse(tests_data) + except ValidationError as e: + return Response( + error={"error": e.errors()}, + status=HTTPStatus.INTERNAL_SERVER_ERROR, ) - return JsonResponse(tests_data, safe=False) + return Response(valid_response.model_dump()) diff --git a/backend/schema.yml b/backend/schema.yml index 716d022b..e38799a5 100644 --- a/backend/schema.yml +++ b/backend/schema.yml @@ -245,6 +245,33 @@ paths: schema: $ref: '#/components/schemas/IssuesBuildResponse' description: '' + /api/issue/{issue_id}/version/{version}/tests: + get: + operationId: issue_version_tests_retrieve + parameters: + - in: path + name: issue_id + schema: + type: string + required: true + - in: path + name: version + schema: + type: string + required: true + tags: + - issue + security: + - cookieAuth: [] + - basicAuth: [] + - {} + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/IssuesTestsResponse' + description: '' /api/log-downloader/: get: operationId: log_downloader_retrieve @@ -1333,11 +1360,39 @@ components: - log_url title: IssueBuildItem type: object + IssueTestItem: + properties: + id: + $ref: '#/components/schemas/Test__Id' + status: + $ref: '#/components/schemas/Test__Status' + duration: + $ref: '#/components/schemas/Test__Duration' + path: + $ref: '#/components/schemas/Test__Path' + start_time: + $ref: '#/components/schemas/Test__StartTime' + environment_compatible: + $ref: '#/components/schemas/Test__EnvironmentCompatible' + required: + - id + - status + - duration + - path + - start_time + - environment_compatible + title: IssueTestItem + type: object IssuesBuildResponse: items: $ref: '#/components/schemas/IssueBuildItem' title: IssuesBuildResponse type: array + IssuesTestsResponse: + items: + $ref: '#/components/schemas/IssueTestItem' + title: IssuesTestsResponse + type: array LocalFilters: properties: issues: