public static Build ExtractBuildInfoForArtifactory(IBuildDetail buildDetail, CodeActivityContext context) { Build build = new Build { modules = new List<Module>(), }; build.started = string.Format(Build.STARTED_FORMAT, buildDetail.StartTime.ToString(artifactoryDateFormat)); IBuildAgent currentAgent = null; foreach (IBuildAgent agent in buildDetail.BuildController.Agents) { if (Uri.Equals(agent.ReservedForBuild, buildDetail.Uri)) { currentAgent = agent; break; } } // build.buildAgent = new BuildAgent { name = currentAgent.Name, version = task.ToolVersion }; build.type = "TFS"; build.agent = new AgentTFS { name = currentAgent.Name, version = "", }; //get the current use from the windows OS System.Security.Principal.WindowsIdentity user; user = System.Security.Principal.WindowsIdentity.GetCurrent(); if (user != null) build.principal = string.Format(@"{0}", user.Name); //Calculate how long it took to do the build build.startedDateMillis = GetTimeStamp(); build.durationMillis = Convert.ToInt64((DateTime.Now - buildDetail.StartTime).TotalMilliseconds); build.number = string.IsNullOrWhiteSpace(buildDetail.BuildNumber) ? build.startedDateMillis : buildDetail.BuildNumber; build.name = buildDetail.BuildDefinition.Name; build.url = string.Format(@"{0}/{1}/_build#_a=summary&buildUri={2}", buildDetail.BuildServer.TeamProjectCollection.Uri.AbsoluteUri, buildDetail.TeamProject, buildDetail.Uri.AbsoluteUri) ; build.vcsRevision = buildDetail.SourceGetVersion; //Add build server properties, if exists. //AddSystemVariables(artifactoryConfig, build); //AddLicenseControl(artifactoryConfig, build, log); //AddBlackDuck(artifactoryConfig, build, log); //ConfigHttpClient(artifactoryConfig, build); return build; }
private static void CreateModule(Build model, StringBuilder sb, int i) { //module start sb.Append("{"); sb.AppendFormat("\"id\":\"{0}\",", model.modules[i].id); sb.Append("\"dependencies\": ["); for (var ii = 0; ii < model.modules[i].Dependencies.Count(); ii++) { sb.Append("{"); sb.AppendFormat("\"type\":\"{0}\",", model.modules[i].Dependencies[ii].type); sb.AppendFormat("\"sha1\":\"{0}\",", model.modules[i].Dependencies[ii].sha1); sb.AppendFormat("\"md5\":\"{0}\",", model.modules[i].Dependencies[ii].md5); sb.AppendFormat("\"id\":\"{0}\",", model.modules[i].Dependencies[ii].id); sb.Append("\"scopes\":["); for (var n = 0; n < model.modules[i].Dependencies[ii].scopes.Count; n++) { sb.AppendFormat( n + 1 < model.modules[i].Dependencies[ii].scopes.Count ? "\"{0}\"," : "\"{0}\"", model.modules[i].Dependencies[ii].scopes[n]); } sb.Append("]}"); if ((ii + 1) < model.modules[i].Dependencies.Count()) { sb.Append(","); } } sb.Append("],"); sb.Append("\"artifacts\": ["); foreach (var artifact in model.modules[i].Artifacts) { sb.Append("{"); sb.AppendFormat("\"type\":\"{0}\",", artifact.type); sb.AppendFormat("\"sha1\":\"{0}\",", artifact.sha1); sb.AppendFormat("\"md5\":\"{0}\",", artifact.md5); sb.AppendFormat("\"name\":\"{0}\"", artifact.name); sb.Append("},"); } if (model.modules[i].Artifacts.Count != 0) sb = (sb.Remove(sb.Length - 1, 1)); sb.Append("]"); //module end sb.Append("}"); }
//system variables start private static void CreateEnvVar(StringBuilder json, Build model) { if (model.properties != null && model.properties.Count > 0) { json.Append("\"properties\":{"); var lastKey = model.properties.LastOrDefault(); String quoteMatch = @""""; String doubleBackSlashMatch = @"\\"; foreach (var kvp in model.properties) { String cleanValue = Regex.Replace(kvp.Value, doubleBackSlashMatch, doubleBackSlashMatch).Replace(quoteMatch, @"\"""); json.AppendFormat("\"{0}\":\"{1}\"", kvp.Key, cleanValue); if (kvp.Key != lastKey.Key) { json.Append(","); } } json.Append("},"); } }
private static void createLicenseControl(StringBuilder json, Build model) { json.Append("\"licenseControl\":{"); json.AppendFormat("\"runChecks\":\"{0}\",", model.licenseControl.runChecks); json.AppendFormat("\"includePublishedArtifacts\":\"{0}\",", model.licenseControl.includePublishedArtifacts); json.AppendFormat("\"autoDiscover\":\"{0}\",", model.licenseControl.autoDiscover); json.Append("\"licenseViolationRecipients\":["); var lastRecip = model.licenseControl.licenseViolationsRecipients.LastOrDefault(); foreach (string recip in model.licenseControl.licenseViolationsRecipients) { json.AppendFormat("\"{0}\"", recip); if (!lastRecip.Equals(recip)) json.Append(","); } json.Append("],"); json.Append("\"scopes\":["); var lastScope = model.licenseControl.scopes.LastOrDefault(); foreach (string scope in model.licenseControl.scopes) { json.AppendFormat("\"{0}\"", scope); if (!lastScope.Equals(scope)) json.Append(","); } json.Append("]"); json.Append("},"); }
//BlackDuck private static void CreateBlackDuck(StringBuilder json, Build model) { if (model.blackDuckGovernance == null) return; json.Append("\"governance\":{"); json.Append("\"blackDuckProperties\":{"); json.AppendFormat("\"runChecks\":\"{0}\",", model.blackDuckGovernance.runChecks); json.AppendFormat("\"appName\":\"{0}\",", model.blackDuckGovernance.appName); json.AppendFormat("\"appVersion\":\"{0}\",", model.blackDuckGovernance.appVersion); if (model.blackDuckGovernance.reportRecipients.Count > 0) { json.Append("\"reportRecipients\":"); var lastRecip = model.blackDuckGovernance.reportRecipients.LastOrDefault(); string recipes = string.Empty; foreach (string recip in model.blackDuckGovernance.reportRecipients) { recipes += recip; if (!lastRecip.Equals(recip)) recipes += " "; } json.AppendFormat("\"{0}\"", recipes); json.Append(","); } if (model.blackDuckGovernance.scopes.Count > 0) { json.Append("\"scopes\":"); var lastScope = model.blackDuckGovernance.scopes.LastOrDefault(); string scopes = string.Empty; foreach (string scope in model.blackDuckGovernance.scopes) { scopes += scope; if (!lastScope.Equals(scope)) scopes += " "; } json.AppendFormat("\"{0}\"", scopes); json.Append(","); } json.AppendFormat("\"includePublishedArtifacts\":\"{0}\",", model.blackDuckGovernance.includePublishedArtifacts); json.AppendFormat("\"autoCreateMissingComponentRequests\":\"{0}\",", model.blackDuckGovernance.autoCreateMissingComponentRequests); json.AppendFormat("\"autoDiscardStaleComponentRequests\":\"{0}\"", model.blackDuckGovernance.autoDiscardStaleComponentRequests); json.Append("}"); json.Append("},"); }