Pull request analysis missing code coverage

Using: Jenkins, GitHub, TypeScript, SonarScanner CLI.

We’re having issues getting code coverage on PRs. The coverage is never showing on PRs but works on the master branch. Also, there’s that message the branch or pullRequest parameter is missing showing up even if those properties are set.

Any idea what’s happening?

image

1 Like

You need to pass additional parameters when using the CLI. This is part of the script we use to wrap our calls to the sonar scanner:

if [[ $CHANGE_ID ]]; then
    # For PRs the head of the branch before the local merge is what we are looking for.
    git log HEAD@{1} -1 -q | head -1 | cut -f 2 -d' '
    sonar_args=(
        "-Dsonar.pullrequest.key=$CHANGE_ID"
        "-Dsonar.pullrequest.base=$CHANGE_TARGET"
        "-Dsonar.pullrequest.branch=$CHANGE_BRANCH"
        "-Dsonar.scm.revision=$(git log HEAD@{1} -1 -q | head -1 | cut -f 2 -d' ')"
    )
else
    branch="$(echo $GIT_BRANCH | sed -e 's|^origin/||')"
    sonar_args=("-Dsonar.branch.name=$branch")
fi

sonar-scanner "${sonar_args[@]}"

In our properties file we have:

sonar.pullrequest.provider = GitHub
sonar.pullrequest.github.repository = our_github_org/repo_name

Note that this uses shell arrays, so if you use a different shell than Bash 4+ you might need to adapt the logic to work with your shell.