-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat(cmd-k): Add DSN lookup to both command palettes #108401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sergical
wants to merge
3
commits into
master
Choose a base branch
from
sergical/cmd-k-dsn-lookup-frontend
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+282
−6
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
static/app/components/commandPalette/useDsnLookupActions.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import {renderHookWithProviders, waitFor} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| import {useDsnLookupActions} from 'sentry/components/commandPalette/useDsnLookupActions'; | ||
|
|
||
| describe('useDsnLookupActions', () => { | ||
| beforeEach(() => { | ||
| MockApiClient.clearMockResponses(); | ||
| }); | ||
|
|
||
| it('returns actions for a valid DSN', async () => { | ||
| const dsn = 'https://abc123def456abc123def456abc123de@o1.ingest.sentry.io/123'; | ||
| MockApiClient.addMockResponse({ | ||
| url: '/organizations/org-slug/dsn-lookup/', | ||
| body: { | ||
| organizationSlug: 'test-org', | ||
| projectSlug: 'test-project', | ||
| projectId: '42', | ||
| projectName: 'Test Project', | ||
| projectPlatform: 'javascript', | ||
| keyLabel: 'Default', | ||
| keyId: '1', | ||
| }, | ||
| match: [MockApiClient.matchQuery({dsn})], | ||
| }); | ||
|
|
||
| const {result} = renderHookWithProviders(() => useDsnLookupActions(dsn)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(result.current.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| expect(result.current).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| key: 'dsn-lookup-issues', | ||
| type: 'navigate', | ||
| to: '/organizations/test-org/issues/?project=42', | ||
| }), | ||
| expect.objectContaining({ | ||
| key: 'dsn-lookup-project-settings', | ||
| type: 'navigate', | ||
| to: '/settings/test-org/projects/test-project/', | ||
| }), | ||
| expect.objectContaining({ | ||
| key: 'dsn-lookup-client-keys', | ||
| type: 'navigate', | ||
| to: '/settings/test-org/projects/test-project/keys/', | ||
| }), | ||
| ]) | ||
| ); | ||
| }); | ||
|
|
||
| it('returns empty array for non-DSN query', () => { | ||
| const mockApi = MockApiClient.addMockResponse({ | ||
| url: '/organizations/org-slug/dsn-lookup/', | ||
| body: {}, | ||
| }); | ||
|
|
||
| const {result} = renderHookWithProviders(() => | ||
| useDsnLookupActions('some random text') | ||
| ); | ||
|
|
||
| expect(result.current).toEqual([]); | ||
| expect(mockApi).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns empty array for empty query', () => { | ||
| const mockApi = MockApiClient.addMockResponse({ | ||
| url: '/organizations/org-slug/dsn-lookup/', | ||
| body: {}, | ||
| }); | ||
|
|
||
| const {result} = renderHookWithProviders(() => useDsnLookupActions('')); | ||
|
|
||
| expect(result.current).toEqual([]); | ||
| expect(mockApi).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
73 changes: 73 additions & 0 deletions
73
static/app/components/commandPalette/useDsnLookupActions.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import {useMemo} from 'react'; | ||
|
|
||
| import type {CommandPaletteActionWithKey} from 'sentry/components/commandPalette/types'; | ||
| import {DSN_PATTERN} from 'sentry/components/search/sources/dsnLookupUtils'; | ||
| import type {DsnLookupResponse} from 'sentry/components/search/sources/dsnLookupUtils'; | ||
| import {IconIssues, IconList, IconSettings} from 'sentry/icons'; | ||
| import {t} from 'sentry/locale'; | ||
| import {useApiQuery} from 'sentry/utils/queryClient'; | ||
| import {useDebouncedValue} from 'sentry/utils/useDebouncedValue'; | ||
| import useOrganization from 'sentry/utils/useOrganization'; | ||
|
|
||
| export function useDsnLookupActions(query: string): CommandPaletteActionWithKey[] { | ||
| const organization = useOrganization(); | ||
| const debouncedQuery = useDebouncedValue(query, 300); | ||
| const isDsn = DSN_PATTERN.test(debouncedQuery); | ||
|
|
||
| const {data} = useApiQuery<DsnLookupResponse>( | ||
| [`/organizations/${organization.slug}/dsn-lookup/`, {query: {dsn: debouncedQuery}}], | ||
| { | ||
| staleTime: 30_000, | ||
| enabled: isDsn, | ||
| } | ||
| ); | ||
|
|
||
| return useMemo(() => { | ||
| if (!data) { | ||
| return []; | ||
| } | ||
|
|
||
| const orgSlug = data.organizationSlug; | ||
| const projectSlug = data.projectSlug; | ||
| const projectId = data.projectId; | ||
| const projectName = data.projectName; | ||
|
|
||
| const actions: CommandPaletteActionWithKey[] = [ | ||
| { | ||
| key: 'dsn-lookup-issues', | ||
| type: 'navigate', | ||
| to: `/organizations/${orgSlug}/issues/?project=${projectId}`, | ||
| display: { | ||
| label: t('Issues for %s', projectName), | ||
| details: t('View issues'), | ||
| icon: <IconIssues />, | ||
| }, | ||
| groupingKey: 'search-result', | ||
| }, | ||
| { | ||
| key: 'dsn-lookup-project-settings', | ||
| type: 'navigate', | ||
| to: `/settings/${orgSlug}/projects/${projectSlug}/`, | ||
| display: { | ||
| label: t('%s Settings', projectName), | ||
| details: t('Project settings'), | ||
| icon: <IconSettings />, | ||
| }, | ||
| groupingKey: 'search-result', | ||
| }, | ||
| { | ||
| key: 'dsn-lookup-client-keys', | ||
| type: 'navigate', | ||
| to: `/settings/${orgSlug}/projects/${projectSlug}/keys/`, | ||
| display: { | ||
| label: t('Client Keys (DSN) for %s', projectName), | ||
| details: t('Manage DSN keys'), | ||
| icon: <IconList />, | ||
| }, | ||
| groupingKey: 'search-result', | ||
| }, | ||
| ]; | ||
|
|
||
| return actions; | ||
| }, [data]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import {useEffect, useMemo, useState} from 'react'; | ||
|
|
||
| import {Client} from 'sentry/api'; | ||
| import {t} from 'sentry/locale'; | ||
| import useOrganization from 'sentry/utils/useOrganization'; | ||
|
|
||
| import {DSN_PATTERN} from './dsnLookupUtils'; | ||
| import type {DsnLookupResponse} from './dsnLookupUtils'; | ||
| import type {ChildProps, ResultItem} from './types'; | ||
| import {makeResolvedTs} from './utils'; | ||
|
|
||
| type Props = { | ||
| children: (props: ChildProps) => React.ReactElement; | ||
| query: string; | ||
| }; | ||
|
|
||
| function DsnLookupSource({query, children}: Props) { | ||
| const organization = useOrganization(); | ||
| const hasDsnLookup = organization.features.includes('cmd-k-dsn-lookup'); | ||
| const isDsn = DSN_PATTERN.test(query); | ||
| const [isLoading, setIsLoading] = useState(false); | ||
| const [data, setData] = useState<DsnLookupResponse | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| if (!hasDsnLookup || !isDsn) { | ||
| setData(null); | ||
| setIsLoading(false); | ||
| return undefined; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| setIsLoading(true); | ||
| const api = new Client(); | ||
| let cancelled = false; | ||
|
|
||
| api | ||
| .requestPromise(`/organizations/${organization.slug}/dsn-lookup/`, { | ||
| query: {dsn: query}, | ||
| }) | ||
| .then((response: DsnLookupResponse) => { | ||
| if (!cancelled) { | ||
| setData(response); | ||
| setIsLoading(false); | ||
| } | ||
| }) | ||
| .catch(() => { | ||
| if (!cancelled) { | ||
| setData(null); | ||
| setIsLoading(false); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [hasDsnLookup, isDsn, query, organization.slug]); | ||
|
|
||
| const results = useMemo(() => { | ||
| if (!data) { | ||
| return []; | ||
| } | ||
|
|
||
| const resolvedTs = makeResolvedTs(); | ||
| const {organizationSlug, projectSlug, projectId, projectName} = data; | ||
|
|
||
| const items: ResultItem[] = [ | ||
| { | ||
| title: t('Issues for %s', projectName), | ||
| description: t('View issues'), | ||
| sourceType: 'dsn-lookup', | ||
| resultType: 'route', | ||
| resolvedTs, | ||
| to: `/organizations/${organizationSlug}/issues/?project=${projectId}`, | ||
| }, | ||
| { | ||
| title: t('%s Settings', projectName), | ||
| description: t('Project settings'), | ||
| sourceType: 'dsn-lookup', | ||
| resultType: 'route', | ||
| resolvedTs, | ||
| to: `/settings/${organizationSlug}/projects/${projectSlug}/`, | ||
| }, | ||
| { | ||
| title: t('Client Keys (DSN) for %s', projectName), | ||
| description: t('Manage DSN keys'), | ||
| sourceType: 'dsn-lookup', | ||
| resultType: 'route', | ||
| resolvedTs, | ||
| to: `/settings/${organizationSlug}/projects/${projectSlug}/keys/`, | ||
| }, | ||
| ]; | ||
|
|
||
| return items.map((item, i) => ({item, score: 0, refIndex: i})); | ||
| }, [data]); | ||
|
|
||
| return children({isLoading, results}); | ||
| } | ||
|
|
||
| export default DsnLookupSource; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| export const DSN_PATTERN = /^https?:\/\/([a-f0-9]{32})(:[a-f0-9]{32})?@[^/]+\/\d+$/; | ||
|
|
||
| export interface DsnLookupResponse { | ||
| keyId: string; | ||
| keyLabel: string; | ||
| organizationSlug: string; | ||
| projectId: string; | ||
| projectName: string; | ||
| projectPlatform: string | null; | ||
| projectSlug: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useDsnLookupActionscrashes when organization context is nullHigh Severity
content.tsxcallsuseOrganization({allowNull: true})and defensively null-checks with optional chaining, indicating the organization context can be null. However,useDsnLookupActionsis always invoked (hooks can't be conditional), and it internally callsuseOrganization()withoutallowNull, which throws when the organization context is missing. The empty-string guard (hasDsnLookup ? query : '') doesn't prevent the crash because the hook body still runsuseOrganization()before checking the query.Additional Locations (1)
static/app/components/commandPalette/ui/content.tsx#L45-L48