Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update shared with owners trigger #1002

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions codecov_auth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,10 +379,6 @@ def is_stale(user: dict) -> bool:
)

stale_users = {user.ownerid for user in resolved_users if is_stale(user)}

# TODO: the existing stale user cleanup script clears the `oauth_token`, though the reason for that is not clear?
# Owner.objects.filter(ownerid__in=stale_users).update(oauth_token=None)

affected_orgs = {
org for org in orgs if stale_users.intersection(set(org.plan_activated_users))
}
Expand Down
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",
}
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ factory-boy
fakeredis
freezegun
https://github.com/codecov/opentelem-python/archive/refs/tags/v0.0.4a1.tar.gz#egg=codecovopentelem
https://github.com/codecov/shared/archive/f96b72f47583a083e0c78ec7c7ff4da5d3ad6a19.tar.gz#egg=shared
https://github.com/codecov/shared/archive/c481846ba657aee3fc15a613a0fd2c18ac1eabd2.tar.gz#egg=shared
google-cloud-pubsub
gunicorn>=22.0.0
https://github.com/photocrowd/django-cursor-pagination/archive/f560902696b0c8509e4d95c10ba0d62700181d84.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ sentry-sdk[celery]==2.13.0
# shared
setproctitle==1.1.10
# via -r requirements.in
shared @ https://github.com/codecov/shared/archive/f96b72f47583a083e0c78ec7c7ff4da5d3ad6a19.tar.gz
shared @ https://github.com/codecov/shared/archive/c481846ba657aee3fc15a613a0fd2c18ac1eabd2.tar.gz
# via -r requirements.in
simplejson==3.17.2
# via -r requirements.in
Expand Down
Loading