diff options
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java')
-rw-r--r-- | src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java | 76 |
1 files changed, 63 insertions, 13 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java index 92c0569..a913b63 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java @@ -9,6 +9,7 @@ import java.sql.*; import java.util.List; import java.util.*; +import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.*; public class JenkinsServer implements TransactionalActor { @@ -23,9 +24,9 @@ public class JenkinsServer implements TransactionalActor { public void act(Connection c) throws Exception { JenkinsDao dao = new JenkinsDao(c); - Option<List<JenkinsEntryXml>> option = client.fetchRss(URI.create(server.uri.toASCIIString() + "/rssAll")); + Option<List<JenkinsEntryXml>> option = client.fetchRss(URI.create(server.url.toASCIIString() + "/rssAll")); - if(option.isNone()) { + if (option.isNone()) { return; } @@ -38,39 +39,88 @@ public class JenkinsServer implements TransactionalActor { for (JenkinsEntryXml entry : list) { Option<JenkinsBuildDto> o = dao.selectBuildByEntryId(entry.id); - if(o.isSome()) { - logger.info("Old event: " + entry.id); + if (o.isSome()) { + logger.debug("Old event: " + entry.id); continue; } - logger.info("New event: " + entry.id + ", fetching build info"); + logger.info("New build: " + entry.id + ", fetching info"); i++; - Option<JenkinsBuildXml> o2 = client.fetchBuild(URI.create(entry.uri.toASCIIString() + "/api/xml")); + Option<JenkinsBuildXml> o2 = client.fetchBuild(apiXml(entry.url)); - if(o2.isNone()) { + if (o2.isNone()) { continue; } JenkinsBuildXml build = o2.some(); + Option<UUID> job = findJob(dao, server, build); + + if (job.isNone()) { + continue; + } + UUID uuid = dao.insertBuild( - server.uuid, + job.some(), entry.id, - build.uri, + build.url, build.result, build.number, build.duration, build.timestamp); logger.info("Build inserted: " + uuid + ", i=" + i); - -// if(i == 1) { -// break; -// } } logger.info("Inserted " + i + " new events."); } + + private Option<UUID> findJob(JenkinsDao dao, JenkinsServerDto server, JenkinsBuildXml build) throws SQLException { + URI jobUrl = createJobUrl(build.url.toASCIIString()); + + Option<JenkinsJobDto> o2 = dao.selectJobByUrl(jobUrl); + + if (o2.isSome()) { + return some(o2.some().uuid); + } + + logger.info("New job: " + jobUrl + ", fetching info"); + + Option<JenkinsJobXml> o = client.fetchJob(apiXml(jobUrl)); + + if (o.isNone()) { + return none(); + } + + JenkinsJobXml xml = o.some(); + + UUID uuid = dao.insertJob(server.uuid, xml.url, xml.displayName); + + logger.info("New job: " + xml.displayName.orSome(xml.url.toASCIIString()) + ", uuid=" + uuid); + + return some(uuid); + } + + /** + * This sucks, a build should really include the URL to the job. + */ + public static URI createJobUrl(String u) { + if (u.matches(".*/[-_a-zA-Z]*=.*/[0-9]*/")) { + u = u.substring(0, u.lastIndexOf("/")); + u = u.substring(0, u.lastIndexOf("/")); + u = u.substring(0, u.lastIndexOf("/") + 1); + + return URI.create(u); + } + + if (u.endsWith("/")) { + u = u.substring(0, u.length() - 1); + } + int i = u.lastIndexOf("/"); + u = u.substring(0, i); + + return URI.create(u + "/"); + } } |