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

Commit ed96da0

Browse files
Add logic for BA endpoint to accept storage_path
1 parent 8945e37 commit ed96da0

File tree

2 files changed

+98
-6
lines changed

2 files changed

+98
-6
lines changed

upload/tests/views/test_bundle_analysis.py

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

105105

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

upload/views/bundle_analysis.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ def post(self, request: HttpRequest) -> Response:
116116
},
117117
)
118118

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-
)
119+
storage_path = data.get("storage_path", None)
120+
url = None
121+
if storage_path is None or not self.is_shelter_request():
122+
upload_external_id = str(uuid.uuid4())
123+
storage_path = StoragePaths.upload.path(upload_key=upload_external_id)
124+
archive_service = ArchiveService(repo)
125+
url = archive_service.storage.create_presigned_put(
126+
get_bucket_name(), storage_path, 30
127+
)
125128

126129
task_arguments = {
127130
# these are used in the upload task when saving an upload record

0 commit comments

Comments
 (0)