Created
February 26, 2016 11:42
-
-
Save akoeplinger/eafdca68056f12862afe to your computer and use it in GitHub Desktop.
Fetch test results from Jenkins
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Xml; | |
using System.Xml.Linq; | |
namespace JenkinsFailedTestsTable | |
{ | |
class MainClass | |
{ | |
static HttpClient http = new HttpClient(); | |
const string jenkinsUrl = "https://jenkins.mono-project.com/job/test-mono-mainline"; | |
public static void Main(string[] args) | |
{ | |
var labels = new[] { "debian-amd64","debian-armel", "debian-armhf", "debian-i386", "osx-i386" }; | |
var history = DateTime.Now.AddDays(-5); | |
var timestamp = (long)(history - new DateTime(1970, 1, 1)).TotalMilliseconds; | |
var sw = Stopwatch.StartNew(); | |
foreach (var label in labels) | |
{ | |
Console.WriteLine($"Failed tests on {label} since {history.Year}-{history.Month:D2}-{history.Day:D2}"); | |
Console.WriteLine("---------------------------------------------"); | |
Console.WriteLine(); | |
var failedTests = GetFailedTests(label, timestamp).Result; | |
var groupedTests = from test in failedTests | |
group test by test into g | |
let count = g.Count() | |
orderby count descending | |
select new { TestName = g.Key, Count = count }; | |
foreach (var entry in groupedTests) | |
Console.WriteLine($"{entry.Count, 3} {entry.TestName}"); | |
} | |
Console.WriteLine(); | |
Console.WriteLine("Data retrieval took " + sw.Elapsed); | |
Console.ReadLine(); | |
} | |
public async static Task<List<string>> GetFailedTests(string label, long timestamp) | |
{ | |
var failedTest = new List<string>(); | |
var xdoc = new XmlDocument(); | |
var builds = await http.GetStringAsync($"{jenkinsUrl}/label={label}/api/xml?xpath=//build[result=%27UNSTABLE%27%20and%20timestamp%3E{timestamp}]&depth=1&wrapper=builds&tree=builds[id,result,timestamp]"); | |
xdoc.LoadXml(builds); | |
foreach (XmlNode build in xdoc.FirstChild.ChildNodes) | |
{ | |
var id = build["id"].InnerText; | |
var tests = await http.GetStringAsync($"{jenkinsUrl}/label={label}/{id}/testReport/api/xml?tree=suites[cases[className,name,status]]&xpath=//case[status=%27REGRESSION%27]|//case[status=%27FAILED%27]&wrapper=testResult"); | |
xdoc.LoadXml(tests); | |
foreach (XmlNode cas in xdoc.FirstChild.ChildNodes) | |
{ | |
failedTest.Add(cas["className"].InnerText + "." + cas["name"].InnerText); | |
} | |
} | |
failedTest.Sort(); | |
return failedTest; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment