Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/openshift/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { vsCommand, VsCommandError } from '../vscommand';
import AddServiceBindingViewLoader, { ServiceBindingFormResponse } from '../webview/add-service-binding/addServiceBindingViewLoader';
import CreateComponentLoader from '../webview/create-component/createComponentLoader';
import { OpenShiftTerminalApi, OpenShiftTerminalManager } from '../webview/openshift-terminal/openShiftTerminal';
import OpenShiftItem, { clusterRequired } from './openshiftItem';
import OpenShiftItem, { clusterRequired, projectRequired } from './openshiftItem';

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

@vsCommand('openshift.component.dev')
@clusterRequired()
@projectRequired()
static async dev(component: ComponentWorkspaceFolder) {
return Component.devRunOn(component, undefined);
}

@vsCommand('openshift.component.dev.manual')
@clusterRequired()
@projectRequired()
static async devManual(component: ComponentWorkspaceFolder): Promise<void> {
await Component.devRunOn(component, undefined, true);
}
Expand Down
37 changes: 36 additions & 1 deletion src/openshift/openshiftItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*-----------------------------------------------------------------------------------------------*/

import { commands, QuickPickItem, window } from 'vscode';
import { inputValue } from '../util/inputValue';
import { Oc } from '../oc/ocWrapper';
import { Project } from '../oc/project';
import { ServerlessFunctionView } from '../serverlessFunction/view';
import { inputValue } from '../util/inputValue';
import * as NameValidator from './nameValidator';

export class QuickPickCommand implements QuickPickItem {
Expand Down Expand Up @@ -88,3 +88,38 @@ export function clusterRequired() {
};
};
}

export function projectRequired() {
return function (_target: any, key: string, descriptor: any): void {
let fnKey: string | undefined;
// eslint-disable-next-line @typescript-eslint/ban-types
let fn: Function | undefined;

if (typeof descriptor.value === 'function') {
fnKey = 'value';
fn = descriptor.value;
} else {
throw new Error('not supported');
}

descriptor[fnKey] = async function (...args: any[]): Promise<any> {
let projects = await Oc.Instance.getProjects();
let activeProject = await Oc.Instance.getActiveProject();
let activeProjectExists = projects.find(project => project.name === activeProject);
if (activeProjectExists) {
return fn.apply(this, args);
}
const SELECT_PROJECT = 'Select or Create Project';
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');
if (result === SELECT_PROJECT) {
await commands.executeCommand('openshift.project.set');
projects = await Oc.Instance.getProjects();
activeProject = await Oc.Instance.getActiveProject();
activeProjectExists = projects.find(project => project.name === activeProject);
if (activeProjectExists) {
return fn.apply(this, args);
}
}
};
};
}
4 changes: 3 additions & 1 deletion src/yamlFileCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import { window } from 'vscode';
import { Oc } from './oc/ocWrapper';
import { clusterRequired } from './openshift/openshiftItem';
import { clusterRequired, projectRequired } from './openshift/openshiftItem';
import { vsCommand } from './vscommand';

export class YamlFileCommands {

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

@vsCommand('openshift.delete')
@clusterRequired()
@projectRequired()
public static async delete(): Promise<string | null> {
const document = window.activeTextEditor ? window.activeTextEditor.document : undefined;
const pleaseSave = 'Please save your changes before executing \'OpenShift: Delete\' command.';
Expand Down
7 changes: 4 additions & 3 deletions test/unit/oc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import { window } from 'vscode';
import { Oc } from '../../src/oc/ocWrapper';
import { Odo } from '../../src/odo/odoWrapper';
import { Project } from '../../src/oc/project';
import { Odo } from '../../src/odo/odoWrapper';
import { ToolsConfig } from '../../src/tools';
import { ChildProcessUtil } from '../../src/util/childProcessUtil';
import { YamlFileCommands } from '../../src/yamlFileCommands';
Expand Down Expand Up @@ -127,11 +127,12 @@ suite('Oc', function() {
expect(savedErr === 'error');
});

test('errors when there is no active project', async function() {
test('shows warning message when there is no active project', async function() {
getActiveProjectStub.resetBehavior();
getActiveProjectStub.resolves(undefined);
sandbox.stub(window, 'activeTextEditor').value(TextEditorMock);
expect(await YamlFileCommands.create()).null;
expect(await YamlFileCommands.create());
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');
});

});