99from sentry .seer .models .project_repository import SeerProjectRepository
1010from sentry .silo .base import SiloMode
1111from sentry .testutils .cases import APITestCase
12+ from sentry .testutils .helpers .features import with_feature
1213from sentry .testutils .outbox import outbox_runner
1314from sentry .testutils .silo import assume_test_silo_mode
1415
@@ -145,6 +146,53 @@ def test_get_reads_project_preferences(self, mock_bulk_get_preferences):
145146 },
146147 ]
147148
149+ @with_feature ("organizations:seer-project-settings-read-from-sentry" )
150+ @patch (
151+ "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_read_preferences_from_sentry_db"
152+ )
153+ @patch (
154+ "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_get_project_preferences"
155+ )
156+ def test_get_reads_from_sentry_db (self , mock_bulk_get_preferences , mock_bulk_read_db ):
157+ """When feature flag enabled, reads preferences from Sentry DB instead of Seer API."""
158+ from sentry .seer .models import SeerProjectPreference , SeerRepoDefinition
159+
160+ project = self .create_project (organization = self .organization , name = "DB Read Project" )
161+ project .update_option (
162+ "sentry:autofix_automation_tuning" , AutofixAutomationTuningSettings .MEDIUM .value
163+ )
164+
165+ mock_bulk_read_db .return_value = {
166+ project .id : SeerProjectPreference (
167+ organization_id = self .organization .id ,
168+ project_id = project .id ,
169+ repositories = [
170+ SeerRepoDefinition (
171+ provider = "github" ,
172+ owner = "test-org" ,
173+ name = "test-repo" ,
174+ external_id = "12345" ,
175+ )
176+ ],
177+ automated_run_stopping_point = AutofixStoppingPoint .OPEN_PR .value ,
178+ )
179+ }
180+
181+ response = self .client .get (self .url )
182+
183+ assert response .status_code == 200
184+ mock_bulk_read_db .assert_called_once ()
185+ mock_bulk_get_preferences .assert_not_called ()
186+ assert response .data == [
187+ {
188+ "projectId" : project .id ,
189+ "autofixAutomationTuning" : AutofixAutomationTuningSettings .MEDIUM .value ,
190+ "automatedRunStoppingPoint" : AutofixStoppingPoint .OPEN_PR .value ,
191+ "automationHandoff" : None ,
192+ "reposCount" : 1 ,
193+ }
194+ ]
195+
148196 @patch (
149197 "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_set_project_preferences"
150198 )
@@ -911,18 +959,22 @@ def test_post_rejects_mismatched_repo_name_or_owner(self, mock_bulk_set_preferen
911959 assert response .data ["detail" ] == "Invalid repository"
912960 mock_bulk_set_preferences .assert_not_called ()
913961
962+ @with_feature ("organizations:seer-project-settings-dual-write" )
963+ @patch (
964+ "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_write_preferences_to_sentry_db"
965+ )
914966 @patch (
915967 "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_set_project_preferences"
916968 )
917969 @patch (
918970 "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_get_project_preferences"
919971 )
920- def test_post_creates_seer_project_repository (
921- self , mock_bulk_get_preferences , mock_bulk_set_preferences
972+ def test_post_writes_to_sentry_db (
973+ self , mock_bulk_get_preferences , mock_bulk_set_preferences , mock_bulk_write_db
922974 ):
923- """Test that POST creates SeerProjectRepository when feature flag is enabled ."""
975+ """When feature flag enabled, writes to Sentry DB instead of Seer API ."""
924976 project = self .create_project (organization = self .organization )
925- repo = Repository .objects .create (
977+ Repository .objects .create (
926978 organization_id = self .organization .id ,
927979 name = "test-org/test-repo" ,
928980 provider = "github" ,
@@ -939,23 +991,25 @@ def test_post_creates_seer_project_repository(
939991 "organizationId" : self .organization .id ,
940992 }
941993
942- with self .feature ("organizations:seer-project-settings-dual-write" ):
943- response = self .client .post (
944- self .url ,
945- {
946- "projectIds" : [project .id ],
947- "automatedRunStoppingPoint" : AutofixStoppingPoint .OPEN_PR .value ,
948- "projectRepoMappings" : {
949- str (project .id ): [repo_data ],
950- },
994+ response = self .client .post (
995+ self .url ,
996+ {
997+ "projectIds" : [project .id ],
998+ "automatedRunStoppingPoint" : AutofixStoppingPoint .OPEN_PR .value ,
999+ "projectRepoMappings" : {
1000+ str (project .id ): [repo_data ],
9511001 },
952- )
1002+ },
1003+ )
9531004
9541005 assert response .status_code == 204
9551006
956- seer_repo = SeerProjectRepository .objects .get (project = project )
957- assert seer_repo .repository_id == repo .id
958- assert project .get_option ("sentry:seer_automated_run_stopping_point" ) == "open_pr"
1007+ mock_bulk_write_db .assert_called_once ()
1008+ preferences = mock_bulk_write_db .call_args [0 ][1 ]
1009+ assert len (preferences ) == 1
1010+ assert preferences [0 ].project_id == project .id
1011+ assert preferences [0 ].repositories [0 ].owner == "test-org"
1012+ assert preferences [0 ].repositories [0 ].name == "test-repo"
9591013
9601014 @patch (
9611015 "sentry.seer.endpoints.organization_autofix_automation_settings.bulk_set_project_preferences"
0 commit comments