lib/sonarqube.js
/**
 *
 * @param {*} login login to the SonarQube API. Use it too for tokens. Let it empty or undefined if no authentication is required.
 * @param {*} password password to the SonarQube API. Use empty value for tokens.
 * @param {*} delayBetweenChecksInSecs Defaults to 5
 */
@param {*} login login to the SonarQube API. Use it too for tokens. Let it empty or undefined if no authentication is required.
@param {*} password password to the SonarQube API. Use empty value for tokens.
@param {*} delayBetweenChecksInSecs Defaults to 5
@param { * } login login to the SonarQube API . Use it too for tokens . Let it empty or undefined if no authentication is required . @param { * } password password to the SonarQube API . Use empty value for tokens . @param { * } delayBetweenChecksInSecs Defaults to 5
function checkReportStatus(
  login,
  password = '',
  delayBetweenChecksInSecs = DEFAULT_DELAY
) {
  return new Promise((resolve, reject) => {
    const reportInfo = fs.readFileSync(REPORT_FILE, 'utf8');
    const taskUrl = reportInfo.match(/ceTaskUrl=(.*)/)[1];
    console.log('Report Status Url : ' + taskUrl);
    const srvUrl = url.parse(taskUrl);
    console.log(`Protocol = ${srvUrl.protocol }`);
    const requestModule = (srvUrl.protocol.startsWith('https')) ? https : http;
    const options = {};
    addAuthHeader(options, login, password);
    const req = requestModule.request(taskUrl, options, response => {
      if (response.statusCode !== 200) {
        console.error('Error requesting the Report status');
        reject('SonarQube replied the status code ' + response.statusCode);
      } else {
        let body = '';
        response.on('data', function(chunk) {
          body += chunk;
        });
        req.on('error', function(err) {
          console.error('Error requesting the Report status');
          reject(err);
        });
        response.on('end', function() {
          debug('Raw response from SonarQube API');
          debug('"' + body + '"');
          if (body === '') {
            resolve('');
          } else {
            const taskResponse = JSON.parse(body);
            resolve(taskResponse.task.status);
          }
        });
      }
    });
    req.end();
  }).then(status => {
    console.log('Report status : ' + status);
    if (status === '' || status === 'IN_PROGRESS' || status === 'PENDING') {
      console.log(
        'Retry until report ends in ' + delayBetweenChecksInSecs + 's'
      );
      return delay(delayBetweenChecksInSecs * 1000).then(() => {
        return checkReportStatus(login, password, delayBetweenChecksInSecs);
      });
    } else {
      return status;
    }
  });
}
function checkReportStatus ( login , password = '' , delayBetweenChecksInSecs = DEFAULT_DELAY ) { return new Promise ( ( resolve , reject ) => { const reportInfo = fs . readFileSync ( REPORT_FILE , 'utf8' ) ; const taskUrl = reportInfo . match ( / ceTaskUrl=(.*) / ) [ 1 ] ; console . log ( 'Report Status Url : ' + taskUrl ) ; const srvUrl = url . parse ( taskUrl ) ; console . log ( ` ${ srvUrl . protocol } ` ) ; const requestModule = ( srvUrl . protocol . startsWith ( 'https' ) ) ? https : http ; const options = { } ; addAuthHeader ( options , login , password ) ; const req = requestModule . request ( taskUrl , options , response => { if ( response . statusCode !== 200 ) { console . error ( 'Error requesting the Report status' ) ; reject ( 'SonarQube replied the status code ' + response . statusCode ) ; } else { let body = '' ; response . on ( 'data' , function ( chunk ) { body += chunk ; } ) ; req . on ( 'error' , function ( err ) { console . error ( 'Error requesting the Report status' ) ; reject ( err ) ; } ) ; response . on ( 'end' , function ( ) { debug ( 'Raw response from SonarQube API' ) ; debug ( '"' + body + '"' ) ; if ( body === '' ) { resolve ( '' ) ; } else { const taskResponse = JSON . parse ( body ) ; resolve ( taskResponse . task . status ) ; } } ) ; } } ) ; req . end ( ) ; } ) . then ( status => { console . log ( 'Report status : ' + status ) ; if ( status === '' || status === 'IN_PROGRESS' || status === 'PENDING' ) { console . log ( 'Retry until report ends in ' + delayBetweenChecksInSecs + 's' ) ; return delay ( delayBetweenChecksInSecs * 1000 ) . then ( ( ) => { return checkReportStatus ( login , password , delayBetweenChecksInSecs ) ; } ) ; } else { return status ; } } ) ; }
@param {*} login login to the SonarQube API.
@param { * } login login to the SonarQube API .
// on first call, API response might be empty