Quality Gate doesn't fail when Rebuild the Branch

Hi All,
This might sound bit basic but I couldn’t find it anywhere else.

My aim is to reject a Pull Request if it fails the Sonar Quality Gate. This works well for the first build of the PR where it analyze the modified files and make the Quality Gate as Failure, however if I re-build the same PR from Jenkins it doesn’t check the over-all files modified in a PR but it takes only for that particular build and since it doesn’t have any new files it make the Quality Gate as Success which obviously enable the PR to be merged to ‘master’.

Could someone through some light if I’m missing anything? or it is not the right approach for the Quality Gate status

Thanks in advance

Hi,

To be clear, you’re using the Jenkins rebuild(?) feature to re-run the job on exactly the same commit? And if so, would you mind sharing why you find that to be useful (particularly if you got all the way through to the point of marking the QG status the first time)?

TBH, I don’t think this is a use case we anticipated.

 
Ann

Hi,
Thanks for your reply!! Yes Jenkins Rebuild - I understand that this case doesn’t make any sense, better I explain the flow:

  1. Cut a new branch ‘xxx’ from ‘master’
  2. Commit file1 to ‘xxx’ - QG Failed
  3. Commit file2 to ‘xxx’ - QG Success (at this point I think that the earlier failure will be caught when we merge branch ‘xxx’ to ‘master’
  4. Create a PR from ‘xxx’ to ‘master’ - QG Failed
  5. Commit file3 to ‘xxx’
    5.1. ‘xxx’ build in jenkins - QC Success
    5.2. ‘PR’ build - QC Success

Since the 2nd PR build is success it lets the branch ‘xxx’ to be merged to ‘master’ and introducing a invalid code in ‘master’.

Is there anything we can do about this please?

Thanks in advance,
Ariv

Hi Ariv,

I think it might help here to know which QG conditions are failing/expected to fail.

 
Ann

Hi Ann,

3 conditions failed
On New Code

Coverage on New Codeis less than 80.0%
New Bugsis greater than 0
Technical Debt Ratio on New Codeis greater than 5.0%

I’m expecting the same Failure on PR builds as well, even if I do any subsequent clean commits.

Instead, if I commit a 2nd file with clean code to the branch the PR becomes Green which obviously letting the defecting code to be merged to ‘master’. Please let me know if I’m missing anything?

Thanks,
Ariv

Further more findings,

below screen shot is the order of execution of a ‘feature’ branch:
image

As you see Run#2 was Red and Run#4 became Green

Below Image showing the committed file is having a bug

And, I’m expecting my PR to get failed because one of the Run was Red, but the PR build is success and the code which showed error in the ‘feature’ branch doesn’t reflect with the PR build though it is listed under the modified files list

Thanks

Hi Ariv,

Thanks for the details. This … might help :smiley:

The first and third conditions are about ratios, so it makes sense to me that adding a new (“better”) file to the PR could change the ratios enough to make those conditions pass. For instance, let’s say file1 adds 5 uncovered lines: 0% coverage on new code. Then file2 adds 20 covered lines: 80% coverage on new code (20 of 25 lines covered). The thing that’s not easily explained away is the failure and then success of the New Bugs condition. Can you verify that the issues raised in the first commit were still open after the 2nd commit?

 
Ann

Hi Ann,

“The thing that’s not easily explained away is the failure and then success of the New Bugs condition”

Could be this due to the below status of the QG of the 2nd file commit?
“Some Quality Gate conditions on New Code were ignored because of the small number of New Lines”

“Can you verify that the issues raised in the first commit were still open after the 2nd commit?”
Yes - first commit and second commit are completely new files, so the first issue is still open.

FYI - I’m committing files on a test basis not with a real-time scenario

Ariv

Hi Ariv,

Sadly, no. That only applies to coverage.

Okay, so then can you share the issue(s) opened in the first commit?

 
Ann

Please find the error caught during the first file commit (with Failure)

Though, this file is listed as modified in the PR - issue is not reported in PR report
image

Could it because QG profile is pointing to ‘Default Sonar way (outdated copy)’ ?

Hi,

Thanks for the screenshots!

Now to dig into them…
If the issue is no longer reported in the PR, where did you go to make the screenshot? Was it taken earlier and you had it in storage? Or did you go to a different branch/PR? (Or just re-create the conditions?)

The interesting thing about this particular issue is that it’s entirely possible that a change in a different file could make it go away. As an example

/* File = A.java */
public Foo getFoo() {
  if (condition) {
    return new Foo();
  } 
  return null;
}

/* File = B.java */
private void doTheThing() {

  Foo myFoo = this.a.getFoo();  // might be null!
  Bar aBar = myFoo.getBar();  // Could be an NPE; issue raised!
}

So let’s say the first commit adds doTheThing in B.java => NPE issue raised. Then the second commit makes a change in A.java:

/* File = A.java */
public Foo getFoo() {
  if (condition) {
    return new Foo();
  } 
  return oldFoo;
}

And B.java’s issue goes away without it having been touched.

 
Ann

Hi Ann,

Thanks for the explanation - NPE makes sense.

Screenshot are correct - PR doesn’t report any issue but it has the two files which are modified as part of the ‘feature’ branch which as reported as failure during branch analysis (refer the one of the images above please).

I think I will have to try it out with a real time scenario to get a better picture - main thing which I want to confirm is I’m not missing any configs during PR analysis.

Thanks,
Ariv