Skip to content

Commit 8c9035e

Browse files
authored
refactor(worker): Inject repository service into ReportService and optimize commit query
1 parent 13ff78c commit 8c9035e

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

apps/worker/services/report/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from shared.reports.resources import Report
4949
from shared.reports.types import TOTALS_MAP
5050
from shared.storage.exceptions import FileNotInStorageError
51+
from shared.torngit.base import TorngitBaseAdapter
5152
from shared.torngit.exceptions import TorngitError
5253
from shared.upload.constants import UploadErrorCode
5354
from shared.utils.sessions import Session, SessionType
@@ -192,11 +193,15 @@ def create_report_upload(
192193

193194
class ReportService(BaseReportService):
194195
def __init__(
195-
self, current_yaml: UserYaml | dict, gh_app_installation_name: str | None = None
196+
self,
197+
current_yaml: UserYaml | dict,
198+
gh_app_installation_name: str | None = None,
199+
repository_service: TorngitBaseAdapter | None = None,
196200
):
197201
super().__init__(current_yaml)
198202
self.flag_dict: dict[str, RepositoryFlag] | None = None
199203
self.gh_app_installation_name = gh_app_installation_name
204+
self.repository_service = repository_service
200205

201206
def has_initialized_report(self, commit: Commit) -> bool:
202207
"""
@@ -413,7 +418,7 @@ def _possibly_shift_carryforward_report(
413418
self, carryforward_report: Report, base_commit: Commit, head_commit: Commit
414419
) -> Report:
415420
try:
416-
provider_service = get_repo_provider_service(
421+
provider_service = self.repository_service or get_repo_provider_service(
417422
repository=head_commit.repository,
418423
installation_name_to_use=self.gh_app_installation_name,
419424
)

apps/worker/tasks/preprocess_upload.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
22

3+
from sqlalchemy.orm import joinedload
4+
35
from app import celery_app
46
from database.enums import CommitErrorTypes
57
from database.models import Commit
8+
from database.models.core import GithubAppInstallation, Owner, Repository
69
from helpers.exceptions import RepositoryWithoutValidBotError
710
from helpers.github_installation import get_installation_name_for_owner_for_task
811
from helpers.save_commit_error import save_commit_error
@@ -99,6 +102,11 @@ def process_impl_within_lock(self, db_session, repoid, commitid):
99102
commit = (
100103
db_session.query(Commit)
101104
.filter(Commit.repoid == repoid, Commit.commitid == commitid)
105+
.options(
106+
joinedload(Commit.repository).joinedload(Repository.author).joinedload(
107+
Owner.github_app_installations
108+
)
109+
)
102110
.first()
103111
)
104112
assert commit, "Commit not found in database."
@@ -123,7 +131,9 @@ def process_impl_within_lock(self, db_session, repoid, commitid):
123131
)
124132
commit_yaml = fetch_commit_yaml_and_possibly_store(commit, repository_service)
125133
report_service = ReportService(
126-
commit_yaml, gh_app_installation_name=installation_name_to_use
134+
commit_yaml,
135+
gh_app_installation_name=installation_name_to_use,
136+
repository_service=repository_service,
127137
)
128138
commit_report = report_service.initialize_and_save_report(commit)
129139
# Persist changes from within the lock

0 commit comments

Comments
 (0)