Skip to content

Commit 72996f2

Browse files
fix(ai-code-review): Add on_new_commit flag
1 parent 2233b18 commit 72996f2

File tree

6 files changed

+128
-7
lines changed

6 files changed

+128
-7
lines changed

src/sentry/features/temporary.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def register_temporary_features(manager: FeatureManager) -> None:
7878
manager.add("organizations:chonk-ui-feedback", OrganizationFeature, FeatureHandlerStrategy.FLAGPOLE, api_expose=True)
7979
# Enables Codecov UI
8080
manager.add("organizations:codecov-ui", OrganizationFeature, FeatureHandlerStrategy.FLAGPOLE, api_expose=True)
81+
# Enable Prevent AI code review to run per commit
82+
manager.add("organizations:code-review-run-per-commit", OrganizationFeature, FeatureHandlerStrategy.FLAGPOLE, api_expose=False)
8183
# Enables Prevent Test Analytics
8284
manager.add("organizations:prevent-test-analytics", OrganizationFeature, FeatureHandlerStrategy.FLAGPOLE, api_expose=True)
8385
# Enable the improved command menu (Cmd+K)

src/sentry/overwatch/endpoints/overwatch_rpc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from sentry.models.organization import Organization
2626
from sentry.models.repository import Repository
2727
from sentry.prevent.models import PreventAIConfiguration
28-
from sentry.prevent.types.config import PREVENT_AI_CONFIG_DEFAULT
28+
from sentry.prevent.types.config import PREVENT_AI_CONFIG_DEFAULT, PREVENT_AI_CONFIG_DEFAULT_V1
2929
from sentry.silo.base import SiloMode
3030

3131
logger = logging.getLogger(__name__)
@@ -154,7 +154,13 @@ def get(self, request: Request) -> Response:
154154
integration_id=github_org_integrations[0].integration_id,
155155
).first()
156156

157-
response_data: dict[str, Any] = deepcopy(PREVENT_AI_CONFIG_DEFAULT)
157+
organization = Organization.objects.filter(id=sentry_org_id).first()
158+
159+
default_config = PREVENT_AI_CONFIG_DEFAULT
160+
if features.has("organizations:code-review-run-per-commit", organization):
161+
default_config = PREVENT_AI_CONFIG_DEFAULT_V1
162+
163+
response_data: dict[str, Any] = deepcopy(default_config)
158164
if config:
159165
response_data["organization"] = config.data
160166

src/sentry/prevent/endpoints/pr_review_github_config.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from rest_framework.request import Request
66
from rest_framework.response import Response
77

8-
from sentry import audit_log
8+
from sentry import audit_log, features
99
from sentry.api.api_owners import ApiOwner
1010
from sentry.api.api_publish_status import ApiPublishStatus
1111
from sentry.api.base import region_silo_endpoint
@@ -15,7 +15,11 @@
1515
from sentry.integrations.types import IntegrationProviderSlug
1616
from sentry.models.organization import Organization
1717
from sentry.prevent.models import PreventAIConfiguration
18-
from sentry.prevent.types.config import ORG_CONFIG_SCHEMA, PREVENT_AI_CONFIG_DEFAULT
18+
from sentry.prevent.types.config import (
19+
ORG_CONFIG_SCHEMA,
20+
PREVENT_AI_CONFIG_DEFAULT,
21+
PREVENT_AI_CONFIG_DEFAULT_V1,
22+
)
1923

2024

2125
class PreventAIConfigPermission(OrganizationPermission):
@@ -59,7 +63,11 @@ def get(
5963
integration_id=github_org_integrations[0].integration_id,
6064
).first()
6165

62-
response_data: dict[str, Any] = deepcopy(PREVENT_AI_CONFIG_DEFAULT)
66+
default_config = PREVENT_AI_CONFIG_DEFAULT
67+
if features.has("organizations:code-review-run-per-commit", organization):
68+
default_config = PREVENT_AI_CONFIG_DEFAULT_V1
69+
70+
response_data: dict[str, Any] = deepcopy(default_config)
6371
if config:
6472
response_data["organization"] = config.data
6573

src/sentry/prevent/types/config.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,40 @@
125125
},
126126
"organization": {},
127127
}
128+
129+
PREVENT_AI_CONFIG_DEFAULT_V1 = {
130+
"schema_version": "v1",
131+
"default_org_config": {
132+
"org_defaults": {
133+
"bug_prediction": {
134+
"enabled": True,
135+
"sensitivity": "medium",
136+
"triggers": {
137+
"on_command_phrase": True,
138+
"on_ready_for_review": True,
139+
# v1 default enables on_new_commit
140+
"on_new_commit": True,
141+
},
142+
},
143+
"test_generation": {
144+
"enabled": True,
145+
"triggers": {
146+
"on_command_phrase": True,
147+
"on_ready_for_review": False,
148+
"on_new_commit": False,
149+
},
150+
},
151+
"vanilla": {
152+
"enabled": True,
153+
"sensitivity": "medium",
154+
"triggers": {
155+
"on_command_phrase": True,
156+
"on_ready_for_review": False,
157+
"on_new_commit": False,
158+
},
159+
},
160+
},
161+
"repo_overrides": {},
162+
},
163+
"organization": {},
164+
}

tests/sentry/overwatch/endpoints/test_overwatch_rpc.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from sentry.constants import ObjectStatus
99
from sentry.prevent.models import PreventAIConfiguration
10-
from sentry.prevent.types.config import PREVENT_AI_CONFIG_DEFAULT
10+
from sentry.prevent.types.config import PREVENT_AI_CONFIG_DEFAULT, PREVENT_AI_CONFIG_DEFAULT_V1
1111
from sentry.silo.base import SiloMode
1212
from sentry.testutils.cases import APITestCase
1313
from sentry.testutils.silo import assume_test_silo_mode
@@ -157,6 +157,52 @@ def test_returns_default_when_no_config(self):
157157
assert resp.status_code == 200
158158
assert resp.data == PREVENT_AI_CONFIG_DEFAULT
159159
assert resp.data["organization"] == {}
160+
# Default config has on_new_commit disabled for bug_prediction
161+
assert (
162+
resp.data["default_org_config"]["org_defaults"]["bug_prediction"]["triggers"][
163+
"on_new_commit"
164+
]
165+
is False
166+
)
167+
168+
@patch(
169+
"sentry.overwatch.endpoints.overwatch_rpc.settings.OVERWATCH_RPC_SHARED_SECRET",
170+
["test-secret"],
171+
)
172+
def test_returns_v1_default_when_feature_flag_enabled(self):
173+
"""Test that V1 default config is returned when code-review-run-per-commit flag is enabled."""
174+
org = self.create_organization()
175+
git_org_name = "test-github-org"
176+
177+
with assume_test_silo_mode(SiloMode.CONTROL):
178+
self.create_integration(
179+
organization=org,
180+
provider="github",
181+
name=git_org_name,
182+
external_id=f"github:{git_org_name}",
183+
status=ObjectStatus.ACTIVE,
184+
)
185+
186+
url = reverse("sentry-api-0-prevent-pr-review-configs-resolved")
187+
params = {
188+
"sentryOrgId": str(org.id),
189+
"gitOrgName": git_org_name,
190+
"provider": "github",
191+
}
192+
auth = self._auth_header_for_get(url, params, "test-secret")
193+
194+
with self.feature({"organizations:code-review-run-per-commit": org}):
195+
resp = self.client.get(url, params, HTTP_AUTHORIZATION=auth)
196+
assert resp.status_code == 200
197+
assert resp.data == PREVENT_AI_CONFIG_DEFAULT_V1
198+
# V1 config has on_new_commit enabled for bug_prediction
199+
assert (
200+
resp.data["default_org_config"]["org_defaults"]["bug_prediction"]["triggers"][
201+
"on_new_commit"
202+
]
203+
is True
204+
)
205+
assert resp.data["organization"] == {}
160206

161207
@patch(
162208
"sentry.overwatch.endpoints.overwatch_rpc.settings.OVERWATCH_RPC_SHARED_SECRET",

tests/sentry/prevent/endpoints/test_pr_review_config.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from sentry.constants import ObjectStatus
66
from sentry.prevent.models import PreventAIConfiguration
7-
from sentry.prevent.types.config import PREVENT_AI_CONFIG_DEFAULT
7+
from sentry.prevent.types.config import PREVENT_AI_CONFIG_DEFAULT, PREVENT_AI_CONFIG_DEFAULT_V1
88
from sentry.silo.base import SiloMode
99
from sentry.testutils.cases import APITestCase
1010
from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
@@ -55,6 +55,28 @@ def test_get_returns_default_when_no_config(self):
5555
assert resp.status_code == 200
5656
assert resp.data == PREVENT_AI_CONFIG_DEFAULT
5757
assert resp.data["organization"] == {}
58+
# Default config has on_new_commit disabled for bug_prediction
59+
assert (
60+
resp.data["default_org_config"]["org_defaults"]["bug_prediction"]["triggers"][
61+
"on_new_commit"
62+
]
63+
is False
64+
)
65+
66+
def test_get_returns_v1_default_when_feature_flag_enabled(self):
67+
"""Test GET endpoint returns V1 default config when code-review-run-per-commit flag is enabled."""
68+
with self.feature("organizations:code-review-run-per-commit"):
69+
resp = self.client.get(self.url)
70+
assert resp.status_code == 200
71+
assert resp.data == PREVENT_AI_CONFIG_DEFAULT_V1
72+
# V1 config has on_new_commit enabled for bug_prediction
73+
assert (
74+
resp.data["default_org_config"]["org_defaults"]["bug_prediction"]["triggers"][
75+
"on_new_commit"
76+
]
77+
is True
78+
)
79+
assert resp.data["organization"] == {}
5880

5981
def test_get_returns_config_when_exists(self):
6082
"""Test GET endpoint returns the saved configuration when it exists."""

0 commit comments

Comments
 (0)