22
33import logging
44
5+ from rest_framework import status
56from rest_framework .request import Request
67from rest_framework .response import Response
78
8- from sentry import options
9+ from sentry import features , options
910from sentry .api .api_owners import ApiOwner
1011from sentry .api .api_publish_status import ApiPublishStatus
1112from sentry .api .base import cell_silo_endpoint
1213from sentry .api .bases .organization import OrganizationEndpoint
1314from sentry .constants import ObjectStatus
1415from sentry .integrations .services .integration import integration_service
1516from sentry .integrations .types import IntegrationProviderSlug
16- from sentry .models .options .project_option import ProjectOption
1717from sentry .models .organization import Organization
18+ from sentry .models .project import Project
1819from sentry .models .repositorysettings import RepositorySettings
1920from sentry .ratelimits .config import RateLimitConfig
20- from sentry .seer .autofix .constants import AutofixAutomationTuningSettings
21+ from sentry .seer .autofix .utils import bulk_get_project_preferences
22+ from sentry .seer .models .project_repository import SeerProjectRepository
23+ from sentry .seer .models .seer_api_models import SeerApiError
2124from sentry .types .ratelimit import RateLimit , RateLimitCategory
2225
2326logger = logging .getLogger (__name__ )
@@ -50,22 +53,29 @@ def is_code_review_enabled(organization_id: int) -> bool:
5053 ).exists ()
5154
5255
53- def is_autofix_enabled (organization_id : int ) -> bool :
56+ def is_autofix_enabled (organization : Organization ) -> bool :
5457 """
5558 Check if autofix/RCA is enabled for any active project in the organization,
56- ie, if any project has sentry:autofix_automation_tuning not set to "off" or None .
59+ ie, if any project has repositories configured in Seer preferences .
5760 """
58- return (
59- ProjectOption .objects .filter (
60- project__organization_id = organization_id ,
61- project__status = ObjectStatus .ACTIVE ,
62- key = "sentry:autofix_automation_tuning" ,
63- )
64- .exclude (value = AutofixAutomationTuningSettings .OFF .value )
65- .exclude (value__isnull = True )
66- .exists ()
61+ if features .has ("organizations:seer-project-settings-read-from-sentry" , organization ):
62+ return SeerProjectRepository .objects .filter (
63+ project__organization_id = organization .id , project__status = ObjectStatus .ACTIVE
64+ ).exists ()
65+
66+ project_ids = list (
67+ Project .objects .filter (
68+ organization_id = organization .id ,
69+ status = ObjectStatus .ACTIVE ,
70+ ).values_list ("id" , flat = True )
6771 )
6872
73+ if not project_ids :
74+ return False
75+
76+ preferences = bulk_get_project_preferences (organization .id , project_ids )
77+ return any (pref and pref .get ("repositories" ) for pref in preferences .values ())
78+
6979
7080@cell_silo_endpoint
7181class OrganizationSeerOnboardingCheck (OrganizationEndpoint ):
@@ -88,9 +98,19 @@ class OrganizationSeerOnboardingCheck(OrganizationEndpoint):
8898
8999 def get (self , request : Request , organization : Organization ) -> Response :
90100 """Check if the organization has completed Seer onboarding/configuration."""
101+ try :
102+ autofix_enabled = is_autofix_enabled (organization )
103+ except SeerApiError as e :
104+ logger .exception (
105+ "seer.onboarding_check.autofix_check_error" ,
106+ extra = {"organization_id" : organization .id , "status_code" : e .status },
107+ )
108+ return Response (
109+ {"detail" : "Failed to check autofix status" },
110+ status = status .HTTP_500_INTERNAL_SERVER_ERROR ,
111+ )
91112 has_scm_integration = has_supported_scm_integration (organization .id )
92113 code_review_enabled = is_code_review_enabled (organization .id )
93- autofix_enabled = is_autofix_enabled (organization .id )
94114 needs_config_reminder = is_in_seer_config_reminder_list (organization )
95115 is_seer_configured = has_scm_integration and (code_review_enabled or autofix_enabled )
96116
0 commit comments