首页 > 解决方案 > 确认 SonarQube 结果适用于主分支

问题描述

因为不知道Sonar是不是在做奇怪的事情,我只是想确认一下它确实是在扫描master分支而不是feature分支。

在此处输入图像描述

我团队中的人说 SonarCube 对功能分支进行了测试。据我所知,它在主分支上运行了测试。我们正在使用 Sonar 7.9.1,我的意图是针对功能分支运行。

詹金斯文件片段:

            stage('Send sonarqube reports') {
                sh """
                    sonar-scanner -Dsonar.host.url=${SONAR_CIRRUS} -Dsonar.java.libraries=/home/bcjenkins/.m2/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar
                """
                JSON_STRING_SONAR_METRICS = sh (
                    script: "curl -X GET -H \'Content-Type: application/json\' -H \'Accept: application/octet-stream\' \'${SONAR_CIRRUS}/api/qualitygates/project_status?projectKey=cirrus-bluecost-ssc-file-generator\'",
                    returnStdout: true
                ).trim()
                println "The JSON_STRING_SONAR_METRICS=${JSON_STRING_SONAR_METRICS}";
                def sonarQube_Results = readJSON text: "${JSON_STRING_SONAR_METRICS}";
                def CodeCoverageEntryThreshold = sonarQube_Results.projectStatus.status;

                if(CodeCoverageEntryThreshold == "ERROR"){
                    error("[cirrus-bluecost-ssc-file-generator] Code qualitygates did not PASSED. Please check the results in our Sonarqube server.\n" +
                    "\tTo view the sonarqube results-> ${SONAR_CIRRUS}/dashboard?id=cirrus-bluecost-ssc-file-generator\n")
                }
            
                else if(CodeCoverageEntryThreshold == "FAILED"){
                    error("[cirrus-bluecost-ssc-file-generator] Code qualitygates did not PASSED. Please check the results in our Sonarqube server.\n" +
                    "\tTo view the sonarqube results-> ${SONAR_CIRRUS}/dashboard?id=cirrus-bluecost-ssc-file-generator\n")
                }

                else {
                    CodeCoverage_resultMsg = "[cirrus-bluecost-ssc-file-generator] Code qualitygates PASSED. Please check the results in our Sonarqube server.\n" +
                    "\tTo view the sonarqube results-> ${SONAR_CIRRUS}/dashboard?id=cirrus-bluecost-ssc-file-generator\n";
                    slackSend channel: "${SONAR_SLACK_CHANNEL}", color: 'good', failOnError: false, message: "${CodeCoverage_resultMsg}", teamDomain: 'ibm-ic2e-sprint', tokenCredentialId: SLACK_DOMAIN_CREDENTIALS
                }
            }

标签: jenkinssonarqube

解决方案


关于https://docs.sonarqube.org/latest/branches/overview/

主/主分支

这是默认分支,通常对应于为下一个版本开发的内容。此分支通常在开发团队中称为“主”或“头”,并在未提供特定分支参数时进行分析。SonarQube 将此分支标记为 Main Branch,并且在 Community Edition 中,这是您可以分析的唯一分支

如果我们使用SonarQube 社区版,我们只能分析mainormater分支。

如果我们需要分析其他分支,我们应该至少使用SonarQube 开发版,如下所述:-

分支分析

从Developer Edition开始提供分支分析。

编辑1:

SonarQube提供SonarLint ( https://www.sonarlint.org/ )作为 IDE 扩展,包括EclipseIntelliJ IDEA等。

我们能够将我们的 IDE 配置为连接到内部SonarQube服务器,并使用此插件在开发人员机器上执行本地分析,然后将它们合并到主分支或主分支。

编辑2:

使用Eclipse配置SonarLint的示例位于https://github.com/SonarSource/sonarlint-eclipse/wiki/Connected-Mode


推荐阅读