gabrie-allaigre / sonar-gitlab-plugin

Add to each commit GitLab in a global commentary on the new anomalies added by this commit and add comment lines of modified files
GNU Lesser General Public License v3.0
713 stars 208 forks source link

Project doesn't exist on the server. All issues will be marked as 'new'. #100

Open jakub-bochenski opened 6 years ago

jakub-bochenski commented 6 years ago

Sonar will emit this warning if you don't have a "default" (empty) branch project on your server.

We hit this because we run the "main" sonar analysis with sonar.branch=integration

To avoid confusion I suggest adding -Dsonar.branch=${gitlabTargetBranch} to the sample (Issues Mode) configurations.

This way the report will always show new issues relative to the branch the merge request is targeting.

gabrie-allaigre commented 6 years ago

Hi, Thank you for the feedback. I think it should not be common to change the main branch. Is ${gitlabTargetBranch} equal to $CI_BUILD_REF_NAME?

jakub-bochenski commented 6 years ago

Yeah, we use https://github.com/jenkinsci/gitlab-plugin/ to run the builds not gitlab CI

${gitlabTargetBranch} is not $CI_BUILD_REF_NAME (this is the source branch of the MR). It's the target branch of the merge request.

Now I was surprised to learn this information is not available on Gitlab CI (e.g. https://gitlab.com/gitlab-org/gitlab-ce/issues/30439). If there is no way to retrieve it on Gitlab CI (except for calling Gitlab API) it's pointless to include this in example configuration.

I can send you some configuration examples for jenkins gitlab plugin if you want to include them :)

gabrie-allaigre commented 6 years ago

GitLab-CI work only with commit not with MR

Yes, I want a example with Jenkins Thanks

jakub-bochenski commented 6 years ago

This is a sample job-dsl config. It's using gitlab-plugin. You need to configure Jenkins-Gitlab integration as per plugin doc's.

It assumes there is a global secret GITLAB_TOKEN configured via the mask-passwords plugin.

I haven't run an actual build with this setup yet, as out actual setup is more complex.

job('sonar-gitlab-integration') {
 description 'Example job to show Sonar issues on Gitlab MR'
 scm {
  git {
   remote {
    url 'git@github.com:gabrie-allaigre/sonar-gitlab-plugin.git'
    name 'origin'
   }
   branch '${gitlabSourceBranch}'
   extensions {
    mergeOptions {
     branch '${gitlabTargetBranch}'
     remote 'origin'
    }
   }
  }
 }
 triggers { gitlab { triggerOnMergeRequest true } }
 wrappers { maskPasswords() }
 steps {
     shell '''
        echo "SONAR_GITLAB_COMMIT_SHA=$(git log --pretty=format:%H origin/master..$GIT_COMMIT | tr '\\n' ',')" > .sonar-envs
     '''
     environmentVariables { propertiesFile '.sonar-envs' }
     maven {
        goals 'clean package'
        goals 'sonar:sonar'
        goals '''
            -Dsonar.analysis.mode=preview
            -Dsonar.branch=${gitlabTargetBranch} 
            -Dsonar.gitlab.commit_sha=$SONAR_GITLAB_COMMIT_SHA
            -Dsonar.gitlab.user_token=$GITLAB_TOKEN
            -Dsonar.gitlab.ref_name=${gitlabSourceBranch}
            -Dsonar.gitlab.project_id=${gitlabMergeRequestTargetProjectId}
            -Dsonar.gitlab.unique_issue_per_inline=true
        '''
     }
 }
 publishers {
    gitLabCommitStatusPublisher {
        name 'jenkins'
        markUnstableAsSuccess false
    }
 }
}
jakub-bochenski commented 6 years ago

When I find some more time I will test this and also add a sample for Jenkins Pipeline