From 6af63ceaccc92b62692961780a9a3f38e69a1fe4 Mon Sep 17 00:00:00 2001 From: Ramin Tadayon Date: Tue, 24 Sep 2024 14:06:24 -0700 Subject: [PATCH] Adds sync flag to prevent cloud sync from opening connect prompt --- .../integrationAuthentication.ts | 42 ++++++++++++++--- src/plus/integrations/integration.ts | 45 +++++++++++++------ 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/plus/integrations/authentication/integrationAuthentication.ts b/src/plus/integrations/authentication/integrationAuthentication.ts index a235a982444d0..25eec2a9be581 100644 --- a/src/plus/integrations/authentication/integrationAuthentication.ts +++ b/src/plus/integrations/authentication/integrationAuthentication.ts @@ -47,7 +47,9 @@ export interface IntegrationAuthenticationProvider extends Disposable { deleteSession(descriptor?: IntegrationAuthenticationSessionDescriptor): Promise; getSession( descriptor?: IntegrationAuthenticationSessionDescriptor, - options?: { createIfNeeded?: boolean; forceNewSession?: boolean; source?: Sources }, + options?: + | { createIfNeeded?: boolean; forceNewSession?: boolean; sync?: never; source?: Sources } + | { createIfNeeded?: never; forceNewSession?: never; sync: boolean; source?: Sources }, ): Promise; get onDidChange(): Event; } @@ -73,7 +75,21 @@ abstract class IntegrationAuthenticationProviderBase; protected abstract deleteAllSecrets(sessionId: string): Promise; @@ -129,7 +145,9 @@ abstract class IntegrationAuthenticationProviderBase { const sessionId = this.getSessionId(descriptor); @@ -254,13 +272,27 @@ export abstract class CloudIntegrationAuthenticationProvider< protected override async fetchOrCreateSession( storedSession: ProviderAuthenticationSession | undefined, descriptor?: IntegrationAuthenticationSessionDescriptor, - options?: { createIfNeeded?: boolean; forceNewSession?: boolean; refreshIfExpired?: boolean; source?: Sources }, + options?: + | { + createIfNeeded?: boolean; + forceNewSession?: boolean; + sync?: never; + refreshIfExpired?: boolean; + source?: Sources; + } + | { + createIfNeeded?: never; + forceNewSession?: never; + sync: boolean; + refreshIfExpired?: boolean; + source?: Sources; + }, ): Promise { // TODO: This is a stopgap to make sure we're not hammering the api on automatic calls to get the session. // Ultimately we want to timestamp calls to syncCloudIntegrations and use that to determine whether we should // make the call or not. let session = - options?.refreshIfExpired || options?.createIfNeeded || options?.forceNewSession + options?.refreshIfExpired || options?.createIfNeeded || options?.forceNewSession || options?.sync ? await this.fetchSession(descriptor) : undefined; diff --git a/src/plus/integrations/integration.ts b/src/plus/integrations/integration.ts index 136fd252e5900..ce2f0266030d5 100644 --- a/src/plus/integrations/integration.ts +++ b/src/plus/integrations/integration.ts @@ -259,7 +259,9 @@ export abstract class IntegrationBase< forceSync = true; } - await this.ensureSession({ createIfNeeded: forceSync }); + // sync option, rather than createIfNeeded, makes sure we don't call connectCloudIntegrations and open a gkdev window + // if there was no session or some problem fetching/refreshing the existing session from the cloud api + await this.ensureSession({ sync: forceSync }); break; case 'disconnected': await this.disconnect({ silent: true }); @@ -295,16 +297,26 @@ export abstract class IntegrationBase< } @gate() - private async ensureSession(options: { - createIfNeeded?: boolean; - forceNewSession?: boolean; - source?: Sources; - }): Promise { - const { createIfNeeded, forceNewSession, source } = options; + private async ensureSession( + options: + | { + createIfNeeded?: boolean; + forceNewSession?: boolean; + sync?: never; + source?: Sources; + } + | { + createIfNeeded?: never; + forceNewSession?: never; + sync: boolean; + source?: Sources; + }, + ): Promise { + const { createIfNeeded, forceNewSession, source, sync } = options; if (this._session != null) return this._session; if (!configuration.get('integrations.enabled')) return undefined; - if (createIfNeeded) { + if (createIfNeeded || sync) { await this.container.storage.deleteWorkspace(this.connectedKey); } else if (this.container.storage.getWorkspace(this.connectedKey) === false) { return undefined; @@ -313,11 +325,16 @@ export abstract class IntegrationBase< let session: ProviderAuthenticationSession | undefined | null; try { const authProvider = await this.authenticationService.get(this.authProvider.id); - session = await authProvider.getSession(this.authProviderDescriptor, { - createIfNeeded: createIfNeeded, - forceNewSession: forceNewSession, - source: source, - }); + session = await authProvider.getSession( + this.authProviderDescriptor, + sync + ? { sync: sync, source: source } + : { + createIfNeeded: createIfNeeded, + forceNewSession: forceNewSession, + source: source, + }, + ); } catch (ex) { await this.container.storage.deleteWorkspace(this.connectedKey); @@ -328,7 +345,7 @@ export abstract class IntegrationBase< session = null; } - if (session === undefined && !createIfNeeded) { + if (session === undefined && !createIfNeeded && !sync) { await this.container.storage.deleteWorkspace(this.connectedKey); }