Developing a plugin that imposes a metric on new code?

For context, my team is using SonarQube 7.7 Enterprise Edition.

My team would like to develop a plugin that imposes a metric on new code the same way the native “… on New Code” metrics do. Specifically, we’re interested in having the metric be applied to the quality gate on short-lived branches.

We reviewed the source for the sonarqube project and see where the Metric instances are being created here:

However, it appears that the only difference between the new-code and non-new-code metrics is the calling of .setDeleteHistoricalData(true). We tried to set this on the metrics in our plugin, but did not see them reflected in the quality gate on our short lived branches.

Since (as far as I know) the branch-handling logic in our paid version of SonarQube is not open-source, we’re wondering if the native metrics are somehow handled/registered differently such that they are allowed to apply to short-lived branches.

Could someone help us understand how to create a Metric instance that will apply to a short-lived branch?

I recently was experimenting with a similar case. I found out that if your metric is prefixed with new_ will be available for the new code conditions on the quality gate.

A second tip: when creating the metric, use .setHidden(false) to display it immediately in the web ui.

1 Like

Thanks for the pointers. I tried and could see the metrics in New Code Conditions , but their alues are not shown in Measures page .

Do I need to create additional metrics with “new_” ? (Possibly No is the answer)

setHidden parameter default value is false.

The class org.sonar.core.issue.DefaultIssue has a method isNew which reads the following field

  // true if the issue did not exist in the previous scan.
  private boolean isNew = true;

DefaultIssue belongs to the sonarcube package and implements the interface org.sonar.api.ce.measure.Issue which belongs to the plugin api. Unfortunately isNew isn’t published in the Issue interface, so that to call the isNew method one has to resort to reflection

  return (Boolean) issue.getClass().getMethod("isNew").invoke(issue);