Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 46e642a

Browse files
committed
Track Upload Sent event
1 parent a8a1c06 commit 46e642a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

upload/tests/views/test_uploads.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def test_uploads_post_tokenless(db, mocker, mock_redis, private, branch, branch_
314314
"upload.views.uploads.trigger_upload_task", return_value=True
315315
)
316316
analytics_service_mock = mocker.patch("upload.views.uploads.AnalyticsService")
317+
amplitude_mock = mocker.path("shared.events.amplitude.AmplitudeEventPublisher")
317318

318319
repository = RepositoryFactory(
319320
name="the_repo",
@@ -431,6 +432,17 @@ def test_uploads_post_tokenless(db, mocker, mock_redis, private, branch, branch_
431432
"uploader_type": "CLI",
432433
},
433434
)
435+
amplitude_mock.return_value.publish.assert_called_with(
436+
"Upload Sent",
437+
{
438+
"user_ownerid": commit.author.ownerid,
439+
"ownerid": commit.repository.author.ownerid,
440+
"repoid": commit.repository.repoid,
441+
"commitid": commit.id,
442+
"pullid": commit.pullid,
443+
"upload_type": "Coverage report",
444+
},
445+
)
434446
else:
435447
assert response.status_code == 401
436448
assert response.json().get("detail") == "Not valid tokenless upload"
@@ -459,6 +471,7 @@ def test_uploads_post_token_required_auth_check(
459471
"upload.views.uploads.trigger_upload_task", return_value=True
460472
)
461473
analytics_service_mock = mocker.patch("upload.views.uploads.AnalyticsService")
474+
amplitude_mock = mocker.path("shared.events.amplitude.AmplitudeEventPublisher")
462475

463476
repository = RepositoryFactory(
464477
name="the_repo",
@@ -583,18 +596,31 @@ def test_uploads_post_token_required_auth_check(
583596
"uploader_type": "CLI",
584597
},
585598
)
599+
amplitude_mock.return_value.publish.assert_called_with(
600+
"Upload Sent",
601+
{
602+
"user_ownerid": commit.author.ownerid,
603+
"ownerid": commit.repository.author.ownerid,
604+
"repoid": commit.repository.repoid,
605+
"commitid": commit.id,
606+
"pullid": commit.pullid,
607+
"upload_type": "Coverage report",
608+
},
609+
)
586610
else:
587611
assert response.status_code == 401
588612
assert response.json().get("detail") == "Not valid tokenless upload"
589613

590614

615+
@patch("shared.events.amplitude.AmplitudeEventPublisher")
591616
@patch("upload.views.uploads.AnalyticsService")
592617
@patch("upload.helpers.jwt.decode")
593618
@patch("upload.helpers.PyJWKClient")
594619
def test_uploads_post_github_oidc_auth(
595620
mock_jwks_client,
596621
mock_jwt_decode,
597622
analytics_service_mock,
623+
amplitude_mock,
598624
db,
599625
mocker,
600626
mock_redis,
@@ -717,6 +743,17 @@ def test_uploads_post_github_oidc_auth(
717743
"uploader_type": "CLI",
718744
},
719745
)
746+
amplitude_mock.return_value.publish.assert_called_with(
747+
"Upload Sent",
748+
{
749+
"user_ownerid": commit.author.ownerid,
750+
"ownerid": commit.repository.author.ownerid,
751+
"repoid": commit.repository.repoid,
752+
"commitid": commit.id,
753+
"pullid": commit.pullid,
754+
"upload_type": "Coverage report",
755+
},
756+
)
720757

721758

722759
@override_settings(SHELTER_SHARED_SECRET="shelter-shared-secret")

upload/views/uploads.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from rest_framework.response import Response
1111
from rest_framework.views import APIView
1212
from shared.api_archive.archive import ArchiveService, MinioEndpoints
13+
from shared.events.amplitude import AmplitudeEventPublisher
1314
from shared.metrics import inc_counter
1415
from shared.upload.utils import UploaderType, insert_coverage_measurement
1516

@@ -167,6 +168,17 @@ def send_analytics_data(
167168
AnalyticsService().account_uploaded_coverage_report(
168169
commit.repository.author.ownerid, analytics_upload_data
169170
)
171+
AmplitudeEventPublisher().publish(
172+
"Upload Sent",
173+
{
174+
"user_ownerid": commit.author.ownerid,
175+
"ownerid": commit.repository.author.ownerid,
176+
"repoid": commit.repository.repoid,
177+
"commitid": commit.id, # Not commit.commitid, we do not want a commit SHA here!
178+
"pullid": commit.pullid,
179+
"upload_type": "Coverage report",
180+
},
181+
)
170182

171183

172184
def get_token_for_analytics(commit: Commit, request: HttpRequest) -> str:

0 commit comments

Comments
 (0)