Skip to content

Commit 30ca9b4

Browse files
suejung-sentryshashjar
authored andcommitted
fix(config): Hook up to new prevent ai config apis (#102532)
1 parent db8b48e commit 30ca9b4

File tree

11 files changed

+488
-411
lines changed

11 files changed

+488
-411
lines changed

static/app/types/organization.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type {WidgetType} from 'sentry/views/dashboards/types';
99
import type {Actor, Avatar, ObjectStatus, Scope} from './core';
1010
import type {ExternalTeam} from './integrations';
1111
import type {OnboardingTaskStatus} from './onboarding';
12-
import type {PreventAIConfig} from './prevent';
1312
import type {Project} from './project';
1413
import type {Relay} from './relay';
1514
import type {User} from './user';
@@ -114,7 +113,6 @@ export interface Organization extends OrganizationSummary {
114113
};
115114
orgRole?: string;
116115
planSampleRate?: number | null;
117-
preventAiConfigGithub?: PreventAIConfig;
118116
}
119117

120118
export interface Team {

static/app/types/prevent.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ export interface PreventAIFeatureConfigsByName {
1717
vanilla: PreventAIFeatureConfig; // "vanilla" basic ai pr review
1818
}
1919

20-
export interface PreventAIOrgConfig {
20+
export interface PreventAIConfig {
2121
org_defaults: PreventAIFeatureConfigsByName;
2222
repo_overrides: Record<string, PreventAIFeatureConfigsByName>;
23-
}
24-
25-
export interface PreventAIConfig {
26-
default_org_config: PreventAIOrgConfig;
27-
github_organizations: Record<string, PreventAIOrgConfig>;
2823
schema_version: string;
2924
}
25+
26+
export const PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT = 'v1';
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import {OrganizationFixture} from 'sentry-fixture/organization';
2+
3+
import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';
4+
5+
import {PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT} from 'sentry/types/prevent';
6+
7+
import {usePreventAIGitHubConfig} from './usePreventAIConfig';
8+
9+
describe('usePreventAIGitHubConfig', () => {
10+
const org = OrganizationFixture({slug: 'test-org'});
11+
const gitOrgName = 'octo-corp';
12+
13+
beforeEach(() => {
14+
MockApiClient.clearMockResponses();
15+
});
16+
17+
it('fetches config from API correctly', async () => {
18+
const configResponse = {
19+
default_org_config: {
20+
org_defaults: {
21+
vanilla: {enabled: true, sensitivity: 'medium'},
22+
test_generation: {enabled: false, sensitivity: 'low'},
23+
bug_prediction: {enabled: false, sensitivity: 'high'},
24+
},
25+
repo_overrides: {
26+
'repo-123': {
27+
vanilla: {enabled: false, sensitivity: 'high'},
28+
test_generation: {enabled: true, sensitivity: 'critical'},
29+
bug_prediction: {enabled: false, sensitivity: 'medium'},
30+
},
31+
},
32+
schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT,
33+
},
34+
organization: {
35+
'octo-corp': {
36+
org_defaults: {
37+
vanilla: {enabled: false, sensitivity: 'low'},
38+
test_generation: {enabled: true, sensitivity: 'medium'},
39+
bug_prediction: {enabled: true, sensitivity: 'high'},
40+
},
41+
repo_overrides: {},
42+
schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT,
43+
},
44+
},
45+
schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT,
46+
};
47+
48+
MockApiClient.addMockResponse({
49+
url: `/organizations/${org.slug}/prevent/ai/github/config/${gitOrgName}/`,
50+
body: configResponse,
51+
});
52+
53+
const {result} = renderHookWithProviders(
54+
() => usePreventAIGitHubConfig({gitOrgName}),
55+
{
56+
organization: org,
57+
}
58+
);
59+
60+
await waitFor(() => {
61+
expect(result.current.isPending).toBe(false);
62+
});
63+
64+
expect(result.current.data).toMatchObject(configResponse);
65+
expect(result.current.isError).toBeFalsy();
66+
});
67+
68+
it('sets isError when API fails', async () => {
69+
MockApiClient.addMockResponse({
70+
url: `/organizations/${org.slug}/prevent/ai/github/config/${gitOrgName}/`,
71+
statusCode: 500,
72+
});
73+
74+
const {result} = renderHookWithProviders(
75+
() => usePreventAIGitHubConfig({gitOrgName}),
76+
{
77+
organization: org,
78+
}
79+
);
80+
81+
await waitFor(() => {
82+
expect(result.current.isPending).toBe(false);
83+
});
84+
85+
expect(result.current.isError).toBe(true);
86+
expect(result.current.data).toBeUndefined();
87+
});
88+
89+
it('uses correct API URL for different org', async () => {
90+
const diffOrg = OrganizationFixture({slug: 'diff-org'});
91+
92+
MockApiClient.addMockResponse({
93+
url: `/organizations/diff-org/prevent/ai/github/config/${gitOrgName}/`,
94+
body: {},
95+
});
96+
97+
const {result} = renderHookWithProviders(
98+
() => usePreventAIGitHubConfig({gitOrgName}),
99+
{
100+
organization: diffOrg,
101+
}
102+
);
103+
104+
await waitFor(() => {
105+
expect(result.current.isPending).toBe(false);
106+
});
107+
108+
expect(result.current.isError).toBeFalsy();
109+
});
110+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type {PreventAIConfig} from 'sentry/types/prevent';
2+
import {useApiQuery} from 'sentry/utils/queryClient';
3+
import type RequestError from 'sentry/utils/requestError/requestError';
4+
import useOrganization from 'sentry/utils/useOrganization';
5+
6+
interface UsePreventAIGitHubConfigOptions {
7+
gitOrgName: string;
8+
}
9+
10+
interface UsePreventAIGitHubConfigResult {
11+
default_org_config: PreventAIConfig;
12+
schema_version: string;
13+
organization?: Record<string, PreventAIConfig>;
14+
}
15+
16+
export function usePreventAIGitHubConfig({gitOrgName}: UsePreventAIGitHubConfigOptions) {
17+
const organization = useOrganization();
18+
19+
return useApiQuery<UsePreventAIGitHubConfigResult, RequestError>(
20+
[`/organizations/${organization.slug}/prevent/ai/github/config/${gitOrgName}/`],
21+
{
22+
staleTime: 30000,
23+
}
24+
);
25+
}

static/app/views/prevent/preventAI/hooks/usePreventAIOrgRepos.spec.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {OrganizationFixture} from 'sentry-fixture/organization';
22
import {OrganizationIntegrationsFixture} from 'sentry-fixture/organizationIntegrations';
3-
import {PreventAIConfigFixture} from 'sentry-fixture/prevent';
43

54
import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary';
65

@@ -9,9 +8,7 @@ import type {OrganizationIntegration} from 'sentry/types/integrations';
98
import {usePreventAIOrgs} from './usePreventAIOrgRepos';
109

1110
describe('usePreventAIOrgRepos', () => {
12-
const mockOrg = OrganizationFixture({
13-
preventAiConfigGithub: PreventAIConfigFixture(),
14-
});
11+
const mockOrg = OrganizationFixture();
1512

1613
const mockResponse: OrganizationIntegration[] = [
1714
OrganizationIntegrationsFixture({

0 commit comments

Comments
 (0)