与主分支同步时,Sonarcloud 中未显示分支覆盖

Branch coverage is not shown in Sonarcloud when syncing with master branch

我正在尝试使用 jacoco 插件 gradle 和 运行 分析我的代码 sonarqube。我的测试是用Groovy写的。我已经使用 bitbucket 管道自动化了这个过程,所以每次我提交代码时,测试都是 运行,最后 jacoco 分析和 sonarqube 是 运行,将报告结果推送到 SonarCloud。显示覆盖率(以百分比表示,link 到 类)并与 dev 分支进行比较(我明确指定 dev 作为 SonarCloud 中的长期分支)。此外,还显示了总体覆盖范围(合并后)。

问题是,当我将 dev 合并到我的分支(其他东西被合并到 dev,所以我同步)时,分支覆盖显示为“-”,即为空。我似乎找不到问题,而不是猜测提交(来自将 dev 合并到我的分支中)有 2 个父项(先前的提交和另一个合并到 dev 中的短期分支),并且不知何故变得困惑。 在我提交了一些东西之后,即使是一行愚蠢的代码,分析也会再次正确显示。

我想知道是否有人解决了这个问题,或者知道为什么会这样。谢谢!

在 build.gradle 我添加了:

 plugins {
    id "org.springframework.boot" version "2.0.2.RELEASE"
    id "org.sonarqube" version "2.7.1"
    id "application"
    id "jacoco"
}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.3"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("$buildDir/jacocoHtml")
    }
}

sonarqube {
   properties {
        // properties related to credentials
    }
}

我的 bitbucket 管道文件是:

image: java:8

clone:
  depth: full  # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - gradle
          - sonar
        script:
          - ./gradlew clean test
          - ./gradlew jacocoTestReport
          - ./gradlew sonarqube
        artifacts:
          - build/libs/**

pipelines:
  default:
    - step: *build-test-sonarcloud
  pull-requests:
    '**':
      - step: *build-test-sonarcloud

在 build.gradle 中,指定 jacoco 和 jacocoTestReport 属性就足够了,如下所示:

jacoco {
    toolVersion = "0.8.3"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
        csv.enabled false
        html.destination file("$buildDir/customJacocoReportDir/test/html")
        xml.destination file("$buildDir/customJacocoReportDir/test/jacocoTestReport.xml")
    }
}

sonarqube {
    properties {
        // define your properties
        property "sonar.jacoco.reportPath", "$buildDir/jacoco/test.exec"
        property "sonar.coverage.jacoco.xmlReportPaths", "$buildDir/customJacocoReportDir/test/jacocoTestReport.xml"
    }
}

然后在 bitbucket-pipelines.yml 中,这样做:

image: java:8

clone:
  depth: full  # SonarCloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - gradle
          - sonar
        script:
          - ./gradlew clean test
          - ./gradlew jacocoTestReport
          - ./gradlew sonarqube
        artifacts:
          - build/libs/**

pipelines:
  default:
    - step: *build-test-sonarcloud