GitLab Code Quality reports

Enabling style compliance reports in GitLab merge requests

Use this procedure to enable the GitLab Code Quality widget for Vale. The widget reports style compliance issues in the GitLab merge request interface.

Procedure
  1. Create a .gitlab-ci.yml file at the repository root with the following content:

    code_quality:
      image:
        name: jdkato/vale:latest
        entrypoint: [""]
      tags: [shared]
      before_script:
        - apk update && apk add git
        - vale sync # pull down VRH rules package specified in vale.ini
      script:
        # list of updated/modified *.adoc files
        - FILES=$(git diff --name-only HEAD~1 HEAD --diff-filter=d "*.adoc")
        # clean out conditions for wider vale coverage
        - sed -i -e 's/ifdef::.*\|ifndef::.*\|ifeval::.*\|endif::.*/ /' ${FILES}
        # use a template to rearrange the vale JSON output
        # run vale with `--no-exit` to pass the build with errors
        - vale ${FILES} --minAlertLevel=error --glob='*.adoc' --output="$(pwd)/vale-json.tmpl" | sed "s/CI_COMMIT_SHA/$CI_COMMIT_SHA/g" > gl-code-quality-report.json
      artifacts:
        reports:
          codequality: gl-code-quality-report.json
  2. Create a vale-json.tmpl file at the repository root:

    {{- /* Modify Vale's output https://docs.errata.ai/vale/cli#--output */ -}}
    
    {{- /* Keep track of our various counts */ -}}
    
    {{- $e := 0 -}}
    {{- $w := 0 -}}
    {{- $s := 0 -}}
    {{- $f := 0 -}}
    
    {{- /* Range over the linted files */ -}}
    
    [
    {{- range $jdx, $file := .Files -}}
    
    {{- $f = add1 $f -}}
    {{- $path := .Path -}}
    
    {{- /* Range over the file's alerts */ -}}
    
    {{- if $jdx -}},{{- end -}}
    {{- range $idx, $a := .Alerts -}}
    
    {{- $error := "" -}}
    {{- if eq .Severity "error" -}}
        {{- $error = "critical" -}}
        {{- $e = add1 $e  -}}
    {{- else if eq .Severity "warning" -}}
        {{- $error = "major" -}}
        {{- $w = add1 $w -}}
    {{- else -}}
        {{- $error = "minor" -}}
        {{- $s = add1 $s -}}
    {{- end}}
    
    {{- /* Variables setup */ -}}
    
    {{- $loc := printf "%d" .Line -}}
    {{- $check := printf "%s" .Check -}}
    {{- $message := printf "%s" .Message -}}
    {{- if $idx -}},{{- end -}}
    
    {{- /* Output */ -}}
    
      {
        "description": "{{$check}}: {{ $message }}",
        "fingerprint": "CI_COMMIT_SHA",
        "severity": "{{ $error }}",
        "location": {
          "path": "{{ $path }}",
          "lines": {
            "begin": {{ $loc }}
          }
        }
      }
    {{end -}}
    {{end -}}
    ]
  3. Add the following entries to .gitignore:

    .vale
    gl-code-quality-report.json
  4. Commit the files to the local repository:

    git add .
    git commit -m "Add GitLab CI/CD code quality pipeline"
  5. Push the changes to the remote repository:

    git push -u origin main

Once merged, a code quality pipeline runs for every new commit added in a PR. Access the report from the Pipelines menu item in the GitLab repository.

Additional resources
  • For more details on GitLab’s Code Quality feature, see the GitLab Docs.