-
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.
- Loading branch information
1 parent
83a5577
commit f9fd14b
Showing
8 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
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,48 @@ | ||
from typing import Optional | ||
|
||
from django.conf import settings | ||
|
||
import services.self_hosted as self_hosted | ||
from codecov.commands.base import BaseInteractor | ||
from codecov.commands.exceptions import Unauthenticated, Unauthorized, ValidationError | ||
from codecov.db import sync_to_async | ||
from codecov_auth.models import Owner | ||
from core.models import Repository | ||
|
||
|
||
class UpdateRepositoryInteractor(BaseInteractor): | ||
def validate_owner(self, owner: Owner): | ||
if not self.current_user.is_authenticated: | ||
raise Unauthenticated() | ||
|
||
if settings.IS_ENTERPRISE: | ||
if not self_hosted.is_admin_owner(self.current_owner): | ||
raise Unauthorized() | ||
else: | ||
if not owner.is_admin(self.current_owner): | ||
raise Unauthorized() | ||
|
||
@sync_to_async | ||
def execute( | ||
self, | ||
repo_name: str, | ||
owner: Owner, | ||
default_branch: Optional[str], | ||
activated: Optional[bool], | ||
): | ||
self.validate_owner(owner) | ||
repo = Repository.objects.filter(author_id=owner.pk, name=repo_name).first() | ||
if not repo: | ||
raise ValidationError("Repo not found") | ||
|
||
if default_branch: | ||
branch = repo.branches.filter(name=default_branch).first() | ||
if branch is None: | ||
raise ValidationError( | ||
f"The branch '{default_branch}' is not in our records. Please provide a valid branch name.", | ||
) | ||
|
||
repo.branch = default_branch | ||
if activated: | ||
repo.activated = activated | ||
repo.save() |
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
2 changes: 1 addition & 1 deletion
2
graphql_api/types/mutation/update_default_organization/update_default_organization.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
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,7 @@ | ||
from graphql_api.helpers.ariadne import ariadne_load_local_graphql | ||
|
||
from .update_repository import error_update_repository, resolve_update_repository | ||
|
||
gql_update_repository = ariadne_load_local_graphql( | ||
__file__, "update_repository.graphql" | ||
) |
10 changes: 10 additions & 0 deletions
10
graphql_api/types/mutation/update_repository/update_repository.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 @@ | ||
union UpdateRepositoryError = UnauthenticatedError | ValidationError | ||
|
||
type UpdateRepositoryPayload { | ||
error: UpdateRepositoryError | ||
} | ||
|
||
input UpdateRepositoryInput { | ||
branch: String | ||
activated: Boolean | ||
} |
25 changes: 25 additions & 0 deletions
25
graphql_api/types/mutation/update_repository/update_repository.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,25 @@ | ||
from ariadne import UnionType | ||
|
||
from graphql_api.helpers.mutation import ( | ||
resolve_union_error_type, | ||
wrap_error_handling_mutation, | ||
) | ||
|
||
|
||
@wrap_error_handling_mutation | ||
async def resolve_update_repository(_, info, input): | ||
command = info.context["executor"].get_command("repository") | ||
owner = info.context["request"].current_owner | ||
repo_name = input.get("repoName") | ||
default_branch = input.get("branch") | ||
activated = input.get("activated") | ||
await command.update_repository( | ||
repo_name, | ||
owner, | ||
default_branch, | ||
activated, | ||
) | ||
|
||
|
||
error_update_repository = UnionType("UpdateRepositoryError") | ||
error_update_repository.type_resolver(resolve_union_error_type) |