Skip to content

Commit 416cd9e

Browse files
authored
Add completion for 'conditionRef' field (#337)
* #244 add completion for 'conditionRef' field Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> * add tests Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
1 parent 92d379a commit 416cd9e

File tree

5 files changed

+91
-10
lines changed

5 files changed

+91
-10
lines changed

src/yaml-support/snippet.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
6+
export interface Snippet<T> {
7+
label: string;
8+
description?: string;
9+
body: T;
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
6+
import { tkn, Command } from '../tkn';
7+
8+
9+
export async function getTknConditionsSnippets(): Promise<string[]> {
10+
const result = await tkn.execute(Command.listConditions());
11+
let data = [];
12+
if (result.error) {
13+
return [];
14+
}
15+
try {
16+
data = JSON.parse(result.stdout).items;
17+
} catch (ignore) {
18+
//show no pipelines if output is not correct json
19+
}
20+
21+
let condition: string[] = data.map((value) => value.metadata.name);
22+
condition = [...new Set(condition)];
23+
return condition;
24+
}

src/yaml-support/tkn-tasks-provider.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
*-----------------------------------------------------------------------------------------------*/
55
import { tkn } from '../tkn';
66
import { TknTask } from '../tekton';
7+
import { Snippet } from './snippet';
78

8-
export interface Snippet {
9-
label: string;
10-
description?: string;
11-
body: TaskSnippet;
12-
}
139

1410
interface TaskSnippet {
1511
name: string;
@@ -56,7 +52,7 @@ interface Param {
5652
value: string | string[];
5753
}
5854

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

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

68-
export function convertTasksToSnippet(rawTasks: TknTask[]): Snippet[] {
69-
const result: Snippet[] = [];
64+
export function convertTasksToSnippet(rawTasks: TknTask[]): Snippet<TaskSnippet>[] {
65+
const result: Snippet<TaskSnippet>[] = [];
7066

7167
for (const task of rawTasks) {
7268
result.push({

src/yaml-support/tkn-yaml-scheme-generator.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import * as vscode from 'vscode';
66
import * as path from 'path';
77
import { readFile } from 'fs-extra'
8-
import { Snippet, getTknTasksSnippets } from './tkn-tasks-provider';
8+
import { getTknTasksSnippets } from './tkn-tasks-provider';
99
import { schemeStorage } from './tkn-scheme-storage'
1010
import { pipelineYaml } from './tkn-yaml';
11+
import { Snippet } from './snippet';
12+
import { getTknConditionsSnippets } from './tkn-conditions-provider';
1113

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

1921

2022
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21-
function injectTaskSnippets(templateObj: any, snippets: Snippet[]): {} {
23+
function injectTaskSnippets(templateObj: any, snippets: Snippet<{}>[]): {} {
2224
templateObj.definitions.PipelineSpec.properties.tasks.defaultSnippets = snippets;
2325
return templateObj;
2426
}
@@ -45,9 +47,17 @@ function injectResourceName(templateObj: any, resNames: string[]): {} {
4547
return templateObj;
4648
}
4749

50+
function injectConditionRefs(templateObj: any, conditions: string[]): {} {
51+
if (conditions && conditions.length > 0) {
52+
templateObj.definitions.PipelineTaskCondition.properties.conditionRef.enum = conditions;
53+
}
54+
return templateObj;
55+
}
56+
4857
async function generate(doc: vscode.TextDocument): Promise<string> {
4958
const template = await readFile(path.join(context.extensionPath, 'scheme', 'pipeline.json'), 'UTF8');
5059
const snippets = await getTknTasksSnippets();
60+
const conditions = await getTknConditionsSnippets();
5161
const definedTasks = pipelineYaml.getPipelineTasksName(doc);
5262
const declaredResources = pipelineYaml.getDeclaredResources(doc);
5363

@@ -57,5 +67,6 @@ async function generate(doc: vscode.TextDocument): Promise<string> {
5767
const tasksRef = snippets.map(value => value.body.taskRef.name);
5868
templateWithSnippets = injectTasksName(templateWithSnippets, definedTasks, tasksRef);
5969
templateWithSnippets = injectResourceName(templateWithSnippets, resNames);
70+
templateWithSnippets = injectConditionRefs(templateWithSnippets, conditions);
6071
return JSON.stringify(templateWithSnippets);
6172
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
import * as vscode from 'vscode';
6+
import * as chai from 'chai';
7+
import * as sinonChai from 'sinon-chai';
8+
import * as sinon from 'sinon';
9+
import { tkn } from '../../src/tkn';
10+
import { getTknConditionsSnippets } from '../../src/yaml-support/tkn-conditions-provider';
11+
12+
const expect = chai.expect;
13+
chai.use(sinonChai);
14+
15+
suite('ConditionRef provider', () => {
16+
17+
const sandbox = sinon.createSandbox();
18+
let tknExecute: sinon.SinonStub;
19+
20+
setup(() => {
21+
tknExecute = sandbox.stub(tkn, 'execute');
22+
});
23+
24+
teardown(() => {
25+
sandbox.restore();
26+
});
27+
28+
test('should return empty array if any error on getting condition list', async () => {
29+
tknExecute.resolves({ error: 'Some error' });
30+
const result = await getTknConditionsSnippets();
31+
expect(result).eql([]);
32+
});
33+
34+
test('should array with condition names', async () => {
35+
tknExecute.resolves({ stdout: '{"items": [{"metadata": {"name": "foo"}}, {"metadata": {"name": "bar"}}]}' });
36+
const result = await getTknConditionsSnippets();
37+
expect(result).eql(['foo', 'bar']);
38+
});
39+
40+
});

0 commit comments

Comments
 (0)