Skip to content

Commit df5cf95

Browse files
Adds sync flag to prevent cloud sync from opening connect prompt (#3612)
1 parent 211a894 commit df5cf95

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed

src/plus/integrations/authentication/integrationAuthentication.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ export interface IntegrationAuthenticationProvider extends Disposable {
4747
deleteSession(descriptor?: IntegrationAuthenticationSessionDescriptor): Promise<void>;
4848
getSession(
4949
descriptor?: IntegrationAuthenticationSessionDescriptor,
50-
options?: { createIfNeeded?: boolean; forceNewSession?: boolean; source?: Sources },
50+
options?:
51+
| { createIfNeeded?: boolean; forceNewSession?: boolean; sync?: never; source?: Sources }
52+
| { createIfNeeded?: never; forceNewSession?: never; sync: boolean; source?: Sources },
5153
): Promise<ProviderAuthenticationSession | undefined>;
5254
get onDidChange(): Event<void>;
5355
}
@@ -73,7 +75,21 @@ abstract class IntegrationAuthenticationProviderBase<ID extends IntegrationId =
7375
protected abstract fetchOrCreateSession(
7476
storedSession: ProviderAuthenticationSession | undefined,
7577
descriptor?: IntegrationAuthenticationSessionDescriptor,
76-
options?: { createIfNeeded?: boolean; forceNewSession?: boolean; refreshIfExpired?: boolean; source?: Sources },
78+
options?:
79+
| {
80+
createIfNeeded?: boolean;
81+
forceNewSession?: boolean;
82+
sync?: never;
83+
refreshIfExpired?: boolean;
84+
source?: Sources;
85+
}
86+
| {
87+
createIfNeeded?: never;
88+
forceNewSession?: never;
89+
sync: boolean;
90+
refreshIfExpired?: boolean;
91+
source?: Sources;
92+
},
7793
): Promise<ProviderAuthenticationSession | undefined>;
7894

7995
protected abstract deleteAllSecrets(sessionId: string): Promise<void>;
@@ -129,7 +145,9 @@ abstract class IntegrationAuthenticationProviderBase<ID extends IntegrationId =
129145
@debug()
130146
async getSession(
131147
descriptor?: IntegrationAuthenticationSessionDescriptor,
132-
options?: { createIfNeeded?: boolean; forceNewSession?: boolean; source?: Sources },
148+
options?:
149+
| { createIfNeeded?: boolean; forceNewSession?: boolean; sync?: never; source?: Sources }
150+
| { createIfNeeded?: never; forceNewSession?: never; sync: boolean; source?: Sources },
133151
): Promise<ProviderAuthenticationSession | undefined> {
134152
const sessionId = this.getSessionId(descriptor);
135153

@@ -254,13 +272,27 @@ export abstract class CloudIntegrationAuthenticationProvider<
254272
protected override async fetchOrCreateSession(
255273
storedSession: ProviderAuthenticationSession | undefined,
256274
descriptor?: IntegrationAuthenticationSessionDescriptor,
257-
options?: { createIfNeeded?: boolean; forceNewSession?: boolean; refreshIfExpired?: boolean; source?: Sources },
275+
options?:
276+
| {
277+
createIfNeeded?: boolean;
278+
forceNewSession?: boolean;
279+
sync?: never;
280+
refreshIfExpired?: boolean;
281+
source?: Sources;
282+
}
283+
| {
284+
createIfNeeded?: never;
285+
forceNewSession?: never;
286+
sync: boolean;
287+
refreshIfExpired?: boolean;
288+
source?: Sources;
289+
},
258290
): Promise<ProviderAuthenticationSession | undefined> {
259291
// TODO: This is a stopgap to make sure we're not hammering the api on automatic calls to get the session.
260292
// Ultimately we want to timestamp calls to syncCloudIntegrations and use that to determine whether we should
261293
// make the call or not.
262294
let session =
263-
options?.refreshIfExpired || options?.createIfNeeded || options?.forceNewSession
295+
options?.refreshIfExpired || options?.createIfNeeded || options?.forceNewSession || options?.sync
264296
? await this.fetchSession(descriptor)
265297
: undefined;
266298

src/plus/integrations/integration.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,9 @@ export abstract class IntegrationBase<
259259
forceSync = true;
260260
}
261261

262-
await this.ensureSession({ createIfNeeded: forceSync });
262+
// sync option, rather than createIfNeeded, makes sure we don't call connectCloudIntegrations and open a gkdev window
263+
// if there was no session or some problem fetching/refreshing the existing session from the cloud api
264+
await this.ensureSession({ sync: forceSync });
263265
break;
264266
case 'disconnected':
265267
await this.disconnect({ silent: true });
@@ -295,16 +297,26 @@ export abstract class IntegrationBase<
295297
}
296298

297299
@gate()
298-
private async ensureSession(options: {
299-
createIfNeeded?: boolean;
300-
forceNewSession?: boolean;
301-
source?: Sources;
302-
}): Promise<ProviderAuthenticationSession | undefined> {
303-
const { createIfNeeded, forceNewSession, source } = options;
300+
private async ensureSession(
301+
options:
302+
| {
303+
createIfNeeded?: boolean;
304+
forceNewSession?: boolean;
305+
sync?: never;
306+
source?: Sources;
307+
}
308+
| {
309+
createIfNeeded?: never;
310+
forceNewSession?: never;
311+
sync: boolean;
312+
source?: Sources;
313+
},
314+
): Promise<ProviderAuthenticationSession | undefined> {
315+
const { createIfNeeded, forceNewSession, source, sync } = options;
304316
if (this._session != null) return this._session;
305317
if (!configuration.get('integrations.enabled')) return undefined;
306318

307-
if (createIfNeeded) {
319+
if (createIfNeeded || sync) {
308320
await this.container.storage.deleteWorkspace(this.connectedKey);
309321
} else if (this.container.storage.getWorkspace(this.connectedKey) === false) {
310322
return undefined;
@@ -313,11 +325,16 @@ export abstract class IntegrationBase<
313325
let session: ProviderAuthenticationSession | undefined | null;
314326
try {
315327
const authProvider = await this.authenticationService.get(this.authProvider.id);
316-
session = await authProvider.getSession(this.authProviderDescriptor, {
317-
createIfNeeded: createIfNeeded,
318-
forceNewSession: forceNewSession,
319-
source: source,
320-
});
328+
session = await authProvider.getSession(
329+
this.authProviderDescriptor,
330+
sync
331+
? { sync: sync, source: source }
332+
: {
333+
createIfNeeded: createIfNeeded,
334+
forceNewSession: forceNewSession,
335+
source: source,
336+
},
337+
);
321338
} catch (ex) {
322339
await this.container.storage.deleteWorkspace(this.connectedKey);
323340

@@ -328,7 +345,7 @@ export abstract class IntegrationBase<
328345
session = null;
329346
}
330347

331-
if (session === undefined && !createIfNeeded) {
348+
if (session === undefined && !createIfNeeded && !sync) {
332349
await this.container.storage.deleteWorkspace(this.connectedKey);
333350
}
334351

0 commit comments

Comments
 (0)