-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
chore(contexts): Simplify user
context
#80704
Merged
Merged
Changes from all commits
Commits
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 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 was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
static/app/components/events/contexts/knownContext/user.spec.tsx
This file contains 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,93 @@ | ||
import {EventFixture} from 'sentry-fixture/event'; | ||
|
||
import {render, screen} from 'sentry-test/reactTestingLibrary'; | ||
|
||
import ContextCard from 'sentry/components/events/contexts/contextCard'; | ||
import { | ||
getUserContextData, | ||
type UserContext, | ||
} from 'sentry/components/events/contexts/knownContext/user'; | ||
|
||
const MOCK_USER_CONTEXT: UserContext = { | ||
email: '[email protected]', | ||
ip_address: '127.0.0.1', | ||
id: '808', | ||
name: 'Leander', | ||
username: 'leeandher', | ||
geo: { | ||
country_code: 'US', | ||
city: 'San Francisco', | ||
subdivision: 'California', | ||
region: 'United States', | ||
}, | ||
// Extra data is still valid and preserved | ||
extra_data: 'something', | ||
unknown_key: 123, | ||
}; | ||
|
||
const MOCK_REDACTION = { | ||
name: { | ||
'': { | ||
rem: [['organization:0', 's', 0, 0]], | ||
len: 5, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('UserContext', function () { | ||
it('returns values and according to the parameters', function () { | ||
expect(getUserContextData({data: MOCK_USER_CONTEXT})).toEqual([ | ||
{ | ||
key: 'email', | ||
subject: 'Email', | ||
value: '[email protected]', | ||
action: {link: 'mailto:[email protected]'}, | ||
}, | ||
{key: 'ip_address', subject: 'IP Address', value: '127.0.0.1'}, | ||
{key: 'id', subject: 'ID', value: '808'}, | ||
{key: 'name', subject: 'Name', value: 'Leander'}, | ||
{key: 'username', subject: 'Username', value: 'leeandher'}, | ||
{ | ||
key: 'geo', | ||
subject: 'Geography', | ||
value: 'San Francisco, California, United States (US)', | ||
}, | ||
{ | ||
key: 'extra_data', | ||
subject: 'extra_data', | ||
value: 'something', | ||
meta: undefined, | ||
}, | ||
{ | ||
key: 'unknown_key', | ||
subject: 'unknown_key', | ||
value: 123, | ||
meta: undefined, | ||
}, | ||
]); | ||
}); | ||
|
||
it('renders with meta annotations correctly', function () { | ||
const event = EventFixture({ | ||
_meta: {contexts: {user: MOCK_REDACTION}}, | ||
}); | ||
|
||
render( | ||
<ContextCard | ||
event={event} | ||
type={'default'} | ||
alias={'user'} | ||
value={{...MOCK_USER_CONTEXT, name: ''}} | ||
/> | ||
); | ||
|
||
expect(screen.getByText('User')).toBeInTheDocument(); | ||
expect(screen.getByText('Email')).toBeInTheDocument(); | ||
expect(screen.getByText('[email protected]')).toBeInTheDocument(); | ||
expect( | ||
screen.getByRole('link', {name: '[email protected]'}) | ||
).toBeInTheDocument(); | ||
expect(screen.getByText('Name')).toBeInTheDocument(); | ||
expect(screen.getByText(/redacted/)).toBeInTheDocument(); | ||
}); | ||
}); |
121 changes: 121 additions & 0 deletions
121
static/app/components/events/contexts/knownContext/user.tsx
This file contains 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,121 @@ | ||
import {getContextKeys} from 'sentry/components/events/contexts/utils'; | ||
import {t} from 'sentry/locale'; | ||
import type {KeyValueListData} from 'sentry/types/group'; | ||
import {defined} from 'sentry/utils'; | ||
|
||
enum UserContextKeys { | ||
ID = 'id', | ||
EMAIL = 'email', | ||
USERNAME = 'username', | ||
IP_ADDRESS = 'ip_address', | ||
NAME = 'name', | ||
GEO = 'geo', | ||
} | ||
|
||
export interface UserContext { | ||
// Any custom keys users may set | ||
[key: string]: any; | ||
[UserContextKeys.ID]?: string; | ||
[UserContextKeys.EMAIL]?: string; | ||
[UserContextKeys.USERNAME]?: string; | ||
[UserContextKeys.IP_ADDRESS]?: string; | ||
[UserContextKeys.NAME]?: string; | ||
[UserContextKeys.GEO]?: Partial<Record<UserContextGeoKeys, string>>; | ||
} | ||
|
||
enum UserContextGeoKeys { | ||
CITY = 'city', | ||
COUNTRY_CODE = 'country_code', | ||
SUBDIVISION = 'subdivision', | ||
REGION = 'region', | ||
} | ||
|
||
const EMAIL_REGEX = /[^@]+@[^\.]+\..+/; | ||
|
||
function formatGeo(geoData: UserContext['geo'] = {}): string | undefined { | ||
if (!geoData) { | ||
return undefined; | ||
} | ||
|
||
const geoStringArray: string[] = []; | ||
|
||
if (geoData.city) { | ||
geoStringArray.push(geoData.city); | ||
} | ||
|
||
if (geoData.subdivision) { | ||
geoStringArray.push(geoData.subdivision); | ||
} | ||
|
||
if (geoData.region) { | ||
geoStringArray.push( | ||
geoData.country_code | ||
? `${geoData.region} (${geoData.country_code})` | ||
: geoData.region | ||
); | ||
} | ||
|
||
return geoStringArray.join(', '); | ||
} | ||
|
||
export function getUserContextData({ | ||
data, | ||
meta, | ||
}: { | ||
data: UserContext; | ||
meta?: Record<keyof UserContext, any>; | ||
}): KeyValueListData { | ||
return getContextKeys({data}).map(ctxKey => { | ||
switch (ctxKey) { | ||
case UserContextKeys.NAME: | ||
return { | ||
key: ctxKey, | ||
subject: t('Name'), | ||
value: data.name, | ||
}; | ||
case UserContextKeys.USERNAME: | ||
return { | ||
key: ctxKey, | ||
subject: t('Username'), | ||
value: data.username, | ||
}; | ||
case UserContextKeys.ID: | ||
return { | ||
key: ctxKey, | ||
subject: t('ID'), | ||
value: data.id, | ||
}; | ||
case UserContextKeys.IP_ADDRESS: | ||
return { | ||
key: ctxKey, | ||
subject: t('IP Address'), | ||
value: data.ip_address, | ||
}; | ||
case UserContextKeys.EMAIL: | ||
return { | ||
key: ctxKey, | ||
subject: t('Email'), | ||
value: data.email, | ||
action: { | ||
link: | ||
defined(data.email) && EMAIL_REGEX.test(data.email) | ||
? `mailto:${data.email}` | ||
: undefined, | ||
}, | ||
}; | ||
case UserContextKeys.GEO: | ||
return { | ||
key: ctxKey, | ||
subject: t('Geography'), | ||
value: formatGeo(data.geo), | ||
}; | ||
default: | ||
return { | ||
key: ctxKey, | ||
subject: ctxKey, | ||
value: data[ctxKey], | ||
meta: meta?.[ctxKey]?.[''], | ||
}; | ||
} | ||
}); | ||
} |
47 changes: 0 additions & 47 deletions
47
static/app/components/events/contexts/user/getUserKnownDataDetails.spec.tsx
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
static/app/components/events/contexts/user/getUserKnownDataDetails.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
I'm going to add these to the serializer since these are used all over but not rendered on issue details for some reason.