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

Commit a4b45ff

Browse files
committed
Merge branch 'main' into oct_17_ba_prometheus
2 parents d8de0bb + 15f9614 commit a4b45ff

File tree

3 files changed

+104
-6
lines changed

3 files changed

+104
-6
lines changed

docker/Dockerfile.requirements

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ RUN apt-get install -y \
2525
libpq-dev \
2626
make \
2727
curl \
28+
libexpat1 \
2829
&& pip install --upgrade pip
2930

3031
WORKDIR /pip-packages/

upload/tests/views/test_bundle_analysis.py

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

104104

105+
@pytest.mark.django_db(databases={"default", "timeseries"})
106+
@override_settings(SHELTER_SHARED_SECRET="shelter-shared-secret")
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+
"upload_external_id": "test-47078f85-2cee-4511-b38d-183c334ef43b",
135+
},
136+
format="json",
137+
headers={"User-Agent": "codecov-cli/0.4.7"},
138+
)
139+
assert res.status_code == 201
140+
141+
# returns presigned storage URL
142+
assert res.json() == {"url": "test-presigned-put"}
143+
144+
create_presigned_put.assert_called_once_with("bundle-analysis", ANY, 30)
145+
call = create_presigned_put.mock_calls[0]
146+
_, storage_path, _ = call.args
147+
match = re.match(r"v1/uploads/([\d\w\-]+)\.json", storage_path)
148+
assert match
149+
(reportid,) = match.groups()
150+
151+
# creates commit
152+
commit = Commit.objects.get(commitid=commit_sha)
153+
assert commit
154+
155+
# saves args in Redis
156+
redis = get_redis_connection()
157+
args = redis.rpop(f"uploads/{repository.repoid}/{commit_sha}/bundle_analysis")
158+
assert json.loads(args) == {
159+
"reportid": reportid,
160+
"build": "test-build",
161+
"build_url": "test-build-url",
162+
"job": "test-job",
163+
"service": "test-service",
164+
"url": f"v1/uploads/{reportid}.json",
165+
"commit": commit_sha,
166+
"report_code": None,
167+
"bundle_analysis_compare_sha": "6fd5b89357fc8cdf34d6197549ac7c6d7e5aaaaa",
168+
}
169+
170+
# sets latest upload timestamp
171+
ts = redis.get(f"latest_upload/{repository.repoid}/{commit_sha}/bundle_analysis")
172+
assert ts
173+
174+
# triggers upload task
175+
upload.assert_called_with(
176+
commitid=commit_sha,
177+
repoid=repository.repoid,
178+
countdown=4,
179+
report_code=None,
180+
report_type="bundle_analysis",
181+
)
182+
mock_sentry_metrics.assert_called_with(
183+
"upload",
184+
tags={
185+
"agent": "cli",
186+
"version": "0.4.7",
187+
"action": "bundle_analysis",
188+
"endpoint": "bundle_analysis",
189+
"repo_visibility": "private",
190+
"is_using_shelter": "no",
191+
"position": "end",
192+
},
193+
)
194+
195+
105196
@pytest.mark.django_db(databases={"default", "timeseries"})
106197
def test_upload_bundle_analysis_org_token(db, client, mocker, mock_redis):
107198
mocker.patch.object(TaskService, "upload")

upload/views/bundle_analysis.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class UploadSerializer(serializers.Serializer):
6464
branch = serializers.CharField(required=False, allow_null=True)
6565
compareSha = serializers.CharField(required=False, allow_null=True)
6666
git_service = serializers.CharField(required=False, allow_null=True)
67+
storage_path = serializers.CharField(required=False, allow_null=True)
68+
upload_external_id = serializers.CharField(required=False, allow_null=True)
6769

6870

6971
class BundleAnalysisView(APIView, ShelterMixin):
@@ -130,12 +132,16 @@ def post(self, request: HttpRequest) -> Response:
130132
},
131133
)
132134

133-
upload_external_id = str(uuid.uuid4())
134-
storage_path = StoragePaths.upload.path(upload_key=upload_external_id)
135-
archive_service = ArchiveService(repo)
136-
url = archive_service.storage.create_presigned_put(
137-
get_bucket_name(), storage_path, 30
138-
)
135+
storage_path = data.get("storage_path", None)
136+
upload_external_id = data.get("upload_external_id", None)
137+
url = None
138+
if not self.is_shelter_request():
139+
upload_external_id = str(uuid.uuid4())
140+
storage_path = StoragePaths.upload.path(upload_key=upload_external_id)
141+
archive_service = ArchiveService(repo)
142+
url = archive_service.storage.create_presigned_put(
143+
get_bucket_name(), storage_path, 30
144+
)
139145

140146
task_arguments = {
141147
# these are used in the upload task when saving an upload record

0 commit comments

Comments
 (0)