From 2c2f8f3d5ba451824bcdbdd9a0af9b3a0b2a7e2d Mon Sep 17 00:00:00 2001 From: Brandon Hartshorn Date: Tue, 13 Oct 2020 17:17:47 -0500 Subject: [PATCH] fix: convert param to string before matching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bence Dányi --- .../__snapshots__/regresion.test.js.snap | 16 +++++++ regression-tests/array-params.yaml | 17 +++++++ rules/no-pipeline-task-cycle.js | 46 +++++++++++-------- 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/regression-tests/__snapshots__/regresion.test.js.snap b/regression-tests/__snapshots__/regresion.test.js.snap index 41693d7..df32cd2 100644 --- a/regression-tests/__snapshots__/regresion.test.js.snap +++ b/regression-tests/__snapshots__/regresion.test.js.snap @@ -114,6 +114,22 @@ Array [ "path": "./regression-tests/array-params.yaml", "rule": "no-unused-param", }, + Object { + "level": "error", + "loc": Object { + "endColumn": 27, + "endLine": 31, + "range": Array [ + 478, + 490, + ], + "startColumn": 15, + "startLine": 31, + }, + "message": "Pipeline 'task-param-array-pipeline' references task 'doesnt-exist' but the referenced task cannot be found. To fix this, include all the task definitions to the lint task for this pipeline.", + "path": "./regression-tests/array-params.yaml", + "rule": "no-missing-resource", + }, Object { "level": "warning", "loc": Object { diff --git a/regression-tests/array-params.yaml b/regression-tests/array-params.yaml index f23400e..83b41e2 100644 --- a/regression-tests/array-params.yaml +++ b/regression-tests/array-params.yaml @@ -19,3 +19,20 @@ spec: - name: qux default: false steps: [] +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: task-param-array-pipeline +spec: + tasks: + - name: task-param-array-task + taskRef: + name: doesnt-exist + params: + - name: compact-args + value: ["first", "second"] + - name: loose-args + value: + - "first" + - "second" diff --git a/rules/no-pipeline-task-cycle.js b/rules/no-pipeline-task-cycle.js index 2e81940..3990b1e 100644 --- a/rules/no-pipeline-task-cycle.js +++ b/rules/no-pipeline-task-cycle.js @@ -1,25 +1,34 @@ const { alg, Graph } = require('graphlib'); -function readParamReference({ value }) { - const taskResultReferenceRegexp = /\$\(tasks\.[a-z|-]*\.results\.[a-z|-]*\)/; - if (!value) return; - if (!value.match(taskResultReferenceRegexp)) return; - const referencedTaskname = value.split('$(tasks.')[1].split('.')[0]; +const RESULT_PATTERN = '\\$\\(tasks\\.([^.]+)\\.results\\.[^.]*\\)'; +const RESULT_REGEX_G = new RegExp(RESULT_PATTERN, 'g'); +const RESULT_REGEX = new RegExp(RESULT_PATTERN); - return { - ref: referencedTaskname, - }; -}; +function getReferences(str) { + const matches = str.match(RESULT_REGEX_G); + if (!matches) return []; + return matches.map(substr => substr.match(RESULT_REGEX)[1]); +} -function paramsReferences({ params }) { - if (params === undefined || params === null) return []; +function getResultReferences(param) { + if (Array.isArray(param.value)) { + return param.value.flatMap(getReferences); + } else if (param.value) { + return getReferences(param.value); + } + return []; +} - return params.map(param => ({ - ...readParamReference(param), - type: 'paramRef', - })) - .filter(({ ref }) => ref !== undefined); -}; +function paramsReferences(task) { + if (!task.params) return []; + return task.params + .flatMap(getResultReferences) + .filter(Boolean) + .map(ref => ({ + ref, + type: 'paramRef', + })); +} function runAfterReferences({ runAfter }) { if (runAfter === undefined || runAfter === null) return []; @@ -28,8 +37,7 @@ function runAfterReferences({ runAfter }) { ref: after, type: 'runAfterRef', })); -}; - +} function resourceInputReferences(task) { const { resources } = task;