sonar-scanner-npm icon indicating copy to clipboard operation
sonar-scanner-npm copied to clipboard

Branch and PR analysis

Open davidrodriguezlosada opened this issue 3 years ago • 1 comments

I think it is not possible yet, so I post it here: It should be great to be able to run branch and PR analysis using this project instead of having to install sonar-scanner for it.

The required parameters to pass to sonar-scanner will be:

  • sonar.branch.name
  • sonar.pullrequest.branch
  • sonar.pullrequest.key
  • sonar.pullrequest.base

davidrodriguezlosada avatar May 03 '21 08:05 davidrodriguezlosada

Seems to work if you pass them in the options. I Tried with AppVeyor and got something like:

const scanner = require('sonarqube-scanner');

var options = {
    "sonar.sources": "./src",
    //...
};

if(process.env.APPVEYOR_PULL_REQUEST_NUMBER){
    var extraoptions = {
        "sonar.pullrequest.key": process.env.APPVEYOR_PULL_REQUEST_NUMBER,
        "sonar.pullrequest.branch": process.env.APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH,
        "sonar.pullrequest.base": process.env.APPVEYOR_REPO_BRANCH
    };
    options = {...options, ...extraoptions};
};

console.log(options);

scanner(
    {
        serverUrl: process.env.SONAR_HOST,
        token: process.env.SONAR_TOKEN,
        options: options,
    },
    () => process.exit()
);

Most of the values are sent in from the CI env vars.

MiguelAlho avatar Jun 02 '22 19:06 MiguelAlho

Hey guys, this is pretty cool. Maybe we can document it? Anyone motivated for a PR?

gabssnake avatar Sep 26 '22 18:09 gabssnake

Note that the scanner itself can detected these settings already automatically for some CI server: https://docs.sonarqube.org/latest/analysis/pull-request/#header-4 https://docs.sonarqube.org/latest/branches/overview/#header-2

ugrave avatar Oct 25 '22 13:10 ugrave

Thank you everyone for your contributions.

I added this topic to the list of potential improvements:

  • #165

Feel free to reopen if you want to try a documentation PR or if anything new comes up.

gabssnake avatar Dec 04 '22 17:12 gabssnake