/** * Constructor * * @param result * @param buildName * @param buildNumber */ public PolicyCheckReport(CheckPoliciesResult result, String buildName, String buildNumber) : this(result) { { this.BuildName = buildName; this.BuildNumber = buildNumber; } }
/* --- Task Methods --- */ // Override the ExecuteTask method. protected override void ExecuteTask() { CreateService(); projects = new List <AgentProjectInfo>(); ScanFiles(); // determine product name and version String productName; if (String.IsNullOrEmpty(Product)) { productName = Project.ProjectName; } else { productName = Product; } String productVersion = ""; if (String.IsNullOrEmpty(ProductVersion)) { productVersion = ProductVersion; } // send check policies request bool doUpdateInventory = false; if (CheckPolicies != null) { Log(Level.Info, "Checking policies"); CheckPoliciesResult checkPoliciesResult = service.CheckPolicies(ApiKey, productName, productVersion, projects); doUpdateInventory = HandlePoliciesResult(checkPoliciesResult); } if (doUpdateInventory) { // send update request Log(Level.Info, "Updating White Source"); UpdateInventoryResult updateInventoryResult = service.Update(ApiKey, productName, productVersion, projects); if (updateInventoryResult != null) { LogUpdateResult(updateInventoryResult); if (CheckPolicies.FailOnRejection) { throw new BuildException("Build failed due to policy violations."); } } } }
private bool HandlePoliciesResult(CheckPoliciesResult result) { // generate report try { Log(Level.Info, "Creating policies report"); PolicyCheckReport report = new PolicyCheckReport(result); report.Generate(CheckPolicies.ReportDir, false); } catch (IOException e) { error(e); } String message; bool hasRejections = result.HasRejections(); // handle rejections if any if (hasRejections && !CheckPolicies.ForceUpdate) { String rejectionsErrorMessage = "Open source rejected by organization policies."; if (CheckPolicies.FailOnRejection) { throw new BuildException(rejectionsErrorMessage); } else { Log(Level.Warning, rejectionsErrorMessage); return(false); } } else { message = hasRejections ? "Some dependencies violate open source policies, however all were forced updated to organization inventory" : "All dependencies conform with open source policies"; Log(Level.Info, message); } return(true); }
/* --- Constructors --- */ /** * Constructor * * @param result */ public PolicyCheckReport(CheckPoliciesResult result) { this.Result = result; }
/* --- Public methods --- */ public void GenerateReport(CheckPoliciesResult result, String buildName, String buildNumber, StreamWriter writer) { writer.WriteLine("<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head><title>White Source - Policy Check Summary</title><meta content='text/html; charset=iso-8859-1' http-equiv='Content-Type' />"); writer.WriteLine("<script>function toggleDetails(anchor, detailsId){var details = document.getElementById(detailsId);if(details.style.display == 'block' ) {details.style.display = 'none';anchor.innerHTML = 'show details'} else {details.style.display = 'block';anchor.innerHTML = 'hide details'}return details;}</script>"); writer.WriteLine("<link rel='stylesheet' href='wss.css'></head><body class='wssBody'><div align='center' style='width: 100%;'><div class='wssContainer'><a class='wssAnchor' href='http://www.whitesourcesoftware.com' target='_blank'><img align='left' src='http://saas.whitesourcesoftware.com/Wss/background/WhiteSource_Logo.png' style='left: 10;' /></a>"); writer.WriteLine("<div align='center'><h1> </h1><h1 class='wssHeader'>Policy Check Summary</h1><h2>"); if (result.HasRejections()) { writer.WriteLine(REJECT); } else { writer.WriteLine(APPROVE); } writer.WriteLine("</h2><br/></div>"); writer.WriteLine("<div align='left'><div class='wssTextAlign' style='width: 500px; padding-left: 10px;'><div><span class='wssGeneralIcon'></span><div class='wssProjectHeader' style='display: inline-block'>General Details</div></div><table style='width: 100%;' class='wssTable wssTextColor'><tbody>"); if (!String.IsNullOrEmpty(buildName)) { writer.WriteLine("<tr><td>Build Name</td><td>$buildName</td></tr>".Replace("$buildName", buildName)); } if (!String.IsNullOrEmpty(buildNumber)) { writer.WriteLine("<tr><td>Build Number</td><td>$buildNumber</td></tr>".Replace("$buildNumber", buildNumber)); } writer.WriteLine("<tr><td>Report creation time</td><td>$creationTime</td></tr></tbody></table></div></div>".Replace("$creationTime", DateTime.Now.ToString())); writer.WriteLine(LINE_SEPARATOR); writer.WriteLine("<div class='wssTextAlign'><div style='padding-bottom: 1px;'><span class='wssProjectIcon'></span><div class='wssProjectHeader' style='display: inline-block'>New Projects</div><span class='wssTextColor wssProjectStats'>"); writer.WriteLine("found $result.newProjects.size() new projects </span></div>".Replace("$result.newProjects.size()", result.NewProjects.Count.ToString())); WriteProjectsSummary(result.NewProjects, "new-project", writer); writer.WriteLine(LINE_SEPARATOR); writer.WriteLine("<div style='padding-bottom: 1px;'><span class='wssProjectIcon'></span><div class='wssProjectHeader' style='display: inline-block'>Existing Projects</div><span class='wssTextColor wssProjectStats'>found $result.existingProjects.size() existing projects </span></div>" .Replace("$result.existingProjects.size()", result.ExistingProjects.Count.ToString())); WriteProjectsSummary(result.ExistingProjects, "existing-project", writer); writer.WriteLine("</div>"); writer.WriteLine(LINE_SEPARATOR); List <LicenseHistogramDataPoint> licenses = CreateLicenseHistogram(result); if (licenses.Count > 0) { writer.WriteLine("<div class='wssTextAlign' style='margin: 10px;'><div style='padding-bottom: 1px;'><span class='wssLicenseIcon'></span><div class='wssProjectHeader' style='display: inline-block'>License Distribution</div></div><table class='wssLicenses wssTextColor'><tr>"); foreach (LicenseHistogramDataPoint license in licenses) { writer.WriteLine("<td style='width: 100px; padding: 0px; vertical-align: bottom;'><table style='border-spacing: 0px 0px; width:100px; text-align:center'><tr><td style=' font-size:12px;'>$license.occurrences<td></tr><tr><td style='padding-bottom:0px; padding-right:40px; padding-left:40px'>" .Replace("$license.occurrences", license.Occurrences.ToString())); writer.WriteLine("<table height='$license.height' class='wssLicenseBar' title='$license.name: $license.occurrences'><tr><td></td></tr></table></td></tr></table></td>" .Replace("$license.height", license.Height) .Replace("$license.name", license.Name) .Replace("$license.occurrences", license.Occurrences.ToString())); } writer.WriteLine("</tr></table><table style='width: 100%;'><tr>"); foreach (LicenseHistogramDataPoint license in licenses) { writer.WriteLine("<td><table style='width: 100px;'><tr><td class='wssTextColor' style='font-size:80%; text-align:center;' title='$license.name'>$license.shortName</td></tr></table></td>" .Replace("$license.name", license.Name) .Replace("$license.shortName", license.GetShortName())); } writer.WriteLine("</tr></table></div>"); } writer.Write("</div></div></body></html>"); writer.Flush(); writer.Close(); }
private List <LicenseHistogramDataPoint> CreateLicenseHistogram(CheckPoliciesResult result) { List <LicenseHistogramDataPoint> dataPoints = new List <LicenseHistogramDataPoint>(); // create distribution histogram Dictionary <String, int> licenseHistogram = new Dictionary <String, int>(); foreach (KeyValuePair <String, List <ResourceInfo> > entry in result.ProjectNewResources) { foreach (ResourceInfo resource in entry.Value) { foreach (String license in resource.Licenses) { int occurrence = 0; if (licenseHistogram.TryGetValue(license, out occurrence)) { licenseHistogram[license] = occurrence + 1; } else { licenseHistogram.Add(license, 1); } } } } // sort by count descending List <KeyValuePair <String, int> > licenses = new List <KeyValuePair <string, int> >(); foreach (KeyValuePair <String, int> pair in licenseHistogram) { licenses.Add(pair); } licenses.Sort(new ValueComparator()); // create data points int licenseCount = licenses.Count; if (licenseCount != 0) { // first licenses foreach (KeyValuePair <String, int> pair in licenses.GetRange(0, Math.Min(LICENSE_LIMIT, licenseCount))) { dataPoints.Add(new LicenseHistogramDataPoint(pair.Key, pair.Value)); } // aggregation of histogram tail int tailSize = licenseCount - LICENSE_LIMIT; int tailSum = 0; if (tailSize > 0) { foreach (KeyValuePair <String, int> pair in licenses.GetRange(LICENSE_LIMIT, tailSize)) { tailSum += pair.Value; } dataPoints.Add(new LicenseHistogramDataPoint(OTHER_LICENSE + " (" + tailSize + ")", tailSum)); } // normalize bar height float factor = MAX_BAR_HEIGHT / (float)Math.Max(tailSum, licenses[0].Value); foreach (LicenseHistogramDataPoint dataPoint in dataPoints) { dataPoint.Height = ((int)(factor * dataPoint.Occurrences)).ToString(); } } return(dataPoints); }