Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add support to object params #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions regression-tests/general/object-params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: object-params
spec:
params:
- name: build-vars
type: object
properties:
environment:
type: string
version:
type: string
- name: environment
type: string
- name: version
type: string
steps:
- name: echo-build-vars
image: ubuntu:alpine
script: |
#!/bin/bash
echo "Environment: $(params.build-vars.environment)"
echo "Version: $(params.build-vars.version)"
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: task-param-object-pipeline
spec:
params:
- name: build-vars
type: object
properties:
environment:
type: string
version:
type: string
tasks:
- name: task-param-array-task
taskRef:
name: object-params
params:
- name: build-vars
value: $(params.build-vars)
- name: environment
value: $(params.build-vars.environment)
- name: version
value: "1.2.3"
36 changes: 36 additions & 0 deletions regression-tests/general/object-params.yaml.expect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"message": "Task 'object-params' defines parameter 'environment', but it's not used anywhere in the spec",
"rule": "no-unused-param",
"level": "warning",
"path": "./regression-tests/general/object-params.yaml",
"loc": {
"range": [
244,
281,
281
],
"startLine": 15,
"startColumn": 7,
"endLine": 17,
"endColumn": 1
}
},
{
"message": "Task 'object-params' defines parameter 'version', but it's not used anywhere in the spec",
"rule": "no-unused-param",
"level": "warning",
"path": "./regression-tests/general/object-params.yaml",
"loc": {
"range": [
287,
320,
320
],
"startLine": 17,
"startColumn": 7,
"endLine": 19,
"endColumn": 1
}
}
]
21 changes: 17 additions & 4 deletions src/rules/no-undefined-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const createVisitor = (resource, params, prefix, report) => (node, path, parent)
for (const item of m) {
const m2 = item.match(r2);
const param = m2[1];
if (!params.includes(param)) {
if (!containsParam(params,param)) {
report(
`Undefined param '${param}' at ${pathToString(path)} in '${resource}'`,
parent,
Expand All @@ -19,16 +19,29 @@ const createVisitor = (resource, params, prefix, report) => (node, path, parent)
}
};

function containsParam(allParams, param) {
if (allParams.some((p) => p.name === param)) return true;
const paramParts = param.split('.');
if (paramParts.length > 1) {
const paramName = paramParts[0];
const propName = paramParts[1];
const paramObj = allParams.find((p) => p.name === paramName);
return (paramObj && paramObj.type === 'object' && paramObj.properties && paramObj.properties[propName])
}

return false;
}

function getParams(crd) {
if (!crd.spec || !crd.spec.params) return [];
return crd.spec.params.map((param) => param.name);
return crd.spec.params
}

function getTaskParams(crd) {
if (!crd.spec) return [];
if (crd.spec.params) return crd.spec.params.map((param) => param.name);
if (crd.spec.params) return crd.spec.params
if (!crd.spec.inputs) return [];
if (crd.spec.inputs.params) return crd.spec.inputs.params.map((param) => param.name);
if (crd.spec.inputs.params) return crd.spec.inputs.params
return [];
}

Expand Down
13 changes: 12 additions & 1 deletion src/rules/no-unused-param.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@ const unused = (params, prefix) => (node) => {

for (const item of m) {
const m2 = item.match(r2);
const param = m2[1];
let param = m2[1];
const parent = getParent(param)
if (parent) {
param = parent;
}
if (params[param] != null) {
params[param]++;
}
}
};

function getParent(param) {
const paramParts = param.split('.');
if (paramParts.length > 1) {
return paramParts[0];
}
}

function getParams(kind, spec) {
if (kind === 'Task' && spec.inputs && spec.inputs.params) return spec.inputs.params;
if (spec.params) return spec.params;
Expand Down