From 7cf971cfe478fc4a170ea17274f44b24eae97e2b Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 6 May 2014 22:30:40 +0200 Subject: o Support for triggering builds from MQ messages with an MVEL expression. --- .../plugins/activemq/ActiveMqBuildTrigger.java | 101 +++++++++++++++++++++ .../jenkinsci/plugins/activemq/ActiveMqPlugin.java | 27 ++++++ .../activemq/ActiveMqBuildTrigger/config.jelly | 6 ++ 3 files changed, 134 insertions(+) create mode 100644 src/main/java/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger.java create mode 100644 src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqBuildTrigger/config.jelly (limited to 'src') 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 { + + 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> 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 @@ + + + + + + -- cgit v1.2.3