From 0f8147b46ca7f059520673ac784411379f2d05ec Mon Sep 17 00:00:00 2001 From: Adrian Date: Mon, 16 Dec 2024 07:48:20 -0700 Subject: [PATCH] 2876 no copilot for ba comment (#953) --- requirements.in | 2 +- requirements.txt | 2 +- .../notify/contexts/__init__.py | 3 ++ services/owner.py | 7 +++- services/repository.py | 11 ++++- services/tests/test_owner_service.py | 33 +++++++++++++++ services/tests/test_repository_service.py | 41 +++++++++++++++++++ tasks/test_results_finisher.py | 5 ++- 8 files changed, 99 insertions(+), 5 deletions(-) diff --git a/requirements.in b/requirements.in index 1ac8da5a2..a612078d8 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/8c5de3fadd3c987304043c6cfd64864f372d674f.tar.gz#egg=shared +https://github.com/codecov/shared/archive/e41a4a1d4dd4efe44a917b47032f1a0d73775426.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 2140731ad..03749d1be 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/8c5de3fadd3c987304043c6cfd64864f372d674f.tar.gz#egg=shared +shared @ https://github.com/codecov/shared/archive/e41a4a1d4dd4efe44a917b47032f1a0d73775426.tar.gz#egg=shared # via -r requirements.in six==1.16.0 # via diff --git a/services/bundle_analysis/notify/contexts/__init__.py b/services/bundle_analysis/notify/contexts/__init__.py index 414b09b91..139813558 100644 --- a/services/bundle_analysis/notify/contexts/__init__.py +++ b/services/bundle_analysis/notify/contexts/__init__.py @@ -6,6 +6,7 @@ import shared.storage from shared.bundle_analysis import BundleAnalysisReport, BundleAnalysisReportLoader from shared.torngit.base import TorngitBaseAdapter +from shared.typings.torngit import AdditionalData, UploadType from shared.validation.types import BundleThreshold from shared.yaml import UserYaml @@ -79,9 +80,11 @@ def repository(self) -> Repository: @cached_property def repository_service(self) -> TorngitBaseAdapter: + additional_data: AdditionalData = {"upload_type": UploadType.BUNDLE_ANALYSIS} return get_repo_provider_service( self.repository, installation_name_to_use=self.gh_app_installation_name, + additional_data=additional_data, ) commit_report = NotificationContextField[CommitReport]() diff --git a/services/owner.py b/services/owner.py index 5051e9f98..ac7e0771c 100644 --- a/services/owner.py +++ b/services/owner.py @@ -14,7 +14,9 @@ log = logging.getLogger(__name__) -def get_owner_provider_service(owner, *, ignore_installation=False): +def get_owner_provider_service( + owner, *, ignore_installation=False, additional_data=None +): _timeouts = [ get_config("setup", "http", "timeouts", "connect", default=15), get_config("setup", "http", "timeouts", "receive", default=30), @@ -23,12 +25,15 @@ def get_owner_provider_service(owner, *, ignore_installation=False): adapter_auth_info = get_adapter_auth_information( owner, ignore_installations=ignore_installation ) + if additional_data is None: + additional_data = {} data = TorngitInstanceData( owner=OwnerInfo( service_id=owner.service_id, ownerid=owner.ownerid, username=owner.username ), installation=adapter_auth_info["selected_installation_info"], fallback_installations=adapter_auth_info["fallback_installations"], + additional_data=additional_data, ) adapter_params = dict( diff --git a/services/repository.py b/services/repository.py index 86f074915..8d2a56baa 100644 --- a/services/repository.py +++ b/services/repository.py @@ -15,7 +15,12 @@ TorngitError, TorngitObjectNotFoundError, ) -from shared.typings.torngit import OwnerInfo, RepoInfo, TorngitInstanceData +from shared.typings.torngit import ( + AdditionalData, + OwnerInfo, + RepoInfo, + TorngitInstanceData, +) from shared.validation.exceptions import InvalidYamlException from shared.yaml import UserYaml from shared.yaml.user_yaml import OwnerContext @@ -39,12 +44,15 @@ def get_repo_provider_service( repository: Repository, installation_name_to_use: str = GITHUB_APP_INSTALLATION_DEFAULT_NAME, + additional_data: AdditionalData = None, ) -> TorngitBaseAdapter: adapter_auth_info = get_adapter_auth_information( repository.owner, repository=repository, installation_name_to_use=installation_name_to_use, ) + if additional_data is None: + additional_data = {} data = TorngitInstanceData( repo=RepoInfo( name=repository.name, @@ -61,6 +69,7 @@ def get_repo_provider_service( ), installation=adapter_auth_info["selected_installation_info"], fallback_installations=adapter_auth_info["fallback_installations"], + additional_data=additional_data, ) adapter_params = dict( diff --git a/services/tests/test_owner_service.py b/services/tests/test_owner_service.py index ab1e454bd..67a63138f 100644 --- a/services/tests/test_owner_service.py +++ b/services/tests/test_owner_service.py @@ -1,4 +1,6 @@ from shared.rate_limits import gh_app_key_name, owner_key_name +from shared.reports.types import UploadType +from shared.typings.torngit import AdditionalData from database.models.core import ( GITHUB_APP_INSTALLATION_DEFAULT_NAME, @@ -27,6 +29,7 @@ def test_get_owner_provider_service(self, dbsession): "repo": {}, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.service == "github" assert res.data == expected_data @@ -70,6 +73,7 @@ def test_get_owner_provider_service_with_installation(self, dbsession, mocker): "app_id": None, }, "fallback_installations": [], + "additional_data": {}, } assert res.service == "github" assert res.data == expected_data @@ -96,6 +100,7 @@ def test_get_owner_provider_service_other_service(self, dbsession): "repo": {}, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.service == "gitlab" assert res.data == expected_data @@ -123,6 +128,7 @@ def test_get_owner_provider_service_different_bot(self, dbsession): "repo": {}, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data["repo"] == expected_data["repo"] assert res.data == expected_data @@ -131,3 +137,30 @@ def test_get_owner_provider_service_different_bot(self, dbsession): "secret": None, "entity_name": owner_key_name(owner.bot.ownerid), } + + def test_get_owner_provider_service_additional_data(self, dbsession): + owner = OwnerFactory.create( + service="gitlab", unencrypted_oauth_token="testenll80qbqhofao65", bot=None + ) + dbsession.add(owner) + dbsession.flush() + additional_data: AdditionalData = {"upload_type": UploadType.BUNDLE_ANALYSIS} + res = get_owner_provider_service(owner, additional_data=additional_data) + expected_data = { + "owner": { + "ownerid": owner.ownerid, + "service_id": owner.service_id, + "username": owner.username, + }, + "repo": {}, + "installation": None, + "fallback_installations": None, + "additional_data": {"upload_type": UploadType.BUNDLE_ANALYSIS}, + } + assert res.service == "gitlab" + assert res.data == expected_data + assert res.token == { + "key": "testenll80qbqhofao65", + "secret": None, + "entity_name": owner_key_name(owner.ownerid), + } diff --git a/services/tests/test_repository_service.py b/services/tests/test_repository_service.py index 001fcbb49..0bd6ec817 100644 --- a/services/tests/test_repository_service.py +++ b/services/tests/test_repository_service.py @@ -7,6 +7,7 @@ from freezegun import freeze_time from shared.encryption.oauth import get_encryptor_from_configuration from shared.rate_limits import gh_app_key_name, owner_key_name +from shared.reports.types import UploadType from shared.torngit.base import TorngitBaseAdapter from shared.torngit.exceptions import ( TorngitClientError, @@ -14,6 +15,7 @@ TorngitServerUnreachableError, ) from shared.typings.torngit import ( + AdditionalData, GithubInstallationInfo, OwnerInfo, RepoInfo, @@ -83,6 +85,38 @@ def test_get_repo_provider_service_github(dbsession, repo): }, "installation": None, "fallback_installations": None, + "additional_data": {}, + } + assert res.data == expected_data + assert repo.owner.service == "github" + assert res._on_token_refresh is not None + assert inspect.isawaitable(res._on_token_refresh(None)) + assert res.token == { + "username": repo.owner.username, + "key": "testyftq3ovzkb3zmt823u3t04lkrt9w", + "secret": None, + "entity_name": owner_key_name(repo.owner.ownerid), + } + + +def test_get_repo_provider_service_additional_data(dbsession, repo): + additional_data: AdditionalData = {"upload_type": UploadType.TEST_RESULTS} + res = get_repo_provider_service(repo, additional_data=additional_data) + expected_data = { + "owner": { + "ownerid": repo.owner.ownerid, + "service_id": repo.owner.service_id, + "username": repo.owner.username, + }, + "repo": { + "name": "example-python", + "using_integration": False, + "service_id": repo.service_id, + "repoid": repo.repoid, + }, + "installation": None, + "fallback_installations": None, + "additional_data": {"upload_type": UploadType.TEST_RESULTS}, } assert res.data == expected_data assert repo.owner.service == "github" @@ -146,6 +180,7 @@ def test_get_repo_provider_service_github_with_installations(dbsession, mocker, "pem_path": None, } ], + "additional_data": {}, } assert res.data == expected_data assert repo.owner.service == "github" @@ -182,6 +217,7 @@ def test_get_repo_provider_service_bitbucket(dbsession): }, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data == expected_data assert repo.owner.service == "bitbucket" @@ -217,6 +253,7 @@ def test_get_repo_provider_service_with_token_refresh_callback(dbsession): }, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data == expected_data assert res._on_token_refresh is not None @@ -253,6 +290,7 @@ def test_get_repo_provider_service_repo_bot(dbsession, mock_configuration): }, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data == expected_data assert res.token == { @@ -309,6 +347,7 @@ def test_get_repo_provider_service_different_bot(dbsession): }, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data["repo"] == expected_data["repo"] assert res.data == expected_data @@ -347,6 +386,7 @@ def test_get_repo_provider_service_no_bot(dbsession): }, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data == expected_data assert res.token == { @@ -1093,6 +1133,7 @@ async def test_get_repo_gh_no_integration(dbsession, mocker): }, "installation": None, "fallback_installations": None, + "additional_data": {}, } assert res.data["repo"] == expected_data["repo"] assert res.data == expected_data diff --git a/tasks/test_results_finisher.py b/tasks/test_results_finisher.py index 1c6c7050c..2036b2431 100644 --- a/tasks/test_results_finisher.py +++ b/tasks/test_results_finisher.py @@ -3,6 +3,8 @@ from typing import Any from asgiref.sync import async_to_sync +from shared.reports.types import UploadType +from shared.typings.torngit import AdditionalData from shared.yaml import UserYaml from sqlalchemy.orm import Session @@ -230,7 +232,8 @@ def process_impl_within_lock( "queue_notify": True, } - repo_service = get_repo_provider_service(repo) + additional_data: AdditionalData = {"upload_type": UploadType.TEST_RESULTS} + repo_service = get_repo_provider_service(repo, additional_data=additional_data) pull = async_to_sync(fetch_and_update_pull_request_information_from_commit)( repo_service, commit, commit_yaml )