public static string GetScanXML(long scanId) { string path = ""; LoginResult loginResult = getLoginResult(); CxWebServiceClient client; try { client = new CxWebServiceClient(loginResult.AuthenticationData); } catch (Exception e) { Logger.Create().Error(e.ToString()); return(null); } string savedFileName = string.Format("report{0}", Guid.NewGuid()); // create status report CxWSReportRequest reportRequest = new CxWSReportRequest(); reportRequest.ScanID = scanId; reportRequest.Type = CxWSReportType.XML; CxWSCreateReportResponse cXWSCreateReportResponse = client.ServiceClient.CreateScanReport(loginResult.SessionId, reportRequest); long reportID = cXWSCreateReportResponse.ID; int numOfTrials = 0; bool resultsObtained = false; while (!resultsObtained && numOfTrials < 100) { CxWSReportStatusResponse cxWSReportStatusResponse = client.ServiceClient.GetScanReportStatus(loginResult.SessionId, reportID); if (cxWSReportStatusResponse.IsReady) { resultsObtained = true; } else { Thread.Sleep(500); } numOfTrials++; } CxWSResponseScanResults cxWSResponseScanResults = client.ServiceClient.GetScanReport(loginResult.SessionId, reportID); if (!cxWSResponseScanResults.IsSuccesfull) { Logger.Create().Error(cxWSResponseScanResults.ErrorMessage); return(null); } StorageHelper.Save(cxWSResponseScanResults.ScanResults, savedFileName); path = savedFileName; return(path); }
/// <summary> /// Generate a Checkmarx scan report for the most recent scan for the given project. /// </summary> /// <param name="checkmarxApiSession"> /// The <see cref="ICheckmarxApiSession"/> to generate the report with. This cannot be null. /// </param> /// <param name="project"> /// The project to get the last scan for. This cannot be null. /// </param> /// <param name="reportType"> /// The result format for the report. /// </param> /// <returns> /// An <see cref="XDocument"/> containing the loaded scan report. /// </returns> /// <exception cref="CheckmarxErrorException"> /// Either the report generation failed or Checkmarx returned invalid XML for the scan report. /// </exception> /// <exception cref="ArgumentNullException"> /// no argument can be null. /// </exception> private static byte[] GenerateLastScanReport(ICheckmarxApiSession checkmarxApiSession, ProjectScannedDisplayData project, CxWSReportType reportType) { if (checkmarxApiSession == null) { throw new ArgumentNullException(nameof(checkmarxApiSession)); } if (project == null) { throw new ArgumentNullException(nameof(project)); } if (!Enum.IsDefined(typeof(CxWSReportType), reportType)) { throw new ArgumentOutOfRangeException(nameof(reportType)); } long reportId; reportId = checkmarxApiSession.CreateScanReport(project.LastScanID, reportType); for (;;) { CxWSReportStatusResponse reportStatusResponse = checkmarxApiSession.GetScanReportStatus(reportId); if (reportStatusResponse.IsFailed) { throw new CheckmarxErrorException( $"Generating report ID {reportId} on scan {project.LastScanID} on project {project.ProjectName} failed"); } else if (reportStatusResponse.IsReady) { break; } // TODO: Consider a better mechanism System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1)); // TODO: Consider a timeout } return(checkmarxApiSession.GetScanReport(reportId)); }