GitLab Pull Request Decoration is not working

Hi @jep

Welcome to our community forum!
Pull Request decoration is available with a commercial license for SonarQube: Developer, Enterprise or Data Center. It’s not available in the Community Edition of SonarQube.
And, if you want to try the Merge Request decoration with your Gitlab instance + Jenkins CI, you would need to upgrade to SonarQube Developer (at least) 8.2 (at least). - I would recommend to go directly to our latest version, 8.5.

HTH,
Carine

@Carine_Bayon thanks for your response. I think I may left out some pertinent information. In addition to using SonarQube 8.1 Community Edition, I’m also using the sonarqube-community-branch plugin, which does provide PR decoration.

As a bit of constructive feedback, it’s very frustrating when one feels there’s no path forward and has to create an account and request assistance in a forum like this. Not only does it take time, but there’s no guarantee that a response will be received, but sometime’s there’s no other option. What’s even more frustrating, however, is when a response is received that really just ignores what was asked.

I understand support is only provided for SQ products, and my use of the community plugin falls outside that scope, but my actual question was could someone tell me which specific variables need to exist in Gitlab CI. I was somewhat relieved to see I received a response, but of course it was really just an upsell that ignored the question itself. I’m not sure if it was intentional or not…I do see a typo in my original post, so I’ll just err on the side of I failed to communicate my request properly.

Anyway, I was fortunate enough to figure it out. If anyone else is using Jenkins as an external CI and want to use SonarQube 8.1 CE to provide PR decoration via the sonarqube-community-branch plugin, you’ll need to ensure the following environment variables exist on the build node:

    GITLAB_CI = true
    CI_API_V4_URL = 'https://your.gitlab/api/v4'
    CI_MERGE_REQUEST_PROJECT_URL = 'https://your.gitlab/your-group/your-project'
    CI_PROJECT_PATH = 'your-group/your-project'
    CI_MERGE_REQUEST_SOURCE_BRANCH_NAME = ''
    CI_MERGE_REQUEST_TARGET_BRANCH_NAME = ''
    CI_COMMIT_REF_NAME = ''
    CI_MERGE_REQUEST_IID = ''
    CI_PROJECT_ID = '1'

The CI_PROJECT_ID can be found by visiting the project in Gitlab, viewing the page source, and searching for the string project_id.

Additionally, if you’re using the Jenkins gitlab-plugin as I am, when a Merge Request triggeres a build, you’ll have to assigne gitlabSourceBranch, gitlabTargetBranch and gitlabMergeRequestIid as so:

    CI_MERGE_REQUEST_SOURCE_BRANCH_NAME = gitlabSourceBranch
    CI_COMMIT_REF_NAME = gitlabSourceBranch
    CI_MERGE_REQUEST_TARGET_BRANCH_NAME = gitlabTargetBranch
    CI_MERGE_REQUEST_IID = gitlabMergeRequestIid

Finally, when calling sonar-scanner, you’ll have to also pass sonar.ci.autoconfig.disabled=true otherwise SQ will detect both Gitlab and Jenkins and will produce an error. Here is what my sonar-scanner looks like in Jenkins:

withSonarQubeEnv(credentialsId: 'sonarqube-token') {
        sh "sonar-scanner \
            -Dsonar.ci.autoconfig.disabled=true \
            -Dsonar.login=${SONAR_AUTH_TOKEN} \
            -Dsonar.qualitygate.wait=false \
            -Dsonar.projectName=MyProject \
            -Dsonar.pullrequest.key=${CI_MERGE_REQUEST_IID} \
            -Dsonar.pullrequest.base=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} \
            -Dsonar.pullrequest.branch=${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} \
            -Dsonar.projectKey=my-project-key"
    }

I hope that helps!

Edit: Also, a shoutout to user @skorhone who led me down the right path with this post in the forum.