Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 0fd7a81

Browse files
authored
disable populating log_context based on commit_sha/commit_id (#977)
1 parent 6f971be commit 0fd7a81

File tree

2 files changed

+17
-54
lines changed

2 files changed

+17
-54
lines changed

helpers/log_context.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sentry_sdk import get_current_span
77
from shared.config import get_config
88

9-
from database.models.core import Commit, Owner, Repository
9+
from database.models.core import Owner, Repository
1010

1111
log = logging.getLogger("log_context")
1212

@@ -28,6 +28,11 @@ class LogContext:
2828
repo_name: str | None = None
2929
repo_id: int | None = None
3030
commit_sha: str | None = None
31+
32+
# TODO: begin populating this again or remove it
33+
# we can populate this again if we reduce query load by passing the Commit,
34+
# Owner, and Repository models we use to populate the log context into the
35+
# various task implementations so they don't fetch the same models again
3136
commit_id: int | None = None
3237

3338
checkpoints_data: dict = field(default_factory=lambda: {})
@@ -58,48 +63,13 @@ def populate_from_sqlalchemy(self, dbsession):
5863
return
5964

6065
try:
61-
can_identify_commit = self.commit_id is not None or (
62-
self.commit_sha is not None and self.repo_id is not None
63-
)
64-
65-
# commit_id or (commit_sha + repo_id) is enough to get everything else
66-
if can_identify_commit:
67-
query = (
68-
dbsession.query(
69-
Commit.id_,
70-
Commit.commitid,
71-
Repository.repoid,
72-
Repository.name,
73-
Owner.ownerid,
74-
Owner.username,
75-
Owner.service,
76-
Owner.plan,
77-
)
78-
.join(Commit.repository)
79-
.join(Repository.owner)
80-
)
81-
82-
if self.commit_id is not None:
83-
query = query.filter(Commit.id_ == self.commit_id)
84-
else:
85-
query = query.filter(
86-
Commit.commitid == self.commit_sha,
87-
Commit.repoid == self.repo_id,
88-
)
89-
90-
(
91-
self.commit_id,
92-
self.commit_sha,
93-
self.repo_id,
94-
self.repo_name,
95-
self.owner_id,
96-
self.owner_username,
97-
self.owner_service,
98-
self.owner_plan,
99-
) = query.first()
66+
# - commit_id (or commit_sha + repo_id) is enough to get everything else
67+
# - however, this slams the commit table and we rarely really need the
68+
# DB PK for a commit, so we don't do this.
69+
# - repo_id is enough to get repo and owner
70+
# - owner_id is just enough to get owner
10071

101-
# repo_id is enough to get repo and owner
102-
elif self.repo_id:
72+
if self.repo_id:
10373
query = (
10474
dbsession.query(
10575
Repository.name,
@@ -120,7 +90,6 @@ def populate_from_sqlalchemy(self, dbsession):
12090
self.owner_plan,
12191
) = query.first()
12292

123-
# owner_id is just enough for owner
12493
elif self.owner_id:
12594
query = dbsession.query(
12695
Owner.username, Owner.service, Owner.plan

helpers/tests/unit/test_log_context.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,11 @@ def test_populate_just_commit_sha(dbsession):
7575

7676

7777
def test_populate_just_commit_id(dbsession):
78-
owner, repo, commit = create_db_records(dbsession)
78+
_owner, _repo, commit = create_db_records(dbsession)
7979
log_context = LogContext(commit_id=commit.id_)
8080
log_context.populate_from_sqlalchemy(dbsession)
8181

8282
assert log_context == LogContext(
83-
repo_id=repo.repoid,
84-
repo_name="example-python",
85-
owner_id=owner.ownerid,
86-
owner_username="codecove2e",
87-
owner_service="github",
88-
owner_plan="users-basic",
89-
commit_sha=commit.commitid,
9083
commit_id=commit.id_,
9184
)
9285

@@ -104,7 +97,6 @@ def test_populate_repo_and_commit_sha(dbsession):
10497
owner_service="github",
10598
owner_plan="users-basic",
10699
commit_sha=commit.commitid,
107-
commit_id=commit.id_,
108100
)
109101

110102

@@ -135,7 +127,9 @@ async def check_context_in_coroutine():
135127

136128
def test_as_dict(dbsession, mocker):
137129
owner, repo, commit = create_db_records(dbsession)
138-
log_context = LogContext(commit_id=commit.id_, task_name="foo", task_id="bar")
130+
log_context = LogContext(
131+
commit_sha=commit.commitid, repo_id=repo.repoid, task_name="foo", task_id="bar"
132+
)
139133
log_context.populate_from_sqlalchemy(dbsession)
140134

141135
mock_span = mocker.Mock()
@@ -147,7 +141,7 @@ def test_as_dict(dbsession, mocker):
147141
assert log_context.as_dict() == {
148142
"task_name": "foo",
149143
"task_id": "bar",
150-
"commit_id": commit.id_,
144+
"commit_id": None,
151145
"commit_sha": commit.commitid,
152146
"repo_id": repo.repoid,
153147
"repo_name": repo.name,

0 commit comments

Comments
 (0)