Skip to content
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 3 commits into from
Nov 14, 2024
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
3 changes: 3 additions & 0 deletions src/sentry/interfaces/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EventUserApiContext(TypedDict, total=False):
username: str | None
ip_address: str | None
name: str | None
geo: dict[str, str] | None
data: dict[str, Any] | None


Expand Down Expand Up @@ -69,6 +70,7 @@ def get_api_context(self, is_public=False, platform=None) -> EventUserApiContext
"username": self.username,
"ip_address": self.ip_address,
"name": self.name,
"geo": self.geo.to_json() if self.geo is not None else None,
"data": self.data,
}

Expand All @@ -80,6 +82,7 @@ def get_api_meta(self, meta, is_public=False, platform=None):
"username": meta.get("username"),
"ip_address": meta.get("ip_address"),
"name": meta.get("name"),
"geo": meta.get("geo"),
"data": meta.get("data"),
}

Expand Down
22 changes: 0 additions & 22 deletions static/app/components/events/contexts/default.tsx

This file was deleted.

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 static/app/components/events/contexts/knownContext/user.tsx
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',
}
Comment on lines +23 to +31
Copy link
Member Author

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.


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]?.[''],
};
}
});
}

This file was deleted.

This file was deleted.

Loading
Loading