Skip to content

Commit

Permalink
Update bot if we see that the bot field has changed in the YAML (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelletran-codecov authored Dec 3, 2024
1 parent eaffc24 commit 2cbfc8c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
https://github.com/codecov/test-results-parser/archive/c840502d1b4dd7d05b2efc2c1328affaf2acd27c.tar.gz#egg=test-results-parser
https://github.com/codecov/shared/archive/f96b72f47583a083e0c78ec7c7ff4da5d3ad6a19.tar.gz#egg=shared
https://github.com/codecov/shared/archive/c481846ba657aee3fc15a613a0fd2c18ac1eabd2.tar.gz#egg=shared
https://github.com/codecov/timestring/archive/d37ceacc5954dff3b5bd2f887936a98a668dda42.tar.gz#egg=timestring
asgiref>=3.7.2
analytics-python==1.3.0b1
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ sentry-sdk==2.13.0
# shared
setuptools==75.6.0
# via nodeenv
shared @ https://github.com/codecov/shared/archive/f96b72f47583a083e0c78ec7c7ff4da5d3ad6a19.tar.gz#egg=shared
shared @ https://github.com/codecov/shared/archive/c481846ba657aee3fc15a613a0fd2c18ac1eabd2.tar.gz#egg=shared
# via -r requirements.in
six==1.16.0
# via
Expand Down
22 changes: 21 additions & 1 deletion services/yaml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
from typing import Any, Mapping

from shared.django_apps.utils.model_utils import get_ownerid_if_member
from shared.torngit.exceptions import TorngitClientError, TorngitError
from shared.validation.exceptions import InvalidYamlException
from shared.yaml import UserYaml
Expand Down Expand Up @@ -113,7 +115,9 @@ async def get_current_yaml(commit: Commit, repository_service) -> dict:
)


def save_repo_yaml_to_database_if_needed(current_commit, new_yaml):
def save_repo_yaml_to_database_if_needed(
current_commit: Commit, new_yaml: UserYaml | Mapping[str, Any]
) -> bool:
repository = current_commit.repository
existing_yaml = get_repo_yaml(repository)
syb = read_yaml_field(existing_yaml, ("codecov", "strict_yaml_branch"))
Expand All @@ -127,6 +131,22 @@ def save_repo_yaml_to_database_if_needed(current_commit, new_yaml):
yaml_branch = read_yaml_field(new_yaml, ("codecov", "branch"))
if yaml_branch:
repository.branch = yaml_branch

maybe_update_repo_bot(new_yaml, repository)
repository.yaml = new_yaml
return True

return False


def maybe_update_repo_bot(
new_yaml: UserYaml | Mapping[str, Any],
repository: Repository,
) -> None:
new_bot_owner_username = read_yaml_field(new_yaml, ("codecov", "bot"))
if new_bot_owner_username:
bot_owner_id = get_ownerid_if_member(
repository.service_id, new_bot_owner_username, repository.ownerid
)
if bot_owner_id and bot_owner_id != repository.bot_id:
repository.bot_id = bot_owner_id
20 changes: 16 additions & 4 deletions services/yaml/tests/test_yaml_saving.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from database.tests.factories import CommitFactory
import pytest

from database.tests.factories import CommitFactory, OwnerFactory
from services.yaml import save_repo_yaml_to_database_if_needed
from test_utils.base import BaseTestCase

Expand All @@ -13,16 +15,26 @@ def test_save_repo_yaml_to_database_if_needed(self, mocker):
assert res
assert commit.repository.yaml == commit_yaml
assert commit.repository.branch == "master"
assert commit.repository.bot_id is None

def test_save_repo_yaml_to_database_if_needed_with_new_branch(self, mocker):
@pytest.mark.django_db
def test_save_repo_yaml_to_database_if_needed_with_new_branch_and_bot(self, mocker):
commit = CommitFactory.create(
branch="master", repository__branch="master", repository__yaml={"old_stuff"}
branch="master",
repository__branch="master",
repository__service_id="github",
repository__yaml={"old_stuff"},
)
commit_yaml = {"new_values": "aHAAA", "codecov": {"branch": "brand_new_branch"}}
bot_owner = OwnerFactory.create(name="robot", service="github")
commit_yaml = {
"new_values": "aHAAA",
"codecov": {"branch": "brand_new_branch", "bot": "robot"},
}
res = save_repo_yaml_to_database_if_needed(commit, commit_yaml)
assert res
assert commit.repository.yaml == commit_yaml
assert commit.repository.branch == "brand_new_branch"
assert commit.repository.bot_id == bot_owner.ownerid

def test_save_repo_yaml_to_database_not_needed(self, mocker):
commit = CommitFactory.create(
Expand Down

0 comments on commit 2cbfc8c

Please sign in to comment.