Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tekton: Delete commands should show confirmation info message and run without terminal #98

Merged
merged 2 commits into from
Dec 12, 2019
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
13 changes: 11 additions & 2 deletions src/tekton/clustertask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import { TektonItem } from './tektonitem';
import { TektonNode, Command } from '../tkn';
import { Progress } from '../util/progress';
import { window } from 'vscode';

export class ClusterTask extends TektonItem {

Expand All @@ -16,8 +18,15 @@ export class ClusterTask extends TektonItem {
if (clustertasks) { ClusterTask.tkn.executeInTerminal(Command.listClusterTasksinTerminal()); }
}

static async delete(clustertask: TektonNode): Promise<void> {
if (clustertask) { ClusterTask.tkn.executeInTerminal(Command.deleteClusterTask(clustertask.getName())); }
static async delete(clustertask: TektonNode): Promise<string> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be good request except some issues with messages:

  1. Every message should start with capital letter.
  2. Should use appropriate articles a/an/the.
  3. Message should finish with './?/!'
  4. Custom resource names should be in camel case: ClusterTask, TaskRun, PiplelineResource, PipelineRun, Task, Pipeline

Below is an example:

        const value = await window.showWarningMessage(`Do you want to delete the ClusterTask '${clustertask.getName()}\'?`, 'Yes', 'Cancel');
        if (value === 'Yes') {
            return Progress.execFunctionWithProgress(`Deleting the ClusterTask '${clustertask.getName()}'.`, () => 
                ClusterTask.tkn.execute(Command.deleteClusterTask(clustertask.getName())))
                .then(() => `The ClusterTask '${clustertask.getName()}' successfully deleted.`)
                .catch((err) => Promise.reject(`Failure to delete the ClusterTask '${clustertask.getName()}': '${err}'.`));

const value = await window.showWarningMessage(`Do you want to delete the ClusterTask '${clustertask.getName()}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the ClusterTask '${clustertask.getName()}'.`, () =>
ClusterTask.tkn.execute(Command.deleteClusterTask(clustertask.getName())))
.then(() => `The ClusterTask '${clustertask.getName()}' successfully deleted.`)
.catch((err) => Promise.reject(`Failed to delete the ClusterTask '${clustertask.getName()}': '${err}'.`));
}
return null;
}

}
12 changes: 9 additions & 3 deletions src/tekton/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as cliInstance from '../cli';
import { Cli } from '../cli';
import * as k8s from 'vscode-kubernetes-tools-api';


export interface NameType {
name: string;
type: string;
Expand Down Expand Up @@ -271,8 +270,15 @@ export class Pipeline extends TektonItem {
if (pipeline) { Pipeline.tkn.executeInTerminal(Command.listPipelinesInTerminal(pipeline.getName())); }
}

static async delete(pipeline: TektonNode): Promise<void> {
if (pipeline) {Pipeline.tkn.executeInTerminal(Command.deletePipeline(pipeline.getName())); }
static async delete(pipeline: TektonNode): Promise<string> {
const value = await window.showWarningMessage(`Do you want to delete the Pipeline '${pipeline.getName()}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the Pipeline '${pipeline.getName()}'.`, () =>
Pipeline.tkn.execute(Command.deletePipeline(pipeline.getName())))
.then(() => `The Pipeline '${pipeline.getName()}' successfully deleted.`)
.catch((err) => Promise.reject(`Failed to delete the Pipeline '${pipeline.getName()}': '${err}'.`));
}
return null;
}

}
13 changes: 11 additions & 2 deletions src/tekton/pipelineresource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { TektonItem } from './tektonitem';
import { TektonNode, Command } from '../tkn';
import * as k8s from 'vscode-kubernetes-tools-api';
import { Progress } from '../util/progress';
import { window } from 'vscode';

export class PipelineResource extends TektonItem {

Expand All @@ -18,8 +20,15 @@ export class PipelineResource extends TektonItem {
if (pipelineresource) { PipelineResource.tkn.executeInTerminal(Command.listPipelineResourcesInTerminal(pipelineresource.getName())); }
}

static async delete(pipelineresource: TektonNode): Promise<void> {
if (pipelineresource) { PipelineResource.tkn.executeInTerminal(Command.deletePipelineResource(pipelineresource.getName())); }
static async delete(pipelineresource: TektonNode): Promise<string> {
const value = await window.showWarningMessage(`Do you want to delete the Resource '${pipelineresource.getName()}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the Resource '${pipelineresource.getName()}'.`, () =>
PipelineResource.tkn.execute(Command.deletePipelineResource(pipelineresource.getName())))
.then(() => `The Resource '${pipelineresource.getName()}' successfully deleted.`)
.catch((err) => Promise.reject(`Failed to delete the Resource '${pipelineresource.getName()}': '${err}'.`));
}
return null;
}

}
14 changes: 11 additions & 3 deletions src/tekton/pipelinerun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import { TektonItem } from './tektonitem';
import { TektonNode, Command } from '../tkn';
import * as k8s from 'vscode-kubernetes-tools-api';
import { window } from 'vscode';
import { Progress } from '../util/progress';

export class PipelineRun extends TektonItem {

Expand Down Expand Up @@ -48,8 +49,15 @@ export class PipelineRun extends TektonItem {
if (pipelinerun) { PipelineRun.tkn.executeInTerminal(Command.cancelPipelineRun(pipelinerun.getName())); }
}

static async delete(pipelinerun: TektonNode): Promise<void> {
if (pipelinerun) { PipelineRun.tkn.executeInTerminal(Command.deletePipelineRun(pipelinerun.getName())); }
static async delete(pipelinerun: TektonNode): Promise<string> {
const value = await window.showWarningMessage(`Do you want to delete the PipelineRun '${pipelinerun.getName()}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the PipelineRun '${pipelinerun.getName()}'.`, () =>
PipelineRun.tkn.execute(Command.deletePipelineRun(pipelinerun.getName())))
.then(() => `The PipelineRun '${pipelinerun.getName()}' successfully deleted.`)
.catch((err) => Promise.reject(`Failed to delete the PipelineRun '${pipelinerun.getName()}': '${err}'.`));
}
return null;
}

}
13 changes: 11 additions & 2 deletions src/tekton/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import { TektonItem } from './tektonitem';
import { TektonNode, Command } from '../tkn';
import { window } from 'vscode';
import { Progress } from '../util/progress';

export class Task extends TektonItem {

Expand All @@ -18,7 +20,14 @@ export class Task extends TektonItem {
if (task) { Task.tkn.executeInTerminal(Command.listTasksinTerminal()); }
}

static async delete(task: TektonNode): Promise<void> {
if (task) { Task.tkn.executeInTerminal(Command.deleteTask(task.getName())); }
static async delete(task: TektonNode): Promise<string> {
const value = await window.showWarningMessage(`Do you want to delete the Task '${task.getName()}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the Task '${task.getName()}'.`, () =>
Task.tkn.execute(Command.deleteTask(task.getName())))
.then(() => `The Task '${task.getName()}' successfully deleted.`)
.catch((err) => Promise.reject(`Failed to delete the Task '${task.getName()}': '${err}'.`));
}
return null;
}
}
13 changes: 11 additions & 2 deletions src/tekton/taskrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import { TektonItem } from './tektonitem';
import { TektonNode, Command } from '../tkn';
import { window } from 'vscode';
import { Progress } from '../util/progress';

export class TaskRun extends TektonItem {

Expand All @@ -24,7 +26,14 @@ export class TaskRun extends TektonItem {
TaskRun.tkn.executeInTerminal(Command.showTaskRunFollowLogs(taskrun.getName()));
}

static async delete(taskrun: TektonNode): Promise<void> {
if (taskrun) { TaskRun.tkn.executeInTerminal(Command.deleteTaskRun(taskrun.getName())); }
static async delete(taskrun: TektonNode): Promise<string> {
const value = await window.showWarningMessage(`Do you want to delete the TaskRun '${taskrun.getName()}\'?`, 'Yes', 'Cancel');
if (value === 'Yes') {
return Progress.execFunctionWithProgress(`Deleting the TaskRun '${taskrun.getName()}'.`, () =>
TaskRun.tkn.execute(Command.deleteTaskRun(taskrun.getName())))
.then(() => `The TaskRun '${taskrun.getName()}' successfully deleted.`)
.catch((err) => Promise.reject(`Failed to delete the TaskRun '${taskrun.getName()}': '${err}'.`));
}
return null;
}
}
12 changes: 6 additions & 6 deletions src/tkn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Command {
}
@verbose
static deletePipeline(name: string) {
return `tkn pipeline delete ${name}`;
return `tkn pipeline delete ${name} -f`;
}
@verbose
static listPipelineResources() {
Expand All @@ -126,7 +126,7 @@ export class Command {
}
@verbose
static deletePipelineResource(name: string) {
return `tkn resource delete ${name}`;
return `tkn resource delete ${name} -f`;
}
@verbose
static listPipelines() {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class Command {
}
@verbose
static deletePipelineRun(name: string) {
return `tkn pipelinerun delete ${name}`;
return `tkn pipelinerun delete ${name} -f`;
}
@verbose
static showPipelineRunLogs(name: string) {
Expand All @@ -182,7 +182,7 @@ export class Command {
}
@verbose
static deleteTask(name: string) {
return `tkn task delete ${name}`;
return `tkn task delete ${name} -f`;
}
@verbose
static listClusterTasks(namespace?: string) {
Expand All @@ -193,15 +193,15 @@ export class Command {
}
@verbose
static deleteClusterTask(name: string) {
return `tkn task delete ${name}`;
return `tkn clustertask delete ${name} -f`;
}
@verbose
static showTaskRunLogs(name: string) {
return `tkn taskrun logs ${name}`;
}
@verbose
static deleteTaskRun(name: string) {
return `tkn taskrun delete ${name}`;
return `tkn taskrun delete ${name} -f`;
}
@verbose
static printTknVersion() {
Expand Down
45 changes: 39 additions & 6 deletions test/tekton/clustertask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const expect = chai.expect;
chai.use(sinonChai);

suite('Tekton/Clustertask', () => {
let execStub: sinon.SinonStub;
let sandbox: sinon.SinonSandbox;
let getClusterTaskStub: sinon.SinonStub;
const clustertaskNode = new TestItem(TknImpl.ROOT, 'test-clustertask', ContextType.CLUSTERTASK, null);
Expand All @@ -26,6 +27,7 @@ suite('Tekton/Clustertask', () => {

setup(() => {
sandbox = sinon.createSandbox();
execStub = sandbox.stub(TknImpl.prototype, 'execute').resolves({error: null, stdout: '', stderr: ''});
sandbox.stub(TknImpl.prototype, 'getClusterTasks').resolves([clustertaskItem]);
getClusterTaskStub = sandbox.stub(TektonItem, 'getClusterTaskNames').resolves([clustertaskItem]);
sandbox.stub(vscode.window, 'showInputBox');
Expand Down Expand Up @@ -68,16 +70,47 @@ suite('Tekton/Clustertask', () => {

});

suite('delete', () => {
let termStub: sinon.SinonStub;
suite('delete command', () => {
let warnStub: sinon.SinonStub;

setup(() => {
termStub = sandbox.stub(TknImpl.prototype, 'executeInTerminal').resolves();
setup(() => {
warnStub = sandbox.stub(vscode.window, 'showWarningMessage');
});

test('delete calls the correct tkn command in terminal', async () => {
test('calls the appropriate tkn command if confirmed', async () => {
warnStub.resolves('Yes');

await ClusterTask.delete(clustertaskItem);
expect(termStub).calledOnceWith(Command.deleteTask(clustertaskItem.getName()));

expect(execStub).calledOnceWith(Command.deleteClusterTask(clustertaskItem.getName()));
});

test('returns a confirmation message text when successful', async () => {
warnStub.resolves('Yes');

const result = await ClusterTask.delete(clustertaskItem);

expect(result).equals(`The ClusterTask '${clustertaskItem.getName()}' successfully deleted.`);
});

test('returns null when cancelled', async() => {
warnStub.resolves('Cancel');

const result = await ClusterTask.delete(clustertaskItem);

expect(result).null;
});

test('throws an error message when command failed', async () => {
warnStub.resolves('Yes');
execStub.rejects('ERROR');
let expectedError;
try {
await ClusterTask.delete(clustertaskItem);
} catch (err) {
expectedError = err;
}
expect(expectedError).equals(`Failed to delete the ClusterTask '${clustertaskItem.getName()}': 'ERROR'.`);
});
});

Expand Down
42 changes: 39 additions & 3 deletions test/tekton/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,49 @@ suite('Tekton/Pipeline', () => {
});

});
suite('delete', () => {

test('describe calls the correct tkn command in terminal', async () => {
suite('delete command', () => {
let warnStub: sinon.SinonStub;

setup(() => {
warnStub = sandbox.stub(vscode.window, 'showWarningMessage');
});

test('calls the appropriate tkn command if confirmed', async () => {
warnStub.resolves('Yes');

await Pipeline.delete(pipelineItem);
expect(termStub).calledOnceWith(Command.deletePipeline(pipelineItem.getName()));

expect(execStub).calledOnceWith(Command.deletePipeline(pipelineItem.getName()));
});

test('returns a confirmation message text when successful', async () => {
warnStub.resolves('Yes');

const result = await Pipeline.delete(pipelineItem);

expect(result).equals(`The Pipeline '${pipelineItem.getName()}' successfully deleted.`);
});

test('returns null when cancelled', async() => {
warnStub.resolves('Cancel');

const result = await Pipeline.delete(pipelineItem);

expect(result).null;
});

test('throws an error message when command failed', async () => {
warnStub.resolves('Yes');
execStub.rejects('ERROR');
let expectedError;
try {
await Pipeline.delete(pipelineItem);
} catch (err) {
expectedError = err;
}
expect(expectedError).equals(`Failed to delete the Pipeline '${pipelineItem.getName()}': 'ERROR'.`);
});
});

suite('restart', () => {
Expand Down
Loading