Setting up the Code Coverage in CodeScene (On-prem)

CodeScene does not generate code coverage data itself but provides a platform to visualize and explore your project’s results. Code coverage data can be produced using various tools that are typically language-specific. These tools generate coverage data in different file formats. Ensure that the tool you choose outputs coverage data in a format supported by CodeScene.

Uploading the Code Coverage data using the upload scripts

 
 

Steps for uploading coverage data for Windows

  1. Download a script for Windows from the CodeScene documentation.  To upload the data, you should use our script as it is, and pass the correct parameters.
    The script should be run from the same folder where it was downloaded. So you either do that or put the full path to the script when running it.
  2. Set CS_HOST, CS_USER, CS_PASSWORD, and CS_TOKEN environment variables.
  3. Use the following command (Windows PowerShell):
upload.ps1 -repopath "value" -file "absolute-path-to-the-code-coverage-file" [-subpath "value"] [-format "value"] [-metric "value"]

where the parameters are:
    • cshost - if not specified will try to get the value from environment variable CS_HOST, if not found will upload to CodeScene Cloud.
    • repopath - absolute path to the cloned git repository, example: /home/myuser/repos/codescene
      • The -repopath should be from the computer where the repository is located. The script requires access to the repository to compute the COMMIT_SHA and fetch the origin URL.

        Usually, you need to clone your repository locally and execute a task that computes the code coverage. The result is then uploaded to CodeScene, so the script needs to know the path to the repo and the path to the coverage file.

        This is an example of the correct path: -repopath "c:\tmp\myproject"
    • subpath - in case you have a monorepo with more than one project you can set the subpath, ex: /frontend
    • format - the code-coverage file format, if not specified the default is: lcov
    • metric - the code-coverage metric, if not specified the default is: line-coverage
    • file - the absolute path to the file containing code-coverage information
An example for uploading code coverage data for On-prem:
#Example
#---------------------
#upload code coverage data on-prem

$Env:CS_HOST="http://localhost:3003"
$Env:CS_USERNAME="test"
$Env:CS_PASSWORD="test"
upload.ps1 -repopath "c:\tmp\myproject" -subpath "\frontend" -file c:\tmp\coverage/lcov.info
 
 
The above would be enough to upload the data. The trigger of an analysis comes after and also can be done via UI at least for the first test.
An example for triggering the analysis:
#trigger new analysis on-prem for project with id 123
$Env:PROJECT_ID=123
$CREDENTIALS = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$Env:CS_USERNAME`:$Env:CS_PASSWORD"))
$HEADER = @{
"Authorization"="Basic $CREDENTIALS"
"Content-Type"="application/json"
}
curl -Method POST -Headers $HEADER $Env:CS_HOST/api/v2/projects/$Env:PROJECT_ID/run-analysis -Body ""
 
The Code Coverage endpoints can be found on the following link: Code Coverage endpoints.