Skip to content

Commit

Permalink
Connects integrations if they weren't connected
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Oct 24, 2024
1 parent 19f192d commit 21d3dc8
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion src/plus/startWork/startWork.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { QuickInputButton } from 'vscode';
import type { QuickInputButton, QuickPick } from 'vscode';
import { ThemeIcon, Uri } from 'vscode';
import type {
AsyncStepResultGenerator,
PartialStepState,
StepGenerator,
StepResultGenerator,
Expand All @@ -11,12 +12,16 @@ import {
canPickStepContinue,
createPickStep,
endSteps,
freezeStep,
QuickCommand,
StepResultBreak,
} from '../../commands/quickCommand';
import { ensureAccessStep } from '../../commands/quickCommand.steps';
import { getSteps } from '../../commands/quickWizard.utils';
import type { OpenWalkthroughCommandArgs } from '../../commands/walkthroughs';
import { proBadge } from '../../constants';
import { Commands } from '../../constants.commands';
import type { IntegrationId } from '../../constants.integrations';
import { HostingIntegrationId } from '../../constants.integrations';
import type { Container } from '../../container';
import { PlusFeatures } from '../../features';
Expand All @@ -26,6 +31,9 @@ import { createQuickPickItemOfT, createQuickPickSeparator } from '../../quickpic
import type { DirectiveQuickPickItem } from '../../quickpicks/items/directive';
import { createDirectiveQuickPickItem, Directive, isDirectiveQuickPickItem } from '../../quickpicks/items/directive';
import { fromNow } from '../../system/date';
import { some } from '../../system/iterable';
import { executeCommand } from '../../system/vscode/command';
import { configuration } from '../../system/vscode/configuration';

export type StartWorkItem = {
item: SearchedIssue;
Expand All @@ -41,6 +49,7 @@ export type StartWorkResult = { items: StartWorkItem[] };
interface Context {
result: StartWorkResult;
title: string;
connectedIntegrations: Map<IntegrationId, boolean>;
}

interface State {
Expand All @@ -63,6 +72,8 @@ function assertsStartWorkStepState(state: StepState<State>): asserts state is St
throw new Error('Missing item');
}

export const supportedStartWorkIntegrations = [HostingIntegrationId.GitHub];

export class StartWorkCommand extends QuickCommand<State> {
constructor(container: Container) {
super(container, 'startWork', 'startWork', `Start Work\u00a0\u00a0${proBadge}`, {
Expand All @@ -82,10 +93,33 @@ export class StartWorkCommand extends QuickCommand<State> {
const context: Context = {
result: { items: [] },
title: this.title,
connectedIntegrations: await this.container.launchpad.getConnectedIntegrations(),
};

while (this.canStepsContinue(state)) {
context.title = this.title;

const hasConnectedIntegrations = [...context.connectedIntegrations.values()].some(c => c);
if (!hasConnectedIntegrations) {
// if (this.container.telemetry.enabled) {
// this.container.telemetry.sendEvent(
// opened ? 'launchpad/steps/connect' : 'launchpad/opened',
// {
// ...context.telemetryContext!,
// connected: false,
// },
// this.source,
// );
// }
const isUsingCloudIntegrations = configuration.get('cloudIntegrations.enabled', undefined, false);
// const result = isUsingCloudIntegrations
// ? yield* this.confirmCloudIntegrationsConnectStep(state, context)
// : yield* this.confirmLocalIntegrationConnectStep(state, context);
const result = yield* this.confirmCloudIntegrationsConnectStep(state, context);
if (result === StepResultBreak) {
return result;
}
}
const result = yield* ensureAccessStep(state, context, PlusFeatures.Launchpad);
if (result === StepResultBreak) continue;

Expand Down Expand Up @@ -135,6 +169,76 @@ export class StartWorkCommand extends QuickCommand<State> {
return state.counter < 0 ? StepResultBreak : undefined;
}

private async *confirmCloudIntegrationsConnectStep(
state: StepState<State>,
context: Context,
): AsyncStepResultGenerator<{ connected: boolean | IntegrationId; resume: () => void }> {
const hasConnectedIntegration = some(context.connectedIntegrations.values(), c => c);
const step = this.createConfirmStep(
`${this.title} \u00a0\u2022\u00a0 Connect an ${hasConnectedIntegration ? 'Additional ' : ''}Integration`,
[
createDirectiveQuickPickItem(Directive.Cancel, undefined, {
label: 'Start Work lets you start work on an issue',
detail: 'Click to learn more about Start Work',
iconPath: new ThemeIcon('rocket'),
onDidSelect: () =>
void executeCommand<OpenWalkthroughCommandArgs>(Commands.OpenWalkthrough, {
step: 'launchpad',
source: 'launchpad',
detail: 'info',
}),
}),
createQuickPickSeparator(),
createQuickPickItemOfT(
{
label: `Connect an ${hasConnectedIntegration ? 'Additional ' : ''}Integration...`,
detail: hasConnectedIntegration
? 'Connect additional integrations to view their issues in Start Work'
: 'Connect an integration to accelerate your work',
picked: true,
},
true,
),
],
createDirectiveQuickPickItem(Directive.Cancel, false, { label: 'Cancel' }),
{
placeholder: hasConnectedIntegration
? 'Connect additional integrations to Start Work'
: 'Connect an integration to get started with Start Work',
buttons: [],
ignoreFocusOut: true,
},
);

// Note: This is a hack to allow the quickpick to stay alive after the user finishes connecting the integration.
// Otherwise it disappears.
let freeze!: () => Disposable;
let quickpick!: QuickPick<any>;
step.onDidActivate = qp => {
quickpick = qp;
freeze = () => freezeStep(step, qp);
};

const selection: StepSelection<typeof step> = yield step;

if (canPickStepContinue(step, state, selection)) {
const previousPlaceholder = quickpick.placeholder;
quickpick.placeholder = 'Connecting integrations...';
quickpick.ignoreFocusOut = true;
const resume = freeze();
const connected = await this.container.integrations.connectCloudIntegrations(
{ integrationIds: supportedStartWorkIntegrations },
{
source: 'launchpad',
},
);
quickpick.placeholder = previousPlaceholder;
return { connected: connected, resume: () => resume[Symbol.dispose]() };
}

return StepResultBreak;
}

private *pickIssueStep(state: StepState<State>, context: Context): StepResultGenerator<StartWorkItem> {
const buildIssueItem = (i: StartWorkItem) => {
const buttons = [StartWorkQuickInputButton];
Expand Down Expand Up @@ -260,6 +364,16 @@ export class StartWorkCommand extends QuickCommand<State> {
if (item != null) {
state.item = item;
}
// void executeGitCommand({
// command: 'branch',
// state: {
// subcommand: 'create',
// repo: undefined,
// name: `${issue.id}-${issue.title}`,
// suggestNameOnly: true,
// //flags: ['--switch'],
// },
// });
}
}

Expand Down

0 comments on commit 21d3dc8

Please sign in to comment.