Skip to content

Commit

Permalink
feat: update upload views and serializer
Browse files Browse the repository at this point in the history
add report_type field to upload endpoint to accept test result uploads
  • Loading branch information
joseph-sentry committed Dec 8, 2023
1 parent e94750d commit 09f44e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
18 changes: 9 additions & 9 deletions upload/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 = (
Expand All @@ -39,6 +42,7 @@ class Meta:
"job_code",
"version",
"storage_path",
"upload_file_type",
)
model = ReportSession

Expand All @@ -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:
Expand Down
27 changes: 20 additions & 7 deletions upload/views/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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
Expand Down

0 comments on commit 09f44e9

Please sign in to comment.