From e05a1d183dd2492acea2ba2ad330f43fcdc65a74 Mon Sep 17 00:00:00 2001 From: Tamas Muncsan Date: Sat, 10 Oct 2020 14:34:46 +0200 Subject: [PATCH] feat: prefer whenexpressions over conditions rule --- .tektonlintrc.yaml | 1 + readme.markdown | 1 + .../__snapshots__/regresion.test.js.snap | 32 +++++++++++++++++++ regression-tests/conditions.yaml | 13 ++++++++ rule-loader.js | 3 ++ rules/prefer-when-expression.js | 10 ++++++ 6 files changed, 60 insertions(+) create mode 100644 rules/prefer-when-expression.js diff --git a/.tektonlintrc.yaml b/.tektonlintrc.yaml index 2bb1426..e92c65b 100644 --- a/.tektonlintrc.yaml +++ b/.tektonlintrc.yaml @@ -22,3 +22,4 @@ rules: # error | warning | off no-unused-param: warning no-missing-resource: error no-undefined-param: error + prefer-when-expression: warning diff --git a/readme.markdown b/readme.markdown index 98f1e59..568bc4b 100644 --- a/readme.markdown +++ b/readme.markdown @@ -208,6 +208,7 @@ for (const problem of problems) { - _kebab-case_ naming violations - `Task` & `Pipeline` definitions with `tekton.dev/v1alpha1` `apiVersion` - Missing `TriggerBinding` parameter values +- Usage of deprecated `Condition` instead of `WhenExpression` [tekton]: https://tekton.dev [node]: https://nodejs.org diff --git a/regression-tests/__snapshots__/regresion.test.js.snap b/regression-tests/__snapshots__/regresion.test.js.snap index 5d17ed6..652ca42 100644 --- a/regression-tests/__snapshots__/regresion.test.js.snap +++ b/regression-tests/__snapshots__/regresion.test.js.snap @@ -178,6 +178,22 @@ Array [ "path": "./regression-tests/conditions.yaml", "rule": "no-invalid-name", }, + Object { + "level": "warning", + "loc": Object { + "endColumn": 1, + "endLine": 43, + "range": Array [ + 773, + 807, + ], + "startColumn": 9, + "startLine": 42, + }, + "message": "Task 'tekton-without-params' in Pipeline 'pipeline-with-missing-condition' is guarded by condition(s) ('missing-condition'). Conditions are deprecated, use WhenExpressions instead.", + "path": "./regression-tests/conditions.yaml", + "rule": "prefer-when-expression", + }, Object { "level": "error", "loc": Object { @@ -194,6 +210,22 @@ Array [ "path": "./regression-tests/conditions.yaml", "rule": "no-missing-resource", }, + Object { + "level": "warning", + "loc": Object { + "endColumn": 1, + "endLine": 56, + "range": Array [ + 1033, + 1131, + ], + "startColumn": 9, + "startLine": 54, + }, + "message": "Task 'tekton-without-params' in Pipeline 'pipeline-with-multiple-conditions' is guarded by condition(s) ('condition-with-unused-params, condition-with-unused-params'). Conditions are deprecated, use WhenExpressions instead.", + "path": "./regression-tests/conditions.yaml", + "rule": "prefer-when-expression", + }, Object { "level": "warning", "loc": Object { diff --git a/regression-tests/conditions.yaml b/regression-tests/conditions.yaml index bb8a752..ee19572 100644 --- a/regression-tests/conditions.yaml +++ b/regression-tests/conditions.yaml @@ -40,3 +40,16 @@ spec: name: task-without-params conditions: - conditionRef: missing-condition +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: pipeline-with-multiple-conditions +spec: + tasks: + - name: tekton-without-params + taskRef: + name: task-without-params + conditions: + - conditionRef: condition-with-unused-params + - conditionRef: condition-with-unused-params diff --git a/rule-loader.js b/rule-loader.js index 7567a94..a6c1fbc 100644 --- a/rule-loader.js +++ b/rule-loader.js @@ -55,6 +55,9 @@ const rules = { // prefer-kebab-case 'prefer-kebab-case': require('./rules/prefer-kebab-case.js'), + + // prefer-when-expression + 'prefer-when-expression': require('./rules/prefer-when-expression.js'), }; module.exports = rules; diff --git a/rules/prefer-when-expression.js b/rules/prefer-when-expression.js new file mode 100644 index 0000000..dcbb664 --- /dev/null +++ b/rules/prefer-when-expression.js @@ -0,0 +1,10 @@ +module.exports = (docs, tekton, report) => { + for (const pipeline of Object.values(tekton.pipelines)) { + for (const task of pipeline.spec.tasks) { + if (task.conditions) { + const guardedBy = task.conditions.map(condition => condition.conditionRef); + report(`Task '${task.name}' in Pipeline '${pipeline.metadata.name}' is guarded by condition(s) ('${guardedBy.join(', ')}'). Conditions are deprecated, use WhenExpressions instead.`, task, 'conditions'); + } + } + } +};