Skip to content

Commit

Permalink
fix: convert param to string before matching
Browse files Browse the repository at this point in the history
Co-authored-by: Bence Dányi <[email protected]>
  • Loading branch information
bhartshorn and madbence committed Oct 18, 2020
1 parent 34247ee commit 2c2f8f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
16 changes: 16 additions & 0 deletions regression-tests/__snapshots__/regresion.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions regression-tests/array-params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
46 changes: 27 additions & 19 deletions rules/no-pipeline-task-cycle.js
Original file line number Diff line number Diff line change
@@ -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 [];
Expand All @@ -28,8 +37,7 @@ function runAfterReferences({ runAfter }) {
ref: after,
type: 'runAfterRef',
}));
};

}

function resourceInputReferences(task) {
const { resources } = task;
Expand Down

0 comments on commit 2c2f8f3

Please sign in to comment.