Skip to content

Commit

Permalink
Run lint script instead of eslint directly
Browse files Browse the repository at this point in the history
Closes #532.
  • Loading branch information
michaelsauter committed May 31, 2022
1 parent 15dfa8b commit db83b0a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 1 addition & 9 deletions build/package/scripts/build-npm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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#*=}";;

Expand Down Expand Up @@ -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

Expand Down
21 changes: 6 additions & 15 deletions deploy/ods-pipeline/charts/tasks/templates/task-ods-build-npm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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=$?
Expand Down
2 changes: 1 addition & 1 deletion docs/design/software-design-specification.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
13 changes: 8 additions & 5 deletions docs/design/software-requirements-specification.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.

|===
Expand Down
19 changes: 6 additions & 13 deletions docs/tasks/ods-build-npm.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion test/tasks/ods-build-npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit db83b0a

Please sign in to comment.