diff --git a/backend/kernelCI_app/typeModels/issues.py b/backend/kernelCI_app/typeModels/issues.py index dac09947..06706fe5 100644 --- a/backend/kernelCI_app/typeModels/issues.py +++ b/backend/kernelCI_app/typeModels/issues.py @@ -31,8 +31,21 @@ class IssueBuildItem(BaseModel): log_url: Optional[str] +class IssueTestItem(BaseModel): + id: str + status: Optional[str] + duration: Optional[Union[int, float]] + path: Optional[str] + start_time: Optional[Union[datetime, str]] + environment_compatible: Optional[Union[str, List[str]]] + + class IssuesBuildResponse(RootModel): root: List[IssueBuildItem] +class IssuesTestsResponse(RootModel): + root: List[IssueTestItem] + + type IssueDict = Dict[Tuple[str, str], Issue] diff --git a/backend/kernelCI_app/views/issueDetailsTestsView.py b/backend/kernelCI_app/views/issueDetailsTestsView.py index 06c2e697..6b63b72d 100644 --- a/backend/kernelCI_app/views/issueDetailsTestsView.py +++ b/backend/kernelCI_app/views/issueDetailsTestsView.py @@ -1,14 +1,18 @@ 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, + IssuesTestsResponse, +) +from drf_spectacular.utils import extend_schema +from rest_framework.views import APIView 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,6 +39,7 @@ 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: @@ -54,4 +59,12 @@ def get( error_message="No tests found for this issue", status_code=HTTPStatus.OK ) - return JsonResponse(tests_data, safe=False) + try: + valid_response = IssuesTestsResponse(tests_data) + except ValidationError as e: + return create_error_response( + error_message=e.errors(), + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + ) + + return JsonResponse(valid_response.model_dump(), safe=False) diff --git a/backend/schema.yml b/backend/schema.yml index b7144635..c3282c97 100644 --- a/backend/schema.yml +++ b/backend/schema.yml @@ -233,6 +233,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 @@ -1262,11 +1289,61 @@ components: - log_url title: IssueBuildItem type: object + IssueTestItem: + properties: + id: + title: Id + type: string + status: + anyOf: + - type: string + - type: 'null' + title: Status + duration: + anyOf: + - type: integer + - type: number + - type: 'null' + title: Duration + path: + anyOf: + - type: string + - type: 'null' + title: Path + start_time: + anyOf: + - format: date-time + type: string + - type: string + - type: 'null' + title: Start Time + environment_compatible: + anyOf: + - type: string + - items: + type: string + type: array + - type: 'null' + title: Environment Compatible + 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 Misc: properties: platform: