Skip to content

Commit d01fd09

Browse files
committed
ref(night-shift): Add bulk_read_preferences unified helper
Extract the feature-flag dispatch into a single bulk_read_preferences() helper in autofix/utils.py that returns dict[int, SeerProjectPreference | None] regardless of whether preferences come from Sentry DB or the Seer API. Simplify _get_eligible_projects in the night-shift cron to use it. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Agent transcript: https://claudescope.sentry.dev/share/rCNWD9ykr9zl5zpI-QmbkrWbXJLts9yV0ajkhGPjAsw
1 parent 26e9896 commit d01fd09

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/sentry/seer/autofix/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,23 @@ def _get_project_option(key: str) -> Any:
818818
return result
819819

820820

821+
def bulk_read_preferences(
822+
organization: Organization, project_ids: list[int]
823+
) -> dict[int, SeerProjectPreference | None]:
824+
"""Read Seer project preferences in bulk, using the correct source based on feature flag.
825+
826+
Always returns ``dict[int, SeerProjectPreference | None]`` regardless of the
827+
underlying read path (Sentry DB or Seer API)."""
828+
if features.has("organizations:seer-project-settings-read-from-sentry", organization):
829+
return bulk_read_preferences_from_sentry_db(organization.id, project_ids)
830+
831+
raw = bulk_get_project_preferences(organization.id, project_ids)
832+
return {
833+
int(pid): SeerProjectPreference.validate(data) if data is not None else None
834+
for pid, data in raw.items()
835+
}
836+
837+
821838
def set_project_seer_preference(preference: SeerProjectPreference) -> None:
822839
"""Set Seer project preference for a single project via Seer API."""
823840
response = make_set_project_preference_request(

src/sentry/tasks/seer/night_shift/cron.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
from sentry.models.organization import Organization, OrganizationStatus
1212
from sentry.models.project import Project
1313
from sentry.seer.autofix.constants import AutofixAutomationTuningSettings
14-
from sentry.seer.autofix.utils import (
15-
bulk_get_project_preferences,
16-
bulk_read_preferences_from_sentry_db,
17-
)
14+
from sentry.seer.autofix.utils import bulk_read_preferences
1815
from sentry.seer.models.night_shift import SeerNightShiftRun, SeerNightShiftRunIssue
1916
from sentry.tasks.base import instrumented_task
2017
from sentry.tasks.seer.night_shift.agentic_triage import agentic_triage_strategy
@@ -182,20 +179,13 @@ def _get_eligible_projects(organization: Organization) -> list[Project]:
182179

183180
project_ids = [p.id for p in projects]
184181
project_map = {p.id: p for p in projects}
185-
186-
if features.has("organizations:seer-project-settings-read-from-sentry", organization):
187-
preferences = bulk_read_preferences_from_sentry_db(organization.id, project_ids)
188-
projects_with_prefs = {pid for pid, pref in preferences.items() if pref is not None}
189-
else:
190-
preferences_by_id = bulk_get_project_preferences(organization.id, project_ids)
191-
projects_with_prefs = {
192-
int(pid) for pid, pref in preferences_by_id.items() if pref is not None
193-
}
182+
preferences = bulk_read_preferences(organization, project_ids)
194183

195184
return [
196185
project_map[pid]
197-
for pid in projects_with_prefs
198-
if pid in project_map
186+
for pid, pref in preferences.items()
187+
if pref is not None
188+
and pid in project_map
199189
and project_map[pid].get_option("sentry:autofix_automation_tuning")
200190
!= AutofixAutomationTuningSettings.OFF
201191
]

0 commit comments

Comments
 (0)