import * as request from "request" ; import * as semver from "semver" ; import * as tl from "azure-pipelines-task-lib/task" ; import Endpoint from "../sonarqube/Endpoint" ; interface RequestData { [ x ] : any ; } function get ( endpoint , path , isJson , query ? ) { tl . debug ( `[SQ] API GET: '${ path }' with query "${ JSON . stringify ( query ) }"` ) ; return new Promise ( ( resolve , reject ) => { const options = { auth : endpoint . auth , } ; if ( query ) { options . qs = query ; options . useQuerystring = true ; } request . get ( { method : "GET" , baseUrl : endpoint . url , uri : path , json : isJson , ... options , } , ( error , response , body ) => { if ( error ) { return logAndReject ( reject , `[SQ] API GET '${ path }' failed, error was: ${ JSON . stringify ( error ) }` ) ; } tl . debug ( `Response: ${ response . statusCode } Body: "${ isString ( body ) ? body : JSON . stringify ( body ) }"` ) ; if ( response . statusCode < 200 || response . statusCode >= 300 ) { return logAndReject ( reject , `[SQ] API GET '${ path }' failed, status code was: ${ response . statusCode }` ) ; } return resolve ( body || ( isJson ? { } : "" ) ) ; } ) ; } ) ; } function isString ( x ) { return Object . prototype . toString . call ( x ) === "[object String]" ; } export function getJSON ( endpoint , path , query ? ) { return get ( endpoint , path , true , query ) ; } export function getServerVersion ( endpoint ) { return get ( endpoint , "/api/server/version" , false ) . then ( semver . coerce ) ; } function logAndReject ( reject , errMsg ) { tl . debug ( errMsg ) ; return reject ( new Error ( errMsg ) ) ; }