Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Commit

Permalink
Support local options and SQ Scanner env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
bellingard committed Oct 23, 2016
1 parent bcd18d0 commit 59873c5
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions dist/sonarqube-scanner-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var path = require('path');
var extend = require('extend');
var readPackage = require('read-pkg');
var slug = require('slug');
var log = require('fancy-log');

module.exports = defineSonarQubeScannerParams;

Expand All @@ -13,55 +14,62 @@ module.exports = defineSonarQubeScannerParams;
function defineSonarQubeScannerParams(params) {
var projectBaseDir = process.cwd();

// set default values if nothing is found later
var serverUrl = "http://localhost:9000";
var token = "";
var projectKey = slug(path.basename(projectBaseDir));
var projectName = projectKey;
var projectVersion = "1.0";
var projectDescription = "No description.";
var projectHomePage = "";

// Check what's passed in the call params
if (params.serverUrl) {
serverUrl = params.serverUrl;
}
if (params.token) {
token = params.token;
}
// #1 - set default values
var sonarqubeScannerParams = {
"sonar.host.url": "http://localhost:9000",
"sonar.login": "",
"sonar.projectKey": slug(path.basename(projectBaseDir)),
"sonar.projectName": path.basename(projectBaseDir),
"sonar.projectVersion": "1.0",
"sonar.projectDescription": "No description.",
"sonar.sources": ".",
"sonar.exclusions": "node_modules/**"
};

// now try to read "package.json" file
// #1 - try to read "package.json" file
try {
var packageFile = path.join(projectBaseDir, "package.json");
fs.accessSync(packageFile, fs.F_OK);
var pkg = readPackage.sync(packageFile);
if (pkg) {
projectKey = slug(pkg.name);
projectName = pkg.name;
projectVersion = pkg.version;
// there's a 'package.json' file - let's grab some info
sonarqubeScannerParams["sonar.projectKey"] = slug(pkg.name);
sonarqubeScannerParams["sonar.projectName"] = pkg.name;
sonarqubeScannerParams["sonar.projectVersion"] = pkg.version;
if (pkg.description) {
projectDescription = pkg.description;
sonarqubeScannerParams["sonar.projectDescription"] = pkg.description;
}
if (pkg.homepage) {
projectHomePage = pkg.homepage;
sonarqubeScannerParams["sonar.links.homepage"] = pkg.homepage;
}
if (pkg.bugs && pkg.bugs.url) {
sonarqubeScannerParams["sonar.links.issues"] = pkg.bugs.url;
}
if (pkg.repository && pkg.repository.url) {
sonarqubeScannerParams["sonar.links.scm"] = pkg.repository.url;
}
}
} catch (e) {
// No "package.json" file - let's remain on the defaults
log('No "package.json" file found. Using default settings.');
}

var sonarqubeScannerParams = {
"sonar.host.url": serverUrl,
"sonar.login": token,
"sonar.projectKey": projectKey,
"sonar.projectName": projectName,
"sonar.projectVersion": projectVersion,
"sonar.projectDescription": projectDescription,
"sonar.links.homepage": projectHomePage,
"sonar.sources": ".",
"sonar.exclusions": "node_modules/**"
};
// #2 - read SONARQUBE_SCANNER_PARAMS if it exists, and if yes extend the current params
var envParams = process.env.SONARQUBE_SCANNER_PARAMS;
if (envParams) {
extend(sonarqubeScannerParams, JSON.parse(envParams));
}

// #3 - check what's passed in the call params - these are prevalent params
if (params.serverUrl) {
sonarqubeScannerParams["sonar.host.url"] = params.serverUrl;
}
if (params.token) {
sonarqubeScannerParams["sonar.login"] = params.token;
}
if (params.options) {
extend(sonarqubeScannerParams, params.options);
}

return sonarqubeScannerParams;
}

0 comments on commit 59873c5

Please sign in to comment.