我可以将笑话代码覆盖率添加到来自管道的 Bitbucket 拉取请求中的报告吗

Posted

技术标签:

【中文标题】我可以将笑话代码覆盖率添加到来自管道的 Bitbucket 拉取请求中的报告吗【英文标题】:Can I add jest code coverage to Reports in Bitbucket Pull Requests from Pipelines 【发布时间】:2020-08-17 10:37:47 【问题描述】:

我有一个简单的 Bitbucket Pipelines 配置:

image: node:12.16.3

pipelines:
  pull-requests:
    '**':
      - step:
          caches:
            - node
          script:
            - yarn install
            - yarn test

test 脚本从 Jest (jest --coverage) 生成代码覆盖率。

我已尝试通过在- yarn test 之后添加以下行来将此覆盖率数据发送到报告 API(请注意,这是从 Bitbuck 文档复制的示例代码,我尚未将其更新为特定于我的数据因为我想让配置有效,然后再试图弄清楚到底需要对数据做什么)

- curl --request PUT 'https://api.bitbucket.org/2.0/repositories/<username>/<reposity-name>/commit/<commit-hash>/reports/mySystem-001' \
  --header 'Content-Type: application/json' \
  --data-raw '
  "title": "Security scan report",
  "details": "This pull request introduces 10 new dependency vulnerabilities.",
  "report_type": "SECURITY",
  "reporter": "mySystem",
  "link": "http://www.mySystem.com/reports/001",
  "result": "FAILED",
  "data": [
  
    "title": "Duration (seconds)",
    "type": "DURATION",
    "value": 14
  ,
  
    "title": "Safe to merge?",
    "type": "BOOLEAN",
    "value": false
  
  ]
'

Bitbucket 一直告诉我我的配置文件无效 - 即使我直接从他们的文档页面复制了这段代码。

是否可以将此代码覆盖率数据发送到相关拉取请求的 Bitbucket 报告 API?如果是这样,我该如何制作 Pipelines yaml 条目?

【问题讨论】:

嗨,你找到解决办法了吗? 【参考方案1】:

如果您使用的是 Bitbucket 云或 Bitbucket 服务器,情况会有所不同。

在 Bitbucket 云上,您需要:

使用本地代理绕过 REST API 上的身份验证。 将 url 方案从 https 更改为 http。 在端点中设置一些参数,这些参数来自 bitbucket 默认环境变量,例如 BITBUCKET_REPO_OWNERBITBUCKET_REPO_SLUGBITBUCKET_COMMIT。 改编来自 bitbucket 的示例应如下所示:
curl --proxy 'http://localhost:29418' \
-X PUT "http://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/commit/$BITBUCKET_COMMIT/reports/mySystem-001 \
-H 'Content-Type: application/json' \
-d '
  "title": "Security scan report",
  "details": "This pull request introduces 10 new dependency vulnerabilities.",
  "report_type": "SECURITY",
  "reporter": "mySystem",
  "link": "http://www.mySystem.com/reports/001",
  "result": "FAILED",
  "data": [
  
    "title": "Duration (seconds)",
    "type": "DURATION",
    "value": 14
  ,
  
    "title": "Safe to merge?",
    "type": "BOOLEAN",
    "value": false
  
  ]
'
Reference Link
Authentication https://support.atlassian.com/bitbucket-cloud/docs/code-insights/#Authentication
Default pipeline env https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Default-variables
Report Creation API https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commit/%7Bcommit%7D/reports/%7BreportId%7D#put

【讨论】:

【参考方案2】:

Jest 代码覆盖率报告格式不同,需要转换成这种格式:


  "files": [
    
      "path": "backend/src/app.ts",
      "coverage": "C:51,52;P:53;U:54,55"
    
  ]

您使用的 API 端点似乎也不正确。它应该是插件文档中提到的 POST 请求:

https://bitbucket.org/atlassian/bitbucket-code-coverage/src/master/code-coverage-plugin/README.md

【讨论】:

这究竟是什么格式?这是专有格式还是某种标准格式?【参考方案3】:

您好,请使用此管道

clone:
  depth: full

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarCloud artifacts will speed up your build
  steps:
    - step: &sonarcloud
        name: Build, test and analyze on SonarCloud
        caches:
          - node
          - sonar
        script:
          - pipe: sonarsource/sonarcloud-scan:1.2.0
            variables:
              SONAR_TOKEN: $SONAR_TOKEN
              EXTRA_ARGS: '-Dsonar.source=src -Dsonar.exclusions=android/**/*,ios/**,node_modules/** -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info'
pipelines:
  pull-requests:
    '**':
      - step:
          image: node:10.15.0
          caches:
            - node
          script:
            - npm install -g yarn
            - yarn install
            - npm run lint
            - npm run test
          artifacts:
            - coverage/**
      - step: *sonarcloud
  branches:
    master:
      - step:
          image: node:10.15.0
          size: 2x
          caches:
            - node
          script:
            - npm install -g yarn
            - yarn install && npx jetify
            - npm run lint
            - npm run test
          artifacts:
            - coverage/**
      - step: *sonarcloud
      - step:
          image: piotrovskyi/react-native-fastlane
          caches:
            - node
          size: 2x
          script:
            - git remote set-url origin $BITBUCKET_GIT_SSH_ORIGIN
            - npm install -g yarn
            - yarn install && npx jetify
            - bundle install
            - npm run bump-patch
            - bundle exec fastlane android _tag
    staging:
      - step:
          image: node:10.15.0
          caches:
            - node
          script:
            - npm install -g yarn
            - yarn install && npx jetify
            - npm run lint
            - npm run test
          artifacts:
            - coverage/**
      - step: *sonarcloud
      - step:
          image: piotrovskyi/react-native-fastlane
          caches:
            - node
          size: 2x
          script:
            - git remote set-url origin $BITBUCKET_GIT_SSH_ORIGIN
            - npm install -g yarn
            - yarn install && npx jetify
            - bundle install
            - npm run bump-patch
            - bundle exec fastlane android generate_apk_prerelease
            - bundle exec fastlane android upload_apk_appcenter_staging token:$APPCENTER_RELEASE_STAGING slackUrl:$SLACK
            - bundle exec fastlane android upload_apk_appcenter_sandbox token:$APPCENTER_RELEASE_SANDBOX slackUrl:$SLACK
    sandbox:
      - step:
          image: node:10.15.0
          caches:
            - node
          script:
            - npm install -g yarn
            - yarn install && npx jetify
            - npm run lint
            - npm run test
          artifacts:
            - coverage/**
      - step: *sonarcloud
      - step:
          image: piotrovskyi/react-native-fastlane
          caches:
            - node
          size: 2x
          script:
            - git remote set-url origin $BITBUCKET_GIT_SSH_ORIGIN
            - npm install -g yarn
            - yarn install && npx jetify
            - bundle install
            - npm run bump-patch
            - bundle exec fastlane android generate_apk_prerelease
            - bundle exec fastlane android upload_apk_appcenter_staging token:$APPCENTER_RELEASE_STAGING slackUrl:$SLACK
            - bundle exec fastlane android upload_apk_appcenter_sandbox token:$APPCENTER_RELEASE_SANDBOX slackUrl:$SLACK

【讨论】:

以上是关于我可以将笑话代码覆盖率添加到来自管道的 Bitbucket 拉取请求中的报告吗的主要内容,如果未能解决你的问题,请参考以下文章

.NET 构建管道中的静态代码分析

使用 docker 将代码覆盖率结果添加到 Azure Pipelines for .NET6 Web API

笑话:自动从测试文件中收集覆盖率

如何检查控制台中的笑话覆盖率?

具有代码质量的 Azure Devops 拉取请求修饰

我们可以将屏幕截图作为附件添加到 Azure 管道中的测试结果吗