From 17f6eaadeb91057cb37309271c0b6e79439f64aa Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 11 Dec 2012 18:26:30 +0100 Subject: o Starting to parse out commit info from the Jenkins build too. --- .../esper/testing/jenkins/JenkinsBuildXml.java | 173 +++++++++++++++++++++ .../esper/testing/jenkins/JenkinsClient.java | 149 ++---------------- .../esper/testing/jenkins/JenkinsJobXml.java | 88 +++++++++++ .../esper/testing/jenkins/JenkinsServerActor.java | 10 +- .../esper/testing/jenkins/JenkinsServerOld.java | 2 +- 5 files changed, 283 insertions(+), 139 deletions(-) create mode 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java create mode 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java mode change 100644 => 100755 src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java (limited to 'src/main/java/io') diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java new file mode 100755 index 0000000..e80097b --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java @@ -0,0 +1,173 @@ +package io.trygvis.esper.testing.jenkins; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import org.jdom2.Element; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fj.F; +import fj.data.Option; +import io.trygvis.esper.testing.Util; + +import static fj.data.List.iterableList; +import static fj.data.Option.none; +import static fj.data.Option.some; +import static fj.data.Option.somes; +import static io.trygvis.esper.testing.Util.childText; +import static io.trygvis.esper.testing.jenkins.JenkinsBuildXml.ChangeSetItemXml.parseChangeSetItem; +import static io.trygvis.esper.testing.jenkins.JenkinsBuildXml.RevisionXml.parseRevision; + +public class JenkinsBuildXml { + private static final Logger logger = LoggerFactory.getLogger(JenkinsBuildXml.class); + + public final URI url; + + public final int number; + + public final Option result; + + public final int duration; + + public final long timestamp; + + public final Option changeSet; + + JenkinsBuildXml(URI url, int number, Option result, int duration, long timestamp, Option changeSet) { + this.url = url; + this.number = number; + this.result = result; + this.duration = duration; + this.timestamp = timestamp; + this.changeSet = changeSet; + } + + public static Option parse(Element root) { + Option url = childText(root, "url").bind(Util.parseUri); + Option number = childText(root, "number").bind(Util.parseInt); + Option result = childText(root, "result"); + Option duration = childText(root, "duration").bind(Util.parseInt); + Option timestamp = childText(root, "timestamp").bind(Util.parseLong); + + if (url.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + if (number.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + if (duration.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + if (timestamp.isNone()) { + logger.warn("Missing required field: "); + return none(); + } + + Option changeSet = none(); + Element e = root.getChild("changeSet"); + if (e != null) { + changeSet = ChangeSetXml.parse(e); + } + + return some(new JenkinsBuildXml(url.some(), number.some(), result, duration.some(), timestamp.some(), changeSet)); + } + + public static class ChangeSetXml { + public final List items; + + public final Option revision; + + public ChangeSetXml(List items, Option revision) { + this.items = items; + this.revision = revision; + } + + public static Option parse(Element changeSet) { + + List items = new ArrayList<>(somes(iterableList(changeSet.getChildren("item")).map(parseChangeSetItem)).toCollection()); + + Option revision = Option.fromNull(changeSet.getChild("revision")).bind(parseRevision); + + return some(new ChangeSetXml(items, revision)); + } + } + + public static class ChangeSetItemXml { + public final String commitId; + + public final DateTime date; + + public final String msg; + + /** + * Only subversion has this field + */ + public final Option user; + + public ChangeSetItemXml(String commitId, DateTime date, String msg, Option user) { + this.commitId = commitId; + this.date = date; + this.msg = msg; + this.user = user; + } + + private static final F> parseDate = new F>() { + DateTimeFormatter parser = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z"); + + public Option f(String s) { + try { + return some(parser.parseDateTime(s)); + } catch (IllegalArgumentException e) { + return none(); + } + } + }; + + public static final F> parseChangeSetItem = new F>() { + public Option f(Element item) { + Option commitId = childText(item, "commitId"); + Option date = childText(item, "date").bind(parseDate); + Option msg = childText(item, "msg"); + Option user = childText(item, "user"); + + if (commitId.isNone() || date.isNone() || msg.isNone()) { + return none(); + } + + return some(new ChangeSetItemXml(commitId.some(), date.some(), msg.some(), user)); + } + }; + } + + public static class RevisionXml { + public final String module; + + public final String revision; + + public RevisionXml(String module, String revision) { + this.module = module; + this.revision = revision; + } + + public static final F> parseRevision = new F>() { + public Option f(Element e) { + Option module = childText(e, "module"); + Option revision = childText(e, "revision"); + + if (module.isNone() || revision.isNone()) { + return none(); + } + + return some(new RevisionXml(module.some(), revision.some())); + } + }; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java index 0aa63c1..39dfc66 100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java @@ -2,7 +2,6 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; -import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.jenkins.JenkinsJobXml.*; import io.trygvis.esper.testing.util.*; import org.apache.abdera.*; @@ -20,7 +19,6 @@ import java.util.*; import java.util.List; import static fj.data.Option.*; -import static io.trygvis.esper.testing.Util.*; import static io.trygvis.esper.testing.util.HttpClient.inputStreamOnly; import static java.lang.System.currentTimeMillis; import static org.apache.commons.lang.StringUtils.*; @@ -80,12 +78,12 @@ public class JenkinsClient { if (d.isNone()) { Option n = Option.none(); - return new JenkinsXml(n, n, n, Collections.emptyList()); + return new JenkinsXml(n, n, n, Collections.emptyList()); } Element root = d.some().getRootElement(); - List jobs = new ArrayList<>(); + List jobs = new ArrayList<>(); for (Element job : root.getChildren("job")) { String name = trimToNull(job.getChildText("name")); String u = trimToNull(job.getChildText("url")); @@ -95,7 +93,7 @@ public class JenkinsClient { continue; } - jobs.add(new JenkinsJobEntryXml(name, u, color)); + jobs.add(new JenkinsXml.JobXml(name, u, color)); } return new JenkinsXml( @@ -155,53 +153,7 @@ public class JenkinsClient { } } - public static class JenkinsBuildXml { - - public final URI url; - public final int number; - public final String result; - public final int duration; - public final long timestamp; - - JenkinsBuildXml(URI url, int number, String result, int duration, long timestamp) { - this.url = url; - this.number = number; - this.result = result; - this.duration = duration; - this.timestamp = timestamp; - } - - public static Option parse(Element root) { - Option url = childText(root, "url").bind(Util.parseUri); - Option number = childText(root, "number").bind(Util.parseInt); - Option result = childText(root, "result"); - Option duration = childText(root, "duration").bind(Util.parseInt); - Option timestamp = childText(root, "timestamp").bind(Util.parseLong); - - if(url.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if(number.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if(result.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if(duration.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - if(timestamp.isNone()) { - logger.warn("Missing required field: "); - return none(); - } - - return some(new JenkinsBuildXml(url.some(), number.some(), result.some(), duration.some(), timestamp.some())); - } - }} +} class JenkinsEntryXml { public final String id; @@ -219,99 +171,24 @@ class JenkinsXml { public final Option nodeName; public final Option nodeDescription; public final Option description; - public final List jobs; + public final List jobs; - JenkinsXml(Option nodeName, Option nodeDescription, Option description, List jobs) { + JenkinsXml(Option nodeName, Option nodeDescription, Option description, List jobs) { this.nodeName = nodeName; this.nodeDescription = nodeDescription; this.description = description; this.jobs = jobs; } -} - -class JenkinsJobEntryXml { - public final String name; - public final String url; - public final String color; - - JenkinsJobEntryXml(String name, String url, String color) { - this.name = name; - this.url = url; - this.color = color; - } -} -class JenkinsJobXml { - enum JenkinsJobType { - FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX - } + public static class JobXml { + public final String name; + public final String url; + public final String color; - public final JenkinsJobType type; - public final Option description; - public final Option displayName; - public final Option name; - public final URI url; - public final Option color; - public final boolean buildable; - public final Option lastBuild; - public final Option lastCompletedBuild; - public final Option lastFailedBuild; - public final Option lastSuccessfulBuild; - public final Option lastUnsuccessfulBuild; - - protected JenkinsJobXml(JenkinsJobType type, Option description, Option displayName, - Option name, URI url, Option color, boolean buildable, - Option lastBuild, Option lastCompletedBuild, - Option lastFailedBuild, Option lastSuccessfulBuild, - Option lastUnsuccessfulBuild) { - this.type = type; - this.description = description; - this.displayName = displayName; - this.name = name; - this.url = url; - this.color = color; - this.buildable = buildable; - this.lastBuild = lastBuild; - this.lastCompletedBuild = lastCompletedBuild; - this.lastFailedBuild = lastFailedBuild; - this.lastSuccessfulBuild = lastSuccessfulBuild; - this.lastUnsuccessfulBuild = lastUnsuccessfulBuild; - } - - static class BuildXml { - public final int number; - public final URI url; - public static F> buildXml = new F>() { - public Option f(Element element) { - Option number = childText(element, "number").bind(Util.parseInt); - Option url = childText(element, "url").bind(Util.parseUri); - - if (number.isNone() || url.isNone()) { - return Option.none(); - } - - return some(new BuildXml(number.some(), url.some())); - } - }; - - BuildXml(int number, URI url) { - this.number = number; + JobXml(String name, String url, String color) { + this.name = name; this.url = url; + this.color = color; } } - - public static JenkinsJobXml parse(URI url, JenkinsJobType type, Element root) { - return new JenkinsJobXml(type, - childText(root, "description"), - childText(root, "displayName"), - childText(root, "name"), - childText(root, "url").bind(Util.parseUri).orSome(url), - childText(root, "color"), - childText(root, "buildable").bind(Util.parseBoolean).orSome(false), - child(root, "lastBuild").bind(BuildXml.buildXml), - child(root, "lastCompletedBuild").bind(BuildXml.buildXml), - child(root, "lastFailedBuild").bind(BuildXml.buildXml), - child(root, "lastSuccessfulBuild").bind(BuildXml.buildXml), - child(root, "lastUnsuccessfulBuild").bind(BuildXml.buildXml)); - } } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java new file mode 100755 index 0000000..8cbe049 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java @@ -0,0 +1,88 @@ +package io.trygvis.esper.testing.jenkins; + +import java.net.URI; + +import org.jdom2.Element; + +import fj.F; +import fj.data.Option; +import io.trygvis.esper.testing.Util; + +import static fj.data.Option.some; +import static io.trygvis.esper.testing.Util.child; +import static io.trygvis.esper.testing.Util.childText; + +class JenkinsJobXml { + enum JenkinsJobType { + FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX + } + + public final JenkinsJobType type; + public final Option description; + public final Option displayName; + public final Option name; + public final URI url; + public final Option color; + public final boolean buildable; + public final Option lastBuild; + public final Option lastCompletedBuild; + public final Option lastFailedBuild; + public final Option lastSuccessfulBuild; + public final Option lastUnsuccessfulBuild; + + protected JenkinsJobXml(JenkinsJobType type, Option description, Option displayName, + Option name, URI url, Option color, boolean buildable, + Option lastBuild, Option lastCompletedBuild, + Option lastFailedBuild, Option lastSuccessfulBuild, + Option lastUnsuccessfulBuild) { + this.type = type; + this.description = description; + this.displayName = displayName; + this.name = name; + this.url = url; + this.color = color; + this.buildable = buildable; + this.lastBuild = lastBuild; + this.lastCompletedBuild = lastCompletedBuild; + this.lastFailedBuild = lastFailedBuild; + this.lastSuccessfulBuild = lastSuccessfulBuild; + this.lastUnsuccessfulBuild = lastUnsuccessfulBuild; + } + + static class BuildXml { + public final int number; + public final URI url; + public static F> buildXml = new F>() { + public Option f(Element element) { + Option number = childText(element, "number").bind(Util.parseInt); + Option url = childText(element, "url").bind(Util.parseUri); + + if (number.isNone() || url.isNone()) { + return Option.none(); + } + + return some(new BuildXml(number.some(), url.some())); + } + }; + + BuildXml(int number, URI url) { + this.number = number; + this.url = url; + } + } + + public static JenkinsJobXml parse(URI url, JenkinsJobType type, Element root) { + return new JenkinsJobXml(type, + childText(root, "description"), + childText(root, "displayName"), + childText(root, "name"), + childText(root, "url").bind(Util.parseUri).orSome(url), + childText(root, "color"), + childText(root, "buildable").bind(Util.parseBoolean).orSome(false), + child(root, "lastBuild").bind(BuildXml.buildXml), + child(root, "lastCompletedBuild").bind(BuildXml.buildXml), + child(root, "lastFailedBuild").bind(BuildXml.buildXml), + child(root, "lastSuccessfulBuild").bind(BuildXml.buildXml), + child(root, "lastUnsuccessfulBuild").bind(BuildXml.buildXml)); + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java index 4bbc007..ebc46ce 100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java @@ -46,7 +46,7 @@ public class JenkinsServerActor implements TransactionalActor { continue; } - logger.info("New build: " + entry.id + ", fetching info"); + logger.debug("Build: " + entry.id + ", fetching info"); Option buildXmlOption = client.fetchBuild(apiXml(entry.url)); @@ -56,6 +56,12 @@ public class JenkinsServerActor implements TransactionalActor { JenkinsBuildXml build = buildXmlOption.some(); + if(build.result.isNone()) { + logger.debug("Not done building, is not available."); + continue; + } + String result = build.result.some(); + URI jobUrl = extrapolateJobUrlFromBuildUrl(build.url.toASCIIString()); Option jobDtoOption = dao.selectJobByUrl(jobUrl); @@ -86,7 +92,7 @@ public class JenkinsServerActor implements TransactionalActor { job, entry.id, build.url, - build.result, + result, build.number, build.duration, build.timestamp); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java old mode 100644 new mode 100755 index 7e1dc9a..cfd9939 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java @@ -86,7 +86,7 @@ public class JenkinsServerOld implements Closeable { JenkinsXml xml = client.fetchJobs(url); List jobUris = new ArrayList<>(xml.jobs.size()); - for (JenkinsJobEntryXml job : xml.jobs) { + for (JenkinsXml.JobXml job : xml.jobs) { jobUris.add(URI.create(job.url)); } -- cgit v1.2.3