Search code examples
javascriptpythongithub-actionscode-coveragesonarcloud

Merge python and javascript coverage to appear in one SonarCloud key


I've got a monorepo that contains a Python Flask API with a Svelte Javascript frontend.

I'm trying to get the coverage into Sonarcloud. It works when I set it up only for Python OR Javascript, but not both at the same time. I saw that the recommended solution is to provide two Sonarcloud keys (one for Python, one for Javascript) and create a sonar organization, as described here for instance. However, I would like to be able to see the coverage of the whole project without separating it into two instances.

Is there a way to merge python and javascript coverage as one lcov (or anything else that can be used by Sonarcloud) file ?

Here is my current github actions workflow (the python and javascript reports generation works fine, but the aggregation does not) : Github-Action

I'm trying to get this branch to provide both coverages at once.

As you can see, I tried to merge them by generating both coverage reports files as lcov, and use lcov to merge them as one report. The problem is that no coverage is returned (SonarCloud displays a coverage of 0%).

Thanks a lot !


Solution

  • It's finally working ! Here's how I did it :

    Specify multiple report paths in sonar-project.properties instead of a single file

    You don't need to merge your report files as a single one. I tried it, and even if it works because both Python and Javascript coverages can be generated in lcov format (which, by the way, is not the case for every language), I wouldn't really recommend it as it tends to make the pipeline harder to read. What you can do instead is to specify the path of your multiple report files in sonar-project.properties, as I did in this commit. You can specify a comma-separated list of files for a single language if you need to.

    Now, it seems like SonarCloud behaves differently depending on your language. At this point, it recognized my Javascript code coverage, but not my Python coverage. The problem here is that the coverage files refer to the code files as "src/yourfilename" instead of "api/src/yourfilename" or "front/src/yourfilename", so SonarCloud gets confused. I think I got lucky that it recognized my Javascript code coverage in the first place.

    Use regexes to update the source file paths in your reports

    What I did was to update my build.yml file to use two regexes (line 35 and 48). These regexes modify the files references so that, instead of having "src/controller.py" for instance, it had "pokeml-api/src/controller.py (because "pokeml-api" is the name of my folder containing my Python code). Now SonarCloud is not confused anymore, and it works !