Skip to content
Open
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
6 changes: 6 additions & 0 deletions apps/studio/src/components/auth-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useIpcListener } from 'src/hooks/use-ipc-listener';
import { useOffline } from 'src/hooks/use-offline';
import { getIpcApi } from 'src/lib/get-ipc-api';
import { isInvalidTokenError } from 'src/lib/is-invalid-oauth-token-error';
import { setSentryWpcomUserIdRenderer } from 'src/lib/renderer-sentry-utils';
import { useI18nLocale } from 'src/stores';
import { setWpcomClient } from 'src/stores/wpcom-api';
import type { WPCOM } from 'wpcom/types';
Expand Down Expand Up @@ -55,6 +56,7 @@ const AuthProvider: React.FC< AuthProviderProps > = ( { children } ) => {
setClient( undefined );
setWpcomClient( undefined );
setUser( undefined );
setSentryWpcomUserIdRenderer( undefined );
} catch ( err ) {
console.error( 'Failed to handle invalid token:', err );
Sentry.captureException( err );
Expand Down Expand Up @@ -85,6 +87,7 @@ const AuthProvider: React.FC< AuthProviderProps > = ( { children } ) => {
setClient( undefined );
setWpcomClient( undefined );
setUser( undefined );
setSentryWpcomUserIdRenderer( undefined );
return;
}

Expand All @@ -98,6 +101,7 @@ const AuthProvider: React.FC< AuthProviderProps > = ( { children } ) => {
email: token.email,
displayName: token.displayName || '',
} );
setSentryWpcomUserIdRenderer( token.id );
} );

const logout = useCallback( async () => {
Expand All @@ -124,6 +128,7 @@ const AuthProvider: React.FC< AuthProviderProps > = ( { children } ) => {
setClient( undefined );
setWpcomClient( undefined );
setUser( undefined );
setSentryWpcomUserIdRenderer( undefined );
} catch ( err ) {
console.error( err );
Sentry.captureException( err );
Expand All @@ -150,6 +155,7 @@ const AuthProvider: React.FC< AuthProviderProps > = ( { children } ) => {
email: token.email,
displayName: token.displayName || '',
} );
setSentryWpcomUserIdRenderer( token.id );
} catch ( err ) {
console.error( err );
Sentry.captureException( err );
Expand Down
5 changes: 5 additions & 0 deletions apps/studio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { pathToFileURL } from 'url';
import * as Sentry from '@sentry/electron/main';
import { PROTOCOL_PREFIX } from '@studio/common/constants';
import { runMigrations } from '@studio/common/lib/migration';
import { getCurrentUserId } from '@studio/common/lib/shared-config';
import { suppressPunycodeWarning } from '@studio/common/lib/suppress-punycode-warning';
import { __, _n, sprintf } from '@wordpress/i18n';
import {
Expand All @@ -35,6 +36,7 @@ import {
} from 'src/lib/bump-stats';
import { handleDeeplink } from 'src/lib/deeplink';
import { getUserLocaleWithFallback } from 'src/lib/locale-node';
import { setSentryWpcomUserIdMain } from 'src/lib/main-sentry-utils';
import { getSentryReleaseInfo } from 'src/lib/sentry-release';
import { startUserDataWatcher, stopUserDataWatcher } from 'src/lib/user-data-watcher';
import { setupLogging } from 'src/logging';
Expand Down Expand Up @@ -110,6 +112,9 @@ async function setupSentryUserId() {
} finally {
await unlockAppdata();
}

const wpcomUserId = await getCurrentUserId();
setSentryWpcomUserIdMain( wpcomUserId ?? undefined );
}

// This is a workaround to ensure that the extension background workers are started
Expand Down
2 changes: 2 additions & 0 deletions apps/studio/src/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { ImportExportEventData } from 'src/lib/import-export/handle-events';
import { defaultImporterOptions, importBackup } from 'src/lib/import-export/import/import-manager';
import { BackupArchiveInfo } from 'src/lib/import-export/import/types';
import { getUserLocaleWithFallback } from 'src/lib/locale-node';
import { setSentryWpcomUserIdMain } from 'src/lib/main-sentry-utils';
import * as oauthClient from 'src/lib/oauth';
import { getAiInstructionsPath } from 'src/lib/server-files-paths';
import { shellOpenExternalWrapper } from 'src/lib/shell-open-external-wrapper';
Expand Down Expand Up @@ -764,6 +765,7 @@ export async function isAuthenticated() {
}

export async function clearAuthenticationToken() {
setSentryWpcomUserIdMain( undefined );
return await updateSharedConfig( { authToken: undefined } );
}

Expand Down
2 changes: 2 additions & 0 deletions apps/studio/src/lib/deeplink/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import wpcomFactory from '@studio/common/lib/wpcom-factory';
import wpcomXhrRequest from '@studio/common/lib/wpcom-xhr-request-factory';
import { z } from 'zod';
import { sendIpcEventToRenderer } from 'src/ipc-utils';
import { setSentryWpcomUserIdMain } from 'src/lib/main-sentry-utils';

const meResponseSchema = z.object( {
ID: z.number(),
Expand Down Expand Up @@ -54,6 +55,7 @@ export async function handleAuthDeeplink( urlObject: URL ): Promise< void > {
try {
const authResult = await handleAuthCallback( hash );
await updateSharedConfig( { authToken: authResult } );
setSentryWpcomUserIdMain( authResult.id );
void sendIpcEventToRenderer( 'auth-updated', { token: authResult } );
} catch ( error ) {
Sentry.captureException( error );
Expand Down
5 changes: 5 additions & 0 deletions apps/studio/src/lib/main-sentry-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Sentry from '@sentry/electron/main';

export function setSentryWpcomUserIdMain( id: number | undefined ) {
Sentry.setTag( 'wpcom.user.id', id );
}
5 changes: 5 additions & 0 deletions apps/studio/src/lib/renderer-sentry-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Sentry from '@sentry/electron/renderer';

export function setSentryWpcomUserIdRenderer( id: number | undefined ) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally added postfix as ...Renderer and ...Main to simplify code reading, since sometimes it feels confusing and not clear - is it error from main or from renderer, so easy to make a mistake.

Sentry.setTag( 'wpcom.user.id', id );
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wpcom.user.id - I see that Sentry uses dot as a delimiter for tags, that's why I also went this way.

}
1 change: 1 addition & 0 deletions apps/studio/src/tests/execute-wp-cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ vi.mock( 'src/constants', () => ( {
} ) );
vi.mock( '@sentry/electron/main', () => ( {
captureException: vi.fn(),
setTag: vi.fn(),
} ) );
import { executeCliCommand } from 'src/modules/cli/lib/execute-command';
import type { CliCommandResult } from 'src/modules/cli/lib/execute-command';
Expand Down
1 change: 1 addition & 0 deletions apps/studio/src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ vi.mock( '@sentry/electron/main', () => ( {
captureException: vi.fn(),
captureMessage: vi.fn(),
setUser: vi.fn(),
setTag: vi.fn(),
} ) );
vi.mock( import( 'src/lib/bump-stats' ), async ( importOriginal ) => {
const actual = await importOriginal();
Expand Down
1 change: 1 addition & 0 deletions apps/studio/src/tests/ipc-handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ vi.mock( '@studio/common/lib/fs-utils' );
vi.mock( '@sentry/electron/main', () => ( {
captureException: vi.fn(),
captureMessage: vi.fn(),
setTag: vi.fn(),
} ) );
vi.mock( 'src/site-server' );
vi.mock( 'src/lib/wordpress-setup', () => ( {
Expand Down
1 change: 1 addition & 0 deletions apps/studio/src/tests/open-file-in-ide.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ vi.mock( '@studio/common/lib/fs-utils', () => ( {
vi.mock( '@sentry/electron/main', () => ( {
captureException: vi.fn(),
captureMessage: vi.fn(),
setTag: vi.fn(),
} ) );
vi.mock( 'src/site-server' );
vi.mock( 'src/lib/is-installed' );
Expand Down
1 change: 1 addition & 0 deletions apps/studio/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ global.ResizeObserver = require( 'resize-observer-polyfill' );
vi.mock( '@sentry/electron/main', () => ( {
captureException: vi.fn(),
captureMessage: vi.fn(),
setTag: vi.fn(),
} ) );

vi.mock( 'electron', () => {
Expand Down
Loading