@villedemontreal/scripting
Version:
Scripting core utilities
46 lines • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SonarScript = exports.SONAR_SCANNER = void 0;
const sonarBase_1 = require("./sonarBase");
const sonarInit_1 = require("./sonarInit");
exports.SONAR_SCANNER = './node_modules/.bin/sonar-scanner';
class SonarScript extends sonarBase_1.SonarBaseScript {
get name() {
return 'sonar';
}
get description() {
return `Analyze current local branch source code and send results to Sonar server
* NOTE *: git v2.22 or above is required (git branch --show-current must be available)`;
}
async configure(command) {
command.option(`-t, --target-branch <branch>`, `Sonar target branch: current source code will be analyzed and compared to this target branch. ` +
`See https://docs.sonarqube.org/7.5/branches/overview/#header-2 for more information. ` +
`Usually set to 'develop'; default target branch is 'master'.`);
}
async main() {
const { sonarHostUrl, sonarProjectKey } = this.getSonarProjectInformation();
const currentBranch = await this.findCurrentGitLocalBranch();
if (!(await this.sonarProjectAlreadyExists(sonarProjectKey, sonarHostUrl))) {
this.logger.warn(`'${sonarProjectKey}' Sonar project does not yet exist on ${sonarHostUrl} ! Initializing it first...`);
await this.invokeScript(sonarInit_1.SonarInitScript, {}, {});
}
// npx sonar-scanner -Dsonar.branch.name=`git branch --show-current` -Dsonar.branch.target=develop
this.logger.info(`Analyzing current branch "${currentBranch}" source code...`);
const args = [`-Dsonar.branch.name=${currentBranch}`];
if (this.options.targetBranch) {
args.push(`-Dsonar.branch.target=${this.options.targetBranch}`);
}
await this.invokeShellCommand(exports.SONAR_SCANNER, args);
}
async findCurrentGitLocalBranch() {
let currentBranch;
await this.invokeShellCommand('git', ['branch', '--show-current'], {
outputHandler: (stdoutOutput) => {
currentBranch = stdoutOutput.trim();
},
});
return currentBranch;
}
}
exports.SonarScript = SonarScript;
//# sourceMappingURL=sonar.js.map