Commit 44cc3c30 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat(lint): enforce GitLab integration

parent 5143a947
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -90,7 +90,8 @@ variables:

### `node-lint` job

The Node template features a job `node-lint` that performs Node.js source code **lint**. This job is **disabled by default**. It can be activated by setting `NODE_LINT_ENABLED`
The Node template features a `node-lint` job that performs a code analysis with [ESLint](https://eslint.org/).
This job is **disabled by default**. It can be activated by setting `NODE_LINT_ENABLED`.

It is bound to the `build` stage, and uses the following variable:

@@ -99,7 +100,18 @@ It is bound to the `build` stage, and uses the following variable:
| `lint-enabled` / `NODE_LINT_ENABLED` | Set to `true` to enable lint analysis                                                                                                                                                                                                                                                                              | _none_ (disabled) |
| `lint-args` / `NODE_LINT_ARGS`       | npm [run script](https://docs.npmjs.com/cli/v8/commands/npm-run-script) arguments to execute the lint analysis <br/> yarn [run script](https://classic.yarnpkg.com/en/docs/cli/run) arguments to execute the lint analysis <br/> pnpm [run script](https://pnpm.io/cli/run) arguments to execute the lint analysis | `run lint`        |

The job generates a lint report that you will find here: `NODE_PROJECT_DIR/reports/node-lint.xslint.json`.
In addition to a textual report in the console, this job produces the following reports, kept for one day:

| Report                                                | Format                                                   | Usage                                                                                                       |
|-------------------------------------------------------|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------------|
| `$NODE_PROJECT_DIR/reports/node-lint.gitlab.json` | [GitLab](https://docs.gitlab.com/ee/ci/testing/code_quality.html#eslint) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscodequality) |
| `$NODE_PROJECT_DIR/reports/node-lint.xslint.json` | JSON ESLint                                               | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/external-issues/)                        |



| Report                                            | Format                                                        | Usage                                                                                                                                                                                                                                  |
| ------------------------------------------------- | ------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `$NODE_PROJECT_DIR/reports/npm-audit.native.json` | [JSON](https://docs.npmjs.com/cli/v9/commands/npm-audit#json) | [DefectDojo integration](https://documentation.defectdojo.com/integrations/parsers/#npm-audit)<br/>_This report is generated only if DefectDojo template is detected, if needed, you can force it with `$DEFECTDOJO_NPMAUDIT_REPORTS`_ |

### `node-build` job

+1 −1
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@
    {
      "id": "node-lint",
      "name": "node lint",
      "description": "node lint analysis",
      "description": "code analysis with [ESLint](https://eslint.org/)",
      "enable_with": "NODE_LINT_ENABLED",
      "variables": [
        {
+25 −16
Original line number Diff line number Diff line
@@ -539,16 +539,6 @@ stages:
    fi
  }

  function sonar_lint_report() {
    if [[ "$SONAR_HOST_URL" ]] || [[ "$SONAR_URL" ]]
    then
      mkdir -p -m 777 reports
      # generate eslint report in json for SonarQube
      # shellcheck disable=SC2086
      $NODE_MANAGER $NODE_LINT_ARGS -- --format=json --output-file=reports/node-lint.xslint.json
    fi
  }

  function configure_publish() {
    # get package scope+name, and target registry url
    pkg_fullname=$(node -pe "require('./package.json').name")
@@ -689,16 +679,35 @@ node-lint:
  extends: .node-base
  stage: build
  script:
    # generate lint report for sonar
    - sonar_lint_report || true
    # display lint result for console
    - $NODE_MANAGER $NODE_LINT_ARGS
    - mkdir -p -m 777 reports
    # maybe generate ESLint report for SonarQube
    - |
      if [[ "$SONAR_HOST_URL" ]] || [[ "$SONAR_URL" ]]
      then
        # generate eslint report for SonarQube
        # shellcheck disable=SC2086
        log_info "SonarQube detedted: producing ESLint JSON report..."
        $NODE_MANAGER $NODE_LINT_ARGS -- --format=json --output-file=reports/node-lint.xslint.json || true
      fi
    # maybe add eslint-formatter-gitlab
    - |
      if ! $NODE_MANAGER list | grep eslint-formatter-gitlab > /dev/null
      then
        log_info "Adding eslint-formatter-gitlab to produce ESLint GitLab report..."
        $NODE_MANAGER add eslint-formatter-gitlab
      fi
    # run ESLint with console output and GitLab report
    # shellcheck disable=SC2086
    - ESLINT_CODE_QUALITY_REPORT=reports/node-lint.gitlab.json $NODE_MANAGER $NODE_LINT_ARGS -- --format=gitlab
  artifacts:
    when: always # store artifact even if test Failed
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    paths:
      - $NODE_PROJECT_DIR/reports/node-lint.xslint.json
    expire_in: 1 day
    paths:
      - $NODE_PROJECT_DIR/reports/node-lint.*
    reports:
      codequality:
        - $NODE_PROJECT_DIR/reports/node-lint.gitlab.json
  rules:
    # exclude if $NODE_LINT_ENABLED unset
    - if: '$NODE_LINT_ENABLED != "true"'