public ProjectStatus GetAnalysisResults(SonarResultsSettings settings, string analysisId) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } try { String sonarUrl = settings.Url; if (!Utility.ValidateUrl(sonarUrl)) { throw new ArgumentException("Invalid SonarQube URL"); } if (settings.IsAuthEnabled) { _Client.Authenticator = settings.GetAuthenticator; } _Logger.Information($"Initializing analysis request"); string url = String.Format(_AnalysisUrl, sonarUrl, analysisId); var request = new RestRequest(url, Method.POST); _Logger.Information($"Setting API endpoint to {url} [POST]"); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; var queryResult = _Client.Execute <ProjectStatusWrapper>(request); ValidateResult(queryResult); ProjectStatusWrapper x = queryResult.Data; return(x.ProjectStatus); } catch (Exception ex) { _Logger.Error($"An error occured while retrieving Analysis Status from SonarQube - {ex.Message}"); throw; } }
public Task GetTaskResults(SonarResultsSettings settings, string taskId) { try { string sonarUrl = settings.Url; if (settings.IsAuthEnabled) { _Client.Authenticator = settings.GetAuthenticator; } if (!Utility.ValidateUrl(sonarUrl)) { throw new ArgumentException("Invalid SonarQube URL"); } string url = String.Format(_TaskUrl, sonarUrl, taskId); _Logger.Information($"Initializing request"); var request = new RestRequest(url, Method.POST); _Logger.Information($"Setting Content-Type to application/json"); request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; _Logger.Information($"Retrieving task status from {url} [POST]"); //Debugger.Launch(); var queryResult = _Client.Execute <TaskWrapper>(request); ValidateResult(queryResult); TaskWrapper x = queryResult.Data; return(x.Task); } catch (Exception ex) { _Logger.Error($"An error occured while retrieving Task Status from SonarQube - {ex.Message}"); throw; } }
public static void SonarQubeResults(this ICakeContext context, SonarResultsSettings settings) { string taskId = SonarTaskIdRetriever.GetTaskId(context); ICakeLog logger = context.Log; logger.Information("Creating a Sonar Task Retriever Client"); SonarTaskClient taskClient = new SonarTaskClient(context); SonarAnalysisClient analysisClient = new SonarAnalysisClient(context); while (true) { var taskResults = taskClient.GetTaskResults(settings, taskId); //until the status is SUCCESS, CANCELED or FAILED if (taskResults.Status != "SUCCESS" && taskResults.Status != "CANCELED" && taskResults.Status != "FAILED") { Thread.Sleep(10000); } else { if (taskResults.Status == "SUCCESS") { var analysisStatus = analysisClient.GetAnalysisResults(settings, taskResults.AnalysisId); logger.Information(analysisStatus); if (analysisStatus.Status == "ERROR" || analysisStatus.Status == "FAILED") { logger.Error($"SonarQube analysis returned {analysisStatus.Status}"); throw new CakeException("SonarQube analysis Failed. Breaking the build"); } logger.Information($"SonarQube analysis returned {analysisStatus.Status}"); } else { logger.Error($"SonarQube Task Failed! - STATUS => {taskResults.Status}"); throw new CakeException($"SonarQube task returned {taskResults.Status}"); } break; } } }