Skip to content

Commit

Permalink
Draft-2 of authorizing to GK when user wants to connect an integratio…
Browse files Browse the repository at this point in the history
…n GLVSC-554
  • Loading branch information
sergeibbb committed Jun 21, 2024
1 parent afcb2f4 commit 4dbbd89
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
5 changes: 2 additions & 3 deletions src/commands/cloudIntegrations.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { Source } from '../constants';
import type { Source, SupportedCloudIntegrationIds } from '../constants';
import { Commands } from '../constants';
import type { Container } from '../container';
import type { IssueIntegrationId } from '../plus/integrations/providers/models';
import { command } from '../system/command';
import { Command } from './base';

export interface ManageCloudIntegrationsCommandArgs extends Source {
integrationId?: IssueIntegrationId.Jira;
integrationId?: SupportedCloudIntegrationIds;
}

@command()
Expand Down
15 changes: 14 additions & 1 deletion src/commands/remoteProviders.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Commands } from '../constants';
import { commands } from 'vscode';
import type { SupportedCloudIntegrationIds } from '../constants';
import { Commands, supportedCloudIntegrationIds } from '../constants';
import type { Container } from '../container';
import type { GitCommit } from '../git/models/commit';
import type { GitRemote } from '../git/models/remote';
Expand All @@ -10,6 +12,7 @@ import { command } from '../system/command';
import { first } from '../system/iterable';
import type { CommandContext } from './base';
import { Command, isCommandContextViewNodeHasRemote } from './base';
import { ManageCloudIntegrationsCommand } from './cloudIntegrations';

export interface ConnectRemoteProviderCommandArgs {
remote: string;
Expand Down Expand Up @@ -93,6 +96,16 @@ export class ConnectRemoteProviderCommand extends Command {
if (integration == null) return false;

const connected = await integration.connect();

if (!connected && remote?.provider != null && supportedCloudIntegrationIds.includes(remote.provider.id)) {
await commands.executeCommand(Commands.PlusManageCloudIntegrations, {
integrationId: remote.provider.id as SupportedCloudIntegrationIds,
source: 'connectRemoteProvider',
detail: repoPath,
});
return;
}

if (
connected &&
!(remotes ?? (await this.container.git.getRemotesWithProviders(repoPath))).some(r => r.default)
Expand Down
10 changes: 8 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Environment } from './container';
import type { StoredSearchQuery } from './git/search';
import type { Subscription, SubscriptionPlanId, SubscriptionState } from './plus/gk/account/subscription';
import type { Integration } from './plus/integrations/integration';
import type { IntegrationId, IssueIntegrationId } from './plus/integrations/providers/models';
import type { HostingIntegrationId, IntegrationId, IssueIntegrationId } from './plus/integrations/providers/models';
import type { TelemetryEventData } from './telemetry/telemetry';
import type { TrackedUsage, TrackedUsageKeys } from './telemetry/usageTracker';

Expand Down Expand Up @@ -834,6 +834,7 @@ export type Sources =
| 'code-suggest'
| 'cloud-patches'
| 'commandPalette'
| 'connectRemoteProvider'
| 'deeplink'
| 'git-commands'
| 'graph'
Expand Down Expand Up @@ -875,6 +876,11 @@ export type SupportedAIModels =
| `openai:${AIModels<'openai'>}`
| 'vscode';

// export const supportedCloudIntegrationIds = [HostingIntegrationId.GitHub, IssueIntegrationId.Jira];
// export type SupportedCloudIntegrationIds = (typeof supportedCloudIntegrationIds)[number];
export const supportedCloudIntegrationIds = ['github', 'jira'];
export type SupportedCloudIntegrationIds = HostingIntegrationId.GitHub | IssueIntegrationId.Jira;

export type SecretKeys =
| `gitlens.integration.auth:${IntegrationId}|${string}`
| `gitlens.${AIProviders}.key`
Expand Down Expand Up @@ -1224,7 +1230,7 @@ export type TelemetryEvents = {
};
/** Sent when a user chooses to manage the cloud integrations */
'cloudIntegrations/settingsOpened': {
'integration.id': IssueIntegrationId | undefined;
'integration.id': SupportedCloudIntegrationIds | undefined;
};

/** Sent when a code suggestion is archived */
Expand Down
21 changes: 1 addition & 20 deletions src/plus/integrations/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ import type { CancellationToken, Event, MessageItem } from 'vscode';
import { EventEmitter, window } from 'vscode';
import type { DynamicAutolinkReference } from '../../annotations/autolinks';
import type { AutolinkReference } from '../../config';
import type { Source } from '../../constants';
import type { Container } from '../../container';
import {
AuthenticationError,
AuthenticationRequiredError,
CancellationError,
ProviderRequestClientError,
} from '../../errors';
import { AuthenticationError, CancellationError, ProviderRequestClientError } from '../../errors';
import type { PagedResult } from '../../git/gitProvider';
import type { Account } from '../../git/models/author';
import type { DefaultBranch } from '../../git/models/defaultBranch';
Expand Down Expand Up @@ -286,19 +280,6 @@ export abstract class IntegrationBase<
if (ex instanceof Error && ex.message.includes('User did not consent')) {
return undefined;
}
if (createIfNeeded && ex instanceof AuthenticationRequiredError) {
const source: Source = {
source: 'integrations',
detail: {
action: 'connect',
integration: this.id,
},
};

Promise.resolve(this.container.subscription.loginOrSignUp(true, source)).catch(
Logger.error.bind(Logger),
);
}

session = null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/plus/integrations/integrationService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AuthenticationSessionsChangeEvent, CancellationToken, Event } from 'vscode';
import { authentication, Disposable, env, EventEmitter, window } from 'vscode';
import { isWeb } from '@env/platform';
import type { Source } from '../../constants';
import type { Source, SupportedCloudIntegrationIds } from '../../constants';
import type { Container } from '../../container';
import type { SearchedIssue } from '../../git/models/issue';
import type { SearchedPullRequest } from '../../git/models/pullRequest';
Expand Down Expand Up @@ -107,7 +107,7 @@ export class IntegrationService implements Disposable {
}
}

async manageCloudIntegrations(integrationId: IssueIntegrationId.Jira | undefined, source: Source | undefined) {
async manageCloudIntegrations(integrationId: SupportedCloudIntegrationIds | undefined, source: Source | undefined) {
if (this.container.telemetry.enabled) {
this.container.telemetry.sendEvent(
'cloudIntegrations/settingsOpened',
Expand Down

0 comments on commit 4dbbd89

Please sign in to comment.