Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitvinnakota-codecov committed May 19, 2024
1 parent 83a5577 commit f9fd14b
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 1 deletion.
48 changes: 48 additions & 0 deletions core/commands/repository/interactors/update_repository.py
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()
8 changes: 8 additions & 0 deletions core/commands/repository/repository.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from codecov.commands.base import BaseCommand
from codecov_auth.models import Owner
from core.models import Repository
from timeseries.models import MeasurementName

from .interactors.activate_measurements import ActivateMeasurementsInteractor
from .interactors.fetch_repository import FetchRepositoryInteractor
from .interactors.get_repository_token import GetRepositoryTokenInteractor
from .interactors.get_upload_token import GetUploadTokenInteractor
from .interactors.regenerate_repository_token import RegenerateRepositoryTokenInteractor
from .interactors.update_repository import UpdateRepositoryInteractor


class RepositoryCommands(BaseCommand):
def fetch_repository(self, owner, name):
return self.get_interactor(FetchRepositoryInteractor).execute(owner, name)

def update_repository(self, owner: Owner, repo: Repository, value: str):
return self.get_interactor(UpdateRepositoryInteractor).execute(
owner, repo, value
)

def get_upload_token(self, repository):
return self.get_interactor(GetUploadTokenInteractor).execute(repository)

Expand Down
1 change: 1 addition & 0 deletions graphql_api/types/mutation/mutation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ type Mutation {
saveSentryState(input: SaveSentryStateInput!): SaveSentryStatePayload
saveTermsAgreement(input: SaveTermsAgreementInput!): SaveTermsAgreementPayload
deleteComponentMeasurements(input: DeleteComponentMeasurementsInput!): DeleteComponentMeasurementsPayload
updateRepository(input: UpdateRepositoryInput!): UpdateRepositoryPayload
}
4 changes: 4 additions & 0 deletions graphql_api/types/mutation/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
resolve_update_default_organization,
)
from .update_profile import error_update_profile, resolve_update_profile
from .update_repository import error_update_repository, resolve_update_repository

mutation_bindable = MutationType()

Expand Down Expand Up @@ -69,6 +70,8 @@
mutation_bindable.field("deleteComponentMeasurements")(
resolve_delete_component_measurements
)
mutation_bindable.field("updateRepository")(resolve_update_repository)


mutation_resolvers = [
mutation_bindable,
Expand All @@ -90,4 +93,5 @@
error_save_terms_agreement,
error_start_trial,
error_cancel_trial,
error_update_repository,
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
union UpdateDefaultOrganizationError = UnauthenticatedError | ValidationError
union UpdateDefaultOrganizationError = UnauthenticatedError | ValidationError | UnauthorizedError

type UpdateDefaultOrganizationPayload {
error: UpdateDefaultOrganizationError
Expand Down
7 changes: 7 additions & 0 deletions graphql_api/types/mutation/update_repository/__init__.py
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"
)
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 graphql_api/types/mutation/update_repository/update_repository.py
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)

0 comments on commit f9fd14b

Please sign in to comment.