Skip to content

Commit

Permalink
perf: remove extra refresh (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanni-guidini authored Oct 16, 2024
1 parent 220be0a commit 7ec53d8
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions services/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
from shared.validation.exceptions import InvalidYamlException
from shared.yaml import UserYaml
from shared.yaml.user_yaml import OwnerContext
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Query
from sqlalchemy.orm import Query, Session, lazyload

from database.enums import CommitErrorTypes
from database.models import Commit, Owner, Pull, Repository
Expand Down Expand Up @@ -530,47 +529,46 @@ async def _pick_best_base_comparedto_pair(

@sentry_sdk.trace
async def fetch_and_update_pull_request_information(
repository_service, db_session, repoid, pullid, current_yaml
repository_service,
db_session: Session,
repoid: int | str,
pullid: int | str,
current_yaml,
) -> EnrichedPull:
pull = (
db_session.query(Pull)
.options(lazyload("repository"))
.filter_by(pullid=pullid, repoid=repoid)
.first()
)
try:
pull_information = await repository_service.get_pull_request(pullid=pullid)
except TorngitClientError:
log.warning(
"Unable to find pull request information on provider to update it due to client error",
extra=dict(repoid=repoid, pullid=pullid),
)
pull = db_session.query(Pull).filter_by(pullid=pullid, repoid=repoid).first()
return EnrichedPull(database_pull=pull, provider_pull=None)
except TorngitError:
log.warning(
"Unable to find pull request information on provider to update it due to unknown provider error",
extra=dict(repoid=repoid, pullid=pullid),
)
pull = db_session.query(Pull).filter_by(pullid=pullid, repoid=repoid).first()
return EnrichedPull(database_pull=pull, provider_pull=None)
db_session.flush()
command = (
insert(Pull.__table__)
.values(
pullid=pullid,
if not pull:
pull = Pull(
repoid=repoid,
issueid=pull_information["id"],
pullid=pullid,
state=pull_information["state"],
title=pull_information["title"],
issueid=pull_information["id"],
)
.on_conflict_do_update(
index_elements=[Pull.repoid, Pull.pullid],
set_=dict(
issueid=pull_information["id"],
state=pull_information["state"],
title=pull_information["title"],
),
)
)
db_session.connection().execute(command)
db_session.flush()
pull = db_session.query(Pull).filter_by(pullid=pullid, repoid=repoid).first()
db_session.refresh(pull)
db_session.add(pull)
db_session.flush()
else:
pull.state = pull_information["state"]
pull.title = pull_information["title"]
pull.issueid = pull_information["id"]
base_commit_sha, compared_to = await _pick_best_base_comparedto_pair(
repository_service, pull, current_yaml, pull_information
)
Expand Down

0 comments on commit 7ec53d8

Please sign in to comment.