Block PR merge if the coverage is less

We are trying to block the PR if the coverage is less than 80%, I created a quality gate for this and the added the repositories to which it should apply, it shows the quality gate is failed if the coverage is less than 80% in sonarcloud but it is not blocking the merge.
Please help with how to set this rule?

Hello @harshavardhanc,

Which version control provider are you using? (Github, Bitbucket Cloud, …)

Tom

Hey @TomVanBraband, Thanks for the reply.
We are using Github.

Did you mark SonarCloud as a required check on Github?
You can do so in Github by going to Settings > Branches > Branch protection rules > Require status checks to pass before merging. This will block you from merging the pull request on Github.

1 Like

yes @TomVanBraband, it is sonarcloud is checked in our Github settings, still it is not blocking the PR.

And can you see the check being reported on the Github pull request?

yes, I can see it says quality gate passed.


Anything I’m missing @TomVanBraband

Ok, I see, the real problem is that the coverage information is not reported for your PR.
Is the coverage information available when you run the SonarCloud analysis on CircleCI? Could you share your CircleCI config.yml?

Here is the config file @TomVanBraband

Hello @harshavardhanc,

It seems that the analysis fails with

[ERROR] Cannot import coverage information for file 'druid-events-validator/src/main/java/org/ekstep/ep/samza/domain/Event.java', coverage data is invalid. Error: {}
java.lang.IllegalStateException: Line 50 is out of range in the file druid-events-validator/src/main/java/org/ekstep/ep/samza/domain/Event.java (lines: 46)

This is likely due to outdated jacoco reports (see this thread), could you try to replace the mvn verify command in your config.yml with mvn clean verify to check if that is the cause?

Tom

Sure @TomVanBraband, will try and update soon.

@TomVanBraband Have replaced it with maven command with mvn clean verify but still the issue persists, we are not getting the coverage for PR.

Hello @harshavardhanc,

What is happening is that the Jacoco reports reference line numbers on files that the analysis can not find in that same file (because the file has less lines than the line number referenced in the jacoco reports).

I have no idea what is causing this, but this seems to be a project configuration issue. I would suggest to start checking the jacoco reports manually to try to figure out why it contains line numbers that do not exist anymore when the sonar analysis is running.

Tom

Thanks for pointing the issue @TomVanBraband, we will try to figure out what’s the issue.

1 Like

@TomVanBraband We are facing this issue for most of our repository, but we are getting coverage for the overall branch but not for PR. Can you please help us with this.

Hello @harshavardhanc,

When I check out the CircleCI builds here they seem to contain the exact same errors as in your PRs. Could you please point me to a build that correctly reports the coverage information?

Thanks,
Tom

@TomVanBraband I found the issue, basically it is not an issue. The PR’s which I was referring doesn’t include actual code which requires coverage, I mean those PR’s includes only configuration,circleci or variable changes. I had checked other PR’s which has actual lines code where we are getting coverage and also PR is getting blocked if it failed to cross the quality gate.

1 Like

That’s good to hear! Thanks for the update!

1 Like

Hi Harsha,
Would you please share how did you add the quality gate? I’m looking for the same solution as you was working back then. Would be highly appreciated.