Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bundle analysis upload endpoint add custom compare SHA #649

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion upload/tests/views/test_bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis(db, client, mocker, mock_redis):
def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mock_sentry_metrics = mocker.patch(
"upload.views.bundle_analysis.sentry_metrics.incr"
Expand All @@ -41,6 +41,7 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis):
"buildURL": "test-build-url",
"job": "test-job",
"service": "test-service",
"compareSha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
},
format="json",
headers={"User-Agent": "codecov-cli/0.4.7"},
Expand Down Expand Up @@ -73,6 +74,7 @@ def test_upload_bundle_analysis(db, client, mocker, mock_redis):
"url": f"v1/uploads/{reportid}.json",
"commit": commit_sha,
"report_code": None,
"bundle_analysis_compare_sha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
}

# sets latest upload timestamp
Expand Down Expand Up @@ -348,3 +350,31 @@ def test_upload_bundle_analysis_measurement_timeseries_disabled(
name=measurement_type.value,
repository_id=repository.pk,
).exists()


@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mocker.patch.object(TaskService, "upload")
mocker.patch(
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)

repository = RepositoryFactory.create()
org_token = OrganizationLevelTokenFactory.create(owner=repository.author)

client = APIClient()
client.credentials(HTTP_AUTHORIZATION=f"token {org_token.token}")

res = client.post(
reverse("upload-bundle-analysis"),
{
"commit": "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef",
"slug": "FakeUser::::NonExistentName",
},
format="json",
)
assert res.status_code == 404
assert res.json() == {"detail": "Repository not found."}
assert not upload.called
16 changes: 12 additions & 4 deletions upload/views/bundle_analysis.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import uuid
from typing import Any, Callable

from django.conf import settings
from django.http import HttpRequest
from rest_framework import serializers, status
from rest_framework.exceptions import NotAuthenticated
from rest_framework.exceptions import NotAuthenticated, NotFound
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework.views import APIView
Expand Down Expand Up @@ -31,7 +33,7 @@


class UploadBundleAnalysisPermission(BasePermission):
def has_permission(self, request, view):
def has_permission(self, request: HttpRequest, view: Any) -> bool:
return request.auth is not None and "upload" in request.auth.get_scopes()


Expand All @@ -44,6 +46,7 @@ class UploadSerializer(serializers.Serializer):
pr = serializers.CharField(required=False, allow_null=True)
service = serializers.CharField(required=False, allow_null=True)
branch = serializers.CharField(required=False, allow_null=True)
compareSha = serializers.CharField(required=False, allow_null=True)


class BundleAnalysisView(APIView, ShelterMixin):
Expand All @@ -54,10 +57,10 @@ class BundleAnalysisView(APIView, ShelterMixin):
RepositoryLegacyTokenAuthentication,
]

def get_exception_handler(self):
def get_exception_handler(self) -> Callable:
return repo_auth_custom_exception_handler

def post(self, request):
def post(self, request: HttpRequest) -> Response:
serializer = UploadSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Expand All @@ -73,6 +76,9 @@ def post(self, request):
else:
raise NotAuthenticated()

if repo is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The authentication classes don't check if the repo exists already?
(asking for my own understanding)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe so, I just added this to appease the type checker because if the repo doesn't exist it will just report null for the repo var.

raise NotFound("Repository not found.")

update_fields = []
if not repo.active or not repo.activated:
repo.active = True
Expand Down Expand Up @@ -117,6 +123,8 @@ def post(self, request):
# these are used for dispatching the task below
"commit": commit.commitid,
"report_code": None,
# custom comparison sha for the current uploaded commit sha
"bundle_analysis_compare_sha": data.get("compareSha"),
}

log.info(
Expand Down
Loading