1- import { updateOrganization } from 'sentry/actionCreators/organizations' ;
2- import type { Organization } from 'sentry/types/organization' ;
31import type {
42 PreventAIConfig ,
53 PreventAIFeatureTriggers ,
64 Sensitivity ,
75} from 'sentry/types/prevent' ;
8- import { fetchMutation , useMutation } from 'sentry/utils/queryClient' ;
6+ import { fetchMutation , useMutation , useQueryClient } from 'sentry/utils/queryClient' ;
97import useOrganization from 'sentry/utils/useOrganization' ;
108
119interface UpdatePreventAIFeatureParams {
1210 enabled : boolean ;
1311 // 'use_org_defaults' is a special case that will remove the entire repo override
1412 feature : 'vanilla' | 'test_generation' | 'bug_prediction' | 'use_org_defaults' ;
15- orgId : string ;
13+ gitOrgName : string ;
14+ originalConfig : PreventAIConfig ;
1615 // if repo is provided, edit repo_overrides for that repo, otherwise edit org_defaults
1716 repoId ?: string ;
1817 sensitivity ?: Sensitivity ;
@@ -21,20 +20,28 @@ interface UpdatePreventAIFeatureParams {
2120
2221export function useUpdatePreventAIFeature ( ) {
2322 const organization = useOrganization ( ) ;
23+ const queryClient = useQueryClient ( ) ;
24+
2425 const { mutateAsync, isPending, error} = useMutation ( {
2526 mutationFn : async ( params : UpdatePreventAIFeatureParams ) => {
26- if ( ! organization . preventAiConfigGithub ) {
27- throw new Error ( 'Organization has no AI Code Review config' ) ;
28- }
29- const newConfig = makePreventAIConfig ( organization . preventAiConfigGithub , params ) ;
27+ const newOrgConfig = makePreventAIOrgConfig ( params . originalConfig , params ) ;
3028
31- return fetchMutation < Partial < Organization > > ( {
29+ return fetchMutation < {
30+ default_org_config : PreventAIConfig ;
31+ organization : Record < string , PreventAIConfig > ;
32+ } > ( {
3233 method : 'PUT' ,
33- url : `/organizations/${ organization . slug } /` ,
34- data : { preventAiConfigGithub : newConfig } ,
34+ url : `/organizations/${ organization . slug } /prevent/ai/github/config/${ params . gitOrgName } /` ,
35+ data : newOrgConfig as unknown as Record < string , PreventAIConfig > ,
36+ } ) ;
37+ } ,
38+ onSuccess : ( _data , variables ) => {
39+ queryClient . invalidateQueries ( {
40+ queryKey : [
41+ `/organizations/${ organization . slug } /prevent/ai/github/config/${ variables . gitOrgName } /` ,
42+ ] ,
3543 } ) ;
3644 } ,
37- onSuccess : updateOrganization ,
3845 } ) ;
3946
4047 return {
@@ -45,46 +52,49 @@ export function useUpdatePreventAIFeature() {
4552}
4653
4754/**
48- * Makes a new PreventAIConfig object with feature settings applied for the specified org and/ or repo
55+ * Makes a new PreventAIOrgConfig object with feature settings applied for the specified repo or org defaults
4956 * 1. Deep clones the original config to prevent mutation
50- * 2. Get the org config for the specified org or create it from default_org_config template if not exists
51- * 3. If editing repo, get the repo override for the specified repo or create it from org_defaults template if not exists
52- * 4. Modifies the specified feature's settings, preserves any unspecified settings.
53- * 5. Special case: 'use_org_defaults' feature type will remove the entire repo override
57+ * 2. If editing repo, get the repo override for the specified repo or create it from org_defaults template if not exists
58+ * 3. Modifies the specified feature's settings, preserves any unspecified settings.
59+ * 4. Special case: 'use_org_defaults' feature type will remove the entire repo override
5460 *
55- * @param originalConfig Original PreventAIConfig object (will not be mutated)
61+ * @param originalConfig Original PreventAIOrgConfig object (will not be mutated)
5662 * @param params Parameters to update
57- * @returns New (copy of) PreventAIConfig object with updates applied
63+ * @returns New (copy of) PreventAIOrgConfig object with updates applied
5864 */
59- export function makePreventAIConfig (
65+ export function makePreventAIOrgConfig (
6066 originalConfig : PreventAIConfig ,
6167 params : UpdatePreventAIFeatureParams
62- ) {
63- const updatedConfig = structuredClone ( originalConfig ) ;
68+ ) : PreventAIConfig {
69+ const updatedOrgConfig = structuredClone ( originalConfig ) ;
70+ if ( ! updatedOrgConfig . schema_version ) {
71+ updatedOrgConfig . schema_version = 'v1' ;
72+ }
6473
65- const orgConfig =
66- updatedConfig . github_organizations [ params . orgId ] ??
67- structuredClone ( updatedConfig . default_org_config ) ;
68- updatedConfig . github_organizations [ params . orgId ] = orgConfig ;
74+ if ( ! updatedOrgConfig . repo_overrides ) {
75+ updatedOrgConfig . repo_overrides = { } ;
76+ }
6977
7078 if ( params . feature === 'use_org_defaults' ) {
7179 if ( ! params . repoId ) {
7280 throw new Error ( 'Repo name is required when feature is use_org_defaults' ) ;
7381 }
7482 if ( params . enabled ) {
75- delete orgConfig . repo_overrides [ params . repoId ] ;
83+ delete updatedOrgConfig . repo_overrides [ params . repoId ] ;
7684 } else {
77- orgConfig . repo_overrides [ params . repoId ] = structuredClone ( orgConfig . org_defaults ) ;
85+ updatedOrgConfig . repo_overrides [ params . repoId ] = structuredClone (
86+ updatedOrgConfig . org_defaults
87+ ) ;
7888 }
79- return updatedConfig ;
89+ return updatedOrgConfig ;
8090 }
8191
82- let featureConfig = orgConfig . org_defaults ;
92+ let featureConfig = updatedOrgConfig . org_defaults ;
8393 if ( params . repoId ) {
84- let repoOverride = orgConfig . repo_overrides [ params . repoId ] ;
94+ let repoOverride = updatedOrgConfig . repo_overrides [ params . repoId ] ;
8595 if ( ! repoOverride ) {
86- repoOverride = structuredClone ( orgConfig . org_defaults ) ;
87- orgConfig . repo_overrides [ params . repoId ] = repoOverride ;
96+ repoOverride = structuredClone ( updatedOrgConfig . org_defaults ) ;
97+ updatedOrgConfig . repo_overrides [ params . repoId ] = repoOverride ;
8898 }
8999 featureConfig = repoOverride ;
90100 }
@@ -95,5 +105,5 @@ export function makePreventAIConfig(
95105 sensitivity : params . sensitivity ?? featureConfig [ params . feature ] . sensitivity ,
96106 } ;
97107
98- return updatedConfig ;
108+ return updatedOrgConfig ;
99109}
0 commit comments