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

Commit 455da2f

Browse files
Add logic for BA endpoint to accept storage_path (#892)
1 parent 3b84808 commit 455da2f

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
lines changed

upload/tests/views/test_bundle_analysis.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,97 @@ def test_upload_bundle_analysis_success(db, client, mocker, mock_redis):
103103
)
104104

105105

106+
@pytest.mark.django_db(databases={"default", "timeseries"})
107+
@override_settings(SHELTER_SHARED_SECRET="shelter-shared-secret")
108+
def test_upload_bundle_analysis_success_shelter(db, client, mocker, mock_redis):
109+
upload = mocker.patch.object(TaskService, "upload")
110+
mock_sentry_metrics = mocker.patch(
111+
"upload.views.bundle_analysis.sentry_metrics.incr"
112+
)
113+
create_presigned_put = mocker.patch(
114+
"services.archive.StorageService.create_presigned_put",
115+
return_value="test-presigned-put",
116+
)
117+
118+
repository = RepositoryFactory.create()
119+
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
120+
121+
client = APIClient()
122+
client.credentials(HTTP_AUTHORIZATION=f"token {repository.upload_token}")
123+
124+
res = client.post(
125+
reverse("upload-bundle-analysis"),
126+
{
127+
"commit": commit_sha,
128+
"slug": f"{repository.author.username}::::{repository.name}",
129+
"build": "test-build",
130+
"buildURL": "test-build-url",
131+
"job": "test-job",
132+
"service": "test-service",
133+
"compareSha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
134+
"storage_path": "shelter/test/path.txt",
135+
"upload_external_id": "test-47078f85-2cee-4511-b38d-183c334ef43b",
136+
},
137+
format="json",
138+
headers={"User-Agent": "codecov-cli/0.4.7"},
139+
)
140+
assert res.status_code == 201
141+
142+
# returns presigned storage URL
143+
assert res.json() == {"url": "test-presigned-put"}
144+
145+
create_presigned_put.assert_called_once_with("bundle-analysis", ANY, 30)
146+
call = create_presigned_put.mock_calls[0]
147+
_, storage_path, _ = call.args
148+
match = re.match(r"v1/uploads/([\d\w\-]+)\.json", storage_path)
149+
assert match
150+
(reportid,) = match.groups()
151+
152+
# creates commit
153+
commit = Commit.objects.get(commitid=commit_sha)
154+
assert commit
155+
156+
# saves args in Redis
157+
redis = get_redis_connection()
158+
args = redis.rpop(f"uploads/{repository.repoid}/{commit_sha}/bundle_analysis")
159+
assert json.loads(args) == {
160+
"reportid": reportid,
161+
"build": "test-build",
162+
"build_url": "test-build-url",
163+
"job": "test-job",
164+
"service": "test-service",
165+
"url": f"v1/uploads/{reportid}.json",
166+
"commit": commit_sha,
167+
"report_code": None,
168+
"bundle_analysis_compare_sha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
169+
}
170+
171+
# sets latest upload timestamp
172+
ts = redis.get(f"latest_upload/{repository.repoid}/{commit_sha}/bundle_analysis")
173+
assert ts
174+
175+
# triggers upload task
176+
upload.assert_called_with(
177+
commitid=commit_sha,
178+
repoid=repository.repoid,
179+
countdown=4,
180+
report_code=None,
181+
report_type="bundle_analysis",
182+
)
183+
mock_sentry_metrics.assert_called_with(
184+
"upload",
185+
tags={
186+
"agent": "cli",
187+
"version": "0.4.7",
188+
"action": "bundle_analysis",
189+
"endpoint": "bundle_analysis",
190+
"repo_visibility": "private",
191+
"is_using_shelter": "no",
192+
"position": "end",
193+
},
194+
)
195+
196+
106197
@pytest.mark.django_db(databases={"default", "timeseries"})
107198
def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
108199
mocker.patch.object(TaskService, "upload")

upload/views/bundle_analysis.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class UploadSerializer(serializers.Serializer):
4949
branch = serializers.CharField(required=False, allow_null=True)
5050
compareSha = serializers.CharField(required=False, allow_null=True)
5151
git_service = serializers.CharField(required=False, allow_null=True)
52+
storage_path = serializers.CharField(required=False, allow_null=True)
53+
upload_external_id = serializers.CharField(required=False, allow_null=True)
5254

5355

5456
class BundleAnalysisView(APIView, ShelterMixin):
@@ -116,12 +118,16 @@ def post(self, request: HttpRequest) -> Response:
116118
},
117119
)
118120

119-
upload_external_id = str(uuid.uuid4())
120-
storage_path = StoragePaths.upload.path(upload_key=upload_external_id)
121-
archive_service = ArchiveService(repo)
122-
url = archive_service.storage.create_presigned_put(
123-
get_bucket_name(), storage_path, 30
124-
)
121+
storage_path = data.get("storage_path", None)
122+
upload_external_id = data.get("upload_external_id", None)
123+
url = None
124+
if not self.is_shelter_request():
125+
upload_external_id = str(uuid.uuid4())
126+
storage_path = StoragePaths.upload.path(upload_key=upload_external_id)
127+
archive_service = ArchiveService(repo)
128+
url = archive_service.storage.create_presigned_put(
129+
get_bucket_name(), storage_path, 30
130+
)
125131

126132
task_arguments = {
127133
# these are used in the upload task when saving an upload record

0 commit comments

Comments
 (0)