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

Commit 837aaa0

Browse files
feat: Track Upload Sent events for Coverage/BA/TA (attempt 2) (#1195)
1 parent 786d94a commit 837aaa0

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-0
lines changed

upload/tests/views/test_bundle_analysis.py

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
OrganizationLevelTokenFactory,
1111
)
1212
from shared.django_apps.core.tests.factories import CommitFactory, RepositoryFactory
13+
from shared.events.amplitude import UNKNOWN_USER_OWNERID
1314

1415
from core.models import Commit
1516
from services.redis_configuration import get_redis_connection
@@ -27,6 +28,9 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
2728
"shared.storage.MinioStorageService.create_presigned_put",
2829
return_value="test-presigned-put",
2930
)
31+
mock_amplitude = mocker.patch(
32+
"shared.events.amplitude.AmplitudeEventPublisher.publish"
33+
)
3034

3135
repository = RepositoryFactory.create()
3236
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
@@ -103,6 +107,19 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
103107
},
104108
)
105109

110+
# emits Amplitude event
111+
mock_amplitude.assert_called_with(
112+
"Upload Received",
113+
{
114+
"user_ownerid": UNKNOWN_USER_OWNERID,
115+
"ownerid": commit.repository.author.ownerid,
116+
"repoid": commit.repository.repoid,
117+
"commitid": commit.id,
118+
"pullid": commit.pullid,
119+
"upload_type": "Bundle",
120+
},
121+
)
122+
106123

107124
@pytest.mark.django_db(databases={"default", "timeseries"})
108125
@override_settings(SHELTER_SHARED_SECRET="shelter-shared-secret")

upload/tests/views/test_test_results.py

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
OwnerFactory,
1414
RepositoryFactory,
1515
)
16+
from shared.events.amplitude import UNKNOWN_USER_OWNERID
1617

1718
from services.redis_configuration import get_redis_connection
1819
from services.task import TaskService
@@ -25,6 +26,9 @@ def test_upload_test_results(db, client, mocker, mock_redis):
2526
"shared.storage.MinioStorageService.create_presigned_put",
2627
return_value="test-presigned-put",
2728
)
29+
mock_amplitude = mocker.patch(
30+
"shared.events.amplitude.AmplitudeEventPublisher.publish"
31+
)
2832

2933
owner = OwnerFactory(service="github", username="codecov")
3034
repository = RepositoryFactory.create(author=owner)
@@ -115,6 +119,19 @@ def test_upload_test_results(db, client, mocker, mock_redis):
115119
},
116120
)
117121

122+
# emits Amplitude event
123+
mock_amplitude.assert_called_with(
124+
"Upload Received",
125+
{
126+
"user_ownerid": UNKNOWN_USER_OWNERID,
127+
"ownerid": commit.repository.author.ownerid,
128+
"repoid": commit.repository.repoid,
129+
"commitid": commit.id,
130+
"pullid": commit.pullid,
131+
"upload_type": "Test results",
132+
},
133+
)
134+
118135

119136
def test_test_results_org_token(db, client, mocker, mock_redis):
120137
mocker.patch.object(TaskService, "upload")

upload/tests/views/test_upload_coverage.py

+14
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def test_upload_coverage_post(db, mocker):
104104
upload_task_mock = mocker.patch(
105105
"upload.views.uploads.trigger_upload_task", return_value=True
106106
)
107+
amplitude_mock = mocker.patch(
108+
"shared.events.amplitude.AmplitudeEventPublisher.publish"
109+
)
107110

108111
repository = RepositoryFactory(
109112
name="the_repo1", author__username="codecov", author__service="github"
@@ -151,6 +154,17 @@ def test_upload_coverage_post(db, mocker):
151154
response_json.get("url")
152155
== f"{settings.CODECOV_DASHBOARD_URL}/{repository.author.service}/{repository.author.username}/{repository.name}/commit/{commit.commitid}"
153156
)
157+
amplitude_mock.assert_called_with(
158+
"Upload Received",
159+
{
160+
"user_ownerid": commit.author.ownerid,
161+
"ownerid": commit.repository.author.ownerid,
162+
"repoid": commit.repository.repoid,
163+
"commitid": commit.id,
164+
"pullid": commit.pullid,
165+
"upload_type": "Coverage report",
166+
},
167+
)
154168

155169
assert ReportSession.objects.filter(
156170
report__commit=commit,

upload/views/bundle_analysis.py

+15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from rest_framework.views import APIView
1212
from shared.api_archive.archive import ArchiveService
1313
from shared.bundle_analysis.storage import StoragePaths, get_bucket_name
14+
from shared.events.amplitude import UNKNOWN_USER_OWNERID, AmplitudeEventPublisher
1415
from shared.metrics import Counter, inc_counter
1516

1617
from codecov_auth.authentication.repo_auth import (
@@ -140,6 +141,20 @@ def post(self, request: HttpRequest) -> Response:
140141
},
141142
)
142143

144+
AmplitudeEventPublisher().publish(
145+
"Upload Received",
146+
{
147+
"user_ownerid": commit.author.ownerid
148+
if commit.author
149+
else UNKNOWN_USER_OWNERID,
150+
"ownerid": repo.author.ownerid,
151+
"repoid": repo.repoid,
152+
"commitid": commit.id, # Not commit.commitid, we do not want a commit SHA here!
153+
"pullid": commit.pullid,
154+
"upload_type": "Bundle",
155+
},
156+
)
157+
143158
storage_path = data.get("storage_path", None)
144159
upload_external_id = data.get("upload_external_id", None)
145160
url = None

upload/views/test_results.py

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from rest_framework.response import Response
99
from rest_framework.views import APIView
1010
from shared.api_archive.archive import ArchiveService, MinioEndpoints
11+
from shared.events.amplitude import UNKNOWN_USER_OWNERID, AmplitudeEventPublisher
1112
from shared.metrics import inc_counter
1213

1314
from codecov_auth.authentication.repo_auth import (
@@ -135,6 +136,20 @@ def post(self, request):
135136
},
136137
)
137138

139+
AmplitudeEventPublisher().publish(
140+
"Upload Received",
141+
{
142+
"user_ownerid": commit.author.ownerid
143+
if commit.author
144+
else UNKNOWN_USER_OWNERID,
145+
"ownerid": repo.author.ownerid,
146+
"repoid": repo.repoid,
147+
"commitid": commit.id, # Not commit.commitid, we do not want a commit SHA here!
148+
"pullid": commit.pullid,
149+
"upload_type": "Test results",
150+
},
151+
)
152+
138153
upload_external_id = str(uuid.uuid4())
139154

140155
url = None

upload/views/upload_coverage.py

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from rest_framework.response import Response
77
from rest_framework.views import APIView
88
from shared.api_archive.archive import ArchiveService
9+
from shared.events.amplitude import UNKNOWN_USER_OWNERID, AmplitudeEventPublisher
910
from shared.metrics import inc_counter
1011

1112
from codecov_auth.authentication.repo_auth import (
@@ -92,6 +93,20 @@ def post(self, request: HttpRequest, *args, **kwargs) -> Response:
9293
),
9394
)
9495

96+
AmplitudeEventPublisher().publish(
97+
"Upload Received",
98+
{
99+
"user_ownerid": commit.author.ownerid
100+
if commit.author
101+
else UNKNOWN_USER_OWNERID,
102+
"ownerid": commit.repository.author.ownerid,
103+
"repoid": commit.repository.repoid,
104+
"commitid": commit.id, # Not commit.commitid, we do not want a commit SHA here.
105+
"pullid": commit.pullid,
106+
"upload_type": "Coverage report",
107+
},
108+
)
109+
95110
# Create report
96111
commit_report_data = dict(
97112
code=request.data.get("code"),

0 commit comments

Comments
 (0)