Created
November 29, 2012 08:55
-
-
Save RhysC/4167687 to your computer and use it in GitHub Desktop.
Team city linq query over http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; | |
var results = from project in TeamCityWebRequest.GetTeamCityProjects() | |
from build in TeamCityWebRequest.GetTeamCityBuilds(project) | |
select new {project, build}; | |
results.Dump(); | |
} | |
public class TeamCityWebRequest | |
{ | |
public const string rootUrl = @"https://myteamcitywebserver.com"; | |
public const string projectUrl = rootUrl + @"/httpAuth/app/rest/projects"; | |
public static string Create(string url) | |
{ | |
var request = WebRequest.Create(url); | |
var username = "USER_NAME"; | |
var password = "PASSWORD"; | |
var cred = new CredentialCache(); | |
cred.Add(new Uri(url), "Basic", new NetworkCredential(username, password)); | |
request.Credentials = cred; | |
request.Headers.Add("Authorization","Basic" + Convert.ToBase64String(new ASCIIEncoding().GetBytes(username +":"+password))); | |
var response = request.GetResponse(); | |
var respStream = response.GetResponseStream(); | |
return new StreamReader(respStream, Encoding.UTF8).ReadToEnd(); | |
} | |
public static IEnumerable<TeamCityProject> GetTeamCityProjects() | |
{ | |
var projectsStr = Create(projectUrl); | |
var projectsXml = XElement.Parse(projectsStr); | |
return from project in projectsXml.Descendants("project") | |
select new TeamCityProject{ | |
Name = project.Attribute("name").Value, | |
Url = string.Format("{0}{1}", rootUrl, project.Attribute("href").Value) | |
}; | |
} | |
public static IEnumerable<TeamCityBuild> GetTeamCityBuilds(TeamCityProject project) | |
{ | |
var projectStr = Create(project.Url); | |
var projectsXml = XElement.Parse(projectStr); | |
return from buildType in projectsXml.Descendants("buildType") | |
select GetTeamCityBuild(buildType.Attribute("id").Value); | |
} | |
private static TeamCityBuild GetTeamCityBuild(string id) | |
{ | |
var buildStr = Create(TeamCityBuild.GetStatusUrl(id)); | |
var buildXml = XElement.Parse(buildStr); | |
return new TeamCityBuild | |
{ | |
Id = id, | |
Name = buildXml.Element("buildType").Attribute("name").Value, | |
Number = buildXml.Attribute("number").Value, | |
BuildStatus = buildXml.Attribute("status").Value | |
}; | |
} | |
} | |
public class TeamCityProject | |
{ | |
public string Name { get; set; } | |
public string Url { get; set; } | |
} | |
// https://ccbuild.cloudapp.net/httpAuth/app/rest/buildTypes/id:bt59 -- shows setting of the given build | |
// https://ccbuild.cloudapp.net/app/rest/builds/buildType:(id:bt59) -- shows status of last build | |
// https://ccbuild.cloudapp.net/app/rest/builds/buildType:(id:bt59)/statusIcon -- shows the current status icon for the build! | |
public class TeamCityBuild | |
{ | |
public string Id { get; set; } | |
public string Name { get; set; } | |
public string Number { get; set; }//ie build number or version | |
public string BuildStatus { get; set; } | |
public string StatusUrl | |
{ | |
get{ return GetStatusUrl(Id); } | |
} | |
public string StatusIconUrl | |
{ | |
get{ return string.Format("{0}/app/rest/builds/buildType:(id:{1})/statusIcon",TeamCityWebRequest.rootUrl,Id);} | |
} | |
public string SettingsUrl | |
{ | |
get{ return string.Format("{0}/httpAuth/app/rest/buildTypes/id:{1}",TeamCityWebRequest.rootUrl,Id);} | |
} | |
public static string GetStatusUrl(string id) | |
{ | |
return string.Format("{0}/app/rest/builds/buildType:(id:{1})",TeamCityWebRequest.rootUrl,id); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment