Skip to content

Commit 1bac5ad

Browse files
authored
CodeAction to inline Tekton Task (redhat-developer#520)
* redhat-developer#486 CodeAction to inline Tekton Task Signed-off-by: Yevhen Vydolob <[email protected]> * Change label Signed-off-by: Yevhen Vydolob <[email protected]> * update progress notification Signed-off-by: Yevhen Vydolob <[email protected]> * add telemetry error log Signed-off-by: Yevhen Vydolob <[email protected]> * add check for annotations proprtie Signed-off-by: Yevhen Vydolob <[email protected]>
1 parent e760918 commit 1bac5ad

18 files changed

+542
-178
lines changed

src/model/common.ts

+18-39
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,7 @@
66
import { YamlNode, YamlMap, YamlSequence, isSequence, YamlScalar } from '../yaml-support/yaml-locator';
77
import { tektonYaml, findNodeByKey } from '../yaml-support/tkn-yaml';
88
import * as _ from 'lodash';
9-
10-
export enum TknElementType {
11-
DOCUMENT,
12-
PIPELINE,
13-
METADATA,
14-
PARAM,
15-
PARAMS,
16-
PIPELINE_SPEC,
17-
PIPELINE_RESOURCES,
18-
PIPELINE_RESOURCE,
19-
PIPELINE_TASKS,
20-
PIPELINE_TASK,
21-
PIPELINE_TASK_REF,
22-
VALUE,
23-
PARAM_SPECS,
24-
PARAM_SPEC,
25-
STRING_ARRAY,
26-
PIPELINE_WORKSPACES,
27-
PIPELINE_WORKSPACE,
28-
PIPELINE_TASK_CONDITIONS,
29-
PIPELINE_TASK_CONDITION,
30-
PIPELINE_TASK_INPUT_RESOURCES,
31-
PIPELINE_TASK_INPUT_RESOURCE,
32-
PIPELINE_TASK_INPUT_RESOURCE_FROM,
33-
PIPELINE_TASK_RUN_AFTER,
34-
PIPELINE_TASK_RESOURCES,
35-
PIPELINE_TASK_OUTPUTS_RESOURCE,
36-
PIPELINE_TASK_OUTPUTS_RESOURCES,
37-
PIPELINE_TASK_PARAMS,
38-
PIPELINE_TASK_WORKSPACE,
39-
PIPELINE_TASK_WORKSPACES,
40-
PIPELINE_TASK_WHEN,
41-
PIPELINE_TASK_WHEN_VALUES,
42-
EMBEDDED_TASK,
43-
PIPELINE_TASK_METADATA,
44-
TASK_RESULT,
45-
TASK_RESULTS,
46-
}
9+
import { TknElementType } from './element-type';
4710

4811
export function insideElement(element: TknElement, pos: number): boolean {
4912
return element.startPosition <= pos && element.endPosition > pos;
@@ -84,7 +47,7 @@ export abstract class TknElement {
8447

8548
get startPosition(): number {
8649
if (!this.node) {
87-
console.error(this);
50+
throw new Error('Can\'t find AST node');
8851
}
8952
return this.node.startPosition;
9053
}
@@ -111,6 +74,22 @@ export abstract class LeafTknElement extends TknElement {
11174

11275
}
11376

77+
export class TknKeyElement extends LeafTknElement {
78+
type = TknElementType.KEY;
79+
80+
constructor(parent: TknElement, node: YamlNode) {
81+
super(parent, node);
82+
}
83+
84+
findElement(pos: number): TknElement {
85+
if (insideElement(this, pos)) {
86+
return this;
87+
}
88+
return undefined;
89+
}
90+
91+
}
92+
11493
export abstract class NodeTknElement extends TknElement {
11594
constructor(parent: TknElement, node: YamlNode) {
11695
super(parent, node);

src/model/document.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6-
import { TknElement, TknElementType, TknBaseRootElement, insideElement } from './common';
6+
import { TknElement, TknBaseRootElement, insideElement } from './common';
7+
import { TknElementType } from './element-type';
78
import { YamlDocument } from '../yaml-support/yaml-locator';
89
import { tektonYaml, TektonYamlType } from '../yaml-support/tkn-yaml';
910
import { Pipeline } from './pipeline/pipeline-model';

src/model/element-type.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
/*-----------------------------------------------------------------------------------------------
3+
* Copyright (c) Red Hat, Inc. All rights reserved.
4+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
5+
*-----------------------------------------------------------------------------------------------*/
6+
7+
export enum TknElementType {
8+
DOCUMENT,
9+
PIPELINE,
10+
METADATA,
11+
PARAM,
12+
PARAMS,
13+
PIPELINE_SPEC,
14+
PIPELINE_RESOURCES,
15+
PIPELINE_RESOURCE,
16+
PIPELINE_TASKS,
17+
PIPELINE_TASK,
18+
PIPELINE_TASK_REF,
19+
VALUE,
20+
PARAM_SPECS,
21+
PARAM_SPEC,
22+
STRING_ARRAY,
23+
PIPELINE_WORKSPACES,
24+
PIPELINE_WORKSPACE,
25+
PIPELINE_TASK_CONDITIONS,
26+
PIPELINE_TASK_CONDITION,
27+
PIPELINE_TASK_INPUT_RESOURCES,
28+
PIPELINE_TASK_INPUT_RESOURCE,
29+
PIPELINE_TASK_INPUT_RESOURCE_FROM,
30+
PIPELINE_TASK_RUN_AFTER,
31+
PIPELINE_TASK_RESOURCES,
32+
PIPELINE_TASK_OUTPUTS_RESOURCE,
33+
PIPELINE_TASK_OUTPUTS_RESOURCES,
34+
PIPELINE_TASK_PARAMS,
35+
PIPELINE_TASK_WORKSPACE,
36+
PIPELINE_TASK_WORKSPACES,
37+
PIPELINE_TASK_WHEN,
38+
PIPELINE_TASK_WHEN_VALUES,
39+
EMBEDDED_TASK,
40+
PIPELINE_TASK_METADATA,
41+
TASK_RESULT,
42+
TASK_RESULTS,
43+
KEY
44+
}

src/model/pipeline/pipeline-model.ts

+14-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6-
import { TknElement, TknElementType, TknArray, TknBaseRootElement, NodeTknElement, TknStringElement, TknValueElement, TknParam } from '../common';
6+
import { TknElement, TknArray, TknBaseRootElement, NodeTknElement, TknStringElement, TknValueElement, TknParam, TknKeyElement } from '../common';
7+
import { TknElementType } from '../element-type';
78
import { YamlMap, YamlSequence, YamlNode, isSequence } from '../../yaml-support/yaml-locator';
89
import { pipelineYaml, getYamlMappingValue, findNodeByKey } from '../../yaml-support/tkn-yaml';
910
import { EmbeddedTask } from './task-model';
@@ -220,7 +221,7 @@ export class PipelineTask extends NodeTknElement {
220221

221222
const taskRefNode = pipelineYaml.getTaskRef(this.node as YamlMap);
222223
if (taskRefNode) {
223-
this._taskRef = new PipelineTaskRef(this, taskRefNode);
224+
this._taskRef = new PipelineTaskRef(this, taskRefNode[0], taskRefNode[1]);
224225
}
225226
}
226227
return this._taskRef;
@@ -340,14 +341,19 @@ export class PipelineTaskRef extends NodeTknElement {
340341

341342
private _name: TknStringElement;
342343
private _kind: TknValueElement<PipelineTaskKind>;
344+
keyNode: TknElement
343345

344-
constructor(parent: PipelineTask, node: YamlMap) {
346+
constructor(parent: PipelineTask, keyNode: YamlNode, node: YamlMap) {
345347
super(parent, node);
348+
this.keyNode = new TknKeyElement(parent, keyNode);
346349
}
347350

348351
get name(): TknStringElement {
349352
if (!this._name) {
350-
this._name = new TknStringElement(this, findNodeByKey('name', this.node as YamlMap))
353+
const nameVal: YamlNode = findNodeByKey('name', this.node as YamlMap);
354+
if (nameVal){
355+
this._name = new TknStringElement(this, nameVal)
356+
}
351357
}
352358
return this._name;
353359
}
@@ -441,7 +447,10 @@ export class WorkspacePipelineDeclaration extends NodeTknElement {
441447

442448
get name(): TknStringElement {
443449
if (!this._name) {
444-
this._name = new TknStringElement(this, findNodeByKey('name', this.node as YamlMap))
450+
const nameNode = findNodeByKey<YamlNode>('name', this.node as YamlMap);
451+
if (nameNode){
452+
this._name = new TknStringElement(this, nameNode);
453+
}
445454
}
446455
return this._name;
447456
}

src/model/pipeline/task-model.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
import { findNodeByKey } from '../../yaml-support/tkn-yaml';
77
import { YamlMap, YamlSequence } from '../../yaml-support/yaml-locator';
8-
import { NodeTknElement, TknArray, TknElement, TknElementType, TknStringElement } from '../common';
8+
import { NodeTknElement, TknArray, TknElement, TknStringElement } from '../common';
9+
import { TknElementType } from '../element-type';
910

1011
export class EmbeddedTask extends NodeTknElement {
1112

src/tekton.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ export interface TknMetadata {
5050
uid?: string;
5151
resourceVersion?: string;
5252
labels?: {[key: string]: string};
53+
managedFields?: unknown[];
54+
selfLink?: string;
55+
creationTimestamp?: string;
56+
ownerReferences?: unknown[];
57+
annotations?: {[key: string]: string};
5358
}
5459

5560
export interface TknParams {

src/util/filename.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
* Licensed under the MIT License. See LICENSE file in the project root for license information.
44
*-----------------------------------------------------------------------------------------------*/
55

6+
const id = new RegExp('^[A-Za-z0-9]+');
7+
const uidRegex = new RegExp('@[A-Za-z0-9]+$');
8+
69
export function newFileName(name: string, uid: string): string {
7-
const id = new RegExp('^[A-Za-z0-9]+');
8-
return `${name}-${uid.match(id)[0]}`;
10+
return `${name}@${uid.match(id)[0]}`;
11+
}
12+
13+
export function originalFileName(name: string): string {
14+
return name.replace(uidRegex, '');
915
}

src/util/tekton-vfs.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import * as os from 'os'
1010
import * as fsx from 'fs-extra';
1111
import { VirtualDocument } from '../yaml-support/yaml-locator';
1212
import { TektonItem } from '../tekton/tektonitem';
13-
import { newFileName } from './filename';
13+
import { originalFileName, newFileName } from './filename';
1414
import { getStderrString } from './stderrstring';
1515

1616
export const TKN_RESOURCE_SCHEME = 'tekton';
1717
export const TKN_RESOURCE_SCHEME_READONLY = 'tekton-ro';
1818
const readonlyRegex = /(taskrun|pipelinerun|pipelineRunFinished|tr)/ as RegExp;
1919

20+
const pipelineRunFinishedRegex = RegExp(`^${ContextType.PIPELINERUNCHILDNODE}/`);
21+
const taskRegex = RegExp(`^${ContextType.TASK}/`);
22+
2023
/**
2124
* Create Uri for Tekton VFS
2225
* @param type tekton resource type
@@ -93,10 +96,7 @@ export class TektonVFSProvider implements FileSystemProvider {
9396
}
9497

9598
private loadK8sResource(resource: string, outputFormat: string, uid = true): Promise<CliExitData> {
96-
const id = new RegExp('-[A-Za-z0-9]+$');
97-
let newResourceName = (uid) ? resource.replace(id, '') : resource;
98-
const pipelineRunFinishedRegex = RegExp(`^${ContextType.PIPELINERUNCHILDNODE}/`);
99-
const taskRegex = RegExp(`^${ContextType.TASK}/`);
99+
let newResourceName = (uid) ? originalFileName(resource) : resource;
100100
if (pipelineRunFinishedRegex.test(newResourceName)) {
101101
newResourceName = newResourceName.replace(`${ContextType.PIPELINERUNCHILDNODE}/`, `${ContextType.PIPELINERUN}/`);
102102
}

0 commit comments

Comments
 (0)