Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add jest code coverage to Reports in Bitbucket Pull Requests from Pipelines

I have a simple Bitbucket Pipelines configuration:

image: node:12.16.3

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

The test script produces code coverage from Jest (jest --coverage).

I have tried to send this coverage data to the Reports API by adding the following line after - yarn test (please note, this is example code copied from Bitbuck docs, I haven't updated it to be specific to my data yet as I want to get the config valid, before trying to figure out what exactly needs to do into the data)

- 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 keeps telling me that my configuration file is invalid - even though I've copied this code directly from their docs page.

Is it possible to send this code coverage data to the Bitbucket Reports API for the associated pull request? And if so, how do I craft that Pipelines yaml entry?

like image 301
Denno Avatar asked Nov 27 '22 19:11

Denno


2 Answers

Jest code coverage reports are different format, you need to convert them into this format:

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

The API endpoint you are using also doesn't seem right. It should be a POST request as mentioned in the plugin docs:

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

like image 131
siddiq Avatar answered Dec 20 '22 17:12

siddiq


It's a bit different if you're using Bitbucket cloud or Bitbucket server.

On Bitbucket cloud, you need to:

  • Use the local proxy in order to bypass authentication on the REST API.
  • Change the url scheme from https to http.
  • Set some parameters in the endpoint which you get from bitbucket default environments variables such as BITBUCKET_REPO_OWNER, BITBUCKET_REPO_SLUG and BITBUCKET_COMMIT.

Adapting the example from bitbucket should look something like this:

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
like image 42
Michael Avatar answered Dec 20 '22 19:12

Michael