diff --git a/CHANGELOG.md b/CHANGELOG.md index a72f4dfb..068a9938 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ listed in the changelog. - Update Go to 1.17 ([#528](https://github.com/opendevstack/ods-pipeline/issues/528)) - Rename `ods-build-typescript` task to `ods-build-npm` ([#503](https://github.com/opendevstack/ods-pipeline/issues/503)) - Implement global caching for Gradle build task ([#490](https://github.com/opendevstack/ods-pipeline/issues/490)) +- Run `lint` script instead of `eslint` directly ([#532](https://github.com/opendevstack/ods-pipeline/issues/532)) ### Fixed - Pipelines fail in clusters with private / self-signed certificates ([#518](https://github.com/opendevstack/ods-pipeline/issues/518)) diff --git a/build/package/scripts/build-npm.sh b/build/package/scripts/build-npm.sh index db8157fa..de17a932 100755 --- a/build/package/scripts/build-npm.sh +++ b/build/package/scripts/build-npm.sh @@ -28,8 +28,6 @@ OUTPUT_DIR="docker" WORKING_DIR="." ARTIFACT_PREFIX="" DEBUG="${DEBUG:-false}" -MAX_LINT_WARNINGS="0" -LINT_FILE_EXT=".js,.ts,.jsx,.tsx,.svelte" COPY_NODE_MODULES="false" while [[ "$#" -gt 0 ]]; do @@ -44,12 +42,6 @@ while [[ "$#" -gt 0 ]]; do --debug) DEBUG="$2"; shift;; --debug=*) DEBUG="${1#*=}";; - --max-lint-warnings) MAX_LINT_WARNINGS="$2"; shift;; - --max-lint-warnings=*) MAX_LINT_WARNINGS="${1#*=}";; - - --lint-file-ext) LINT_FILE_EXT="$2"; shift;; - --lint-file-ext=*) LINT_FILE_EXT="${1#*=}";; - --build-dir) BUILD_DIR="$2"; shift;; --build-dir=*) BUILD_DIR="${1#*=}";; @@ -101,7 +93,7 @@ npm ci --ignore-scripts echo "Linting ..." set +e -npx eslint src --ext "${LINT_FILE_EXT}" --format compact --max-warnings "${MAX_LINT_WARNINGS}" > eslint-report.txt +npm run lint > eslint-report.txt exitcode=$? set -e diff --git a/deploy/ods-pipeline/charts/tasks/templates/task-ods-build-npm.yaml b/deploy/ods-pipeline/charts/tasks/templates/task-ods-build-npm.yaml index 251adcf2..cf51add3 100644 --- a/deploy/ods-pipeline/charts/tasks/templates/task-ods-build-npm.yaml +++ b/deploy/ods-pipeline/charts/tasks/templates/task-ods-build-npm.yaml @@ -12,13 +12,16 @@ spec: The following steps are executed: - checks that package.json and package-lock.json exists to require best practice of using lock files. See also link:https://github.com/opendevstack/ods-pipeline/discussions/411[discussion 411] - - linting using `eslint` + - linting using `npm run lint` - build application, using `npm run build` - test execution - SonarQube quality scan - For `eslint` to work there needs to be a config file (`eslintrc.json` or similar) at the root of the working directory. - This can be done by running `eslint --init` or by following the link:https://eslint.org/docs/user-guide/getting-started[official documentation] + For linting to work there needs to be a `lint` task in the `package.json` file, + for example `npx eslint src --format compact`, together with a config file + (`eslintrc.json` or similar) at the root of the working directory. This can + be done by running `eslint --init` or by following the + ink:https://eslint.org/docs/user-guide/getting-started[official documentation] The exact build recipe can be found at link:https://github.com/opendevstack/ods-pipeline/blob/master/build/package/scripts/build-npm.sh[build/package/scripts/build-npm.sh]. @@ -68,16 +71,6 @@ spec: For single build repos enabling build caching has limited benefits. For multi build repos enabling this is recommended unless the build is dependant on files outside of the working directory. See ADR caching-build-tasks for more details and workarounds. type: string default: "false" - - name: max-lint-warnings - description: >- - Maximum of allowed linting warnings after which eslint will exit with an error. - Set to "-1" to never exit with an error due to warnings. - type: string - default: "0" - - name: lint-file-ext - description: File extensions to lint separated by a comma. - type: string - default: ".js,.ts,.jsx,.tsx,.svelte" - name: sonar-quality-gate description: Whether quality gate needs to pass. type: string @@ -163,8 +156,6 @@ spec: --working-dir=$(params.working-dir) \ --output-dir=$(params.output-dir) \ --debug=${DEBUG} \ - --max-lint-warnings=$(params.max-lint-warnings) \ - --lint-file-ext=$(params.lint-file-ext) \ --build-dir=$(params.build-dir) \ --copy-node-modules=$(params.copy-node-modules) build_exit=$? diff --git a/docs/design/software-design-specification.adoc b/docs/design/software-design-specification.adoc index 064bfca0..361cb0a5 100644 --- a/docs/design/software-design-specification.adoc +++ b/docs/design/software-design-specification.adoc @@ -331,7 +331,7 @@ For traceability package.json and package-lock.json are copied into the `dist` d Runs `npm run test`, creating code coverage and xUnit reports. The artifacts are placed in the working directory and in `.ods/artifacts/code-coverage` and `.ods/artifacts/xunit-reports`, respectively. -Runs `eslint` to lint the source code and fails if there are any errors or warnings. The files to lint default to all files with an `.js`, `.ts`, `.jsx`, `.tsx`, `.svelte` extension inside `src` and can be set by the `lint-file-ext` task parameter. The amount of allowed warnings defaults to 0 and can be set by the `max-lint-warnings` task parameter. +Runs `npm run lint` to lint the source code. If there are any errors or warnings, the script should exit with a non-zero exit code. Supplies default SonarQube project properties file if required (SDS-SHARED-3). diff --git a/docs/design/software-requirements-specification.adoc b/docs/design/software-requirements-specification.adoc index acacd76d..5b0be85c 100644 --- a/docs/design/software-requirements-specification.adoc +++ b/docs/design/software-requirements-specification.adoc @@ -224,22 +224,25 @@ a| The task shall build a Python application. [cols="1,3"] |=== | SRS-TASK-BUILD-NPM-1 -a| The task shall run test, creating code coverage and xUnit reports. +| The task shall run the NPM lint script. + +| SRS-TASK-BUILD-NPM-2 +a| The task shall run the NPM test script, creating code coverage and xUnit reports. * Artifacts shall be made available to SonarQube and designated for upload to Nexus. -| SRS-TASK-BUILD-NPM-2 +| SRS-TASK-BUILD-NPM-3 a| The task shall build a Node.JS application using NPM. * Destination directory shall be customizable -| SRS-TASK-BUILD-NPM-3 +| SRS-TASK-BUILD-NPM-4 | See SRS-TASK-SHARED-1. -| SRS-TASK-BUILD-NPM-4 +| SRS-TASK-BUILD-NPM-5 | See SRS-TASK-SHARED-2. -| SRS-TASK-BUILD-NPM-5 +| SRS-TASK-BUILD-NPM-6 | See SRS-TASK-SHARED-3. |=== diff --git a/docs/tasks/ods-build-npm.adoc b/docs/tasks/ods-build-npm.adoc index 253d060b..83f18213 100644 --- a/docs/tasks/ods-build-npm.adoc +++ b/docs/tasks/ods-build-npm.adoc @@ -7,13 +7,16 @@ Builds Node.js applications using NPM. The following steps are executed: - checks that package.json and package-lock.json exists to require best practice of using lock files. See also link:https://github.com/opendevstack/ods-pipeline/discussions/411[discussion 411] -- linting using `eslint` +- linting using `npm run lint` - build application, using `npm run build` - test execution - SonarQube quality scan -For `eslint` to work there needs to be a config file (`eslintrc.json` or similar) at the root of the working directory. -This can be done by running `eslint --init` or by following the link:https://eslint.org/docs/user-guide/getting-started[official documentation] +For linting to work there needs to be a `lint` task in the `package.json` file, +for example `npx eslint src --format compact`, together with a config file +(`eslintrc.json` or similar) at the root of the working directory. This can +be done by running `eslint --init` or by following the +ink:https://eslint.org/docs/user-guide/getting-started[official documentation] The exact build recipe can be found at link:https://github.com/opendevstack/ods-pipeline/blob/master/build/package/scripts/build-npm.sh[build/package/scripts/build-npm.sh]. @@ -68,16 +71,6 @@ without leading `./` and trailing `/`. | If enabled tasks uses or populates cache with the output dir contents (and artifacts) so that a build can be skipped if the `working-dir` contents did not change. For single build repos enabling build caching has limited benefits. For multi build repos enabling this is recommended unless the build is dependant on files outside of the working directory. See ADR caching-build-tasks for more details and workarounds. -| max-lint-warnings -| 0 -| Maximum of allowed linting warnings after which eslint will exit with an error. Set to "-1" to never exit with an error due to warnings. - - -| lint-file-ext -| .js,.ts,.jsx,.tsx,.svelte -| File extensions to lint separated by a comma. - - | sonar-quality-gate | false | Whether quality gate needs to pass. diff --git a/test/tasks/ods-build-npm_test.go b/test/tasks/ods-build-npm_test.go index 545abfe3..2f27d7c0 100644 --- a/test/tasks/ods-build-npm_test.go +++ b/test/tasks/ods-build-npm_test.go @@ -112,7 +112,7 @@ func TestTaskODSBuildNPM(t *testing.T) { checkFilesExist(t, wsDir, wantFile) wantLintReportContent := "/workspace/source/src/index.ts: line 3, col 31, Warning - Unexpected any. Specify a different type. (@typescript-eslint/no-explicit-any)\n\n1 problem" - checkFileContent(t, wsDir, filepath.Join(pipelinectxt.LintReportsPath, "report.txt"), wantLintReportContent) + checkFileContentContains(t, wsDir, filepath.Join(pipelinectxt.LintReportsPath, "report.txt"), wantLintReportContent) }, }, "fail pulling image if unsupported node version is specified": { diff --git a/test/testdata/workspaces/javascript-sample-app-build-dir/package.json b/test/testdata/workspaces/javascript-sample-app-build-dir/package.json index 6f43bd01..a418dbfb 100644 --- a/test/testdata/workspaces/javascript-sample-app-build-dir/package.json +++ b/test/testdata/workspaces/javascript-sample-app-build-dir/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { + "lint": "echo 'no linting'", "build": "mkdir -p build && cp -r src build/", "test": "JEST_JUNIT_OUTPUT_DIR='build/test-results/test' JEST_JUNIT_OUTPUT_NAME='report.xml' npx jest --reporters=default --reporters=jest-junit --coverage --coverageDirectory=build/coverage --forceExit", "start": "node build/src/index.js" diff --git a/test/testdata/workspaces/javascript-sample-app/package.json b/test/testdata/workspaces/javascript-sample-app/package.json index c24776d4..4176720b 100644 --- a/test/testdata/workspaces/javascript-sample-app/package.json +++ b/test/testdata/workspaces/javascript-sample-app/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { + "lint": "echo 'no linting'", "build": "mkdir -p dist && cp -r src dist/", "test": "JEST_JUNIT_OUTPUT_DIR='build/test-results/test' JEST_JUNIT_OUTPUT_NAME='report.xml' npx jest --reporters=default --reporters=jest-junit --coverage --coverageDirectory=build/coverage --forceExit", "start": "node dist/src/index.js" diff --git a/test/testdata/workspaces/typescript-sample-app-lint-error/package.json b/test/testdata/workspaces/typescript-sample-app-lint-error/package.json index e95ae9b4..35286291 100644 --- a/test/testdata/workspaces/typescript-sample-app-lint-error/package.json +++ b/test/testdata/workspaces/typescript-sample-app-lint-error/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { + "lint": "npx eslint src --ext=.js,.ts --format=compact --max-warnings=0", "build": "tsc", "test": "JEST_JUNIT_OUTPUT_DIR='build/test-results/test' JEST_JUNIT_OUTPUT_NAME='report.xml' npx jest --reporters=default --reporters=jest-junit --coverage --coverageDirectory=build/coverage --forceExit ./dist", "start": "node dist/src/index.js" diff --git a/test/testdata/workspaces/typescript-sample-app/package.json b/test/testdata/workspaces/typescript-sample-app/package.json index e95ae9b4..35286291 100644 --- a/test/testdata/workspaces/typescript-sample-app/package.json +++ b/test/testdata/workspaces/typescript-sample-app/package.json @@ -3,6 +3,7 @@ "version": "1.0.0", "main": "index.js", "scripts": { + "lint": "npx eslint src --ext=.js,.ts --format=compact --max-warnings=0", "build": "tsc", "test": "JEST_JUNIT_OUTPUT_DIR='build/test-results/test' JEST_JUNIT_OUTPUT_NAME='report.xml' npx jest --reporters=default --reporters=jest-junit --coverage --coverageDirectory=build/coverage --forceExit ./dist", "start": "node dist/src/index.js"