Why do my qualitygate conditions automatically get prefixed with new_ and how to prevent this?

Hi all,

I try to ex-/import quality gate conditions from multiple instances to a single instance. Therefore I have to rely on a central configuration as code source for our new sonarqube infrastructure.
However… testing a bit with sonarqube’s API I got some question marks. Some have been already been answered, but I need some more clarification on that.
I am no developer and thus have a hard time understanding coding related doc’s: MetricDefinitions

I also have a hard time finding a way to safely remove the right condition based on the ID, I have to filter out by myself…

Given one of my qualitygates from an old sonarqube server instance:

    "id": "AWqRA4567xPW4pTcJQrb",
    "name": "Java Related",
    "conditions": [
        {
            "id": "AYmr4TXA_R_dyf0mP-ZE",
            "metric": "new_security_rating",
            "op": "GT",
            "error": "1"
        },
        {
            "id": "AYmr4TXA_R_dyf0mP-ZF",
            "metric": "new_reliability_rating",
            "op": "GT",
            "error": "1"
        },
        {
            "id": "AYmr4TXA_R_dyf0mP-ZG",
            "metric": "new_maintainability_rating",
            "op": "GT",
            "error": "1"
        },
        {
            "id": "AYmr4TXA_R_dyf0mP-ZD",
            "metric": "security_rating",
            "op": "GT",
            "error": "4"
        },
        {
            "id": "AYmr4TXA_R_dyf0mP-Y2",
            "metric": "coverage",
            "op": "LT",
            "error": "10"
        },
        {
            "id": "AYmr4TXA_R_dyf0mP-Y7",
            "metric": "duplicated_lines_density",
            "op": "GT",
            "error": "5"
        },
        {
            "id": "AYmr4TXA_R_dyf0mP-Y8",
            "metric": "reliability_rating",
            "op": "GT",
            "error": "4"

I use these conditions and create a dict for my ansible playbook.

I used the metric names that are shown in the UI. I think when I add ‘new_’ to these conditions, they might get double prefixed.

  - qualitygate_name: Java Related
    operatoren:
      - metric: security_rating
        operator: GT
        error: 1
        state: present
      - metric: reliability_rating
        operator: GT
        error: 1
        state: present
      - metric: maintainability_rating
        operator: GT
        error: 1
        state: present
      - metric: security_rating
        operator: GT
        error: 4
        state: present
      - metric: coverage
        operator: LT
        error: 10
        state: present
      - metric: duplicated_lines_density
        operator: GT
        error: 5
        state: present
      - metric: reliability_rating
        operator: GT
        error: 4
        state: present

With that list, I create my quality gate and conditions and receive that:

    "msg": [
        {
            "error": "1",
            "id": "AZQX1yyeOyQ12MM6ybmX",
            "metric": "new_security_rating",
            "op": "GT"
        },
        {
            "error": "1",
            "id": "AZQX1yyjOyQ12MM6ybmY",
            "metric": "new_reliability_rating",
            "op": "GT"
        },
        {
            "error": "1",
            "id": "AZQX1yylOyQ12MM6ybmZ",
            "metric": "new_maintainability_rating",
            "op": "GT"
        },
        {
            "error": "80",
            "id": "AZQX1yyoOyQ12MM6ybma",
            "metric": "new_coverage",
            "op": "LT"
        },
        {
            "error": "3",
            "id": "AZQX1yyvOyQ12MM6ybmb",
            "metric": "new_duplicated_lines_density",
            "op": "GT"
        },
        {
            "error": "100",
            "id": "AZQX1yyxOyQ12MM6ybmc",
            "metric": "new_security_hotspots_reviewed",
            "op": "LT"
        }
    ]
}

Hey @YourDudeness

What’s going on is that when you create a new Quality Gate, it is automatically populated with the default conditions as the response here.

What this tells me is that the conditions you’re specifying here are probably never being applied

I think this means that the conditions you’re specifying are never being applied to the Quality Gate, not that they are being automatically prefixed with new_. You can test this by adding a completely new condition not present in the default conditions (say, lines_to_cover, as an example)

Oh, I see what you mean. So that mean’s I’ll have to remove all conditions first and then I can try to apply my conditions, right?

For your desired final state, I believe so. However, for just testing the addition of conditions to your Quality Gate, it’s probably not strictly necessary.

What I mean is – if you’ve defined 5 conditions to add to the QG, your automation should work whether the default conditions are added upon QG creation or not, right? I think something must not be right with how your automation calls POST api/qualitygates/create_condition

Basically I add em with that task after I create the qualitygate itself.

- name: Add Condition
  ansible.builtin.uri:
    url: "{{ protocol }}://{{ server_fqdn }}/api/qualitygates/create_condition"
    user: "{{ token }}"
    method: POST
    body_format: form-urlencoded
    body:
        error: "{{ item.1.error }}"
        metric: "{{ item.1.metric }}"
        gateName: "{{ item.0.qualitygate_name }}"
        op: "{{ item.1.operator }}"
  loop: "{{ qualitygate | subelements('operatoren', 'skip_missing=True') }}"
  when: item.1.state == "present"
  ignore_errors: true
  failed_when: _result_qualitygates.status != 200 and _result_qualitygates.status != 400
  register: _condition_result

What is the best practice to import qualitygates and conditions?

I’m not an Ansible user so I’m not sure I can help you out there. My point is that you should make sure this part (adding conditions) is actually working, putting the default conditions to the side for the moment (your initial assumption that your automation was adding the wrong conditions)

HI @Colin,

I see an idea here to improve quality gate management. We could imagine a mechanism similar to the quality profile through export, import XML.
In our case, we envision managing these XML files in source control.

@Bachri_Abdel This is definitely something we’ve considered before! SONAR-5766. We decided not to move forward, but that doesn’t mean we’ll never look at it again. Thanks for the feedback.

We currently have some technical difficulties in the office. That’s why I am currently unable to compare my configuration and results. This is just a lil beep to not close this topic.

This is a Community, not a service desk. We don’t close unsolved topics for inactivity (and only users, not moderators, can mark topics solved). :slight_smile: