Define custom metrics programatically


(Jose Maria Cubel) #1


Regarding Configuring Portfolios and Applications, I can define a portolio containing all projects having a specific custom metric text value.

It is mandatory to define custom metrics to projects through SonarQube UI? I guess if there is a way to set project custom metric values programatically (i.e. by means of sonar-scanner parameters or web api).

(G Ann Campbell) #2


I really don’t want to advise you on the use of this feature because we’re about to release (e.t.a. next week) 7.4, which includes the ability to aggregate a Portfolio based on project (and application) tags. And with the release of this new feature, we deprecate aggregation by custom measure.

So… if you can wait a week to get started, you’ll likely be happier in the long run.

That said, you should be able to programmatically call the same Web APIs (see the link to the onboard api documentation in your footer) that the interface does. Also, I want to point out the difference between a metric (a thing which can be measured) which is set up once by the global admin and made available to all projects, and a measure which is turned on for a project by a project (or global) admin, and then (theoretically) updated periodically.

It’s likely that those first two steps aren’t worth automating, and that the last one might be.


(Jose Maria Cubel) #3

Thanks for your quick reply.

I have some restrictions in my current project and I’m only allowed to upgrade to SonarQube LTS versions.

  • When will be LTS 7.x released?
  • Will project (an application) tags included in LTS 7.x?
  • I’ll be able to set those tags programatically in some way, will I?

My aim is to propagate as tags or custom metrics some metadata I have defined in the Jenkins jobs that perform SonarQube analysis.

We have a lot of projects in our instance and this approach would save us many maintainance tasks as changing the metadata in Jenkins jobs would update immediately SonarQube custom metrics (or tags).



(G Ann Campbell) #4

Hi JM,

That’s a great question!
More seriously, we don’t have a firm schedule for that right now, but my current estimate would be Q2 2019.

Versions are generally cumulative, so what’s in 7.4 should appear in some form in 7.xLTS

Yep. See the part about being able to do with the Web API things you can do in the UI. :slight_smile:


(G Ann Campbell) #5

One more thing I should have mentioned: New custom measure values only take effect with the next analysis. So if you’re going to incorporate a value update into your CI chain, you want those web service calls to run before analysis.


(Jose Maria Cubel) #6

Thank you, Ann.

I already knew this restriction and I’ve defined the custom measure set up as the first step in my CI chain. Everything works fine, as expected :sweat_smile:

I only had to keep in mind whether the measure exists in order to use create o update method.

By the way, will the project tag approach be applied immediately or it’ll be also applied on the next analysis?

(G Ann Campbell) #7


That’s an excellent question (meaning: we’re aware of these structural cases, but haven’t covered them yet :roll_eyes:). In general, an update to a project’s measures (let’s say you mark an issue FP; the project’s issue counts are updated dynamically) will automatically trigger recalculation of the relevant portfolio(s). But an update that affect’s a project’s inclusion in a portfolio won’t trigger a portfolio recalculation. So the project will remain in - or out of - the portfolio until something else triggers a recalculation.