Skip to content

Commit 2dd179f

Browse files
fix(config): Hook up to new prevent ai config apis
1 parent dd6f8c2 commit 2dd179f

File tree

11 files changed

+481
-407
lines changed

11 files changed

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