From 09f44e94157b0b3548c22496c821e393b282830d Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Fri, 8 Dec 2023 15:47:17 -0500 Subject: [PATCH] feat: update upload views and serializer add report_type field to upload endpoint to accept test result uploads --- upload/serializers.py | 18 +++++++++--------- upload/views/uploads.py | 27 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/upload/serializers.py b/upload/serializers.py index 7ed7b811ef..a13c297145 100644 --- a/upload/serializers.py +++ b/upload/serializers.py @@ -6,6 +6,8 @@ from reports.models import CommitReport, ReportResults, ReportSession, RepositoryFlag from services.archive import ArchiveService +from typing import Dict + class FlagListField(serializers.ListField): child = serializers.CharField() @@ -20,6 +22,7 @@ class UploadSerializer(serializers.ModelSerializer): version = serializers.CharField(write_only=True, required=False) url = serializers.SerializerMethodField() storage_path = serializers.CharField(write_only=True, required=False) + report_type = serializers.CharField(write_only=True, required=False) class Meta: read_only_fields = ( @@ -39,6 +42,7 @@ class Meta: "job_code", "version", "storage_path", + "upload_file_type", ) model = ReportSession @@ -54,15 +58,11 @@ def get_url(self, obj: ReportSession): commit = obj.report.commit return f"{settings.CODECOV_DASHBOARD_URL}/{repository.author.service}/{repository.author.username}/{repository.name}/commit/{commit.commitid}" - def create(self, validated_data): - flag_names = ( - validated_data.pop("flags") if "flags" in validated_data.keys() else [] - ) - _ = ( - validated_data.pop("version") - if "version" in validated_data.keys() - else None - ) + def create(self, validated_data: Dict): + flag_names = validated_data.pop("flags", []) + _ = validated_data.pop("version", None) + _ = validated_data.pop("report_type", None) + upload = ReportSession.objects.create(**validated_data) flags = [] if upload: diff --git a/upload/views/uploads.py b/upload/views/uploads.py index 6bcf7ddb8b..8310fde290 100644 --- a/upload/views/uploads.py +++ b/upload/views/uploads.py @@ -55,11 +55,22 @@ def perform_create(self, serializer: UploadSerializer): validate_activated_repo(repository) commit = self.get_commit(repository) report = self.get_report(commit) - version = ( - serializer.validated_data["version"] - if "version" in serializer.validated_data - else None + + version = serializer.validated_data.get("version", None) + report_type = serializer.validated_data.get( + "report_type", CommitReport.ReportType.COVERAGE ) + if report_type not in [ + CommitReport.ReportType.COVERAGE, + CommitReport.ReportType.TEST_RESULTS, + ]: + log.warning( + "Upload report_type is included in data but is not coverage or test_results, automatically assuming it's a coverage report", + extra=dict( + repo=repository.name, commit=commit.commitid, cli_version=version + ), + ) + log.info( "Request to create new upload", extra=dict( @@ -88,7 +99,9 @@ def perform_create(self, serializer: UploadSerializer): ) instance.storage_path = path instance.save() - self.trigger_upload_task(repository, commit.commitid, instance, report) + self.trigger_upload_task( + repository, commit.commitid, instance, report, report_type + ) metrics.incr("uploads.accepted", 1) self.activate_repo(repository) self.send_analytics_data(commit, instance, version) @@ -104,7 +117,7 @@ def list( ): return HttpResponseNotAllowed(permitted_methods=["POST"]) - def trigger_upload_task(self, repository, commit_sha, upload, report): + def trigger_upload_task(self, repository, commit_sha, upload, report, report_type): log.info( "Triggering upload task", extra=dict( @@ -122,7 +135,7 @@ def trigger_upload_task(self, repository, commit_sha, upload, report): "report_code": report.code, "reportid": str(report.external_id), } - dispatch_upload_task(task_arguments, repository, redis) + dispatch_upload_task(task_arguments, repository, redis, report_type) def activate_repo(self, repository): # Only update the fields if needed