Skip to content

Commit

Permalink
Add command to open task definition for taskrun item (#302)
Browse files Browse the repository at this point in the history
* #252 add command to open task definition for taskrun item

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* add support for ClusterTask taskrun

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>

* check if taskRun is not undefined

Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
  • Loading branch information
evidolob committed May 27, 2020
1 parent 5fe2fd6 commit 4313cdb
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off",
"debug.node.autoAttach": "off",
"svg.preview.background": "transparent"
"svg.preview.background": "custom"
}
4 changes: 4 additions & 0 deletions images/dark/go-to-task.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions images/light/go-to-task.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@
"dark": "images/dark/go-to-file.svg"
}
},
{
"command": "tekton.open.task",
"title": "Open Task Definition",
"icon": {
"light": "images/light/go-to-task.svg",
"dark": "images/dark/go-to-task.svg"
}
},
{
"command": "tekton.pipeline.preview",
"title": "Open Pipeline preview to the Side",
Expand Down Expand Up @@ -728,10 +736,20 @@
"when": "view =~ /^tekton(CustomTree|PipelineExplorer)View/ && viewItem == taskrun",
"group": "c2"
},
{
"command": "tekton.open.task",
"when": "view =~ /^tekton(CustomTree|PipelineExplorer)View/ && viewItem == taskrun",
"group": "inline@1"
},
{
"command": "tekton.edit",
"when": "view =~ /^tekton(CustomTree|PipelineExplorer)View/ && viewItem =~ /^(pipeline|pipelinerun|clustertask|pipelineresource|taskrun|triggertemplates|triggerbinding|eventlistener|conditions|task|clustertriggerbinding)$/",
"group": "inline"
"group": "inline@2"
},
{
"command": "tekton.open.task",
"when": "view =~ /^tekton(CustomTree|PipelineExplorer)View/ && viewItem == taskrun",
"group": "c0@2"
},
{
"command": "tekton.openInEditor",
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
vscode.commands.registerCommand('tekton.custom.explorer.removeItem', removeItemFromCustomTree),
vscode.commands.registerCommand('k8s.tekton.run.logs', k8sCommands.showLogs),
vscode.commands.registerCommand('k8s.tekton.run.followLogs', k8sCommands.followLogs),
vscode.commands.registerCommand('tekton.open.task', (context)=> execute(TaskRun.openDefinition, context)),
pipelineExplorer,
// Temporarily loaded resource providers
vscode.workspace.registerFileSystemProvider(TKN_RESOURCE_SCHEME, tektonVfsProvider, { isCaseSensitive: true, }),
Expand Down
37 changes: 34 additions & 3 deletions src/tekton/taskrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
*-----------------------------------------------------------------------------------------------*/

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

export class TaskRun extends TektonItem {

static async listFromPipelineRun(pipelineRun: TektonNode): Promise<void> {
if (pipelineRun) {
TaskRun.tkn.executeInTerminal(Command.listTaskRunsForPipelineRunInTerminal(pipelineRun.getName()));
if (pipelineRun) {
TaskRun.tkn.executeInTerminal(Command.listTaskRunsForPipelineRunInTerminal(pipelineRun.getName()));
}
}

Expand Down Expand Up @@ -40,4 +40,35 @@ export class TaskRun extends TektonItem {
}
return null;
}

static async openDefinition(taskRun: TektonNode): Promise<void> {
if (!taskRun) {
return;
}
const taskName = await TaskRun.getTaskNameByTaskRun(taskRun.getName());
if (taskName) {
TektonItem.loadTektonResource(taskName[0], taskName[1]);
}
}

private static async getTaskNameByTaskRun(taskRunName: string): Promise<[string, string] | undefined> {
const result = await TaskRun.tkn.execute(Command.getTaskRun(taskRunName), undefined, false);
if (result.error) {
window.showErrorMessage(`TaskRun may not have started yet, try again when it starts running. "${result.error}"`)
return;
}
let data: PipelineTaskRunData;
try {
data = JSON.parse(result.stdout);
// eslint-disable-next-line no-empty
} catch (ignore) {
}

if (data.metadata.labels['tekton.dev/conditionCheck']) {
window.showErrorMessage(`Cannot find Condition definition for: ${taskRunName}. Please look in Pipeline definition`);
return;
}

return [data.spec.taskRef?.kind ?? 'task', data.metadata.labels['tekton.dev/task']];
}
}
11 changes: 9 additions & 2 deletions src/tkn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export class Command {
return newK8sCommand('get', 'taskrun', '-l', `tekton.dev/task=${task}`, '-o', 'json');
}

static getTaskRun(taskRunName: string): CliCommand {
return newK8sCommand('get', 'taskrun', taskRunName, '-o', 'json');
}

@verbose
static listTaskRunsForTasksInTerminal(task: string): CliCommand {
return newTknCommand('taskrun', 'list', task);
Expand Down Expand Up @@ -364,7 +368,7 @@ export class Command {
return newK8sCommand('create', '--save-config', '-f', file);
}
static apply(file: string): CliCommand {
return newK8sCommand('apply','-f', file);
return newK8sCommand('apply', '-f', file);
}
}

Expand Down Expand Up @@ -553,13 +557,15 @@ export class TektonNodeImpl implements TektonNode {

}

type PipelineTaskRunData = {
export type PipelineTaskRunData = {
metadata?: {
creationTimestamp: string;
name: string;
labels: {
'tekton.dev/pipelineTask': string;
'tekton.dev/pipelineRun': string;
'tekton.dev/task': string;
'tekton.dev/conditionCheck'?: string;
};
};
status?: {
Expand All @@ -571,6 +577,7 @@ type PipelineTaskRunData = {
spec: {
taskRef: {
name: string;
kind: string;
};
};
};
Expand Down
43 changes: 43 additions & 0 deletions test/tekton/taskrun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TaskRun } from '../../src/tekton/taskrun';
import { TknImpl, Command, ContextType } from '../../src/tkn';
import { TektonItem } from '../../src/tekton/tektonitem';
import { pipelineExplorer } from '../../src/pipeline/pipelineExplorer';
import { CliExitData } from '../../src/cli';

const expect = chai.expect;
chai.use(sinonChai);
Expand Down Expand Up @@ -179,4 +180,46 @@ suite('Tekton/TaskRun', () => {
});
});
});

suite('Open Task Definition', () => {

let loadTektonResourceStub: sinon.SinonStub;
let showErrorStub: sinon.SinonStub;
setup(() => {
loadTektonResourceStub = sandbox.stub(TektonItem, 'loadTektonResource');
showErrorStub = sandbox.stub(vscode.window, 'showErrorMessage');
});

test('openDefinition should ask taskrun definition', async () => {
execStub.resolves({ stdout: '{"metadata":{"labels": {"tekton.dev/task": "FooTaskName"}}, "spec":{"taskRef":{"kind": "FooKind"}}}' } as CliExitData);
loadTektonResourceStub.returns(undefined);

await TaskRun.openDefinition(taskrunItem);

expect(execStub).calledOnceWith(Command.getTaskRun(taskrunItem.getName()));
expect(loadTektonResourceStub).calledOnceWith('FooKind', 'FooTaskName');
});

test('openDefinition should check errors on fetching taskrun definition', async () => {
execStub.resolves({ error: 'Foo error' } as CliExitData);
loadTektonResourceStub.returns(undefined);

await TaskRun.openDefinition(taskrunItem);

expect(execStub).calledOnceWith(Command.getTaskRun(taskrunItem.getName()));
expect(loadTektonResourceStub).not.called;
expect(showErrorStub).calledOnceWith('TaskRun may not have started yet, try again when it starts running. "Foo error"');
});

test('openDefinition should show error if trying to open condition taskrun', async () => {
execStub.resolves({ stdout: '{"metadata":{"labels": {"tekton.dev/task": "FooTaskName","tekton.dev/conditionCheck": "bar-condition-run"}}, "spec":{"taskRef":{"kind": "FooKind"}}}' } as CliExitData);
loadTektonResourceStub.returns(undefined);

await TaskRun.openDefinition(taskrunItem);

expect(execStub).calledOnceWith(Command.getTaskRun(taskrunItem.getName()));
expect(loadTektonResourceStub).not.called;
expect(showErrorStub).calledOnceWith('Cannot find Condition definition for: taskrun. Please look in Pipeline definition');
});
});
});

0 comments on commit 4313cdb

Please sign in to comment.