summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2014-05-06 22:30:40 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2014-05-06 22:30:40 +0200
commit7cf971cfe478fc4a170ea17274f44b24eae97e2b (patch)
treea9155d9d732462e2122bbe4d11323a07f442439f
parent7e2d2c074c3f09266dda31c772f4bec61e8d5742 (diff)
downloadactivemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.tar.gz
activemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.tar.bz2
activemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.tar.xz
activemq-plugin-7cf971cfe478fc4a170ea17274f44b24eae97e2b.zip
o Support for triggering builds from MQ messages with an MVEL expression.
-rw-r--r--pom.xml8
-rw-r--r--src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java101
-rw-r--r--src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java27
-rw-r--r--src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly6
4 files changed, 141 insertions, 1 deletions
diff --git a/pom.xml b/pom.xml
index 28f454f..3cebc1e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -26,6 +27,11 @@
<artifactId>activemq-pool</artifactId>
<version>${version.activemq}</version>
</dependency>
+ <dependency>
+ <groupId>org.mvel</groupId>
+ <artifactId>mvel2</artifactId>
+ <version>2.1.9.Final</version>
+ </dependency>
</dependencies>
<licenses>
diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java
new file mode 100644
index 0000000..f3a2a79
--- /dev/null
+++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java
@@ -0,0 +1,101 @@
+package org.jenkinsci.plugins.activemq;
+
+import hudson.Extension;
+import hudson.model.BuildableItem;
+import hudson.model.Item;
+import hudson.triggers.Trigger;
+import hudson.triggers.TriggerDescriptor;
+import hudson.util.FormValidation;
+import org.kohsuke.stapler.DataBoundConstructor;
+import org.kohsuke.stapler.QueryParameter;
+import org.mvel2.MVEL;
+import org.slf4j.Logger;
+
+import java.io.Serializable;
+import java.util.Map;
+
+import static java.util.Collections.singletonMap;
+import static org.apache.commons.lang.StringUtils.trimToNull;
+import static org.jenkinsci.plugins.activemq.ActiveMqClient.BuildRequest;
+import static org.mvel2.MVEL.compileExpression;
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class ActiveMqBuildTrigger extends Trigger<BuildableItem> {
+
+ private static final Logger log = getLogger(ActiveMqBuildTrigger.class);
+
+ private String mvel;
+
+ private transient Serializable compiledMvel;
+
+ // parameters['jobName'] == 'fast-build'
+ @DataBoundConstructor
+ public ActiveMqBuildTrigger(String mvel) {
+ log.info("ActiveMqBuildTrigger.ActiveMqBuildTrigger");
+ setMvel_(mvel);
+ }
+
+ public String getMvel() {
+ log.info("ActiveMqBuildTrigger.getMvel");
+ return mvel;
+ }
+
+ private void setMvel_(String mvel) {
+ mvel = trimToNull(mvel);
+ if (mvel == null) {
+ this.mvel = null;
+ this.compiledMvel = null;
+ return;
+ }
+
+ try {
+ compiledMvel = compileExpression(mvel);
+ this.mvel = mvel;
+ } catch (Exception e) {
+ log.warn("Unable to compile MVEL", e);
+ }
+ }
+
+// public FormValidation doCheckMvel(@QueryParameter String mvel) {
+// mvel = trimToNull(mvel);
+// if (mvel == null) {
+// return FormValidation.ok();
+// }
+// try {
+// compileExpression(mvel);
+// return FormValidation.ok();
+// } catch (Exception e) {
+// log.info("Unable to compile MVEL: " + mvel, e);
+// return FormValidation.error(e, "Unable to compile MVEL");
+// }
+// }
+
+// public Serializable getCompiledMvel() {
+// return compiledMvel;
+// }
+
+ public boolean matches(BuildRequest req) {
+ if (compiledMvel == null) {
+ return false;
+ }
+
+ Map<String, Map<String, String>> context = singletonMap("parameters", req.parameters);
+ Boolean b = MVEL.executeExpression(compiledMvel, context, Boolean.class);
+
+ return b != null && b;
+ }
+
+ @Extension
+ public static class DescriptorImpl extends TriggerDescriptor {
+
+ @Override
+ public boolean isApplicable(Item item) {
+ return item instanceof BuildableItem;
+ }
+
+ @Override
+ public String getDisplayName() {
+ return "ActiveMQ Trigger";
+ }
+ }
+}
diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java
index c9c277c..cc73898 100644
--- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java
+++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java
@@ -2,9 +2,12 @@ package org.jenkinsci.plugins.activemq;
import hudson.Extension;
import hudson.Plugin;
+import hudson.model.AbstractProject;
import hudson.model.Build;
+import hudson.model.Cause;
import hudson.model.TaskListener;
import jenkins.model.GlobalConfiguration;
+import jenkins.model.Jenkins;
import org.slf4j.Logger;
import javax.annotation.Nonnull;
@@ -88,5 +91,29 @@ public class ActiveMqPlugin extends Plugin implements ActiveMqClient.BuildReques
public void onBuildRequest(ActiveMqClient.BuildRequest req) {
log.info("ActiveMqPlugin.onBuildRequest");
log.info(req.parameters.toString());
+
+ for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
+ log.info("Checking project " + project.getDisplayName());
+ ActiveMqBuildTrigger trigger = (ActiveMqBuildTrigger) project.getTrigger(ActiveMqBuildTrigger.class);
+
+ if (trigger == null) {
+ log.debug("No trigger configured");
+ continue;
+ }
+
+ if (trigger.matches(req)) {
+ log.debug("MVEL matched!");
+ project.scheduleBuild(new ActiveMqCause());
+ } else {
+ log.debug("MVEL did not match");
+ }
+ }
+ }
+
+ public static class ActiveMqCause extends Cause {
+ @Override
+ public String getShortDescription() {
+ return "ActiveMQ cause";
+ }
}
}
diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly
new file mode 100644
index 0000000..bcfde6e
--- /dev/null
+++ b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly
@@ -0,0 +1,6 @@
+<?jelly escape-by-default='true'?>
+<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
+ <f:entry title="MVEL Expression" field="mvel">
+ <f:textarea />
+ </f:entry>
+</j:jelly>