|
| 1 | +import logging |
| 2 | + |
| 3 | +from rest_framework import status |
| 4 | +from rest_framework.generics import ListCreateAPIView |
| 5 | +from rest_framework.response import Response |
| 6 | + |
| 7 | +from codecov_auth.authentication.repo_auth import ( |
| 8 | + GitHubOIDCTokenAuthentication, |
| 9 | + GlobalTokenAuthentication, |
| 10 | + OrgLevelTokenAuthentication, |
| 11 | + RepositoryLegacyTokenAuthentication, |
| 12 | + TokenlessAuthentication, |
| 13 | + UploadTokenRequiredAuthenticationCheck, |
| 14 | + repo_auth_custom_exception_handler, |
| 15 | +) |
| 16 | +from upload.serializers import ( |
| 17 | + CommitReportSerializer, |
| 18 | + CommitSerializer, |
| 19 | + UploadSerializer, |
| 20 | +) |
| 21 | +from upload.throttles import UploadsPerCommitThrottle, UploadsPerWindowThrottle |
| 22 | +from upload.views.commits import CommitLogicMixin |
| 23 | +from upload.views.reports import ReportLogicMixin |
| 24 | +from upload.views.uploads import CanDoCoverageUploadsPermission, UploadLogicMixin |
| 25 | + |
| 26 | +log = logging.getLogger(__name__) |
| 27 | + |
| 28 | + |
| 29 | +class CombinedUploadMixin(CommitLogicMixin, ReportLogicMixin, UploadLogicMixin): |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | +class CombinedUploadView(ListCreateAPIView, CombinedUploadMixin): |
| 34 | + permission_classes = [CanDoCoverageUploadsPermission] |
| 35 | + authentication_classes = [ |
| 36 | + UploadTokenRequiredAuthenticationCheck, |
| 37 | + GlobalTokenAuthentication, |
| 38 | + OrgLevelTokenAuthentication, |
| 39 | + GitHubOIDCTokenAuthentication, |
| 40 | + RepositoryLegacyTokenAuthentication, |
| 41 | + TokenlessAuthentication, |
| 42 | + ] |
| 43 | + throttle_classes = [UploadsPerCommitThrottle, UploadsPerWindowThrottle] |
| 44 | + |
| 45 | + def get_exception_handler(self): |
| 46 | + return repo_auth_custom_exception_handler |
| 47 | + |
| 48 | + def create(self, request, *args, **kwargs): |
| 49 | + # Create commit |
| 50 | + create_commit_data = dict( |
| 51 | + branch=request.data.get("branch"), |
| 52 | + commit_sha=request.data.get("commit_sha"), |
| 53 | + fail_on_error=True, |
| 54 | + git_service=request.data.get("git_service"), |
| 55 | + parent_sha=request.data.get("parent_sha"), |
| 56 | + pull_request_number=request.data.get("pull_request_number"), |
| 57 | + slug=request.data.get("slug"), |
| 58 | + token=request.data.get("token"), |
| 59 | + ) |
| 60 | + commit_serializer = CommitSerializer(data=create_commit_data) |
| 61 | + commit_serializer.is_valid(raise_exception=True) |
| 62 | + |
| 63 | + repository = self.get_repo() |
| 64 | + commit = self.create_commit(commit_serializer, repository) |
| 65 | + |
| 66 | + # Create report |
| 67 | + commit_report_data = dict( |
| 68 | + code=request.data.get("code"), |
| 69 | + commit_sha=request.data.get("commit_sha"), |
| 70 | + fail_on_error=True, |
| 71 | + git_service=request.data.get("git_service"), |
| 72 | + slug=request.data.get("slug"), |
| 73 | + token=request.data.get("token"), |
| 74 | + ) |
| 75 | + commit_report_serializer = CommitReportSerializer(data=commit_report_data) |
| 76 | + report = self.create_report(commit_report_serializer, repository, commit) |
| 77 | + |
| 78 | + # Do upload |
| 79 | + upload_data = dict( |
| 80 | + branch=request.data.get("branch"), |
| 81 | + build_code=request.data.get("build_code"), |
| 82 | + build_url=request.data.get("build_url"), |
| 83 | + commit_sha=request.data.get("commit_sha"), |
| 84 | + disable_file_fixes=request.data.get("disable_file_fixes"), |
| 85 | + disable_search=request.data.get("disable_search"), |
| 86 | + dry_run=request.data.get("dry_run"), |
| 87 | + env_vars=request.data.get("env_vars"), |
| 88 | + fail_on_error=request.data.get("fail_on_error"), |
| 89 | + files_search_exclude_folders=request.data.get( |
| 90 | + "files_search_exclude_folders" |
| 91 | + ), |
| 92 | + files_search_explicitly_listed_files=request.data.get( |
| 93 | + "files_search_explicitly_listed_files" |
| 94 | + ), |
| 95 | + files_search_root_folder=request.data.get("files_search_root_folder"), |
| 96 | + flags=request.data.get("flags"), |
| 97 | + gcov_args=request.data.get("gcov_args"), |
| 98 | + gcov_executable=request.data.get("gcov_executable"), |
| 99 | + gcov_ignore=request.data.get("gcov_ignore"), |
| 100 | + gcov_include=request.data.get("gcov_include"), |
| 101 | + git_service=request.data.get("git_service"), |
| 102 | + handle_no_reports_found=request.data.get("handle_no_reports_found"), |
| 103 | + job_code=request.data.get("job_code"), |
| 104 | + name=request.data.get("name"), |
| 105 | + network_filter=request.data.get("network_filter"), |
| 106 | + network_prefix=request.data.get("network_prefix"), |
| 107 | + network_root_folder=request.data.get("network_root_folder"), |
| 108 | + plugin_names=request.data.get("plugin_names"), |
| 109 | + pull_request_number=request.data.get("pull_request_number"), |
| 110 | + report_code=report.code, |
| 111 | + report_type=request.data.get("report_type"), |
| 112 | + slug=request.data.get("slug"), |
| 113 | + swift_project=request.data.get("swift_project"), |
| 114 | + token=request.data.get("token"), |
| 115 | + use_legacy_uploader=request.data.get("use_legacy_uploader"), |
| 116 | + ) |
| 117 | + upload_serializer = UploadSerializer(data=upload_data) |
| 118 | + upload_serializer.is_valid(raise_exception=True) |
| 119 | + |
| 120 | + instance = self.create_upload(upload_serializer, repository, commit, report) |
| 121 | + if instance: |
| 122 | + return Response(data=instance.data, status=status.HTTP_201_CREATED) |
| 123 | + else: |
| 124 | + return Response( |
| 125 | + upload_serializer.errors, status=status.HTTP_400_BAD_REQUEST |
| 126 | + ) |
0 commit comments