|
1 | 1 | from http import HTTPStatus
|
2 |
| -from typing import Optional |
3 | 2 | from urllib.parse import urlencode
|
4 | 3 | from drf_spectacular.utils import extend_schema
|
5 | 4 | from rest_framework.response import Response
|
6 | 5 | from rest_framework.views import APIView
|
7 | 6 |
|
8 |
| -from django.http import JsonResponse |
| 7 | +from django.http import HttpRequest, JsonResponse |
9 | 8 | from django.urls import reverse
|
10 | 9 | from pydantic import ValidationError
|
11 | 10 |
|
12 | 11 | from kernelCI_app.constants.general import DEFAULT_ORIGIN
|
13 |
| -from kernelCI_app.helpers.errorHandling import create_api_error_response |
14 | 12 | from kernelCI_app.queries.tree import get_latest_tree
|
15 | 13 | from kernelCI_app.typeModels.commonOpenApiParameters import (
|
16 |
| - COMMIT_HASH_PATH_PARAM, |
17 | 14 | GIT_BRANCH_PATH_PARAM,
|
18 | 15 | TREE_NAME_PATH_PARAM,
|
19 | 16 | )
|
|
25 | 22 | from kernelCI_app.constants.localization import ClientStrings
|
26 | 23 |
|
27 | 24 |
|
28 |
| -class BaseTreeLatest(APIView): |
29 |
| - |
| 25 | +class TreeLatest(APIView): |
| 26 | + @extend_schema( |
| 27 | + responses=TreeLatestResponse, |
| 28 | + parameters=[ |
| 29 | + TREE_NAME_PATH_PARAM, |
| 30 | + GIT_BRANCH_PATH_PARAM, |
| 31 | + TreeLatestQueryParameters, |
| 32 | + ], |
| 33 | + methods=["GET"], |
| 34 | + ) |
30 | 35 | def get(
|
31 | 36 | self,
|
32 |
| - request, |
| 37 | + request: HttpRequest, |
33 | 38 | tree_name: str,
|
34 | 39 | git_branch: str,
|
35 |
| - commit_hash: Optional[str] = None, |
36 | 40 | ) -> JsonResponse:
|
37 | 41 | try:
|
38 | 42 | parsed_params = TreeLatestPathParameters(
|
39 |
| - tree_name=tree_name, git_branch=git_branch, commit_hash=commit_hash |
| 43 | + tree_name=tree_name, git_branch=git_branch |
40 | 44 | )
|
| 45 | + query_params = TreeLatestQueryParameters(**request.GET.dict()) |
41 | 46 | except ValidationError as e:
|
42 | 47 | return Response(data=e.json, status=HTTPStatus.BAD_REQUEST)
|
43 | 48 |
|
44 |
| - tree_not_found_error_message = ClientStrings.TREE_NOT_FOUND |
45 |
| - origin = request.GET.get("origin") |
46 |
| - if origin is None: |
47 |
| - origin = DEFAULT_ORIGIN |
48 |
| - tree_not_found_error_message += ( |
49 |
| - f"{ClientStrings.TREE_LATEST_DEFAULT_ORIGIN} {DEFAULT_ORIGIN}" |
50 |
| - ) |
| 49 | + origin = query_params.origin |
51 | 50 |
|
52 | 51 | tree_data = get_latest_tree(
|
53 | 52 | tree_name=parsed_params.tree_name,
|
54 | 53 | git_branch=parsed_params.git_branch,
|
55 | 54 | origin=origin,
|
56 |
| - git_commit_hash=parsed_params.commit_hash, |
| 55 | + git_commit_hash=query_params.commit_hash, |
57 | 56 | )
|
58 | 57 |
|
59 | 58 | if tree_data is None:
|
60 |
| - return create_api_error_response( |
61 |
| - error_message=tree_not_found_error_message, |
62 |
| - status_code=HTTPStatus.OK, |
| 59 | + tree_not_found_error_message = ClientStrings.TREE_NOT_FOUND |
| 60 | + if request.GET.get("origin") is None: |
| 61 | + tree_not_found_error_message += ( |
| 62 | + f" {ClientStrings.TREE_LATEST_DEFAULT_ORIGIN} {DEFAULT_ORIGIN}" |
| 63 | + ) |
| 64 | + |
| 65 | + params = parsed_params.model_dump() |
| 66 | + params.update(query_params.model_dump()) |
| 67 | + |
| 68 | + return Response( |
| 69 | + data={"error": tree_not_found_error_message, "params": params}, |
| 70 | + status=HTTPStatus.OK, |
63 | 71 | )
|
64 | 72 |
|
65 | 73 | # The `old_api_url` is only used in the backend response in order to
|
66 | 74 | # keep compatibility with the full treeDetails endpoint in case someone
|
67 |
| - # expects this format. It should be removed soon. |
| 75 | + # expects this format. It should be removed when we drop the support for the old format. |
68 | 76 | # TODO: remove this field from the response.
|
69 | 77 | old_base_url = reverse(
|
70 | 78 | "treeDetailsView",
|
@@ -106,48 +114,3 @@ def get(
|
106 | 114 | return Response(data=e.json(), status=HTTPStatus.INTERNAL_SERVER_ERROR)
|
107 | 115 |
|
108 | 116 | return Response(valid_response.model_dump())
|
109 |
| - |
110 |
| - |
111 |
| -class TreeCheckoutInfo(BaseTreeLatest): |
112 |
| - @extend_schema( |
113 |
| - responses=TreeLatestResponse, |
114 |
| - parameters=[ |
115 |
| - COMMIT_HASH_PATH_PARAM, |
116 |
| - TREE_NAME_PATH_PARAM, |
117 |
| - GIT_BRANCH_PATH_PARAM, |
118 |
| - TreeLatestQueryParameters, |
119 |
| - ], |
120 |
| - methods=["GET"], |
121 |
| - ) |
122 |
| - def get( |
123 |
| - self, |
124 |
| - request, |
125 |
| - tree_name: str, |
126 |
| - git_branch: str, |
127 |
| - commit_hash: str, |
128 |
| - ) -> JsonResponse: |
129 |
| - return super().get( |
130 |
| - request=request, |
131 |
| - commit_hash=commit_hash, |
132 |
| - tree_name=tree_name, |
133 |
| - git_branch=git_branch, |
134 |
| - ) |
135 |
| - |
136 |
| - |
137 |
| -class TreeLatestCheckout(BaseTreeLatest): |
138 |
| - @extend_schema( |
139 |
| - responses=TreeLatestResponse, |
140 |
| - parameters=[ |
141 |
| - TREE_NAME_PATH_PARAM, |
142 |
| - GIT_BRANCH_PATH_PARAM, |
143 |
| - TreeLatestQueryParameters, |
144 |
| - ], |
145 |
| - methods=["GET"], |
146 |
| - ) |
147 |
| - def get( |
148 |
| - self, |
149 |
| - request, |
150 |
| - tree_name: str, |
151 |
| - git_branch: str, |
152 |
| - ) -> JsonResponse: |
153 |
| - return super().get(request=request, tree_name=tree_name, git_branch=git_branch) |
0 commit comments