Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions static/app/types/organization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {WidgetType} from 'sentry/views/dashboards/types';
import type {Actor, Avatar, ObjectStatus, Scope} from './core';
import type {ExternalTeam} from './integrations';
import type {OnboardingTaskStatus} from './onboarding';
import type {PreventAIConfig} from './prevent';
import type {Project} from './project';
import type {Relay} from './relay';
import type {User} from './user';
Expand Down Expand Up @@ -114,7 +113,6 @@ export interface Organization extends OrganizationSummary {
};
orgRole?: string;
planSampleRate?: number | null;
preventAiConfigGithub?: PreventAIConfig;
}

export interface Team {
Expand Down
9 changes: 3 additions & 6 deletions static/app/types/prevent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export interface PreventAIFeatureConfigsByName {
vanilla: PreventAIFeatureConfig; // "vanilla" basic ai pr review
}

export interface PreventAIOrgConfig {
export interface PreventAIConfig {
org_defaults: PreventAIFeatureConfigsByName;
repo_overrides: Record<string, PreventAIFeatureConfigsByName>;
}

export interface PreventAIConfig {
default_org_config: PreventAIOrgConfig;
github_organizations: Record<string, PreventAIOrgConfig>;
schema_version: string;
}

export const PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT = 'v1';
110 changes: 110 additions & 0 deletions static/app/views/prevent/preventAI/hooks/usePreventAIConfig.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import {OrganizationFixture} from 'sentry-fixture/organization';

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

import {PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT} from 'sentry/types/prevent';

import {usePreventAIGitHubConfig} from './usePreventAIConfig';

describe('usePreventAIGitHubConfig', () => {
const org = OrganizationFixture({slug: 'test-org'});
const gitOrgName = 'octo-corp';

beforeEach(() => {
MockApiClient.clearMockResponses();
});

it('fetches config from API correctly', async () => {
const configResponse = {
default_org_config: {
org_defaults: {
vanilla: {enabled: true, sensitivity: 'medium'},
test_generation: {enabled: false, sensitivity: 'low'},
bug_prediction: {enabled: false, sensitivity: 'high'},
},
repo_overrides: {
'repo-123': {
vanilla: {enabled: false, sensitivity: 'high'},
test_generation: {enabled: true, sensitivity: 'critical'},
bug_prediction: {enabled: false, sensitivity: 'medium'},
},
},
schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT,
},
organization: {
'octo-corp': {
org_defaults: {
vanilla: {enabled: false, sensitivity: 'low'},
test_generation: {enabled: true, sensitivity: 'medium'},
bug_prediction: {enabled: true, sensitivity: 'high'},
},
repo_overrides: {},
schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT,
},
},
schema_version: PREVENT_AI_CONFIG_SCHEMA_VERSION_DEFAULT,
};

MockApiClient.addMockResponse({
url: `/organizations/${org.slug}/prevent/ai/github/config/${gitOrgName}/`,
body: configResponse,
});

const {result} = renderHookWithProviders(
() => usePreventAIGitHubConfig({gitOrgName}),
{
organization: org,
}
);

await waitFor(() => {
expect(result.current.isPending).toBe(false);
});

expect(result.current.data).toMatchObject(configResponse);
expect(result.current.isError).toBeFalsy();
});

it('sets isError when API fails', async () => {
MockApiClient.addMockResponse({
url: `/organizations/${org.slug}/prevent/ai/github/config/${gitOrgName}/`,
statusCode: 500,
});

const {result} = renderHookWithProviders(
() => usePreventAIGitHubConfig({gitOrgName}),
{
organization: org,
}
);

await waitFor(() => {
expect(result.current.isPending).toBe(false);
});

expect(result.current.isError).toBe(true);
expect(result.current.data).toBeUndefined();
});

it('uses correct API URL for different org', async () => {
const diffOrg = OrganizationFixture({slug: 'diff-org'});

MockApiClient.addMockResponse({
url: `/organizations/diff-org/prevent/ai/github/config/${gitOrgName}/`,
body: {},
});

const {result} = renderHookWithProviders(
() => usePreventAIGitHubConfig({gitOrgName}),
{
organization: diffOrg,
}
);

await waitFor(() => {
expect(result.current.isPending).toBe(false);
});

expect(result.current.isError).toBeFalsy();
});
});
25 changes: 25 additions & 0 deletions static/app/views/prevent/preventAI/hooks/usePreventAIConfig.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {PreventAIConfig} from 'sentry/types/prevent';
import {useApiQuery} from 'sentry/utils/queryClient';
import type RequestError from 'sentry/utils/requestError/requestError';
import useOrganization from 'sentry/utils/useOrganization';

interface UsePreventAIGitHubConfigOptions {
gitOrgName: string;
}

interface UsePreventAIGitHubConfigResult {
default_org_config: PreventAIConfig;
schema_version: string;
organization?: Record<string, PreventAIConfig>;
}

export function usePreventAIGitHubConfig({gitOrgName}: UsePreventAIGitHubConfigOptions) {
const organization = useOrganization();

return useApiQuery<UsePreventAIGitHubConfigResult, RequestError>(
[`/organizations/${organization.slug}/prevent/ai/github/config/${gitOrgName}/`],
{
staleTime: 30000,
}
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {OrganizationFixture} from 'sentry-fixture/organization';
import {OrganizationIntegrationsFixture} from 'sentry-fixture/organizationIntegrations';
import {PreventAIConfigFixture} from 'sentry-fixture/prevent';

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

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

describe('usePreventAIOrgRepos', () => {
const mockOrg = OrganizationFixture({
preventAiConfigGithub: PreventAIConfigFixture(),
});
const mockOrg = OrganizationFixture();

const mockResponse: OrganizationIntegration[] = [
OrganizationIntegrationsFixture({
Expand Down
Loading
Loading