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

Commit 1a5f6e0

Browse files
committed
feat: handle file not found from CLI
we want to be able to notify the user with a comment when they get a file not found error from the CLI so this is our way of doing that we create a "placeholder" upload which represents this attempt at an upload that was not able to find a file this upload goes through the upload flow with a placeholder storage path (because we don't want the user to upload anything here) and when it gets to the TA processor, that step will create an upload error which will get notified like all other errors
1 parent ddb4755 commit 1a5f6e0

File tree

2 files changed

+92
-12
lines changed

2 files changed

+92
-12
lines changed

upload/tests/views/test_test_results.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
from shared.django_apps.codecov_auth.tests.factories import (
88
OrganizationLevelTokenFactory,
99
)
10+
from shared.django_apps.core.models import Commit
1011
from shared.django_apps.core.tests.factories import (
1112
CommitFactory,
1213
OwnerFactory,
1314
RepositoryFactory,
1415
)
16+
from shared.django_apps.reports.models import ReportSession
1517

16-
from core.models import Commit
1718
from services.redis_configuration import get_redis_connection
1819
from services.task import TaskService
1920

@@ -377,3 +378,73 @@ def test_update_repo_fields_when_upload_is_triggered(
377378
assert repository.active is True
378379
assert repository.activated is True
379380
assert repository.test_analytics_enabled is True
381+
382+
383+
def test_upload_test_results_file_not_found(db, client, mocker, mock_redis):
384+
upload = mocker.patch.object(TaskService, "upload")
385+
create_presigned_put = mocker.patch(
386+
"shared.api_archive.archive.StorageService.create_presigned_put",
387+
return_value="test-presigned-put",
388+
)
389+
390+
owner = OwnerFactory(service="github", username="codecov")
391+
repository = RepositoryFactory.create(author=owner)
392+
commit_sha = "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef"
393+
394+
client = APIClient()
395+
client.credentials(HTTP_AUTHORIZATION=f"token {repository.upload_token}")
396+
397+
res = client.post(
398+
reverse("upload-test-results"),
399+
{
400+
"commit": commit_sha,
401+
"slug": f"{repository.author.username}::::{repository.name}",
402+
"build": "test-build",
403+
"buildURL": "test-build-url",
404+
"job": "test-job",
405+
"service": "github-actions",
406+
"branch": "aaaaaa",
407+
"file_not_found": True,
408+
},
409+
format="json",
410+
headers={"User-Agent": "codecov-cli/0.4.7"},
411+
)
412+
assert res.status_code == 201
413+
414+
assert res.data is None
415+
416+
create_presigned_put.assert_not_called()
417+
418+
commit = Commit.objects.get(commitid=commit_sha)
419+
assert commit
420+
assert commit.branch is not None
421+
422+
redis = get_redis_connection()
423+
args = json.loads(
424+
redis.rpop(f"uploads/{repository.repoid}/{commit_sha}/test_results")
425+
)
426+
assert args == {
427+
"reportid": mocker.ANY,
428+
"build": "test-build",
429+
"build_url": "test-build-url",
430+
"job": "test-job",
431+
"service": "github-actions",
432+
"url": "placeholder",
433+
"commit": commit_sha,
434+
"report_code": None,
435+
"flags": None,
436+
}
437+
438+
# sets latest upload timestamp
439+
ts = redis.get(f"latest_upload/{repository.repoid}/{commit_sha}/test_results")
440+
assert ts
441+
442+
# triggers upload task
443+
upload.assert_called_with(
444+
commitid=commit_sha,
445+
repoid=repository.repoid,
446+
report_code=None,
447+
report_type="test_results",
448+
arguments=args,
449+
countdown=4,
450+
)

upload/views/test_results.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class UploadSerializer(serializers.Serializer):
5151
pr = serializers.CharField(required=False)
5252
branch = serializers.CharField(required=False, allow_null=True)
5353
storage_path = serializers.CharField(required=False)
54+
file_not_found = serializers.BooleanField(required=False)
5455

5556

5657
class TestResultsView(
@@ -136,18 +137,23 @@ def post(self, request):
136137

137138
upload_external_id = str(uuid.uuid4())
138139

139-
archive_service = ArchiveService(repo)
140+
url = None
141+
file_not_found = data.get("file_not_found", False)
142+
if file_not_found:
143+
storage_path = "placeholder"
144+
else:
145+
archive_service = ArchiveService(repo)
140146

141-
storage_path = data.get("storage_path", None)
142-
if storage_path is None or not self.is_shelter_request():
143-
storage_path = MinioEndpoints.test_results.get_path(
144-
date=timezone.now().strftime("%Y-%m-%d"),
145-
repo_hash=archive_service.get_archive_hash(repo),
146-
commit_sha=data["commit"],
147-
uploadid=upload_external_id,
148-
)
147+
storage_path = data.get("storage_path", None)
148+
if storage_path is None or not self.is_shelter_request():
149+
storage_path = MinioEndpoints.test_results.get_path(
150+
date=timezone.now().strftime("%Y-%m-%d"),
151+
repo_hash=archive_service.get_archive_hash(repo),
152+
commit_sha=data["commit"],
153+
uploadid=upload_external_id,
154+
)
149155

150-
url = archive_service.create_presigned_put(storage_path)
156+
url = archive_service.create_presigned_put(storage_path)
151157

152158
task_arguments = {
153159
# these are used in the upload task when saving an upload record
@@ -181,4 +187,7 @@ def post(self, request):
181187
report_type=CommitReport.ReportType.TEST_RESULTS,
182188
)
183189

184-
return Response({"raw_upload_location": url}, status=201)
190+
if url is None:
191+
return Response(status=201)
192+
else:
193+
return Response({"raw_upload_location": url}, status=201)

0 commit comments

Comments
 (0)