From 2cbfc8c2ef4ecc5377945788f85217cb13eca6c9 Mon Sep 17 00:00:00 2001 From: michelletran-codecov <167130096+michelletran-codecov@users.noreply.github.com> Date: Tue, 3 Dec 2024 14:47:23 -0500 Subject: [PATCH] Update bot if we see that the bot field has changed in the YAML (#926) --- requirements.in | 2 +- requirements.txt | 2 +- services/yaml/__init__.py | 22 +++++++++++++++++++++- services/yaml/tests/test_yaml_saving.py | 20 ++++++++++++++++---- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/requirements.in b/requirements.in index ddc55ba0e..457f350b4 100644 --- a/requirements.in +++ b/requirements.in @@ -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 diff --git a/requirements.txt b/requirements.txt index 72b6481d3..01e3a66b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/services/yaml/__init__.py b/services/yaml/__init__.py index 66309c566..c362f0cd2 100644 --- a/services/yaml/__init__.py +++ b/services/yaml/__init__.py @@ -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 @@ -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")) @@ -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 diff --git a/services/yaml/tests/test_yaml_saving.py b/services/yaml/tests/test_yaml_saving.py index 292535d7b..3bb512a8c 100644 --- a/services/yaml/tests/test_yaml_saving.py +++ b/services/yaml/tests/test_yaml_saving.py @@ -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 @@ -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(