Skip to content

Commit 06aa060

Browse files
Sends context on some registration/login cases
1 parent 2fc1624 commit 06aa060

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,8 @@ export type Sources =
857857
| 'scm-input'
858858
| 'subscription'
859859
| 'walkthrough'
860-
| 'welcome';
860+
| 'welcome'
861+
| 'worktrees';
861862

862863
export interface Source {
863864
source: Sources;

src/plus/gk/account/authenticationConnection.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import type { ServerConnection } from '../serverConnection';
1313

1414
export const LoginUriPathPrefix = 'login';
1515
export const AuthenticationUriPathPrefix = 'did-authenticate';
16+
export const enum AuthenticationContext {
17+
Graph = 'graph',
18+
Worktrees = 'worktrees',
19+
VisualFileHistory = 'visual_file_history',
20+
}
1621

1722
interface AccountInfo {
1823
id: string;
@@ -62,7 +67,12 @@ export class AuthenticationConnection implements Disposable {
6267
}
6368

6469
@debug()
65-
async login(scopes: string[], scopeKey: string, signUp: boolean = false): Promise<string> {
70+
async login(
71+
scopes: string[],
72+
scopeKey: string,
73+
signUp: boolean = false,
74+
context?: AuthenticationContext,
75+
): Promise<string> {
6676
this.updateStatusBarItem(true);
6777

6878
// Include a state parameter here to prevent CSRF attacks
@@ -76,9 +86,9 @@ export class AuthenticationConnection implements Disposable {
7686

7787
const uri = this.container.getGkDevUri(
7888
signUp ? 'register' : 'login',
79-
`${scopes.includes('gitlens') ? 'source=gitlens&' : ''}state=${encodeURIComponent(
80-
gkstate,
81-
)}&redirect_uri=${encodeURIComponent(callbackUri.toString(true))}`,
89+
`${scopes.includes('gitlens') ? 'source=gitlens&' : ''}${
90+
context != null ? `context=${context}&` : ''
91+
}state=${encodeURIComponent(gkstate)}&redirect_uri=${encodeURIComponent(callbackUri.toString(true))}`,
8292
);
8393

8494
void (await openUrl(uri.toString(true)));

src/plus/gk/account/authenticationProvider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { debug } from '../../../system/decorators/log';
1111
import { Logger } from '../../../system/logger';
1212
import { getLogScope, setLogScopeExit } from '../../../system/logger.scope';
1313
import type { ServerConnection } from '../serverConnection';
14+
import type { AuthenticationContext } from './authenticationConnection';
1415
import { AuthenticationConnection } from './authenticationConnection';
1516

1617
interface StoredSession {
@@ -31,6 +32,7 @@ const authenticationLabel = 'GitKraken: GitLens';
3132
export interface AuthenticationProviderOptions {
3233
signUp?: boolean;
3334
signIn?: { code: string; state?: string };
35+
context?: AuthenticationContext;
3436
}
3537

3638
export class AccountAuthenticationProvider implements AuthenticationProvider, Disposable {
@@ -99,7 +101,7 @@ export class AccountAuthenticationProvider implements AuthenticationProvider, Di
99101
const token =
100102
options?.signIn != null
101103
? await this._authConnection.getTokenFromCodeAndState(options.signIn.code, options.signIn.state)
102-
: await this._authConnection.login(scopes, scopesKey, options?.signUp);
104+
: await this._authConnection.login(scopes, scopesKey, options?.signUp, options?.context);
103105
const session = await this.createSessionForToken(token, scopes);
104106

105107
const sessions = await this._sessionsPromise;

src/plus/gk/account/subscriptionService.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import type { GKCheckInResponse } from '../checkin';
4747
import { getSubscriptionFromCheckIn } from '../checkin';
4848
import type { ServerConnection } from '../serverConnection';
4949
import { ensurePlusFeaturesEnabled } from '../utils';
50+
import { AuthenticationContext } from './authenticationConnection';
5051
import { authenticationProviderId, authenticationProviderScopes } from './authenticationProvider';
5152
import type { Organization } from './organization';
5253
import type { Subscription } from './subscription';
@@ -361,7 +362,31 @@ export class SubscriptionService implements Disposable {
361362
);
362363
}
363364

364-
return this.loginCore({ signUp: signUp, source: source });
365+
let context: AuthenticationContext | undefined;
366+
switch (source?.source) {
367+
case 'graph':
368+
context = AuthenticationContext.Graph;
369+
break;
370+
case 'timeline':
371+
context = AuthenticationContext.VisualFileHistory;
372+
break;
373+
case 'git-commands':
374+
if (
375+
source.detail != null &&
376+
typeof source.detail !== 'string' &&
377+
(source.detail['action'] === 'worktree' ||
378+
source.detail['step.title'] === 'Create Worktree' ||
379+
source.detail['step.title'] === 'Open Worktree')
380+
) {
381+
context = AuthenticationContext.Worktrees;
382+
}
383+
break;
384+
case 'worktrees':
385+
context = AuthenticationContext.Worktrees;
386+
break;
387+
}
388+
389+
return this.loginCore({ signUp: signUp, source: source, context: context });
365390
}
366391

367392
async loginWithCode(authentication: { code: string; state?: string }, source?: Source): Promise<boolean> {
@@ -382,6 +407,7 @@ export class SubscriptionService implements Disposable {
382407
signUp?: boolean;
383408
source?: Source;
384409
signIn?: { code: string; state?: string };
410+
context?: AuthenticationContext;
385411
}): Promise<boolean> {
386412
// Abort any waiting authentication to ensure we can start a new flow
387413
await this.container.accountAuthentication.abort();
@@ -390,6 +416,7 @@ export class SubscriptionService implements Disposable {
390416
const session = await this.ensureSession(true, {
391417
signIn: options?.signIn,
392418
signUp: options?.signUp,
419+
context: options?.context,
393420
});
394421
const loggedIn = Boolean(session);
395422
if (loggedIn) {
@@ -941,7 +968,12 @@ export class SubscriptionService implements Disposable {
941968
@debug()
942969
private async ensureSession(
943970
createIfNeeded: boolean,
944-
options?: { force?: boolean; signUp?: boolean; signIn?: { code: string; state?: string } },
971+
options?: {
972+
force?: boolean;
973+
signUp?: boolean;
974+
signIn?: { code: string; state?: string };
975+
context?: AuthenticationContext;
976+
},
945977
): Promise<AuthenticationSession | undefined> {
946978
if (this._sessionPromise != null) {
947979
void (await this._sessionPromise);
@@ -954,6 +986,7 @@ export class SubscriptionService implements Disposable {
954986
this._sessionPromise = this.getOrCreateSession(createIfNeeded, {
955987
signUp: options?.signUp,
956988
signIn: options?.signIn,
989+
context: options?.context,
957990
}).then(
958991
s => {
959992
this._session = s;
@@ -975,7 +1008,7 @@ export class SubscriptionService implements Disposable {
9751008
@debug()
9761009
private async getOrCreateSession(
9771010
createIfNeeded: boolean,
978-
options?: { signUp?: boolean; signIn?: { code: string; state?: string } },
1011+
options?: { signUp?: boolean; signIn?: { code: string; state?: string }; context?: AuthenticationContext },
9791012
): Promise<AuthenticationSession | null> {
9801013
const scope = getLogScope();
9811014

0 commit comments

Comments
 (0)