@villedemontreal/scripting
Version:
Scripting core utilities
66 lines • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SonarBaseScript = void 0;
const path = require("path");
const request = require("superagent");
const url_1 = require("url");
const src_1 = require("../../src");
const properties = require('java-properties');
class SonarBaseScript extends src_1.ScriptBase {
async sonarProjectAlreadyExists(sonarProjectKey, sonarHostUrl) {
let res;
this.logger.debug(`*** Calling Sonar host check whether ${sonarHostUrl} Sonar instance is reachable...`);
try {
res = await request.head(new url_1.URL(sonarHostUrl).toString()).redirects(5).timeout(20000);
}
catch (err) {
this.logger.error(`"${sonarHostUrl}" Sonar server is not reachable.`);
throw err;
}
this.logger.debug(`*** Calling Sonar API to check whether ${sonarProjectKey} project exists in ${sonarHostUrl} Sonar instance...`);
try {
res = await request
.get(this.getBranchesListSonarEndpointUrl(sonarHostUrl))
.query({ project: sonarProjectKey })
.timeout(10000);
}
catch (err) {
if (err.response?.notFound) {
// 404 is the only http error we want to keep track of
res = err.response;
}
else {
throw err;
}
}
this.logger.debug('*** Sonar API response :', { status: res.statusCode, text: res.text });
if (res.ok) {
return true;
}
if (res.notFound) {
return false;
}
throw { msg: 'Unexpected response from Sonar API!', response: res };
}
getSonarProjectInformation() {
const sonarProperties = properties.of('sonar-project.properties');
const result = {
sonarHostUrl: sonarProperties.get('sonar.host.url'),
sonarProjectKey: sonarProperties.get('sonar.projectKey'),
};
if (!result.sonarHostUrl) {
throw new Error('"sonar.host.url" property must be defined in "sonar-project.properties" file!');
}
if (!result.sonarProjectKey) {
throw new Error('"sonar.projectKey" property must be defined in "sonar-project.properties" file!');
}
return result;
}
getBranchesListSonarEndpointUrl(sonarHostUrl) {
const endpointUrl = new url_1.URL(sonarHostUrl);
endpointUrl.pathname = path.join(endpointUrl.pathname, 'api/project_branches/list');
return endpointUrl.toString();
}
}
exports.SonarBaseScript = SonarBaseScript;
//# sourceMappingURL=sonarBase.js.map