Skip to content

Commit b2e159b

Browse files
authored
Fix support for custom tasks (redhat-developer#517)
* redhat-developer#509 Fix support for custom tasks Signed-off-by: Yevhen Vydolob <[email protected]> * Remove unused import * Fix test Signed-off-by: Yevhen Vydolob <[email protected]> * Fix tests Signed-off-by: Yevhen Vydolob <[email protected]>
1 parent 81374ee commit b2e159b

File tree

6 files changed

+70
-8
lines changed

6 files changed

+70
-8
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@
955955
"@types/string-format": "^2.0.0",
956956
"@types/tmp": "0.1.0",
957957
"@types/validator": "^10.11.2",
958-
"@types/vscode": "^1.50.0",
958+
"@types/vscode": "^1.54.0",
959959
"@typescript-eslint/eslint-plugin": "^2.12.0",
960960
"@typescript-eslint/parser": "^2.12.0",
961961
"base64-inline-loader": "^1.1.1",

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ async function generate(doc: vscode.TextDocument, schemaPath: string): Promise<s
185185
const templateObj = JSON.parse(template);
186186
let templateWithSnippets = injectTaskSnippets(templateObj, snippets);
187187
const tasksRef = snippets.map(value => value.body.taskRef.name);
188+
const customTasks = pipelineYaml.getCustomTasks(doc);
189+
if (customTasks.length > 0){
190+
tasksRef.push(...customTasks);
191+
}
188192
templateWithSnippets = injectTasksName(templateWithSnippets, definedTasks, tasksRef);
189193
templateWithSnippets = injectResourceName(templateWithSnippets, resNames);
190194
templateWithSnippets = injectConditionRefs(templateWithSnippets, conditions);

src/yaml-support/tkn-yaml.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,38 @@ export class PipelineYaml {
183183
return getTasksSeq(specMap);
184184
}
185185

186+
getCustomTasks(vsDocument: vscode.TextDocument): string[]{
187+
const result: string[] = [];
188+
if (tektonYaml.isTektonYaml(vsDocument) === TektonYamlType.Pipeline) {
189+
const yamlDocuments = yamlLocator.getYamlDocuments(vsDocument);
190+
for (const doc of yamlDocuments) {
191+
const rootMap = tektonYaml.getRootMap(doc);
192+
if (rootMap) {
193+
const specMap = getSpecMap(rootMap);
194+
if (specMap) {
195+
const tasksSeq = getTasksSeq(specMap);
196+
if (tasksSeq) {
197+
for (const taskNode of tasksSeq.items) {
198+
if (taskNode.kind === 'MAPPING') {
199+
const taskRef = findNodeByKey<YamlMap>('taskRef', taskNode as YamlMap);
200+
if (taskRef){
201+
const apiVersion = findNodeByKey<YamlNode>('apiVersion', taskRef);
202+
const kind = findNodeByKey<YamlNode>('kind', taskRef);
203+
const nameValue = findNodeByKey<YamlNode>('name', taskRef);
204+
if (nameValue && apiVersion && kind) {
205+
result.push(nameValue.raw.trim());
206+
}
207+
}
208+
}
209+
}
210+
}
211+
}
212+
}
213+
}
214+
}
215+
return result;
216+
}
217+
186218
getTaskRef(task: YamlMap): YamlMap {
187219
return findNodeByKey<YamlMap>('taskRef', task);
188220
}

test/tekton/triggertemplate.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,11 @@ suite('Tekton/Pipeline', () => {
105105
error: '',
106106
stdout: JSON.stringify(route)
107107
});
108-
const writeTextStub = sandbox.stub(vscode.env.clipboard, 'writeText').resolves('http://test.openshift.com');
109108
const infoMsg = sandbox.stub(vscode.window, 'showInformationMessage').resolves('Expose URl successfully copied');
110109
await TriggerTemplate.copyExposeUrl(triggerTemplateItem);
111110
expect(exeStub).called;
112111
expect(infoMsg).is.calledOnce;
113-
expect(writeTextStub).called;
112+
expect(await vscode.env.clipboard.readText()).equal('http://test.openshift.com');
114113
});
115114

116115
test('copy expose URL for triggerRef', async () => {
@@ -126,12 +125,12 @@ suite('Tekton/Pipeline', () => {
126125
error: '',
127126
stdout: JSON.stringify(route)
128127
});
129-
const writeTextStub = sandbox.stub(vscode.env.clipboard, 'writeText').resolves('http://test.openshift.com');
128+
130129
const infoMsg = sandbox.stub(vscode.window, 'showInformationMessage').resolves('Expose URl successfully copied');
131130
await TriggerTemplate.copyExposeUrl(triggerTemplateItem);
132131
expect(exeStub).called;
133132
expect(infoMsg).is.calledOnce;
134-
expect(writeTextStub).called;
133+
expect(await vscode.env.clipboard.readText()).equal('http://test.openshift.com');
135134
});
136135

137136
test('return null if no EventListener found', async () => {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,31 @@ suite('Pipeline scheme generator', () => {
9090
description: 'Foo Result Description'
9191
});
9292
});
93+
94+
test('Generator should add custom task name to tasks ref enum', async () => {
95+
const yaml = `
96+
apiVersion: tekton.dev/v1beta1
97+
kind: Pipeline
98+
metadata:
99+
name: example
100+
spec:
101+
params:
102+
- name: pipeline-param
103+
default: hello
104+
tasks:
105+
- name: example-task
106+
taskRef:
107+
apiVersion: example.dev/v0
108+
kind: Example
109+
name: my-example
110+
params:
111+
- name: task-param
112+
value: "$(params.pipeline-param)"
113+
`
114+
const doc = new TestTextDocument(vscode.Uri.file(path.join('foo', 'PipelineSchemeGenerator3.yaml')), yaml);
115+
const result = await generateScheme(doc, path.resolve(__dirname, '..', '..', '..', 'scheme', 'tekton.dev', 'v1beta1_Pipeline.json'));
116+
const scheme = JSON.parse(result);
117+
expect(scheme.definitions.TaskRef.properties.name.enum).is.contains('my-example');
118+
119+
});
93120
});

0 commit comments

Comments
 (0)