How does SonarQube define New Code in a Pull Request?

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)
    10.3
  • how is SonarQube deployed: zip, Docker, Helm
    Helm
  • what are you trying to achieve
    Use SonarQube to determine if a PR meets certain code quality.
  • what have you tried so far to achieve this
    Enable SonarQube for my github repository. Create a project on SonarQube, and every time I make a PR, a SonarQube report will be automatically generated.

Issue:
Most of time, SonarQube uses all code within a PR as new code, which is what I expect.

Sometimes, SonarQube treats code outside a PR as new code. For example, as I didn’t set version number in my repo, my SonarQube is treating all of my code as new code at branch level. When I create a PR and try to merge into my main branch, SonarQube will use all of my code in main branch as new code; rather than just code in my PR.

I’m not sure why SonarQube behaves differently in my situation.

Any help is appreciated!

Hi @yigedaboluo ,

To better understand and resolve this issue, I recommend consulting the following sections of the SonarQube documentation:

  • Defining new code: Defining new code This section explains how SonarQube determines what constitutes new code. By understanding this mechanism, you can verify that your settings are correctly configured.
  • Pull request analysis: Pull request analysis & SonarQube This section details the process of analyzing pull requests. It will help you understand how SonarQube compares different versions of your code and identifies changes.
1 Like

Hey Bachri,

Thank you so much your help! I found what I need in those documents. Really appreciate it!

I also noticed that if we are creating a PR that merges a forked repo to original, it will always treat every line of code in the repo as new code (we didn’t use versioning). Is there a way to get around this? Looking forward to your advice.

Thank you so very much; I hope you have a great week ahead.

1 Like