Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Closed
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
134 changes: 12 additions & 122 deletions upload/tests/views/test_bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
mock_sentry_metrics = mocker.patch(
"upload.views.bundle_analysis.sentry_metrics.incr"
)
create_presigned_put = mocker.patch(
"services.archive.StorageService.create_presigned_put",
Expand Down Expand Up @@ -89,8 +89,9 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
report_code=None,
report_type="bundle_analysis",
)
mock_metrics.assert_called_with(
**{
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "bundle_analysis",
Expand All @@ -106,8 +107,8 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
@override_settings(SHELTER_SHARED_SECRET="shelter-shared-secret")
def test_upload_bundle_analysis_success_shelter(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
mock_sentry_metrics = mocker.patch(
"upload.views.bundle_analysis.sentry_metrics.incr"
)
create_presigned_put = mocker.patch(
"services.archive.StorageService.create_presigned_put",
Expand Down Expand Up @@ -179,8 +180,9 @@ def test_upload_bundle_analysis_success_shelter(db, client, mocker, mock_redis):
report_code=None,
report_type="bundle_analysis",
)
mock_metrics.assert_called_with(
**{
mock_sentry_metrics.assert_called_with(
"upload",
tags={
"agent": "cli",
"version": "0.4.7",
"action": "bundle_analysis",
Expand All @@ -199,9 +201,6 @@ def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

repository = RepositoryFactory.create()
org_token = OrganizationLevelTokenFactory.create(owner=repository.author)
Expand All @@ -218,17 +217,6 @@ def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
format="json",
)
assert res.status_code == 201
mock_metrics.assert_called_with(
**{
"agent": "unknown-user-agent",
"version": "unknown-user-agent",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "private",
"is_using_shelter": "no",
"position": "end",
},
)


@pytest.mark.django_db(databases={"default", "timeseries"})
Expand All @@ -238,9 +226,6 @@ def test_upload_bundle_analysis_existing_commit(db, client, mocker, mock_redis):
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

repository = RepositoryFactory.create()
commit = CommitFactory.create(repository=repository)
Expand All @@ -265,17 +250,6 @@ def test_upload_bundle_analysis_existing_commit(db, client, mocker, mock_redis):
report_code=None,
report_type="bundle_analysis",
)
mock_metrics.assert_called_with(
**{
"agent": "unknown-user-agent",
"version": "unknown-user-agent",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "private",
"is_using_shelter": "no",
"position": "end",
},
)


def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
Expand All @@ -284,9 +258,6 @@ def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

repository = RepositoryFactory.create()
commit = CommitFactory.create(repository=repository)
Expand Down Expand Up @@ -315,16 +286,6 @@ def test_upload_bundle_analysis_missing_args(db, client, mocker, mock_redis):
assert res.status_code == 400
assert res.json() == {"commit": ["This field is required."]}
assert not upload.called
mock_metrics.assert_called_with(
**{
"agent": "unknown-user-agent",
"version": "unknown-user-agent",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"is_using_shelter": "no",
"position": "start",
},
)


def test_upload_bundle_analysis_invalid_token(db, client, mocker, mock_redis):
Expand Down Expand Up @@ -363,9 +324,6 @@ def test_upload_bundle_analysis_github_oidc_auth(
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)
repository = RepositoryFactory()
mock_jwt_decode.return_value = {
"repository": f"url/{repository.name}",
Expand All @@ -386,31 +344,18 @@ def test_upload_bundle_analysis_github_oidc_auth(
format="json",
)
assert res.status_code == 201
mock_metrics.assert_called_with(
**{
"agent": "unknown-user-agent",
"version": "unknown-user-agent",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "private",
"is_using_shelter": "no",
"position": "end",
},
)


@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_measurement_datasets_created(
db, client, mocker, mock_redis
):
mocker.patch.object(TaskService, "upload")
mocker.patch("upload.views.bundle_analysis.sentry_metrics.incr")
mocker.patch(
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

repository = RepositoryFactory.create()
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
Expand Down Expand Up @@ -447,32 +392,18 @@ def test_upload_bundle_analysis_measurement_datasets_created(
repository_id=repository.pk,
).exists()

mock_metrics.assert_called_with(
**{
"agent": "cli",
"version": "0.4.7",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "private",
"is_using_shelter": "no",
"position": "end",
},
)


@override_settings(TIMESERIES_ENABLED=False)
@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_measurement_timeseries_disabled(
db, client, mocker, mock_redis
):
mocker.patch.object(TaskService, "upload")
mocker.patch("upload.views.bundle_analysis.sentry_metrics.incr")
mocker.patch(
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

repository = RepositoryFactory.create()
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
Expand Down Expand Up @@ -509,18 +440,6 @@ def test_upload_bundle_analysis_measurement_timeseries_disabled(
repository_id=repository.pk,
).exists()

mock_metrics.assert_called_with(
**{
"agent": "cli",
"version": "0.4.7",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "private",
"is_using_shelter": "no",
"position": "end",
},
)


@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
Expand All @@ -530,9 +449,6 @@ def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
"services.archive.StorageService.create_presigned_put",
return_value="test-presigned-put",
)
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

repository = RepositoryFactory.create()
org_token = OrganizationLevelTokenFactory.create(owner=repository.author)
Expand All @@ -552,24 +468,10 @@ def test_upload_bundle_analysis_no_repo(db, client, mocker, mock_redis):
assert res.json() == {"detail": "Repository not found."}
assert not upload.called

mock_metrics.assert_called_with(
**{
"agent": "unknown-user-agent",
"version": "unknown-user-agent",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"is_using_shelter": "no",
"position": "start",
},
)


@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_tokenless_success(db, client, mocker, mock_redis):
upload = mocker.patch.object(TaskService, "upload")
mock_metrics = mocker.patch(
"upload.views.bundle_analysis.BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels"
)

create_presigned_put = mocker.patch(
"services.archive.StorageService.create_presigned_put",
Expand Down Expand Up @@ -606,18 +508,6 @@ def test_upload_bundle_analysis_tokenless_success(db, client, mocker, mock_redis
assert upload.called
create_presigned_put.assert_called_once_with("bundle-analysis", ANY, 30)

mock_metrics.assert_called_with(
**{
"agent": "cli",
"version": "0.4.7",
"action": "bundle_analysis",
"endpoint": "bundle_analysis",
"repo_visibility": "public",
"is_using_shelter": "no",
"position": "end",
},
)


@pytest.mark.django_db(databases={"default", "timeseries"})
def test_upload_bundle_analysis_tokenless_no_repo(db, client, mocker, mock_redis):
Expand Down
36 changes: 12 additions & 24 deletions upload/views/bundle_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from rest_framework.permissions import BasePermission
from rest_framework.response import Response
from rest_framework.views import APIView
from sentry_sdk import metrics as sentry_metrics
from shared.bundle_analysis.storage import StoragePaths, get_bucket_name
from shared.metrics import Counter

from codecov_auth.authentication.repo_auth import (
BundleAnalysisTokenlessAuthentication,
Expand All @@ -33,21 +33,6 @@
log = logging.getLogger(__name__)


BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER = Counter(
"bundle_analysis_upload_views_runs",
"Number of times a raw report processor was run and with what result",
[
"agent",
"version",
"action",
"endpoint",
"is_using_shelter",
"repo_visibility",
"position",
],
)


class UploadBundleAnalysisPermission(BasePermission):
def has_permission(self, request: HttpRequest, view: Any) -> bool:
return request.auth is not None and "upload" in request.auth.get_scopes()
Expand Down Expand Up @@ -81,15 +66,16 @@ def get_exception_handler(self) -> Callable:
return repo_auth_custom_exception_handler

def post(self, request: HttpRequest) -> Response:
BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels(
**generate_upload_sentry_metrics_tags(
sentry_metrics.incr(
"upload",
tags=generate_upload_sentry_metrics_tags(
action="bundle_analysis",
endpoint="bundle_analysis",
request=self.request,
is_shelter_request=self.is_shelter_request(),
position="start",
)
).inc()
),
)
serializer = UploadSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Expand Down Expand Up @@ -168,16 +154,18 @@ def post(self, request: HttpRequest) -> Response:
task_arguments=task_arguments,
),
)
BUNDLE_ANALYSIS_UPLOAD_VIEWS_COUNTER.labels(
**generate_upload_sentry_metrics_tags(

sentry_metrics.incr(
"upload",
tags=generate_upload_sentry_metrics_tags(
action="bundle_analysis",
endpoint="bundle_analysis",
request=self.request,
repository=repo,
is_shelter_request=self.is_shelter_request(),
position="end",
)
).inc()
),
)

dispatch_upload_task(
task_arguments,
Expand Down
Loading