|
| 1 | +import logging |
| 2 | +from typing import Any, Callable |
| 3 | + |
| 4 | +from django.http import HttpRequest |
| 5 | +from rest_framework import serializers, status |
| 6 | +from rest_framework.generics import CreateAPIView |
| 7 | +from rest_framework.response import Response |
| 8 | +from shared.metrics import inc_counter |
| 9 | + |
| 10 | +from codecov_auth.authentication.repo_auth import ( |
| 11 | + GitHubOIDCTokenAuthentication, |
| 12 | + GlobalTokenAuthentication, |
| 13 | + OrgLevelTokenAuthentication, |
| 14 | + RepositoryLegacyTokenAuthentication, |
| 15 | + UploadTokenRequiredAuthenticationCheck, |
| 16 | + repo_auth_custom_exception_handler, |
| 17 | +) |
| 18 | +from services.task import TaskService |
| 19 | +from upload.helpers import generate_upload_prometheus_metrics_labels |
| 20 | +from upload.metrics import API_UPLOAD_COUNTER |
| 21 | +from upload.views.base import GetterMixin |
| 22 | +from upload.views.uploads import CanDoCoverageUploadsPermission |
| 23 | + |
| 24 | +log = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +class TransplantReportSerializer(serializers.Serializer): |
| 28 | + from_sha = serializers.CharField(required=True) |
| 29 | + to_sha = serializers.CharField(required=True) |
| 30 | + |
| 31 | + |
| 32 | +class TransplantReportView(CreateAPIView, GetterMixin): |
| 33 | + permission_classes = [CanDoCoverageUploadsPermission] |
| 34 | + authentication_classes = [ |
| 35 | + UploadTokenRequiredAuthenticationCheck, |
| 36 | + GlobalTokenAuthentication, |
| 37 | + OrgLevelTokenAuthentication, |
| 38 | + GitHubOIDCTokenAuthentication, |
| 39 | + RepositoryLegacyTokenAuthentication, |
| 40 | + ] |
| 41 | + |
| 42 | + def get_exception_handler(self) -> Callable[[Exception, dict[str, Any]], Response]: |
| 43 | + return repo_auth_custom_exception_handler |
| 44 | + |
| 45 | + def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> Response: |
| 46 | + inc_counter( |
| 47 | + API_UPLOAD_COUNTER, |
| 48 | + labels=generate_upload_prometheus_metrics_labels( |
| 49 | + action="coverage", |
| 50 | + endpoint="transplant_report", |
| 51 | + request=self.request, |
| 52 | + is_shelter_request=self.is_shelter_request(), |
| 53 | + ), |
| 54 | + ) |
| 55 | + serializer = TransplantReportSerializer(data=request.data) |
| 56 | + if not serializer.is_valid(): |
| 57 | + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
| 58 | + |
| 59 | + data = serializer.validated_data |
| 60 | + TaskService().transplant_report( |
| 61 | + repo_id=self.get_repo().repoid, |
| 62 | + from_sha=data["from_sha"], |
| 63 | + to_sha=data["to_sha"], |
| 64 | + ) |
| 65 | + |
| 66 | + return Response( |
| 67 | + data={"result": "All good, transplant scheduled"}, |
| 68 | + status=status.HTTP_200_OK, |
| 69 | + ) |
0 commit comments