Skip to content

Commit

Permalink
Add change for yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-codecov committed Nov 25, 2024
1 parent 1a9d7a4 commit c217ba7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
34 changes: 34 additions & 0 deletions codecov_auth/commands/owner/interactors/set_yaml_on_owner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Optional

import yaml
from shared.django_apps.core.models import Repository
from shared.django_apps.utils.model_utils import get_ownerid_if_member
from shared.validation.exceptions import InvalidYamlException
from shared.yaml.validation import validate_yaml

Expand Down Expand Up @@ -50,13 +52,45 @@ def convert_yaml_to_dict(self, yaml_input: str) -> Optional[dict]:
message = f"Error at {str(e.error_location)}: {e.error_message}"
raise ValidationError(message)

def yaml_side_effects(self, old_yaml: dict, new_yaml: dict):
old_yaml_branch = old_yaml and old_yaml.get("codecov", {}).get("branch")
new_yaml_branch = new_yaml and new_yaml.get("codecov", {}).get("branch")

# Update all repositories from owner if branch is updated in yaml
if new_yaml_branch != old_yaml_branch:
repos = Repository.objects.filter(author_id=self.owner.ownerid)
repos.update(
branch=new_yaml_branch or old_yaml_branch
) # Keeps old_branch if new_branch is None

old_yaml_bot = old_yaml and old_yaml.get("codecov", {}).get("bot")
new_yaml_bot = new_yaml and new_yaml.get("codecov", {}).get("bot")

# Update owner's bot column if bot is updated in yaml
if new_yaml_bot != old_yaml_bot:
new_bot = (
get_ownerid_if_member(
service=self.owner.service,
owner_username=new_yaml_bot,
owner_id=self.owner.ownerid,
)
or old_yaml_bot
or None
)
self.owner.bot = new_bot
self.owner.save()

@sync_to_async
def execute(self, username: str, yaml_input: str) -> Owner:
self.validate()
self.owner = self.get_owner(username)
self.authorize()
old_yaml = self.owner.yaml
self.owner.yaml = self.convert_yaml_to_dict(yaml_input)
if self.owner.yaml:
self.owner.yaml[OWNER_YAML_TO_STRING_KEY] = yaml_input
self.owner.save()

# side effects
self.yaml_side_effects(old_yaml=old_yaml, new_yaml=self.owner.yaml)
return self.owner
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import pytest
from django.test import TransactionTestCase
from shared.django_apps.core.tests.factories import OwnerFactory
from shared.django_apps.core.tests.factories import OwnerFactory, RepositoryFactory

from codecov.commands.exceptions import (
NotFound,
Unauthenticated,
Unauthorized,
ValidationError,
)
from codecov.db import sync_to_async

from ..set_yaml_on_owner import SetYamlOnOwnerInteractor

Expand All @@ -21,6 +22,18 @@
bot: 'codecov'
"""

good_yaml_with_bot_and_branch = """
codecov:
branch: 'test-1'
bot: 'codecov'
"""

yaml_with_changed_branch_and_bot = """
codecov:
branch: 'test-2'
bot: 'codecov-2'
"""

bad_yaml_not_dict = """
hey
"""
Expand Down Expand Up @@ -143,3 +156,15 @@ async def test_yaml_has_comments(self):
"# comment 3\n"
" #comment 4\n",
}

async def test_user_changes_yaml_bot_and_branch(self):
await sync_to_async(RepositoryFactory)(author=self.org, branch="fake-branch")
owner_updated = await self.execute(
self.current_owner, self.org.username, yaml_with_changed_branch_and_bot
)
# check the interactor returns the right owner
assert owner_updated.ownerid == self.org.ownerid
assert owner_updated.yaml == {
"codecov": {"branch": "test-2", "bot": "codecov-2"},
"to_string": "\ncodecov:\n branch: 'test-2'\n bot: 'codecov-2'\n",
}

0 comments on commit c217ba7

Please sign in to comment.