Skip to content

Commit

Permalink
Add completion for 'conditionRef' field (#337)
Browse files Browse the repository at this point in the history
* #244 add completion for 'conditionRef' field

Signed-off-by: Yevhen Vydolob <[email protected]>

* add tests

Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob authored Jul 9, 2020
1 parent 92d379a commit 416cd9e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 10 deletions.
10 changes: 10 additions & 0 deletions src/yaml-support/snippet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

export interface Snippet<T> {
label: string;
description?: string;
body: T;
}
24 changes: 24 additions & 0 deletions src/yaml-support/tkn-conditions-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { tkn, Command } from '../tkn';


export async function getTknConditionsSnippets(): Promise<string[]> {
const result = await tkn.execute(Command.listConditions());
let data = [];
if (result.error) {
return [];
}
try {
data = JSON.parse(result.stdout).items;
} catch (ignore) {
//show no pipelines if output is not correct json
}

let condition: string[] = data.map((value) => value.metadata.name);
condition = [...new Set(condition)];
return condition;
}
12 changes: 4 additions & 8 deletions src/yaml-support/tkn-tasks-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
*-----------------------------------------------------------------------------------------------*/
import { tkn } from '../tkn';
import { TknTask } from '../tekton';
import { Snippet } from './snippet';

export interface Snippet {
label: string;
description?: string;
body: TaskSnippet;
}

interface TaskSnippet {
name: string;
Expand Down Expand Up @@ -56,7 +52,7 @@ interface Param {
value: string | string[];
}

export async function getTknTasksSnippets(): Promise<Snippet[]> {
export async function getTknTasksSnippets(): Promise<Snippet<TaskSnippet>[]> {
const [rawClusterTasks, rawTasks] = await Promise.all([tkn.getRawClusterTasks(), tkn.getRawTasks()]);

const allRawTasks = rawClusterTasks.concat(rawTasks);
Expand All @@ -65,8 +61,8 @@ export async function getTknTasksSnippets(): Promise<Snippet[]> {
return snippets;
}

export function convertTasksToSnippet(rawTasks: TknTask[]): Snippet[] {
const result: Snippet[] = [];
export function convertTasksToSnippet(rawTasks: TknTask[]): Snippet<TaskSnippet>[] {
const result: Snippet<TaskSnippet>[] = [];

for (const task of rawTasks) {
result.push({
Expand Down
15 changes: 13 additions & 2 deletions src/yaml-support/tkn-yaml-scheme-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { readFile } from 'fs-extra'
import { Snippet, getTknTasksSnippets } from './tkn-tasks-provider';
import { getTknTasksSnippets } from './tkn-tasks-provider';
import { schemeStorage } from './tkn-scheme-storage'
import { pipelineYaml } from './tkn-yaml';
import { Snippet } from './snippet';
import { getTknConditionsSnippets } from './tkn-conditions-provider';

let context: vscode.ExtensionContext;
export function generateScheme(extContext: vscode.ExtensionContext, vsDocument: vscode.TextDocument): Promise<string> {
Expand All @@ -18,7 +20,7 @@ export function generateScheme(extContext: vscode.ExtensionContext, vsDocument:


// eslint-disable-next-line @typescript-eslint/no-explicit-any
function injectTaskSnippets(templateObj: any, snippets: Snippet[]): {} {
function injectTaskSnippets(templateObj: any, snippets: Snippet<{}>[]): {} {
templateObj.definitions.PipelineSpec.properties.tasks.defaultSnippets = snippets;
return templateObj;
}
Expand All @@ -45,9 +47,17 @@ function injectResourceName(templateObj: any, resNames: string[]): {} {
return templateObj;
}

function injectConditionRefs(templateObj: any, conditions: string[]): {} {
if (conditions && conditions.length > 0) {
templateObj.definitions.PipelineTaskCondition.properties.conditionRef.enum = conditions;
}
return templateObj;
}

async function generate(doc: vscode.TextDocument): Promise<string> {
const template = await readFile(path.join(context.extensionPath, 'scheme', 'pipeline.json'), 'UTF8');
const snippets = await getTknTasksSnippets();
const conditions = await getTknConditionsSnippets();
const definedTasks = pipelineYaml.getPipelineTasksName(doc);
const declaredResources = pipelineYaml.getDeclaredResources(doc);

Expand All @@ -57,5 +67,6 @@ async function generate(doc: vscode.TextDocument): Promise<string> {
const tasksRef = snippets.map(value => value.body.taskRef.name);
templateWithSnippets = injectTasksName(templateWithSnippets, definedTasks, tasksRef);
templateWithSnippets = injectResourceName(templateWithSnippets, resNames);
templateWithSnippets = injectConditionRefs(templateWithSnippets, conditions);
return JSON.stringify(templateWithSnippets);
}
40 changes: 40 additions & 0 deletions test/yaml-support/tkn-condition-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import * as sinon from 'sinon';
import { tkn } from '../../src/tkn';
import { getTknConditionsSnippets } from '../../src/yaml-support/tkn-conditions-provider';

const expect = chai.expect;
chai.use(sinonChai);

suite('ConditionRef provider', () => {

const sandbox = sinon.createSandbox();
let tknExecute: sinon.SinonStub;

setup(() => {
tknExecute = sandbox.stub(tkn, 'execute');
});

teardown(() => {
sandbox.restore();
});

test('should return empty array if any error on getting condition list', async () => {
tknExecute.resolves({ error: 'Some error' });
const result = await getTknConditionsSnippets();
expect(result).eql([]);
});

test('should array with condition names', async () => {
tknExecute.resolves({ stdout: '{"items": [{"metadata": {"name": "foo"}}, {"metadata": {"name": "bar"}}]}' });
const result = await getTknConditionsSnippets();
expect(result).eql(['foo', 'bar']);
});

});

0 comments on commit 416cd9e

Please sign in to comment.