-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bundle Analysis: Add mutation for toggling bundle caching configurati…
…on (#1084)
- Loading branch information
1 parent
a0c8267
commit e3a2467
Showing
11 changed files
with
328 additions
and
1 deletion.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
core/commands/repository/interactors/tests/test_update_bundle_cache_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import pytest | ||
from asgiref.sync import async_to_sync | ||
from django.test import TransactionTestCase | ||
from shared.django_apps.bundle_analysis.models import CacheConfig | ||
from shared.django_apps.core.tests.factories import ( | ||
OwnerFactory, | ||
RepositoryFactory, | ||
) | ||
|
||
from codecov.commands.exceptions import ValidationError | ||
|
||
from ..update_bundle_cache_config import UpdateBundleCacheConfigInteractor | ||
|
||
|
||
class UpdateBundleCacheConfigInteractorTest(TransactionTestCase): | ||
databases = {"default"} | ||
|
||
def setUp(self): | ||
self.org = OwnerFactory(username="test-org") | ||
self.repo = RepositoryFactory(author=self.org, name="test-repo", active=True) | ||
self.user = OwnerFactory(permission=[self.repo.pk]) | ||
|
||
@async_to_sync | ||
def execute(self, owner, repo_name=None, cache_config=[]): | ||
return UpdateBundleCacheConfigInteractor(owner, "github").execute( | ||
repo_name=repo_name, | ||
owner_username="test-org", | ||
cache_config=cache_config, | ||
) | ||
|
||
def test_repo_not_found(self): | ||
with pytest.raises(ValidationError): | ||
self.execute(owner=self.user, repo_name="wrong") | ||
|
||
def test_bundle_not_found(self): | ||
with pytest.raises( | ||
ValidationError, match="The following bundle names do not exist: wrong" | ||
): | ||
self.execute( | ||
owner=self.user, | ||
repo_name="test-repo", | ||
cache_config=[{"bundle_name": "wrong", "toggle_caching": True}], | ||
) | ||
|
||
def test_some_bundles_not_found(self): | ||
CacheConfig.objects.create( | ||
repo_id=self.repo.pk, bundle_name="bundle1", is_caching=True | ||
) | ||
with pytest.raises( | ||
ValidationError, match="The following bundle names do not exist: bundle2" | ||
): | ||
self.execute( | ||
owner=self.user, | ||
repo_name="test-repo", | ||
cache_config=[ | ||
{"bundle_name": "bundle1", "toggle_caching": False}, | ||
{"bundle_name": "bundle2", "toggle_caching": True}, | ||
], | ||
) | ||
|
||
def test_update_bundles_successfully(self): | ||
CacheConfig.objects.create( | ||
repo_id=self.repo.pk, bundle_name="bundle1", is_caching=True | ||
) | ||
CacheConfig.objects.create( | ||
repo_id=self.repo.pk, bundle_name="bundle2", is_caching=True | ||
) | ||
|
||
res = self.execute( | ||
owner=self.user, | ||
repo_name="test-repo", | ||
cache_config=[ | ||
{"bundle_name": "bundle1", "toggle_caching": False}, | ||
{"bundle_name": "bundle2", "toggle_caching": True}, | ||
], | ||
) | ||
|
||
assert res == [ | ||
{"bundle_name": "bundle1", "is_cached": False}, | ||
{"bundle_name": "bundle2", "is_cached": True}, | ||
] | ||
|
||
assert len(CacheConfig.objects.all()) == 2 | ||
|
||
query = CacheConfig.objects.filter(repo_id=self.repo.pk, bundle_name="bundle1") | ||
assert len(query) == 1 | ||
assert query[0].is_caching == False | ||
|
||
query = CacheConfig.objects.filter(repo_id=self.repo.pk, bundle_name="bundle2") | ||
assert len(query) == 1 | ||
assert query[0].is_caching == True |
71 changes: 71 additions & 0 deletions
71
core/commands/repository/interactors/update_bundle_cache_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Dict, List | ||
|
||
from shared.django_apps.bundle_analysis.models import CacheConfig | ||
from shared.django_apps.bundle_analysis.service.bundle_analysis import ( | ||
BundleAnalysisCacheConfigService, | ||
) | ||
|
||
from codecov.commands.base import BaseInteractor | ||
from codecov.commands.exceptions import ValidationError | ||
from codecov.db import sync_to_async | ||
from codecov_auth.models import Owner | ||
from core.models import Repository | ||
|
||
|
||
class UpdateBundleCacheConfigInteractor(BaseInteractor): | ||
def validate( | ||
self, repo: Repository, cache_config: List[Dict[str, str | bool]] | ||
) -> None: | ||
if not repo: | ||
raise ValidationError("Repo not found") | ||
|
||
# Find any missing bundle names | ||
bundle_names = [ | ||
bundle["bundle_name"] | ||
for bundle in cache_config | ||
# the value of bundle_name is always a string, just do this check to appease mypy | ||
if isinstance(bundle["bundle_name"], str) | ||
] | ||
existing_bundle_names = set( | ||
CacheConfig.objects.filter( | ||
repo_id=repo.pk, bundle_name__in=bundle_names | ||
).values_list("bundle_name", flat=True) | ||
) | ||
missing_bundles = set(bundle_names) - existing_bundle_names | ||
if missing_bundles: | ||
raise ValidationError( | ||
f"The following bundle names do not exist: {', '.join(missing_bundles)}" | ||
) | ||
|
||
@sync_to_async | ||
def execute( | ||
self, | ||
owner_username: str, | ||
repo_name: str, | ||
cache_config: List[Dict[str, str | bool]], | ||
) -> List[Dict[str, str | bool]]: | ||
author = Owner.objects.filter( | ||
username=owner_username, service=self.service | ||
).first() | ||
repo = ( | ||
Repository.objects.viewable_repos(self.current_owner) | ||
.filter(author=author, name=repo_name) | ||
.first() | ||
) | ||
|
||
self.validate(repo, cache_config) | ||
|
||
results = [] | ||
for bundle in cache_config: | ||
bundle_name = bundle["bundle_name"] | ||
is_caching = bundle["toggle_caching"] | ||
BundleAnalysisCacheConfigService.update_cache_option( | ||
repo.pk, bundle_name, is_caching | ||
) | ||
results.append( | ||
{ | ||
"bundle_name": bundle_name, | ||
"is_cached": is_caching, | ||
} | ||
) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
graphql_api/tests/mutation/test_update_bundle_cache_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import TransactionTestCase | ||
from shared.django_apps.core.tests.factories import OwnerFactory | ||
|
||
from graphql_api.tests.helper import GraphQLTestHelper | ||
|
||
query = """ | ||
mutation($input: ActivateMeasurementsInput!) { | ||
activateMeasurements(input: $input) { | ||
error { | ||
__typename | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
query = """ | ||
mutation UpdateBundleCacheConfig( | ||
$owner: String! | ||
$repoName: String! | ||
$bundles: [BundleCacheConfigInput!]! | ||
) { | ||
updateBundleCacheConfig(input: { | ||
owner: $owner, | ||
repoName: $repoName, | ||
bundles: $bundles | ||
}) { | ||
results { | ||
bundleName | ||
isCached | ||
} | ||
error { | ||
__typename | ||
... on UnauthenticatedError { | ||
message | ||
} | ||
... on ValidationError { | ||
message | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
class UpdateBundleCacheConfigTestCase(GraphQLTestHelper, TransactionTestCase): | ||
def setUp(self): | ||
self.owner = OwnerFactory() | ||
|
||
def test_when_unauthenticated(self): | ||
data = self.gql_request( | ||
query, | ||
variables={ | ||
"owner": "codecov", | ||
"repoName": "test-repo", | ||
"bundles": [{"bundleName": "pr_bundle1", "toggleCaching": True}], | ||
}, | ||
) | ||
assert ( | ||
data["updateBundleCacheConfig"]["error"]["__typename"] | ||
== "UnauthenticatedError" | ||
) | ||
|
||
@patch( | ||
"core.commands.repository.interactors.update_bundle_cache_config.UpdateBundleCacheConfigInteractor.execute" | ||
) | ||
def test_when_authenticated(self, execute): | ||
data = self.gql_request( | ||
query, | ||
owner=self.owner, | ||
variables={ | ||
"owner": "codecov", | ||
"repoName": "test-repo", | ||
"bundles": [{"bundleName": "pr_bundle1", "toggleCaching": True}], | ||
}, | ||
) | ||
assert data == {"updateBundleCacheConfig": {"results": [], "error": None}} |
10 changes: 10 additions & 0 deletions
10
graphql_api/types/inputs/bundle_analysis_cache_config.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
input BundleCacheConfigInput { | ||
bundleName: String! | ||
toggleCaching: Boolean! | ||
} | ||
|
||
input UpdateBundleCacheConfigInput { | ||
owner: String! | ||
repoName: String! | ||
bundles: [BundleCacheConfigInput!]! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
graphql_api/types/mutation/update_bundle_cache_config/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from graphql_api.helpers.ariadne import ariadne_load_local_graphql | ||
|
||
from .update_bundle_cache_config import ( | ||
error_update_bundle_cache_config, | ||
resolve_update_bundle_cache_config, | ||
) | ||
|
||
gql_update_bundle_cache_config = ariadne_load_local_graphql( | ||
__file__, "update_bundle_cache_config.graphql" | ||
) | ||
|
||
__all__ = [ | ||
"error_update_bundle_cache_config", | ||
"resolve_update_bundle_cache_config", | ||
] |
11 changes: 11 additions & 0 deletions
11
graphql_api/types/mutation/update_bundle_cache_config/update_bundle_cache_config.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
union UpdateBundleCacheConfigError = UnauthenticatedError | ValidationError | ||
|
||
type UpdateBundleCacheConfigResult { | ||
bundleName: String | ||
isCached: Boolean | ||
} | ||
|
||
type UpdateBundleCacheConfigPayload { | ||
results: [UpdateBundleCacheConfigResult!] | ||
error: UpdateBundleCacheConfigError | ||
} |
30 changes: 30 additions & 0 deletions
30
graphql_api/types/mutation/update_bundle_cache_config/update_bundle_cache_config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any, Dict, List | ||
|
||
from ariadne import UnionType | ||
from graphql import GraphQLResolveInfo | ||
|
||
from core.commands.repository.repository import RepositoryCommands | ||
from graphql_api.helpers.mutation import ( | ||
require_authenticated, | ||
resolve_union_error_type, | ||
wrap_error_handling_mutation, | ||
) | ||
|
||
|
||
@wrap_error_handling_mutation | ||
@require_authenticated | ||
async def resolve_update_bundle_cache_config( | ||
_: Any, info: GraphQLResolveInfo, input: Dict[str, Any] | ||
) -> Dict[str, List[Dict[str, str | bool]]]: | ||
command: RepositoryCommands = info.context["executor"].get_command("repository") | ||
|
||
results = await command.update_bundle_cache_config( | ||
repo_name=input.get("repo_name", ""), | ||
owner_username=input.get("owner", ""), | ||
cache_config=input.get("bundles", []), | ||
) | ||
return {"results": results} | ||
|
||
|
||
error_update_bundle_cache_config = UnionType("UpdateBundleCacheConfigError") | ||
error_update_bundle_cache_config.type_resolver(resolve_union_error_type) |