You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bitrise集成SonarCloud:PR代码覆盖率显示0新行求助

iOS项目Pull Request中SonarCloud显示0 New Lines问题排查

我有一个iOS项目,主分支的SonarCloud代码覆盖率显示正常,但Pull Request中显示0 New Lines。以下是我在Bitrise中针对PR的执行步骤:

$BITRISE_SOURCE_DIR/xccov-to-sonarqube-generic.sh $BITRISE_XCRESULT_PATH > Coverage.xml

sonar-scanner -Dsonar.projectKey={porjectKey} \
-Dsonar.organization={projectOrgatization} \
-Dsonar.pullrequest.base=$BITRISEIO_GIT_BRANCH_DEST \
-Dsonar.pullrequest.branch=$BITRISE_GIT_BRANCH \
-Dsonar.pullrequest.key=$PULL_REQUEST_ID \
-Dsonar.host.url=https://sonarcloud.io \
-Dsonar.coverageReportPaths=Coverage.xml \
-Dsonar.language=swift \
-Dsonar.sources=App,ImageShare \
-Dsonar.tests=Tests \
-Dsonar.c.file.suffixes=- \
-Dsonar.cpp.file.suffixes=- \
-Dsonar.objc.file.suffixes=- \

附上SonarScanner日志:

INFO: Scanner configuration file: /opt/homebrew/Cellar/sonar-scanner/5.0.1.3006/libexec/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 5.0.1.3006
INFO: Java 20.0.2 Homebrew (64-bit)
INFO: Mac OS X 13.5 aarch64
INFO: User cache: /Users/vagrant/.sonar/cache
INFO: Analyzing on SonarCloud
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Load global settings
INFO: Load global settings (done) | time=268ms
INFO: Server id: 1BD809FA-AWHW8ct9-T_TB3XqouNu
INFO: User cache: /Users/vagrant/.sonar/cache
INFO: Loading required plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=586ms
INFO: Load/download plugins
INFO: Load/download plugins (done) | time=819ms
INFO: Found an active CI vendor: 'Bitrise'
INFO: Load project settings for component key: 'ProjectKey'
INFO: Load project settings for component key: 'ProjectKey' (done) | time=216ms
INFO: Process project properties
INFO: Project key: ProjectKey
INFO: Base dir: /Users/vagrant/git
INFO: Working dir: /Users/vagrant/git/.scannerwork
INFO: Load project branches
INFO: Load project branches (done) | time=235ms
INFO: Check ALM binding of project 'ProjectKey'
INFO: Detected project binding: BOUND
INFO: Check ALM binding of project 'ProjectKey' (done) | time=231ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=553ms
INFO: Load branch configuration
INFO: Load branch configuration (done) | time=1ms
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=284ms
INFO: Load active rules
INFO: Load active rules (done) | time=12355ms
INFO: Organization key: OrganizationKey
INFO: Branch name: test-coverage, type: short-lived
INFO: Preprocessing files...
INFO: 2 languages detected in 1341 preprocessed files
INFO: 0 files ignored because of inclusion/exclusion patterns
INFO: 574 files ignored because of scm ignore settings
INFO: Loading plugins for detected languages
INFO: Load/download plugins
INFO: Load/download plugins (done) | time=32ms
INFO: Load project repositories
INFO: Load project repositories (done) | time=1005ms
INFO: SCM collecting changed files in the branch
INFO: SCM collecting changed files in the branch (done) | time=226ms
INFO: Indexing files...
INFO: Project configuration:
INFO:   Excluded sources: **/build-wrapper-dump.json
INFO:   Excluded sources for duplication: Tests
INFO: 1341 files indexed
INFO: Quality profile for json: Sonar way
INFO: Quality profile for swift: Sonar way
INFO: ------------- Run sensors on module ProjectKey
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=205ms
INFO: Sensor cache enabled
INFO: Load sensor cache
INFO: Load sensor cache (19 KB) | time=1394ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=0ms
INFO: Sensor IaC CloudFormation Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC CloudFormation Sensor [iac] (done) | time=87ms
INFO: Sensor IaC AzureResourceManager Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC AzureResourceManager Sensor [iac] (done) | time=61ms
INFO: Sensor TextAndSecretsSensor [text]
INFO: 954 source files to be analyzed
INFO: 954/954 source files have been analyzed
INFO: Sensor TextAndSecretsSensor [text] (done) | time=3058ms
INFO: Sensor Swift Code Quality and Security [swift]
INFO: 590 source files to be analyzed
INFO: 590/590 source files have been analyzed
INFO: Sensor Swift Code Quality and Security [swift] (done) | time=8211ms
INFO: Sensor IaC Docker Sensor [iac]
INFO: 0 source files to be analyzed
INFO: 0/0 source files have been analyzed
INFO: Sensor IaC Docker Sensor [iac] (done) | time=24ms
INFO: Sensor Generic Coverage Report
INFO: Parsing /Users/vagrant/git/cov.xml
INFO: Imported coverage data for 494 files
INFO: Sensor Generic Coverage Report (done) | time=265ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=11ms
INFO: CPD Executor 127 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 463 files
INFO: CPD Executor CPD calculation finished (done) | time=70ms
INFO: SCM writing changed lines
INFO: SCM writing changed lines (done) | time=15ms
INFO: Analysis report generated in 242ms, dir size=734 KB
INFO: Analysis report compressed in 456ms, zip size=613 KB
INFO: Analysis report uploaded in 1437ms
INFO: ANALYSIS SUCCESSFUL, you can find the results at: https://sonarcloud.io/dashboard?id=ProjectKey&branch=test-coverage&resolved=false
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at https://sonarcloud.io/api/ce/task?id=taskId
INFO: Analysis total time: 36.084 s

点击任务链接https://sonarcloud.io/api/ce/task?id=taskId时,返回:

{"errors":[{"msg":"Project doesn\u0027t exist"}]}

问题排查与解决建议

1. 覆盖率报告文件名不匹配

从日志可以看到,SonarScanner尝试解析的是/Users/vagrant/git/cov.xml,但Bitrise步骤中生成的是Coverage.xml。虽然MacOS默认大小写不敏感,但SonarScanner在处理文件路径时可能存在严格匹配问题,导致实际导入的覆盖率数据和预期不符。

解决步骤:

  • 修改生成报告的命令,统一文件名:$BITRISE_SOURCE_DIR/xccov-to-sonarqube-generic.sh $BITRISE_XCRESULT_PATH > cov.xml
  • 或者修改sonar-scanner参数:-Dsonar.coverageReportPaths=cov.xml

2. PR分支参数验证

检查PR相关参数是否正确传递给SonarScanner:

  • 在Bitrise步骤中添加变量打印,确认参数值:
    echo "PR base branch: $BITRISEIO_GIT_BRANCH_DEST"
    echo "PR branch: $BITRISE_GIT_BRANCH"
    echo "PR ID: $PULL_REQUEST_ID"
    
  • 确保BITRISEIO_GIT_BRANCH_DEST是主分支(如main或master),BITRISE_GIT_BRANCH是当前PR的分支名称,参数无空值或错误值。

3. SCM变更文件检测问题

SonarCloud依赖SCM(Git)识别PR中的变更文件,如果Bitrise的Git克隆是浅克隆(默认可能只拉取当前分支),会导致SCM无法对比主分支和PR分支的差异,从而无法识别新增行。

解决步骤:

  • 在Bitrise的Git Clone步骤中,添加--depth=0参数,拉取完整的仓库历史和所有分支。

4. SonarCloud项目绑定与权限问题

任务链接返回Project doesn't exist,说明存在项目权限或绑定问题:

  • 确认sonar.projectKey和sonar.organization参数填写正确,和SonarCloud中项目的Key一致。
  • 检查Bitrise中配置的SonarCloud Token是否有该项目的访问权限,Token需要具备项目分析权限。
  • 确认SonarCloud项目已正确绑定到对应的ALM(如GitHub/GitLab),绑定状态显示为BOUND(日志中已显示,但需验证绑定的项目是否正确)。

5. PR新增代码的测试覆盖验证

即使整体覆盖率正常,PR中的新增代码可能没有对应的测试用例,或者测试用例未在Bitrise中执行:

  • 检查PR中的新增/修改代码是否有对应的测试用例。
  • 确认Bitrise中执行了所有测试用例,生成的$BITRISE_XCRESULT_PATH包含这些测试的覆盖数据。可以通过本地执行测试并导出xcresult验证覆盖情况。

内容的提问来源于stack exchange,提问作者ChikabuZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 22:32:04