static BuildStatus RetrieveBuildStatus( string serverUrl, string username, string password, IEnumerable<string> buildTypeIds) { buildTypeIds = buildTypeIds.ToArray(); dynamic query = new Query(serverUrl, username, password); var buildStatus = BuildStatus.Passed; try { foreach (var project in query.Projects) { if (!project.BuildTypesExists) { continue; } foreach (var buildType in project.BuildTypes) { if (buildTypeIds.Any() && buildTypeIds.All(id => id != buildType.Id)) { continue; } if ("true".Equals(buildType.Paused, StringComparison.CurrentCultureIgnoreCase)) { continue; } var builds = buildType.Builds; var latestBuild = builds.First; if (latestBuild == null) { continue; } if ("success".Equals(latestBuild.Status, StringComparison.CurrentCultureIgnoreCase)) { dynamic runningBuild = new Query(serverUrl, username, password) { RestBasePath = string.Format("/httpAuth/app/rest/buildTypes/id:{0}/builds/running:any", buildType.Id) }; runningBuild.Load(); if ("success".Equals(runningBuild.Status, StringComparison.CurrentCultureIgnoreCase)) { continue; } } var isUnstableBuild = false; foreach (var property in latestBuild.Properties ?? new dynamic[0]) { if ("system.BuildState".Equals(property.Name, StringComparison.CurrentCultureIgnoreCase) && "unstable".Equals(property.Value, StringComparison.CurrentCultureIgnoreCase)) { isUnstableBuild = true; } if ("BuildState".Equals(property.Name, StringComparison.CurrentCultureIgnoreCase) && "unstable".Equals(property.Value, StringComparison.CurrentCultureIgnoreCase)) { isUnstableBuild = true; } } if (isUnstableBuild) { continue; } var buildId = buildType.Id; dynamic investigationQuery = new Query(serverUrl, username, password); investigationQuery.RestBasePath = @"/httpAuth/app/rest/buildTypes/id:" + buildId + @"/"; buildStatus = BuildStatus.Failed; foreach (var investigation in investigationQuery.Investigations) { var investigationState = investigation.State; if ("taken".Equals(investigationState, StringComparison.CurrentCultureIgnoreCase) || "fixed".Equals(investigationState, StringComparison.CurrentCultureIgnoreCase)) { buildStatus = BuildStatus.Investigating; } } if (buildStatus == BuildStatus.Failed) { return buildStatus; } } } } catch (Exception e) { Console.Error.WriteLine(e); return BuildStatus.Unavailable; } return buildStatus; }
static BuildStatus RetrieveBuildStatus( string serverUrl, string username, string password, IEnumerable<string> buildTypeIds, IEnumerable<string> buildTypeIdsExcluded) { Logger.Verbose("Checking build status."); buildTypeIds = buildTypeIds.ToArray(); buildTypeIdsExcluded = buildTypeIdsExcluded.ToArray(); dynamic query = new Query(serverUrl, username, password); var buildStatus = BuildStatus.Passed; try { var couldFindProjects = false; foreach (var project in query.Projects) { couldFindProjects = true; Logger.Verbose("Checking Project '{0}'.", project.Name); if (!project.BuildTypesExists) { Logger.Verbose("Bypassing Project '{0}' because it has no 'BuiltTypes' property defined.", project.Name); continue; } foreach (var buildType in project.BuildTypes) { Logger.Verbose("Checking Built Type '{0}\\{1}'.", project.Name, buildType.Name); if ((buildTypeIds.Any() && buildTypeIds.All(id => id != buildType.Id)) || (buildTypeIdsExcluded.Any() && buildTypeIdsExcluded.All(id => id == buildType.id))) { Logger.Verbose("Bypassing Built Type '{0}\\{1}' because it does NOT match configured built-type list to monitor.", project.Name, buildType.Name); continue; } if (buildType.PausedExists && "true".Equals(buildType.Paused, StringComparison.CurrentCultureIgnoreCase)) { Logger.Verbose("Bypassing Built Type '{0}\\{1}' because it has 'Paused' property set to 'true'.", project.Name, buildType.Name); continue; } var builds = buildType.Builds; var latestBuild = builds.First; if (latestBuild == null) { Logger.Verbose("Bypassing Built Type '{0}\\{1}' because no built history is available to it yet.", project.Name, buildType.Name); continue; } if ("success".Equals(latestBuild.Status, StringComparison.CurrentCultureIgnoreCase)) { dynamic runningBuild = new Query(serverUrl, username, password) { RestBasePath = string.Format("/httpAuth/app/rest/buildTypes/id:{0}/builds/running:any", buildType.Id) }; runningBuild.Load(); if ("success".Equals(runningBuild.Status, StringComparison.CurrentCultureIgnoreCase)) { Logger.Verbose("Bypassing Built Type '{0}\\{1}' because status of last build and all running builds are 'success'.", project.Name, buildType.Name); continue; } } if (latestBuild.PropertiesExists) { var isUnstableBuild = false; foreach (var property in latestBuild.Properties) { if ( "system.BuildState".Equals(property.Name, StringComparison.CurrentCultureIgnoreCase) && "unstable".Equals(property.Value, StringComparison.CurrentCultureIgnoreCase)) { isUnstableBuild = true; } if ("BuildState".Equals(property.Name, StringComparison.CurrentCultureIgnoreCase) && "unstable".Equals(property.Value, StringComparison.CurrentCultureIgnoreCase)) { isUnstableBuild = true; } } if (isUnstableBuild) { Logger.Verbose("Bypassing Built Type '{0}\\{1}' because it is marked as 'unstable'.", project.Name, buildType.Name); continue; } } Logger.Verbose("Now checking investigation status of Built Type '{0}\\{1}'.", project.Name, buildType.Name); var buildId = buildType.Id; dynamic investigationQuery = new Query(serverUrl, username, password); investigationQuery.RestBasePath = @"/httpAuth/app/rest/buildTypes/id:" + buildId + @"/"; buildStatus = BuildStatus.Failed; foreach (var investigation in investigationQuery.Investigations) { var investigationState = investigation.State; if ("taken".Equals(investigationState, StringComparison.CurrentCultureIgnoreCase) || "fixed".Equals(investigationState, StringComparison.CurrentCultureIgnoreCase)) { Logger.Verbose("Investigation status of Built Type '{0}\\{1}' detected as either 'taken' or 'fixed'.", project.Name, buildType.Name); buildStatus = BuildStatus.Investigating; } } if (buildStatus == BuildStatus.Failed) { Logger.Verbose("Concluding status of Built Type '{0}\\{1}' as FAIL.", project.Name, buildType.Name); return BuildStatus.Failed; } } } if (!couldFindProjects) { Logger.Verbose("No Projects found! Please ensure if TeamCity URL is valid and also TeamCity setup and credentials are correct."); return BuildStatus.Unavailable; } } catch (Exception exception) { Logger.Error(exception); return BuildStatus.Unavailable; } return buildStatus; }
bool TryFind(string bindingName, out object result) { if (document == null) { Load(bindingName); result = new Query(baseUrl, username, password, document); return true; } if (bindingName.Equals("first", StringComparison.CurrentCultureIgnoreCase)) { var enumerator = GetEnumerator(); if (enumerator.MoveNext()) { result = enumerator.Current; return true; } result = null; return true; } string value; IEnumerable<XElement> selectedDecendants; if (!TryFind(bindingName, document, out value, out selectedDecendants)) { if (childDocument == null) { if (!TryRetrieveChildDocument(document, out childDocument)) { result = null; return false; } } if (childDocument == null) { result = null; return false; } if (!TryFind(bindingName, childDocument, out value, out selectedDecendants)) { result = null; return false; } } if (value != null) { result = value; return true; } if (selectedDecendants != null) { if (selectedDecendants.Count() == 1 && !selectedDecendants.First().HasElements && bindingName.Equals(selectedDecendants.First().Name.LocalName, StringComparison.CurrentCultureIgnoreCase)) { XDocument stepChildDocument; if (TryRetrieveChildDocument(selectedDecendants.First(), out stepChildDocument)) { result = new Query(baseUrl, username, password, stepChildDocument); return true; } result = selectedDecendants.First().Value; return true; } result = new Query(baseUrl, username, password, new XDocument(selectedDecendants)); return true; } result = null; return false; }
static BuildStatus RetrieveBuildStatus( string serverUrl, string username, string password, out List<string> buildTypeNames) { dynamic query = new Query(serverUrl, username, password); buildTypeNames = null; var buildStatus = BuildStatus.Passed; try { foreach (var project in query.Projects) { if (!project.BuildTypesExists) { continue; } foreach (var buildType in project.BuildTypes) { if ("true".Equals(buildType.Paused, StringComparison.CurrentCultureIgnoreCase)) { continue; } var builds = buildType.Builds; var latestBuild = builds.First; if (latestBuild == null) { continue; } if ("success".Equals(latestBuild.Status, StringComparison.CurrentCultureIgnoreCase)) { continue; } var isUnstableBuild = false; foreach (var property in latestBuild.Properties) { if ("system.BuildState".Equals(property.Name, StringComparison.CurrentCultureIgnoreCase) && "unstable".Equals(property.Value, StringComparison.CurrentCultureIgnoreCase)) { isUnstableBuild = true; } if ("BuildState".Equals(property.Name, StringComparison.CurrentCultureIgnoreCase) && "unstable".Equals(property.Value, StringComparison.CurrentCultureIgnoreCase)) { isUnstableBuild = true; } } if (isUnstableBuild) { continue; } var buildId = buildType.Id; dynamic investigationQuery = new Query(serverUrl, username, password); investigationQuery.RestBasePath = @"/httpAuth/app/rest/buildTypes/id:" + buildId +@"/"; buildStatus = BuildStatus.Failed; foreach (var investigation in investigationQuery.Investigations) { var investigationState = investigation.State; if ("taken".Equals(investigationState, StringComparison.CurrentCultureIgnoreCase) || "fixed".Equals(investigationState, StringComparison.CurrentCultureIgnoreCase)) { buildStatus = BuildStatus.Investigating; } } if (buildStatus == BuildStatus.Failed) { return buildStatus; } } } } catch (Exception) { return BuildStatus.Unavailable; } return buildStatus; }