New code definition

By default, your SonarQube Cloud project uses the global setting baseline for new code. You can apply a specific new code option for your project if desire.

When your project is created, the new code definition set at the organization level is applied to your project by default. However, you can select another new code definition for your project. See Quality standards and new code for additional information.

Setting the new code definition for your project

As a project admin, you can set the new code definition for your project in the UI (except the Specific version and Specific date options) or using the Web API, at creation time or anytime later as explained below.

For more compliance with the Clean as You Code methodology, the Specific version and Specific date options can only be set using the Web API, as it would require frequent user action to be kept up to date.

In the UI

To set or change the new code definition for your project in the UI:

  1. Retrieve your project. See Retrieving projects for more details.

  2. Go to Administration > New Code.

  3. Select the option you want to apply to your project.

  4. Select Save.

The new code definition used by your project can be defined in SonarQube Cloud at any time. Note that you must run a new analysis to see your changes take affect.

Via the Web API

To use the Web API to set your new code definition, you need to use an alternative endpoint, POST api/settings/set.

You need to make two separate API calls as explained below depending on the selected new code option.

Previous version

Previous version

First call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period

value: previous_version

Second call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period.type


value: previous_version

Specific version

Specific version

First call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period

value: <version>

Second call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period.type


value: version

Number of days

Number of days

First call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period

value: <number_of_days>

Second call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period.type


value: days

Specific date

Specific date

First call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period

value: <YYYY-MM-DD>

Second call

Endpoint

api/settings/set

Method

POST

Parameters

component: <project_key>

key: sonar.leak.period.type

value: date

It’s not necessary to pass the organization key since the project key is unique across all the organizations (The component parameter accepts only a single value).

Additional setup and recommendations

Make sure to follow the recommendations about the Checked-out code.

We also recommend completing your merges using the fast-forward option without a merge commit; examples include GitHub’s squash and merge or rebase and merge options. That way, blame for merged commits will always have a more recent commit date.

If using Previous version option

The current version of a project is determined in different ways depending on the build system:

  • If the analysis is done using the SonarScanner for Maven, then SonarQube Server reads the version from the pom.xml file.

  • If the analysis is done with the SonarScanner for Gradle then SonarQube Server reads the version from the build.gradle file.

  • In all other cases, you must explicitly specify the version by setting the analysis parameter sonar.projectVersion.

Last updated

Was this helpful?