From c5db21145c43e09b301eced13deddfa553f85116 Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Wed, 24 Jul 2024 11:38:41 -0400 Subject: [PATCH] fix(test results): accept null branch and add default branch previously the test results upload endpoint did not require a branch field in the data included with the upload but it also did not allow null values for the branch field, so in v0.7.3 of the CLI when we added the branch field to the upload, some uploads were broken because the CLI would not be able to detect a branch and so the branch field would be set to None. This change modifies the endpoint to accept null values for the branch and if the endpoint is creating the Commit object in the database and the branch field is set to null then it will set the branch field of the commit to the default branch of the repo it's targetting. --- upload/tests/views/test_test_results.py | 63 +++++++++++++++++++++++++ upload/views/test_results.py | 4 +- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/upload/tests/views/test_test_results.py b/upload/tests/views/test_test_results.py index a10094e36b..adca7505bf 100644 --- a/upload/tests/views/test_test_results.py +++ b/upload/tests/views/test_test_results.py @@ -36,6 +36,7 @@ def test_upload_test_results(db, client, mocker, mock_redis): "buildURL": "test-build-url", "job": "test-job", "service": "test-service", + "branch": "aaaaaa", }, format="json", headers={"User-Agent": "codecov-cli/0.4.7"}, @@ -63,6 +64,7 @@ def test_upload_test_results(db, client, mocker, mock_redis): # creates commit commit = Commit.objects.get(commitid=commit_sha) assert commit + assert commit.branch is not None # saves args in Redis redis = get_redis_connection() @@ -123,6 +125,7 @@ def test_test_results_org_token(db, client, mocker, mock_redis): { "commit": "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef", "slug": f"{repository.author.username}::::{repository.name}", + "branch": "aaaaaa", }, format="json", ) @@ -157,6 +160,7 @@ def test_test_results_github_oidc_token( { "commit": "6fd5b89357fc8cdf34d6197549ac7c6d7e5977ef", "slug": f"{repository.author.username}::::{repository.name}", + "branch": "aaaaaa", }, format="json", ) @@ -223,6 +227,65 @@ def test_upload_test_results_missing_args(db, client, mocker, mock_redis): assert not upload.called +def test_upload_test_results_missing_branch_no_commit(db, client, mocker, mock_redis): + upload = mocker.patch.object(TaskService, "upload") + mocker.patch( + "services.archive.StorageService.create_presigned_put", + return_value="test-presigned-put", + ) + + repository = RepositoryFactory.create() + + client = APIClient() + client.credentials(HTTP_AUTHORIZATION=f"token {repository.upload_token}") + + commit_sha = "aaaaaa" + res = client.post( + reverse("upload-test-results"), + { + "commit": "aaaaaa", + "slug": f"{repository.author.username}::::{repository.name}", + }, + format="json", + ) + assert res.status_code == 201 + + assert upload.called + + commit = Commit.objects.get(commitid=commit_sha) + assert commit.branch is not None + + +def test_upload_test_results_branch_none_no_commit(db, client, mocker, mock_redis): + upload = mocker.patch.object(TaskService, "upload") + mocker.patch( + "services.archive.StorageService.create_presigned_put", + return_value="test-presigned-put", + ) + + repository = RepositoryFactory.create() + + client = APIClient() + client.credentials(HTTP_AUTHORIZATION=f"token {repository.upload_token}") + + commit_sha = "aaaaaa" + res = client.post( + reverse("upload-test-results"), + { + "commit": "aaaaaa", + "slug": f"{repository.author.username}::::{repository.name}", + "branch": None, + }, + format="json", + ) + assert res.status_code == 201 + + assert upload.called + + commit = Commit.objects.get(commitid=commit_sha) + assert commit.branch is not None + + def test_update_repo_fields_when_upload_is_triggered( db, client, mocker, mock_redis ) -> None: diff --git a/upload/views/test_results.py b/upload/views/test_results.py index 095bd9fa5c..241afe5789 100644 --- a/upload/views/test_results.py +++ b/upload/views/test_results.py @@ -42,7 +42,7 @@ class UploadSerializer(serializers.Serializer): job = serializers.CharField(required=False) flags = FlagListField(required=False) pr = serializers.CharField(required=False) - branch = serializers.CharField(required=False) + branch = serializers.CharField(required=False, allow_null=True) service = serializers.CharField(required=False) storage_path = serializers.CharField(required=False) @@ -105,7 +105,7 @@ def post(self, request): commitid=data["commit"], repository=repo, defaults={ - "branch": data.get("branch"), + "branch": data.get("branch") or repo.branch, "pullid": data.get("pr"), "merged": False if data.get("pr") is not None else None, "state": "pending",