Skip to content

Commit 4df77b0

Browse files
committed
Prompt the user to select a project
When trying to run "Start Dev", "Manually Start Dev", "Create" (to create resources from a YAML file), or "Delete" (to delete resources from a YAML file), if the current project doesn't exist, display a notification initicating that a project needs to be selected or created, with a button that opens the wizard to do this. Fixes #2581 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 976e968 commit 4df77b0

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

src/openshift/component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { vsCommand, VsCommandError } from '../vscommand';
1919
import AddServiceBindingViewLoader, { ServiceBindingFormResponse } from '../webview/add-service-binding/addServiceBindingViewLoader';
2020
import CreateComponentLoader from '../webview/create-component/createComponentLoader';
2121
import { OpenShiftTerminalApi, OpenShiftTerminalManager } from '../webview/openshift-terminal/openShiftTerminal';
22-
import OpenShiftItem, { clusterRequired } from './openshiftItem';
22+
import OpenShiftItem, { clusterRequired, projectRequired } from './openshiftItem';
2323

2424
function createCancelledResult(stepName: string): any {
2525
const cancelledResult: any = new String('');
@@ -299,12 +299,14 @@ export class Component extends OpenShiftItem {
299299

300300
@vsCommand('openshift.component.dev')
301301
@clusterRequired()
302+
@projectRequired()
302303
static async dev(component: ComponentWorkspaceFolder) {
303304
return Component.devRunOn(component, undefined);
304305
}
305306

306307
@vsCommand('openshift.component.dev.manual')
307308
@clusterRequired()
309+
@projectRequired()
308310
static async devManual(component: ComponentWorkspaceFolder): Promise<void> {
309311
await Component.devRunOn(component, undefined, true);
310312
}

src/openshift/openshiftItem.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*-----------------------------------------------------------------------------------------------*/
55

66
import { commands, QuickPickItem, window } from 'vscode';
7-
import { inputValue } from '../util/inputValue';
87
import { Oc } from '../oc/ocWrapper';
98
import { Project } from '../oc/project';
109
import { ServerlessFunctionView } from '../serverlessFunction/view';
10+
import { inputValue } from '../util/inputValue';
1111
import * as NameValidator from './nameValidator';
1212

1313
export class QuickPickCommand implements QuickPickItem {
@@ -88,3 +88,38 @@ export function clusterRequired() {
8888
};
8989
};
9090
}
91+
92+
export function projectRequired() {
93+
return function (_target: any, key: string, descriptor: any): void {
94+
let fnKey: string | undefined;
95+
// eslint-disable-next-line @typescript-eslint/ban-types
96+
let fn: Function | undefined;
97+
98+
if (typeof descriptor.value === 'function') {
99+
fnKey = 'value';
100+
fn = descriptor.value;
101+
} else {
102+
throw new Error('not supported');
103+
}
104+
105+
descriptor[fnKey] = async function (...args: any[]): Promise<any> {
106+
let projects = await Oc.Instance.getProjects();
107+
let activeProject = await Oc.Instance.getActiveProject();
108+
let activeProjectExists = projects.find(project => project.name === activeProject);
109+
if (activeProjectExists) {
110+
return fn.apply(this, args);
111+
}
112+
const SELECT_PROJECT = 'Select or Create Project';
113+
const result = await window.showWarningMessage('The current project doesn\'t exist. Please select an existing project to work with or create a new project', SELECT_PROJECT, 'Cancel');
114+
if (result === SELECT_PROJECT) {
115+
await commands.executeCommand('openshift.project.set');
116+
projects = await Oc.Instance.getProjects();
117+
activeProject = await Oc.Instance.getActiveProject();
118+
activeProjectExists = projects.find(project => project.name === activeProject);
119+
if (activeProjectExists) {
120+
return fn.apply(this, args);
121+
}
122+
}
123+
};
124+
};
125+
}

src/yamlFileCommands.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
import { window } from 'vscode';
77
import { Oc } from './oc/ocWrapper';
8-
import { clusterRequired } from './openshift/openshiftItem';
8+
import { clusterRequired, projectRequired } from './openshift/openshiftItem';
99
import { vsCommand } from './vscommand';
1010

1111
export class YamlFileCommands {
1212

1313
@vsCommand('openshift.create')
1414
@clusterRequired()
15+
@projectRequired()
1516
public static async create(): Promise<string | null> {
1617
const document = window.activeTextEditor ? window.activeTextEditor.document : undefined;
1718
const pleaseSave = 'Please save your changes before executing \'OpenShift: Create\' command.';
@@ -54,6 +55,7 @@ export class YamlFileCommands {
5455

5556
@vsCommand('openshift.delete')
5657
@clusterRequired()
58+
@projectRequired()
5759
public static async delete(): Promise<string | null> {
5860
const document = window.activeTextEditor ? window.activeTextEditor.document : undefined;
5961
const pleaseSave = 'Please save your changes before executing \'OpenShift: Delete\' command.';

test/unit/oc.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import * as sinon from 'sinon';
88
import * as sinonChai from 'sinon-chai';
99
import { window } from 'vscode';
1010
import { Oc } from '../../src/oc/ocWrapper';
11-
import { Odo } from '../../src/odo/odoWrapper';
1211
import { Project } from '../../src/oc/project';
12+
import { Odo } from '../../src/odo/odoWrapper';
1313
import { ToolsConfig } from '../../src/tools';
1414
import { ChildProcessUtil } from '../../src/util/childProcessUtil';
1515
import { YamlFileCommands } from '../../src/yamlFileCommands';
@@ -127,11 +127,12 @@ suite('Oc', function() {
127127
expect(savedErr === 'error');
128128
});
129129

130-
test('errors when there is no active project', async function() {
130+
test('shows warning message when there is no active project', async function() {
131131
getActiveProjectStub.resetBehavior();
132132
getActiveProjectStub.resolves(undefined);
133133
sandbox.stub(window, 'activeTextEditor').value(TextEditorMock);
134-
expect(await YamlFileCommands.create()).null;
134+
expect(await YamlFileCommands.create());
135+
expect(warnStub).to.be.calledOnceWithExactly('The current project doesn\'t exist. Please select an existing project to work with or create a new project', 'Select or Create Project', 'Cancel');
135136
});
136137

137138
});

0 commit comments

Comments
 (0)