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

Simplifies get_installation_name a bit #749

Merged
merged 1 commit into from
Sep 30, 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
14 changes: 7 additions & 7 deletions helpers/github_installation.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import logging

from sqlalchemy.orm import Session

from database.models.core import (
GITHUB_APP_INSTALLATION_DEFAULT_NAME,
Owner,
OwnerInstallationNameToUseForTask,
)
from helpers.cache import cache

log = logging.getLogger(__file__)


@cache.cache_function(ttl=86400) # 1 day
def get_installation_name_for_owner_for_task(
dbsession: Session, task_name: str, owner: Owner
) -> str:
def get_installation_name_for_owner_for_task(task_name: str, owner: Owner) -> str:
if owner.service not in ["github", "github_enterprise"]:
# The `installation` concept only exists in GitHub.
# We still return a default here, primarily to satisfy types.
return GITHUB_APP_INSTALLATION_DEFAULT_NAME

dbsession = owner.get_db_session()
config_for_owner = (
dbsession.query(OwnerInstallationNameToUseForTask)
.filter(
Expand Down
9 changes: 4 additions & 5 deletions helpers/tests/unit/test_github_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def test_get_installation_name_for_owner_for_task(dbsession: Session):
owner = OwnerFactory()
owner = OwnerFactory(service="github")
other_owner = OwnerFactory()
task_name = "app.tasks.notify.Notify"
installation_task_config = OwnerInstallationNameToUseForTask(
Expand All @@ -21,14 +21,13 @@ def test_get_installation_name_for_owner_for_task(dbsession: Session):
dbsession.add_all([owner, installation_task_config])
dbsession.flush()
assert (
get_installation_name_for_owner_for_task(dbsession, task_name, owner)
== "my_installation"
get_installation_name_for_owner_for_task(task_name, owner) == "my_installation"
)
assert (
get_installation_name_for_owner_for_task(dbsession, task_name, other_owner)
get_installation_name_for_owner_for_task(task_name, other_owner)
== GITHUB_APP_INSTALLATION_DEFAULT_NAME
)
assert (
get_installation_name_for_owner_for_task(dbsession, "other_task", owner)
get_installation_name_for_owner_for_task("other_task", owner)
== GITHUB_APP_INSTALLATION_DEFAULT_NAME
)
14 changes: 9 additions & 5 deletions services/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from database.enums import CommitErrorTypes
from database.models import Commit, Owner, Pull, Repository
from database.models.core import GITHUB_APP_INSTALLATION_DEFAULT_NAME
from helpers.cache import cache
from helpers.save_commit_error import save_commit_error
from helpers.token_refresh import get_token_refresh_callback
from services.github import get_github_app_for_commit
Expand All @@ -45,7 +46,7 @@
def get_repo_provider_service_for_specific_commit(
commit: Commit,
fallback_installation_name: str = GITHUB_APP_INSTALLATION_DEFAULT_NAME,
) -> torngit.base.TorngitBaseAdapter:
) -> TorngitBaseAdapter:
"""Gets a Torngit adapter (potentially) using a specific GitHub app as the authentication source.
If the commit doesn't have a particular app assigned to it, return regular `get_repo_provider_service` choice

Expand Down Expand Up @@ -84,13 +85,16 @@ def get_repo_provider_service_for_specific_commit(
on_token_refresh=None,
**data,
)
return _get_repo_provider_service_instance(repository.service, **adapter_params)
return _get_repo_provider_service_instance(repository.service, adapter_params)


@sentry_sdk.trace
@cache.cache_function(
ttl=5 * 60 # this TTL is quite conservative, it could potentially be higher
)
def get_repo_provider_service(
repository: Repository,
installation_name_to_use: Optional[str] = GITHUB_APP_INSTALLATION_DEFAULT_NAME,
installation_name_to_use: str = GITHUB_APP_INSTALLATION_DEFAULT_NAME,
) -> TorngitBaseAdapter:
adapter_auth_info = get_adapter_auth_information(
repository.owner,
Expand Down Expand Up @@ -121,10 +125,10 @@ def get_repo_provider_service(
on_token_refresh=get_token_refresh_callback(adapter_auth_info["token_owner"]),
**data,
)
return _get_repo_provider_service_instance(repository.service, **adapter_params)
return _get_repo_provider_service_instance(repository.service, adapter_params)


def _get_repo_provider_service_instance(service: str, **adapter_params):
def _get_repo_provider_service_instance(service: str, adapter_params: dict):
_timeouts = [
get_config("setup", "http", "timeouts", "connect", default=30),
get_config("setup", "http", "timeouts", "receive", default=60),
Expand Down
10 changes: 6 additions & 4 deletions services/tests/test_repository_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,10 +1178,12 @@ def test_get_repo_provider_service_for_specific_commit(
)
mock_get_instance.assert_called_with(
"github",
**data,
token="the app token",
token_type_mapping=None,
on_token_refresh=None,
dict(
**data,
token="the app token",
token_type_mapping=None,
on_token_refresh=None,
),
)


Expand Down
2 changes: 1 addition & 1 deletion tasks/bundle_analysis_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def process_impl_within_lock(
}

installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, commit.repository.owner
self.name, commit.repository.owner
)
notifier = BundleAnalysisNotifyService(
commit, commit_yaml, gh_app_installation_name=installation_name_to_use
Expand Down
7 changes: 2 additions & 5 deletions tasks/commit_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

from asgiref.sync import async_to_sync
from shared.celery_config import commit_update_task_name
from shared.torngit.exceptions import (
TorngitClientError,
TorngitRepoNotFoundError,
)
from shared.torngit.exceptions import TorngitClientError, TorngitRepoNotFoundError

from app import celery_app
from database.models import Commit
Expand Down Expand Up @@ -39,7 +36,7 @@ def run_impl(
was_updated = False
try:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, repository.owner
self.name, repository.owner
)
repository_service = get_repo_provider_service(
repository, installation_name_to_use=installation_name_to_use
Expand Down
2 changes: 1 addition & 1 deletion tasks/compute_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def run_impl(
log.info("Computing comparison", extra=log_extra)
current_yaml = get_repo_yaml(repo)
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, repo.owner
self.name, repo.owner
)

comparison_proxy = self.get_comparison_proxy(
Expand Down
8 changes: 3 additions & 5 deletions tasks/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,9 @@ def run_impl_within_lock(
}

try:
installation_name_to_use = None
if commit.repository.owner.service in ["github", "github_enterprise"]:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, commit.repository.owner
)
installation_name_to_use = get_installation_name_for_owner_for_task(
self.name, commit.repository.owner
)
repository_service = get_repo_provider_service(
commit.repository, installation_name_to_use=installation_name_to_use
)
Expand Down
2 changes: 1 addition & 1 deletion tasks/preprocess_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def process_impl_within_lock(
)
assert commit, "Commit not found in database."
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, commit.repository.owner
self.name, commit.repository.owner
)
repository_service = self.get_repo_service(commit, installation_name_to_use)
if repository_service is None:
Expand Down
2 changes: 1 addition & 1 deletion tasks/save_report_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def run_impl(

try:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, commit.repository.owner
self.name, commit.repository.owner
)
repository_service = get_repo_provider_service(
commit.repository, installation_name_to_use=installation_name_to_use
Expand Down
6 changes: 2 additions & 4 deletions tasks/sync_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
from helpers.exceptions import RepositoryWithoutValidBotError
from helpers.github_installation import get_installation_name_for_owner_for_task
from helpers.metrics import metrics
from rollouts import (
SYNC_PULL_USE_MERGE_COMMIT_SHA,
)
from rollouts import SYNC_PULL_USE_MERGE_COMMIT_SHA
from services.comparison.changes import get_changes
from services.redis import get_redis_connection
from services.report import Report, ReportService
Expand Down Expand Up @@ -113,7 +111,7 @@ def run_impl_within_lock(
assert repository
try:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, repository.owner
self.name, repository.owner
)
repository_service = get_repo_provider_service(
repository, installation_name_to_use=installation_name_to_use
Expand Down
2 changes: 1 addition & 1 deletion tasks/sync_repo_languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def run_impl(
)
log.info("Syncing repository languages", extra=log_extra)
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, repository.owner
self.name, repository.owner
)
repository_service = get_repo_provider_service(
repository, installation_name_to_use=installation_name_to_use
Expand Down
2 changes: 1 addition & 1 deletion tasks/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def run_impl_within_lock(
was_updated, was_setup = False, False
try:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, repository.owner
self.name, repository.owner
)
repository_service = get_repo_provider_service(
repository, installation_name_to_use=installation_name_to_use
Expand Down
2 changes: 1 addition & 1 deletion tasks/upload_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def save_report_results(
commitid = commit.commitid
try:
installation_name_to_use = get_installation_name_for_owner_for_task(
db_session, self.name, repository.owner
self.name, repository.owner
)
repository_service = get_repo_provider_service(
repository, installation_name_to_use=installation_name_to_use
Expand Down
Loading