From 125368d4fd53e89bb40f147ae6461fe4803376a8 Mon Sep 17 00:00:00 2001 From: Radzislaw Galler Date: Fri, 8 Aug 2014 23:32:58 +0100 Subject: [PATCH 1/3] Added dependency on token-macro-plugin --- pom.xml | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index c527ad4..28c7d04 100644 --- a/pom.xml +++ b/pom.xml @@ -40,10 +40,9 @@ http://repo.jenkins-ci.org/public/ - java.net-m2-repository - http://maven.jenkins-ci.org:8081/content/repositories/releases/ - - + java.net-m2-repository + http://maven.jenkins-ci.org:8081/content/repositories/releases/ + @@ -52,11 +51,18 @@ http://repo.jenkins-ci.org/public/ - jenkins-m2-repository - Jenkins Plugin Repository - http://maven.jenkins- - ci.org:8081/content/repositories/releases/ - default - + jenkins-m2-repository + Jenkins Plugin Repository + http://maven.jenkins-ci.org:8081/content/repositories/releases/ + default + + + + org.jenkins-ci.plugins + token-macro + 1.10 + true + + From 4c3f480d075436e627b75b9706d05ec115c1036d Mon Sep 17 00:00:00 2001 From: Radzislaw Galler Date: Fri, 8 Aug 2014 23:33:46 +0100 Subject: [PATCH 2/3] Tokens, build params and envs in tags are now expanded --- src/main/java/com/flowdock/jenkins/FlowdockNotifier.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java b/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java index cfb4c10..3ea34fd 100644 --- a/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java +++ b/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java @@ -16,6 +16,7 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.QueryParameter; +import org.jenkinsci.plugins.tokenmacro.TokenMacro; import java.io.PrintStream; import java.util.HashMap; @@ -122,17 +123,18 @@ protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, Buil try { FlowdockAPI api = new FlowdockAPI(getDescriptor().apiUrl(), flowToken); TeamInboxMessage msg = TeamInboxMessage.fromBuild(build, buildResult); - msg.setTags(notificationTags); + String expandedTags = TokenMacro.expandAll(build, listener, notificationTags); + msg.setTags(expandedTags); api.pushTeamInboxMessage(msg); listener.getLogger().println("Flowdock: Team Inbox notification sent successfully"); if(build.getResult() != Result.SUCCESS && chatNotification) { ChatMessage chatMsg = ChatMessage.fromBuild(build, buildResult); - chatMsg.setTags(notificationTags); + chatMsg.setTags(expandedTags); api.pushChatMessage(chatMsg); logger.println("Flowdock: Chat notification sent successfully"); } - } catch(FlowdockException ex) { + } catch(Exception ex) { logger.println("Flowdock: failed to send notification"); logger.println("Flowdock: " + ex.getMessage()); } From a464d1c2d3bb89fe9f05498b3d179b0d5acbf88b Mon Sep 17 00:00:00 2001 From: Radzislaw Galler Date: Tue, 23 Sep 2014 21:09:11 +0100 Subject: [PATCH 3/3] Applied review suggestions. --- .../flowdock/jenkins/FlowdockNotifier.java | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java b/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java index 3ea34fd..64a23be 100644 --- a/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java +++ b/src/main/java/com/flowdock/jenkins/FlowdockNotifier.java @@ -3,6 +3,7 @@ import com.flowdock.jenkins.exception.FlowdockException; import hudson.Extension; import hudson.Launcher; +import hudson.Plugin; import hudson.util.FormValidation; import hudson.model.AbstractBuild; import hudson.model.BuildListener; @@ -16,8 +17,8 @@ import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.QueryParameter; -import org.jenkinsci.plugins.tokenmacro.TokenMacro; +import java.io.IOException; import java.io.PrintStream; import java.util.HashMap; import java.util.Map; @@ -36,6 +37,7 @@ public class FlowdockNotifier extends Notifier { private final boolean notifyUnstable; private final boolean notifyAborted; private final boolean notifyNotBuilt; + private final Plugin tokenMacroPlugin; // Fields in config.jelly must match the parameter names in the "DataBoundConstructor" @DataBoundConstructor @@ -61,6 +63,9 @@ public FlowdockNotifier(String flowToken, String notificationTags, String chatNo this.notifyMap.put(BuildResult.UNSTABLE, this.notifyUnstable); this.notifyMap.put(BuildResult.ABORTED, this.notifyAborted); this.notifyMap.put(BuildResult.NOT_BUILT, this.notifyNotBuilt); + + // set up optional dependency plugins + tokenMacroPlugin = jenkins.model.Jenkins.getInstance().getPlugin("token-macro"); } public String getFlowToken() { @@ -104,7 +109,7 @@ public boolean needsToRunAfterFinalized() { } @Override - public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) { + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { BuildResult buildResult = BuildResult.fromBuild(build); if(shouldNotify(buildResult)) { notifyFlowdock(build, buildResult, listener); @@ -118,15 +123,25 @@ public boolean shouldNotify(BuildResult buildResult) { return notifyMap.get(buildResult); } - protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, BuildListener listener) { + protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, BuildListener listener) throws IOException, InterruptedException { PrintStream logger = listener.getLogger(); try { FlowdockAPI api = new FlowdockAPI(getDescriptor().apiUrl(), flowToken); TeamInboxMessage msg = TeamInboxMessage.fromBuild(build, buildResult); - String expandedTags = TokenMacro.expandAll(build, listener, notificationTags); + String expandedTags = null; + + if(tokenMacroPlugin != null) { + try { + expandedTags = org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(build, listener, notificationTags); + } catch(org.jenkinsci.plugins.tokenmacro.MacroEvaluationException e) { + logger.println("Error substituting with token-macro plugin: " + e); + } + } else { + expandedTags = notificationTags; + } msg.setTags(expandedTags); api.pushTeamInboxMessage(msg); - listener.getLogger().println("Flowdock: Team Inbox notification sent successfully"); + logger.println("Flowdock: Team Inbox notification sent successfully"); if(build.getResult() != Result.SUCCESS && chatNotification) { ChatMessage chatMsg = ChatMessage.fromBuild(build, buildResult); @@ -134,7 +149,7 @@ protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, Buil api.pushChatMessage(chatMsg); logger.println("Flowdock: Chat notification sent successfully"); } - } catch(Exception ex) { + } catch(FlowdockException ex) { logger.println("Flowdock: failed to send notification"); logger.println("Flowdock: " + ex.getMessage()); }