groovydre:S134 FP with Jenkinsfile

  • What language is this for? Groovy
  • Which rule? S134
  • Why do you believe it’s a false-positive/false-negative? Pretty much any “declarative pipeline” Jenkinsfile has four levels of nesting before reaching the actual executable code, which is already more than the three allowed by the rule’s default configuration, and it is desirable to allow one or two levels within the executable code. A “scripted pipeline” Jenkinsfile starts out with two levels, but likely has more nested structures inside that.
  • Are you using
    SonarQube Cloud
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
    Sample declarative pipeline from the Jenkins documentation at Creating your first Pipeline :
pipeline {
    agent { docker { image 'maven:3.10.0-eclipse-temurin-21-alpine' } }
    stages {
        stage('build') {
            steps {
                sh 'mvn --version'
            }
        }
    }
}

Sample scripted pipeline:

node {
    stage('Build') {
        docker.image('maven:3.10.0-eclipse-temurin-21-alpine').inside {
            sh 'mvn --version'
        }
    }
}

I’m not sure about the best solution, but I’d like to write Jenkinsfiles without receiving warnings from S134, without modifying any Sonar configuration.

I agree. Same problem on our side.

The rule is talking about Control flow statements “if”, “for”, “while”, “switch” and “try” but it counts “pipeline”, “stages”, “stage”, “steps”.. as well as “node”, “timeout”, “stage”, … and none of these is really increasing complexity.

The behavior leads to having this issue in every pipeline.

Hello @jilles-sg and @Wiebke,

Thank you for reaching out.

I agree with both of you, this rule should not count expected Jenkins nested blocks.

I have added a ticket in our internal backlog to fix it. It should be available in the next release of the analyzer.

Have a good day!

Hi @sebastien.marichal ,

Thank you for taking this into account.

Will the solution be that the rule will ignore a fixed hardcoded set of expected blocks? Or will it be possible to at least add individual blocks for exclusion in some configuration field?

Option two would be preferred by us :slight_smile:

Regards, Wiebke

Hello @Wiebke,

As for now, we have a set of hardcoded expected blocks.

Your suggestion seems legitimate, could you tell me a bit more why a set of hardcoded blocks won’t suit your needs?

I am not fluent in Jenkins and I would to understand your needs regarding this to ensure we can provide the best experience for you.

Thanks,

Hello @sebastien.marichal

I just thought that it might be a bit more comfortable if we could adapt the exception list to our needs. E.g. if new expected blocks are introduced by a jenkins update and the installed sonar version does not support it yet.

There might also be examples/reasons that we want to exclude additional statements from the complexity check which are not keywords from Jenkins.

Regards, Wiebke