Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit c217ba7

Browse files
Add change for yaml
1 parent 1a9d7a4 commit c217ba7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

codecov_auth/commands/owner/interactors/set_yaml_on_owner.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from typing import Optional
33

44
import yaml
5+
from shared.django_apps.core.models import Repository
6+
from shared.django_apps.utils.model_utils import get_ownerid_if_member
57
from shared.validation.exceptions import InvalidYamlException
68
from shared.yaml.validation import validate_yaml
79

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

55+
def yaml_side_effects(self, old_yaml: dict, new_yaml: dict):
56+
old_yaml_branch = old_yaml and old_yaml.get("codecov", {}).get("branch")
57+
new_yaml_branch = new_yaml and new_yaml.get("codecov", {}).get("branch")
58+
59+
# Update all repositories from owner if branch is updated in yaml
60+
if new_yaml_branch != old_yaml_branch:
61+
repos = Repository.objects.filter(author_id=self.owner.ownerid)
62+
repos.update(
63+
branch=new_yaml_branch or old_yaml_branch
64+
) # Keeps old_branch if new_branch is None
65+
66+
old_yaml_bot = old_yaml and old_yaml.get("codecov", {}).get("bot")
67+
new_yaml_bot = new_yaml and new_yaml.get("codecov", {}).get("bot")
68+
69+
# Update owner's bot column if bot is updated in yaml
70+
if new_yaml_bot != old_yaml_bot:
71+
new_bot = (
72+
get_ownerid_if_member(
73+
service=self.owner.service,
74+
owner_username=new_yaml_bot,
75+
owner_id=self.owner.ownerid,
76+
)
77+
or old_yaml_bot
78+
or None
79+
)
80+
self.owner.bot = new_bot
81+
self.owner.save()
82+
5383
@sync_to_async
5484
def execute(self, username: str, yaml_input: str) -> Owner:
5585
self.validate()
5686
self.owner = self.get_owner(username)
5787
self.authorize()
88+
old_yaml = self.owner.yaml
5889
self.owner.yaml = self.convert_yaml_to_dict(yaml_input)
5990
if self.owner.yaml:
6091
self.owner.yaml[OWNER_YAML_TO_STRING_KEY] = yaml_input
6192
self.owner.save()
93+
94+
# side effects
95+
self.yaml_side_effects(old_yaml=old_yaml, new_yaml=self.owner.yaml)
6296
return self.owner

codecov_auth/commands/owner/interactors/tests/test_set_yaml_on_owner.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import pytest
22
from django.test import TransactionTestCase
3-
from shared.django_apps.core.tests.factories import OwnerFactory
3+
from shared.django_apps.core.tests.factories import OwnerFactory, RepositoryFactory
44

55
from codecov.commands.exceptions import (
66
NotFound,
77
Unauthenticated,
88
Unauthorized,
99
ValidationError,
1010
)
11+
from codecov.db import sync_to_async
1112

1213
from ..set_yaml_on_owner import SetYamlOnOwnerInteractor
1314

@@ -21,6 +22,18 @@
2122
bot: 'codecov'
2223
"""
2324

25+
good_yaml_with_bot_and_branch = """
26+
codecov:
27+
branch: 'test-1'
28+
bot: 'codecov'
29+
"""
30+
31+
yaml_with_changed_branch_and_bot = """
32+
codecov:
33+
branch: 'test-2'
34+
bot: 'codecov-2'
35+
"""
36+
2437
bad_yaml_not_dict = """
2538
hey
2639
"""
@@ -143,3 +156,15 @@ async def test_yaml_has_comments(self):
143156
"# comment 3\n"
144157
" #comment 4\n",
145158
}
159+
160+
async def test_user_changes_yaml_bot_and_branch(self):
161+
await sync_to_async(RepositoryFactory)(author=self.org, branch="fake-branch")
162+
owner_updated = await self.execute(
163+
self.current_owner, self.org.username, yaml_with_changed_branch_and_bot
164+
)
165+
# check the interactor returns the right owner
166+
assert owner_updated.ownerid == self.org.ownerid
167+
assert owner_updated.yaml == {
168+
"codecov": {"branch": "test-2", "bot": "codecov-2"},
169+
"to_string": "\ncodecov:\n branch: 'test-2'\n bot: 'codecov-2'\n",
170+
}

0 commit comments

Comments
 (0)