Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 37 ยท Rules and Quality Gates

Quality Gates

Understand quality gate conditions on new and overall code, the Sonar way default gate, how to build a custom gate, and how pipelines read the pass or fail result.

A quality gate is the answer to one question: is this code good enough to ship? It is a list of conditions on measures; if every condition passes the gate is Passed, and if any fails the gate is Failed. The result is shown on the dashboard, sent to pull requests and read by pipelines.

What a gate does

Measures Bugs, coverage, duplication.
Conditions A threshold for each measure.
Verdict Passed or Failed.

One failing condition fails the gate.

1 / 2

Conditions

A condition has a metric, an operator and a threshold, applied either to new code or to overall code. Examples:

  • Reliability rating on new code is worse than A
  • Security hotspots reviewed on new code is less than 100%
  • Coverage on new code is less than 80%
  • Duplicated lines on new code is greater than 3%

Gates work best when they judge new code, because you can always satisfy a rule about code you are writing today, while a threshold on overall code may be impossible for an old project. That is the Clean as You Code philosophy, covered in the next lesson.

The built-in Sonar way gate

SonarQube ships a built-in gate called Sonar way, and it is the default for new projects. In current versions it requires, on new code: no new issues, all new security hotspots reviewed, coverage of at least 80% and at most 3% duplicated lines. Older versions expressed it as ratings (A for reliability, security and maintainability) plus coverage and duplication. Check the gate page in your server to see exactly what your version enforces. The built-in gate is read-only; copy it to make your own.

Custom gates

Create a custom gate in Quality Gates > Create, or copy Sonar way and adjust. Then assign it to the project (Project Settings > Quality Gate) or make it the default. Guidelines:

  • Keep it short. Four to six conditions are easier to explain and fix than twenty.
  • Prefer new-code conditions. Add an overall-code condition only for a metric you control, such as "overall security rating is A".
  • Do not set coverage so high that people write meaningless tests to pass.
  • Start slightly lenient on a legacy project, tighten over time.

Evaluating a gate

The stand-in below implements gate evaluation: a list of conditions applied to a dictionary of measures. Edit the numbers to see which condition fails and why.

Python Evaluate a quality gate (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that each condition describes the failure zone, just like in the real UI, where you pick the operator "is less than" and a value; the gate fails when the measure falls in that zone.

Reading the result from a pipeline

Pipelines can ask two ways:

  1. Let the scanner wait: set sonar.qualitygate.wait=true (and optionally sonar.qualitygate.timeout) and the scan command exits with a failure when the gate fails.
  2. Poll the API after the scan: GET /api/qualitygates/project_status?projectKey=my-shop returns the status and the failing conditions.

The second stand-in reads a status document like the API returns (a sample shaped like it, not real output) and turns it into an exit code, as a pipeline step would.

Python Turn a gate result into an exit code (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Gate hygiene

  • A gate that is always red is ignored, a gate that is always green protects nothing. Review it every few months.
  • Treat a failed gate on a pull request as a normal review comment, not a punishment.
  • Assign different gates to different kinds of project if needed (a prototype and a payment service need not be equal), but avoid a gate per project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When is a quality gate Failed?

    A gate passes only if every condition passes; one failing condition fails it.
  2. 2. Why do gates usually judge new code?

    Focusing on new code lets a legacy project pass by keeping its changes clean.
  3. 3. Which scanner property makes the scan wait for the gate result?

    sonar.qualitygate.wait=true makes the scanner wait and fail the command when the gate fails.
  4. 4. Can you edit the built-in Sonar way gate directly?

    Built-in gates are read-only; copy the gate to customize it.

Summary

  • A quality gate is a list of conditions on measures with a pass or fail result.
  • Prefer conditions on new code; keep the gate short.
  • Sonar way is the built-in read-only default; copy it to customize.
  • Pipelines read the result with sonar.qualitygate.wait or the project status API.
  • Review the gate regularly so it stays meaningful.

Next: New Code and Clean as You Code