Skip to content

Commit

Permalink
CodeAction to inline Tekton Task (#520)
Browse files Browse the repository at this point in the history
* #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]>
  • Loading branch information
evidolob authored Mar 12, 2021
1 parent e760918 commit 1bac5ad
Show file tree
Hide file tree
Showing 18 changed files with 542 additions and 178 deletions.
57 changes: 18 additions & 39 deletions src/model/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,7 @@
import { YamlNode, YamlMap, YamlSequence, isSequence, YamlScalar } from '../yaml-support/yaml-locator';
import { tektonYaml, findNodeByKey } from '../yaml-support/tkn-yaml';
import * as _ from 'lodash';

export enum TknElementType {
DOCUMENT,
PIPELINE,
METADATA,
PARAM,
PARAMS,
PIPELINE_SPEC,
PIPELINE_RESOURCES,
PIPELINE_RESOURCE,
PIPELINE_TASKS,
PIPELINE_TASK,
PIPELINE_TASK_REF,
VALUE,
PARAM_SPECS,
PARAM_SPEC,
STRING_ARRAY,
PIPELINE_WORKSPACES,
PIPELINE_WORKSPACE,
PIPELINE_TASK_CONDITIONS,
PIPELINE_TASK_CONDITION,
PIPELINE_TASK_INPUT_RESOURCES,
PIPELINE_TASK_INPUT_RESOURCE,
PIPELINE_TASK_INPUT_RESOURCE_FROM,
PIPELINE_TASK_RUN_AFTER,
PIPELINE_TASK_RESOURCES,
PIPELINE_TASK_OUTPUTS_RESOURCE,
PIPELINE_TASK_OUTPUTS_RESOURCES,
PIPELINE_TASK_PARAMS,
PIPELINE_TASK_WORKSPACE,
PIPELINE_TASK_WORKSPACES,
PIPELINE_TASK_WHEN,
PIPELINE_TASK_WHEN_VALUES,
EMBEDDED_TASK,
PIPELINE_TASK_METADATA,
TASK_RESULT,
TASK_RESULTS,
}
import { TknElementType } from './element-type';

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

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

}

export class TknKeyElement extends LeafTknElement {
type = TknElementType.KEY;

constructor(parent: TknElement, node: YamlNode) {
super(parent, node);
}

findElement(pos: number): TknElement {
if (insideElement(this, pos)) {
return this;
}
return undefined;
}

}

export abstract class NodeTknElement extends TknElement {
constructor(parent: TknElement, node: YamlNode) {
super(parent, node);
Expand Down
3 changes: 2 additions & 1 deletion src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { TknElement, TknElementType, TknBaseRootElement, insideElement } from './common';
import { TknElement, TknBaseRootElement, insideElement } from './common';
import { TknElementType } from './element-type';
import { YamlDocument } from '../yaml-support/yaml-locator';
import { tektonYaml, TektonYamlType } from '../yaml-support/tkn-yaml';
import { Pipeline } from './pipeline/pipeline-model';
Expand Down
44 changes: 44 additions & 0 deletions src/model/element-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

export enum TknElementType {
DOCUMENT,
PIPELINE,
METADATA,
PARAM,
PARAMS,
PIPELINE_SPEC,
PIPELINE_RESOURCES,
PIPELINE_RESOURCE,
PIPELINE_TASKS,
PIPELINE_TASK,
PIPELINE_TASK_REF,
VALUE,
PARAM_SPECS,
PARAM_SPEC,
STRING_ARRAY,
PIPELINE_WORKSPACES,
PIPELINE_WORKSPACE,
PIPELINE_TASK_CONDITIONS,
PIPELINE_TASK_CONDITION,
PIPELINE_TASK_INPUT_RESOURCES,
PIPELINE_TASK_INPUT_RESOURCE,
PIPELINE_TASK_INPUT_RESOURCE_FROM,
PIPELINE_TASK_RUN_AFTER,
PIPELINE_TASK_RESOURCES,
PIPELINE_TASK_OUTPUTS_RESOURCE,
PIPELINE_TASK_OUTPUTS_RESOURCES,
PIPELINE_TASK_PARAMS,
PIPELINE_TASK_WORKSPACE,
PIPELINE_TASK_WORKSPACES,
PIPELINE_TASK_WHEN,
PIPELINE_TASK_WHEN_VALUES,
EMBEDDED_TASK,
PIPELINE_TASK_METADATA,
TASK_RESULT,
TASK_RESULTS,
KEY
}
19 changes: 14 additions & 5 deletions src/model/pipeline/pipeline-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

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

const taskRefNode = pipelineYaml.getTaskRef(this.node as YamlMap);
if (taskRefNode) {
this._taskRef = new PipelineTaskRef(this, taskRefNode);
this._taskRef = new PipelineTaskRef(this, taskRefNode[0], taskRefNode[1]);
}
}
return this._taskRef;
Expand Down Expand Up @@ -340,14 +341,19 @@ export class PipelineTaskRef extends NodeTknElement {

private _name: TknStringElement;
private _kind: TknValueElement<PipelineTaskKind>;
keyNode: TknElement

constructor(parent: PipelineTask, node: YamlMap) {
constructor(parent: PipelineTask, keyNode: YamlNode, node: YamlMap) {
super(parent, node);
this.keyNode = new TknKeyElement(parent, keyNode);
}

get name(): TknStringElement {
if (!this._name) {
this._name = new TknStringElement(this, findNodeByKey('name', this.node as YamlMap))
const nameVal: YamlNode = findNodeByKey('name', this.node as YamlMap);
if (nameVal){
this._name = new TknStringElement(this, nameVal)
}
}
return this._name;
}
Expand Down Expand Up @@ -441,7 +447,10 @@ export class WorkspacePipelineDeclaration extends NodeTknElement {

get name(): TknStringElement {
if (!this._name) {
this._name = new TknStringElement(this, findNodeByKey('name', this.node as YamlMap))
const nameNode = findNodeByKey<YamlNode>('name', this.node as YamlMap);
if (nameNode){
this._name = new TknStringElement(this, nameNode);
}
}
return this._name;
}
Expand Down
3 changes: 2 additions & 1 deletion src/model/pipeline/task-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

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

export class EmbeddedTask extends NodeTknElement {

Expand Down
5 changes: 5 additions & 0 deletions src/tekton.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export interface TknMetadata {
uid?: string;
resourceVersion?: string;
labels?: {[key: string]: string};
managedFields?: unknown[];
selfLink?: string;
creationTimestamp?: string;
ownerReferences?: unknown[];
annotations?: {[key: string]: string};
}

export interface TknParams {
Expand Down
10 changes: 8 additions & 2 deletions src/util/filename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

const id = new RegExp('^[A-Za-z0-9]+');
const uidRegex = new RegExp('@[A-Za-z0-9]+$');

export function newFileName(name: string, uid: string): string {
const id = new RegExp('^[A-Za-z0-9]+');
return `${name}-${uid.match(id)[0]}`;
return `${name}@${uid.match(id)[0]}`;
}

export function originalFileName(name: string): string {
return name.replace(uidRegex, '');
}
10 changes: 5 additions & 5 deletions src/util/tekton-vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import * as os from 'os'
import * as fsx from 'fs-extra';
import { VirtualDocument } from '../yaml-support/yaml-locator';
import { TektonItem } from '../tekton/tektonitem';
import { newFileName } from './filename';
import { originalFileName, newFileName } from './filename';
import { getStderrString } from './stderrstring';

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

const pipelineRunFinishedRegex = RegExp(`^${ContextType.PIPELINERUNCHILDNODE}/`);
const taskRegex = RegExp(`^${ContextType.TASK}/`);

/**
* Create Uri for Tekton VFS
* @param type tekton resource type
Expand Down Expand Up @@ -93,10 +96,7 @@ export class TektonVFSProvider implements FileSystemProvider {
}

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

0 comments on commit 1bac5ad

Please sign in to comment.