diff options
Diffstat (limited to 'src/main')
32 files changed, 297 insertions, 117 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/AbstractEntity.java b/src/main/java/io/trygvis/esper/testing/AbstractEntity.java index 927a223..927a223 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/AbstractEntity.java +++ b/src/main/java/io/trygvis/esper/testing/AbstractEntity.java diff --git a/src/main/java/io/trygvis/esper/testing/Config.java b/src/main/java/io/trygvis/esper/testing/Config.java index d1bc2bd..e184d46 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/Config.java +++ b/src/main/java/io/trygvis/esper/testing/Config.java @@ -41,6 +41,7 @@ public class Config { } } + public final String appName; public final GitoriousConfig gitorious; public final long nexusUpdateInterval; @@ -53,8 +54,10 @@ public class Config { private BoneCPDataSource dataSource; - public Config(GitoriousConfig gitorious, long nexusUpdateInterval, long jenkinsUpdateInterval, String databaseUrl, - String databaseUsername, String databasePassword) { + public Config(String appName, GitoriousConfig gitorious, + long nexusUpdateInterval, long jenkinsUpdateInterval, + String databaseUrl, String databaseUsername, String databasePassword) { + this.appName = appName; this.gitorious = gitorious; this.nexusUpdateInterval = nexusUpdateInterval; this.jenkinsUpdateInterval = jenkinsUpdateInterval; @@ -73,7 +76,8 @@ public class Config { properties.load(inputStream); } - return new Config(GitoriousConfig.fromProperties(properties), + return new Config(appName, + GitoriousConfig.fromProperties(properties), getProperty(properties, "nexus.updateInterval").bind(parseInt).valueE("Missing required property: nexus.updateInterval") * 1000, getProperty(properties, "jenkins.updateInterval").bind(parseInt).valueE("Missing required property: jenkins.updateInterval") * 1000, trimToNull(properties.getProperty("database.url")), diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java index 142efd6..1a7cb0b 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -25,6 +25,7 @@ public class Daos implements Closeable { } private final Connection connection; + public final FileDao fileDao; public final GitoriousEventDao gitoriousEventDao; public final GitoriousProjectDao gitoriousProjectDao; public final GitoriousRepositoryDao gitoriousRepositoryDao; @@ -37,6 +38,7 @@ public class Daos implements Closeable { public Daos(Connection c) throws SQLException { this.connection = c; this.seq = counter++; + fileDao = new FileDao(c); gitoriousEventDao = new GitoriousEventDao(c); gitoriousProjectDao = new GitoriousProjectDao(c); gitoriousRepositoryDao = new GitoriousRepositoryDao(c); @@ -58,4 +60,8 @@ public class Daos implements Closeable { public void commit() throws SQLException { connection.commit(); } + + public void rollback() throws SQLException { + connection.rollback(); + } } diff --git a/src/main/java/io/trygvis/esper/testing/EntityRef.java b/src/main/java/io/trygvis/esper/testing/EntityRef.java index a4d25cd..cafcdd5 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/EntityRef.java +++ b/src/main/java/io/trygvis/esper/testing/EntityRef.java @@ -3,6 +3,8 @@ package io.trygvis.esper.testing; import java.sql.*; import java.util.*; +import static io.trygvis.esper.testing.EntityRef.JenkinsBuildRef.*; + public abstract class EntityRef { public final UUID uuid; public final String type; @@ -22,20 +24,22 @@ public abstract class EntityRef { UUID uuid = UUID.fromString(rs.getString(i)); switch (type) { - case "jenkins": - return new JenkinsRef(uuid); + case JENKINS_BUILD_REF: + return new JenkinsBuildRef(uuid); default: throw new SQLException("Unknown reference type: " + type); } } - public static class JenkinsRef extends EntityRef { - private JenkinsRef(UUID uuid) { - super(uuid, "jenkins"); + public static class JenkinsBuildRef extends EntityRef { + public static final String JENKINS_BUILD_REF = "jenkins-build"; + + private JenkinsBuildRef(UUID uuid) { + super(uuid, JENKINS_BUILD_REF); } } - public static JenkinsRef jenkinsRef(UUID uuid) { - return new JenkinsRef(uuid); + public static JenkinsBuildRef jenkinsBuildRef(UUID uuid) { + return new JenkinsBuildRef(uuid); } } diff --git a/src/main/java/io/trygvis/esper/testing/Util.java b/src/main/java/io/trygvis/esper/testing/Util.java index c33e64c..c33e64c 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/Util.java +++ b/src/main/java/io/trygvis/esper/testing/Util.java diff --git a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java index 612a087..612a087 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java diff --git a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java index 3183304..3183304 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/badge/UnbreakablePoller.java diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java index 416faf6..c7923d0 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDao.java @@ -81,6 +81,15 @@ public class BuildDao { } } + public SqlOption<UUID> findBuildByReference(EntityRef ref) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT uuid FROM build WHERE reference_type=? AND reference_uuid=?")) { + int i = 1; + s.setString(i++, ref.type); + s.setString(i, ref.uuid.toString()); + return fromRs(s.executeQuery()).map(getUuid); + } + } + public List<BuildDto> selectBuildsByPerson(UUID person, PageRequest page) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + BUILD + " FROM build b, build_participant bp WHERE bp.person=? AND b.uuid = bp.build ORDER BY created_date LIMIT ? OFFSET ?")) { int i = 1; diff --git a/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java b/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java index c418970..affe1c8 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/FileDao.java @@ -1,10 +1,13 @@ package io.trygvis.esper.testing.core.db; +import io.trygvis.esper.testing.util.sql.*; + import java.io.*; import java.net.*; import java.sql.*; import java.util.*; +import static io.trygvis.esper.testing.util.sql.SqlOption.*; import static java.lang.System.*; public class FileDao { @@ -27,4 +30,15 @@ public class FileDao { return uuid; } } + + public SqlOption<InputStream> load(UUID uuid) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT data FROM file WHERE uuid=?")) { + s.setString(1, uuid.toString()); + ResultSet rs = s.executeQuery(); + if(!rs.next()) { + return none(); + } + return some(rs.getBinaryStream(1)); + } + } } diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java index 62c4f2b..96569dd 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDao.java @@ -14,7 +14,7 @@ import static java.lang.System.*; public class PersonDao { private final Connection c; - public static final String PERSON = "uuid, created_date, name"; + public static final String PERSON = "uuid, created_date, name, mail"; public static final SqlF<ResultSet, PersonDto> person = new SqlF<ResultSet, PersonDto>() { public PersonDto apply(ResultSet rs) throws SQLException { @@ -22,6 +22,7 @@ public class PersonDao { return new PersonDto( UUID.fromString(rs.getString(i++)), new DateTime(rs.getTimestamp(i++).getTime()), + rs.getString(i++), rs.getString(i)); } }; @@ -63,6 +64,19 @@ public class PersonDao { // Person // ----------------------------------------------------------------------- + public UUID insertPerson(String mail, String name) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO person(" + PERSON + ") VALUES(?, ?, ?, ?)")) { + UUID uuid = UUID.randomUUID(); + int i = 1; + s.setString(i++, uuid.toString()); + s.setTimestamp(i++, new Timestamp(currentTimeMillis())); + s.setString(i++, name); + s.setString(i, mail); + s.executeUpdate(); + return uuid; + } + } + public SqlOption<PersonDto> selectPerson(UUID uuid) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=?")) { int i = 1; @@ -71,7 +85,15 @@ public class PersonDao { } } - public List<PersonDto> selectPerson(PageRequest pageRequest) throws SQLException { + public SqlOption<PersonDto> selectPersonByMail(String mail) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE mail=?")) { + int i = 1; + s.setString(i, mail); + return fromRs(s.executeQuery()).map(person); + } + } + + public List<PersonDto> selectPersons(PageRequest pageRequest) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person ORDER BY created_date, name LIMIT ? OFFSET ?")) { int i = 1; s.setInt(i++, pageRequest.count.orSome(10)); @@ -95,6 +117,29 @@ public class PersonDao { } // ----------------------------------------------------------------------- + // Person Jenkins User + // ----------------------------------------------------------------------- + + public void insertPersonJenkinsUser(UUID person, UUID jenkinsUser) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO person_jenkins_user(person, jenkins_user) VALUES(?, ?)")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i, jenkinsUser.toString()); + s.executeUpdate(); + } + } + + public boolean hasPersonJenkinsUser(UUID person, UUID jenkinsUser) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT 1 FROM person_jenkins_user WHERE person=? AND jenkins_user=?")) { + int i = 1; + s.setString(i++, person.toString()); + s.setString(i, jenkinsUser.toString()); + ResultSet rs = s.executeQuery(); + return rs.next(); + } + } + + // ----------------------------------------------------------------------- // Badge // ----------------------------------------------------------------------- diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java index c643fc7..6dbea74 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java @@ -7,9 +7,11 @@ import java.util.*; public class PersonDto extends AbstractEntity { public final String name; + public final String mail; - public PersonDto(UUID uuid, DateTime createdDate, String name) { + public PersonDto(UUID uuid, DateTime createdDate, String name, String mail) { super(uuid, createdDate); this.name = name; + this.mail = mail; } } diff --git a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java index bfcd013..3270db1 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java @@ -1,22 +1,28 @@ package io.trygvis.esper.testing.core.jenkins; import com.jolbox.bonecp.*; +import fj.data.*; import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.core.*; import io.trygvis.esper.testing.core.db.*; import io.trygvis.esper.testing.jenkins.*; +import io.trygvis.esper.testing.jenkins.xml.*; +import io.trygvis.esper.testing.util.*; import io.trygvis.esper.testing.util.sql.*; +import org.joda.time.*; import org.slf4j.*; +import java.io.*; import java.sql.*; import java.util.*; import static fj.data.Option.*; import static io.trygvis.esper.testing.Config.*; -import static io.trygvis.esper.testing.EntityRef.*; +import static io.trygvis.esper.testing.EntityRef.jenkinsBuildRef; public class JenkinsBuildPoller implements TablePoller.NewRowCallback<JenkinsBuildDto> { - Logger logger = LoggerFactory.getLogger(getClass()); + private final Logger logger = LoggerFactory.getLogger(getClass()); + private final XmlParser xmlParser = new XmlParser(); public static void main(String[] args) throws Exception { String pollerName = "jenkins_build"; @@ -32,17 +38,43 @@ public class JenkinsBuildPoller implements TablePoller.NewRowCallback<JenkinsBui new TablePoller<>(pollerName, tableName, columnNames, some("array_length(users, 1) > 0"), f, callback).work(dataSource); } - public void process(Connection c, JenkinsBuildDto jenkinsBuild) throws SQLException { + public void process(Connection c, final JenkinsBuildDto jenkinsBuild) throws SQLException { Daos daos = new Daos(c); - BuildDao buildDao = daos.buildDao; - PersonDao personDao = daos.personDao; + final BuildDao buildDao = daos.buildDao; - UUID uuid = buildDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid)); + SqlOption<InputStream> file = daos.fileDao.load(jenkinsBuild.file); + + if (file.isNone()) { + logger.warn("File unavailable: " + jenkinsBuild.file); + return; + } + + Option<JenkinsBuildXml> o = file.toFj(). + bind(xmlParser.parseDocument). + bind(JenkinsBuildXml.parse); + + if (o.isNone()) { + logger.warn("Unable to parse and process xml: " + jenkinsBuild.file); + return; + } + + final JenkinsBuildXml jenkinsBuildXml = o.some(); + + SqlOption<UUID> uuidBuildO = buildDao.findBuildByReference(jenkinsBuild.toRef()); + + UUID uuidBuild = uuidBuildO.getOrElse(new SqlP0<UUID>() { + public UUID apply() throws SQLException { + return buildDao.insertBuild( + new DateTime(jenkinsBuildXml.timestamp), + "SUCCESS".equals(jenkinsBuildXml.result.orSome("")), + jenkinsBuildRef(jenkinsBuild.uuid)); + } + }); int knownPersons = 0, unknownPersons = 0; for (UUID user : jenkinsBuild.users) { - SqlOption<PersonDto> personO = personDao.selectPersonByJenkinsUuid(user); + SqlOption<PersonDto> personO = daos.personDao.selectPersonByJenkinsUuid(user); // This happens if no one has claimed the user id. if (personO.isNone()) { @@ -54,10 +86,12 @@ public class JenkinsBuildPoller implements TablePoller.NewRowCallback<JenkinsBui UUID person = personO.get().uuid; logger.info("Created build participant, person={}", person); - buildDao.insertBuildParticipant(uuid, person); + buildDao.insertBuildParticipant(uuidBuild, person); } - logger.info("Created build uuid={}, #participants={}, #knownPersons={}, #unknonwnPersons={}", uuid, + logger.info("Created build uuid={}, #participants={}, #knownPersons={}, #unknonwnPersons={}", uuidBuild, jenkinsBuild.users.length, knownPersons, unknownPersons); + + throw new SQLException("wat"); } } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java index 5c7249f..c067bf5 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java @@ -6,27 +6,25 @@ import org.joda.time.*; import java.net.*; import java.util.*; +import static io.trygvis.esper.testing.EntityRef.jenkinsBuildRef; + public class JenkinsBuildDto extends AbstractEntity { public final UUID job; public final UUID file; public final String entryId; public final URI url; - public final String result; - public final int number; - public final int duration; - public final DateTime timestamp; public final UUID[] users; - public JenkinsBuildDto(UUID uuid, DateTime createdDate, UUID job, UUID file, String entryId, URI url, String result, int number, int duration, DateTime timestamp, UUID[] users) { + public JenkinsBuildDto(UUID uuid, DateTime createdDate, UUID job, UUID file, String entryId, URI url, UUID[] users) { super(uuid, createdDate); this.job = job; this.file = file; this.entryId = entryId; this.url = url; - this.result = result; - this.number = number; - this.duration = duration; - this.timestamp = timestamp; this.users = users; } + + public EntityRef toRef() { + return jenkinsBuildRef(uuid); + } } 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 40ce661..24ea6d9 100644..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,8 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; -import io.trygvis.esper.testing.jenkins.JenkinsJobXml.*; +import io.trygvis.esper.testing.jenkins.xml.*; +import io.trygvis.esper.testing.jenkins.xml.JenkinsJobXml.*; import io.trygvis.esper.testing.util.*; import org.apache.abdera.*; import org.apache.abdera.model.*; @@ -18,10 +19,10 @@ import java.net.*; import java.util.*; import java.util.List; -import static fj.P.p; +import static fj.P.*; import static fj.data.Option.*; -import static io.trygvis.esper.testing.util.HttpClient.inputStreamOnly; -import static java.lang.System.currentTimeMillis; +import static io.trygvis.esper.testing.util.HttpClient.*; +import static java.lang.System.*; import static org.apache.commons.lang.StringUtils.*; public class JenkinsClient { @@ -147,7 +148,7 @@ public class JenkinsClient { case "mavenModuleSetBuild": case "mavenBuild": case "freeStyleBuild": - return JenkinsBuildXml.parse(root).map(new F<JenkinsBuildXml, P2<JenkinsBuildXml, byte[]>>() { + return JenkinsBuildXml.parse(root.getDocument()).map(new F<JenkinsBuildXml, P2<JenkinsBuildXml, byte[]>>() { public P2<JenkinsBuildXml, byte[]> f(JenkinsBuildXml x) { return p(x, d.some()._2()); } @@ -157,43 +158,4 @@ public class JenkinsClient { return Option.none(); } } - -} - -class JenkinsEntryXml { - public final String id; - public final DateTime timestamp; - public final URI url; - - JenkinsEntryXml(String id, DateTime timestamp, URI url) { - this.id = id; - this.timestamp = timestamp; - this.url = url; - } -} - -class JenkinsXml { - public final Option<String> nodeName; - public final Option<String> nodeDescription; - public final Option<String> description; - public final List<JobXml> jobs; - - JenkinsXml(Option<String> nodeName, Option<String> nodeDescription, Option<String> description, List<JobXml> jobs) { - this.nodeName = nodeName; - this.nodeDescription = nodeDescription; - this.description = description; - this.jobs = jobs; - } - - public static class JobXml { - public final String name; - public final String url; - public final String color; - - JobXml(String name, String url, String color) { - this.name = name; - this.url = url; - this.color = color; - } - } } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java index e94c7fd..aec9ab3 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java @@ -1,19 +1,19 @@ package io.trygvis.esper.testing.jenkins; import fj.data.*; +import io.trygvis.esper.testing.jenkins.xml.*; import io.trygvis.esper.testing.util.sql.*; import org.joda.time.*; import java.net.*; import java.sql.*; -import java.util.*; import java.util.List; +import java.util.*; -import static fj.data.Option.*; -import static io.trygvis.esper.testing.Util.toList; -import static io.trygvis.esper.testing.Util.toUuidArray; -import static io.trygvis.esper.testing.util.sql.ResultSetF.getInt; -import static io.trygvis.esper.testing.util.sql.SqlOption.fromRs; +import static fj.data.Option.fromNull; +import static io.trygvis.esper.testing.Util.*; +import static io.trygvis.esper.testing.util.sql.ResultSetF.*; +import static io.trygvis.esper.testing.util.sql.SqlOption.*; import static java.lang.System.*; public class JenkinsDao { @@ -53,7 +53,7 @@ public class JenkinsDao { } }; - public static final String JENKINS_BUILD = "uuid, created_date, job, file, entry_id, url, result, number, duration, timestamp, users"; + public static final String JENKINS_BUILD = "uuid, created_date, job, file, entry_id, url, users"; public static final SqlF<ResultSet, JenkinsBuildDto> jenkinsBuild = new SqlF<ResultSet, JenkinsBuildDto>() { public JenkinsBuildDto apply(ResultSet rs) throws SQLException { @@ -65,10 +65,6 @@ public class JenkinsDao { UUID.fromString(rs.getString(i++)), rs.getString(i++), URI.create(rs.getString(i++)), - rs.getString(i++), - rs.getInt(i++), - rs.getInt(i++), - new DateTime(rs.getTimestamp(i++).getTime()), toUuidArray(rs, i)); } }; @@ -191,8 +187,8 @@ public class JenkinsDao { } } - public UUID insertBuild(UUID job, UUID file, String entryId, URI url, String result, int number, int duration, long timestamp, UUID[] users) throws SQLException { - try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_build(" + JENKINS_BUILD + ") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) { + public UUID insertBuild(UUID job, UUID file, String entryId, URI url, UUID[] users) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_build(" + JENKINS_BUILD + ") VALUES(?, ?, ?, ?, ?, ?, ?)")) { UUID uuid = UUID.randomUUID(); int i = 1; s.setString(i++, uuid.toString()); @@ -200,11 +196,7 @@ public class JenkinsDao { s.setString(i++, job.toString()); s.setString(i++, file.toString()); s.setString(i++, entryId); - s.setString(i++, url.toASCIIString()); - s.setString(i++, result); - s.setInt(i++, number); - s.setInt(i++, duration); - s.setTimestamp(i++, new Timestamp(timestamp)); + s.setString(i, url.toASCIIString()); s.setArray(i, c.createArrayOf("varchar", users)); s.executeUpdate(); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java index 5ca449e..e59a953 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJob.java @@ -2,6 +2,8 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; +import io.trygvis.esper.testing.jenkins.xml.JenkinsJobXml; + import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.apiXml; import static java.lang.System.currentTimeMillis; 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 c34f422..00d55c2 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java @@ -3,6 +3,7 @@ package io.trygvis.esper.testing.jenkins; import fj.*; import fj.data.*; import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.jenkins.xml.*; import io.trygvis.esper.testing.util.object.*; import io.trygvis.esper.testing.util.sql.*; import org.slf4j.*; @@ -153,10 +154,6 @@ public class JenkinsServerActor implements TransactionalActor { buildXmlFile, entry.id, build.url, - result, - build.number, - build.duration, - build.timestamp, users.toArray(new UUID[users.size()])); logger.info("Build inserted: {}, #users={} item #{}/{}", uuid, users.size(), i, list.size()); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java index 3cf9e7c..c603506 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java @@ -15,4 +15,8 @@ public class JenkinsServerDto extends AbstractEntity { this.url = url; this.enabled = enabled; } + + public URI userUrl(String id) { + return URI.create(url.toASCIIString() + "/users/" + id); + } } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java index e21f336..38c088a 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java @@ -5,6 +5,7 @@ import fj.data.*; import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.apiXml; +import io.trygvis.esper.testing.jenkins.xml.JenkinsXml; import io.trygvis.esper.testing.util.object.*; import org.joda.time.*; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsBuildXml.java index cb92736..506407e 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildXml.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsBuildXml.java @@ -1,9 +1,10 @@ -package io.trygvis.esper.testing.jenkins; +package io.trygvis.esper.testing.jenkins.xml; import java.net.URI; import java.util.ArrayList; import java.util.List; +import org.jdom2.Document; import org.jdom2.Element; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; @@ -20,8 +21,8 @@ import static fj.data.List.iterableList; import static fj.data.Option.*; import static io.trygvis.esper.testing.Util.childText; import static io.trygvis.esper.testing.Util.parseInt; -import static io.trygvis.esper.testing.jenkins.JenkinsBuildXml.ChangeSetItemXml.parseChangeSetItem; -import static io.trygvis.esper.testing.jenkins.JenkinsBuildXml.RevisionXml.parseRevision; +import static io.trygvis.esper.testing.jenkins.xml.JenkinsBuildXml.ChangeSetItemXml.parseChangeSetItem; +import static io.trygvis.esper.testing.jenkins.xml.JenkinsBuildXml.RevisionXml.parseRevision; public class JenkinsBuildXml { private static final Logger logger = LoggerFactory.getLogger(JenkinsBuildXml.class); @@ -47,7 +48,15 @@ public class JenkinsBuildXml { this.changeSet = changeSet; } - public static Option<JenkinsBuildXml> parse(Element root) { + public static F<Document, Option<JenkinsBuildXml>> parse = new F<Document, Option<JenkinsBuildXml>>() { + public Option<JenkinsBuildXml> f(Document document) { + return parse(document); + } + }; + + public static Option<JenkinsBuildXml> parse(Document doc) { + Element root = doc.getRootElement(); + Option<URI> url = childText(root, "url").bind(Util.parseUri); Option<Integer> number = childText(root, "number").bind(Util.parseInt); Option<String> result = childText(root, "result"); @@ -92,7 +101,7 @@ public class JenkinsBuildXml { public static Option<ChangeSetXml> parse(Element changeSet) { - List<ChangeSetItemXml> items = new ArrayList<>(somes(iterableList(changeSet.getChildren("item")).map(parseChangeSetItem)).toCollection()); + List<ChangeSetItemXml> items = new ArrayList<>(Option.somes(iterableList(changeSet.getChildren("item")).map(parseChangeSetItem)).toCollection()); Option<RevisionXml> revision = Option.fromNull(changeSet.getChild("revision")).bind(parseRevision); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java new file mode 100755 index 0000000..c801ccf --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsEntryXml.java @@ -0,0 +1,17 @@ +package io.trygvis.esper.testing.jenkins.xml; + +import java.net.URI; + +import org.joda.time.DateTime; + +public class JenkinsEntryXml { + public final String id; + public final DateTime timestamp; + public final URI url; + + public JenkinsEntryXml(String id, DateTime timestamp, URI url) { + this.id = id; + this.timestamp = timestamp; + this.url = url; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsJobXml.java index ca661b8..eb4c874 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsJobXml.java @@ -1,4 +1,4 @@ -package io.trygvis.esper.testing.jenkins; +package io.trygvis.esper.testing.jenkins.xml; import java.net.URI; @@ -12,8 +12,8 @@ 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 { +public class JenkinsJobXml { + public enum JenkinsJobType { FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX, MATRIX_CONFIGURATION } diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java new file mode 100755 index 0000000..d599484 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/xml/JenkinsXml.java @@ -0,0 +1,31 @@ +package io.trygvis.esper.testing.jenkins.xml; + +import java.util.List; + +import fj.data.Option; + +public class JenkinsXml { + public final Option<String> nodeName; + public final Option<String> nodeDescription; + public final Option<String> description; + public final List<JobXml> jobs; + + public JenkinsXml(Option<String> nodeName, Option<String> nodeDescription, Option<String> description, List<JobXml> jobs) { + this.nodeName = nodeName; + this.nodeDescription = nodeDescription; + this.description = description; + this.jobs = jobs; + } + + public static class JobXml { + public final String name; + public final String url; + public final String color; + + public JobXml(String name, String url, String color) { + this.name = name; + this.url = url; + this.color = color; + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java b/src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java index 5a4c4d3..5a4c4d3 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java +++ b/src/main/java/io/trygvis/esper/testing/util/EasyX509TrustManager.java diff --git a/src/main/java/io/trygvis/esper/testing/util/XmlParser.java b/src/main/java/io/trygvis/esper/testing/util/XmlParser.java index 624785c..a1bda95 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/util/XmlParser.java +++ b/src/main/java/io/trygvis/esper/testing/util/XmlParser.java @@ -1,5 +1,6 @@ package io.trygvis.esper.testing.util; +import fj.F; import fj.data.*; import org.h2.util.*; import org.jdom2.*; @@ -25,8 +26,13 @@ public class XmlParser { xmlInputFactory = XMLInputFactory.newFactory(); } - public Option<Document> parseDocument(InputStream stream) { + public F<InputStream, Option<Document>> parseDocument = new F<InputStream, Option<Document>>() { + public Option<Document> f(InputStream inputStream) { + return parseDocument(inputStream); + } + }; + public Option<Document> parseDocument(InputStream stream) { try { if (debugXml) { // TODO: Pretty print diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java index 7169372..7169372 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java +++ b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java index e4e8197..e4e8197 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java index 286a872..43fb440 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java @@ -1,5 +1,7 @@ package io.trygvis.esper.testing.util.sql; +import fj.data.*; + import java.sql.*; public abstract class SqlOption<A> { @@ -45,6 +47,10 @@ public abstract class SqlOption<A> { public abstract A getOrElse(A a); + public abstract A getOrElse(SqlP0<A> p) throws SQLException; + + public abstract Option<A> toFj(); + public static <A> SqlOption<A> fromNull(A a) { if (a != null) { return some(a); @@ -74,6 +80,14 @@ public abstract class SqlOption<A> { return a; } + public A getOrElse(SqlP0<A> p) throws SQLException { + return p.apply(); + } + + public Option<A> toFj() { + return Option.none(); + } + public String toString() { return "None"; } @@ -102,6 +116,14 @@ public abstract class SqlOption<A> { return this.a; } + public A getOrElse(SqlP0<A> p) throws SQLException { + return a; + } + + public Option<A> toFj() { + return Option.some(a); + } + public String toString() { return "Some(" + a + ")"; } diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java new file mode 100755 index 0000000..23c48d1 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlP0.java @@ -0,0 +1,7 @@ +package io.trygvis.esper.testing.util.sql; + +import java.sql.*; + +public abstract class SqlP0<A> { + public abstract A apply() throws SQLException; +} diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java index 9b22674..7548a32 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/CoreResource.java @@ -40,7 +40,7 @@ public class CoreResource extends AbstractResource { return da.inTransaction(new DatabaseAccess.DaosCallback<List<PersonJson>>() { public List<PersonJson> run(Daos daos) throws SQLException { List<PersonJson> list = new ArrayList<>(); - for (PersonDto person : daos.personDao.selectPerson(pageRequest)) { + for (PersonDto person : daos.personDao.selectPersons(pageRequest)) { list.add(getPersonJson(daos, person)); } return list; diff --git a/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java index 292be71..aab108b 100644..100755 --- a/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java +++ b/src/main/java/io/trygvis/esper/testing/web/resource/JenkinsResource.java @@ -1,17 +1,21 @@ package io.trygvis.esper.testing.web.resource; +import fj.*; +import fj.data.*; import io.trygvis.esper.testing.*; import io.trygvis.esper.testing.jenkins.*; +import io.trygvis.esper.testing.jenkins.xml.*; +import io.trygvis.esper.testing.util.*; import io.trygvis.esper.testing.util.sql.*; import io.trygvis.esper.testing.web.*; import org.joda.time.*; -import javax.ws.rs.*; -import javax.ws.rs.core.*; import java.net.*; import java.sql.*; import java.util.*; import java.util.List; +import javax.ws.rs.*; +import javax.ws.rs.core.*; @Path("/resource/jenkins") public class JenkinsResource extends AbstractResource { @@ -102,6 +106,8 @@ public class JenkinsResource extends AbstractResource { protected abstract T run() throws SQLException; + private final XmlParser xmlParser = new XmlParser(); + public T run(Daos daos) throws SQLException { this.daos = daos; return run(); @@ -120,22 +126,30 @@ public class JenkinsResource extends AbstractResource { } }; - protected SqlF<JenkinsJobDto,JenkinsJobJson> getJenkinsJobJson = new SqlF<JenkinsJobDto, JenkinsJobJson>() { + protected SqlF<JenkinsJobDto, JenkinsJobJson> getJenkinsJobJson = new SqlF<JenkinsJobDto, JenkinsJobJson>() { public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.displayName.toNull()); } }; - protected SqlF<JenkinsJobDto,JenkinsJobJson> getJenkinsJobJsonDetail = new SqlF<JenkinsJobDto, JenkinsJobJson>() { + protected SqlF<JenkinsJobDto, JenkinsJobJson> getJenkinsJobJsonDetail = new SqlF<JenkinsJobDto, JenkinsJobJson>() { public JenkinsJobJson apply(JenkinsJobDto job) throws SQLException { int buildCount = daos.jenkinsDao.selectBuildCountByJob(job.uuid); return new JenkinsJobJson(job.uuid, job.createdDate, job.server, job.displayName.toNull(), buildCount); } }; - protected SqlF<JenkinsBuildDto,JenkinsBuildJson> getJenkinsBuildJson = new SqlF<JenkinsBuildDto, JenkinsBuildJson>() { + protected SqlF<JenkinsBuildDto, JenkinsBuildJson> getJenkinsBuildJson = new SqlF<JenkinsBuildDto, JenkinsBuildJson>() { public JenkinsBuildJson apply(JenkinsBuildDto dto) throws SQLException { - return new JenkinsBuildJson(dto.uuid, dto.createdDate, dto.result); + Option<String> result = daos.fileDao.load(dto.file).toFj(). + bind(xmlParser.parseDocument). + bind(JenkinsBuildXml.parse).map(new F<JenkinsBuildXml, String>() { + public String f(JenkinsBuildXml xml) { + return xml.result.orSome("unknown"); + } + }); + + return new JenkinsBuildJson(dto.uuid, dto.createdDate, result.orSome("unknown")); } }; } diff --git a/src/main/resources/ddl-jenkins.sql b/src/main/resources/ddl-jenkins.sql index c75f836..3e420a2 100644..100755 --- a/src/main/resources/ddl-jenkins.sql +++ b/src/main/resources/ddl-jenkins.sql @@ -44,10 +44,10 @@ CREATE TABLE jenkins_build ( file CHAR(36) NOT NULL, entry_id VARCHAR(1000) NOT NULL, url VARCHAR(1000) NOT NULL, - result VARCHAR(100) NOT NULL, - number INT NOT NULL, - duration INT NOT NULL, - timestamp TIMESTAMP NOT NULL, +-- result VARCHAR(100) NOT NULL, +-- number INT NOT NULL, +-- duration INT NOT NULL, +-- timestamp TIMESTAMP NOT NULL, users CHAR(36) [], CONSTRAINT pk_jenkins_build PRIMARY KEY (UUID), |