From c4f0a1df97afdd4bf274b4a52935667071f079c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reto=20Sch=C3=BCttel?= Date: Mon, 23 Feb 2015 09:37:06 +0100 Subject: [PATCH] Customizable messages: simplify structure again --- .../plugins/hipchat/HipChatNotifier.java | 92 +++++-------------- .../plugins/hipchat/NotificationType.java | 88 ++++++------------ .../hipchat/HipChatNotifier/config.jelly | 47 ++-------- .../plugins/hipchat/Messages.properties | 18 ++-- .../hipchat/HipChatNotifierBuilder.java | 49 ++-------- .../plugins/hipchat/NotificationTypeTest.java | 38 +++----- 6 files changed, 94 insertions(+), 238 deletions(-) diff --git a/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java b/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java index 5de89f8..69b4a59 100644 --- a/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java +++ b/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java @@ -1,6 +1,5 @@ package jenkins.plugins.hipchat; -import com.google.common.base.Preconditions; import hudson.Extension; import hudson.Launcher; import hudson.Util; @@ -15,8 +14,6 @@ import hudson.tasks.Notifier; import hudson.tasks.Publisher; import hudson.util.FormValidation; -import java.io.IOException; -import java.util.logging.Logger; import jenkins.model.Jenkins; import jenkins.plugins.hipchat.impl.HipChatV1Service; import jenkins.plugins.hipchat.impl.HipChatV2Service; @@ -27,7 +24,10 @@ import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.export.Exported; -import static jenkins.plugins.hipchat.NotificationType.*; +import java.io.IOException; +import java.util.logging.Logger; + +import static jenkins.plugins.hipchat.NotificationType.STARTED; @SuppressWarnings({"unchecked"}) public class HipChatNotifier extends Notifier { @@ -43,20 +43,14 @@ public class HipChatNotifier extends Notifier { private boolean notifyFailure; private boolean notifyBackToNormal; - private String messageStarted; - private String messageBackToNormal; - private String messageSuccess; - private String messageFailure; - private String messageAborted; - private String messageNotBuilt; - private String messageUnstable; + private String startJobMessage; + private String completeJobMessage; @DataBoundConstructor public HipChatNotifier(String token, String room, boolean startNotification, boolean notifySuccess, boolean notifyAborted, boolean notifyNotBuilt, boolean notifyUnstable, boolean notifyFailure, boolean notifyBackToNormal, - String messageStarted, String messageBackToNormal, String messageSuccess, String messageFailure, - String messageAborted, String messageNotBuilt, String messageUnstable) { + String startJobMessage, String completeJobMessage) { this.token = token; this.room = room; this.startNotification = startNotification; @@ -66,13 +60,9 @@ public HipChatNotifier(String token, String room, boolean startNotification, boo this.notifyUnstable = notifyUnstable; this.notifyFailure = notifyFailure; this.notifyBackToNormal = notifyBackToNormal; - this.messageStarted = messageStarted; - this.messageBackToNormal = messageBackToNormal; - this.messageSuccess = messageSuccess; - this.messageFailure = messageFailure; - this.messageAborted = messageAborted; - this.messageNotBuilt = messageNotBuilt; - this.messageUnstable = messageUnstable; + + this.startJobMessage = startJobMessage; + this.completeJobMessage = completeJobMessage; } /* notification enabled disabled setter/getter */ @@ -135,66 +125,28 @@ public void setNotifyBackToNormal(boolean notifyBackToNormal) { /* notification message configuration*/ - public String getMessageStarted() { - return messageStarted; - } - - public void setMessageStarted(String messageStarted) { - this.messageStarted = messageStarted; - } - - public String getMessageBackToNormal() { - return messageBackToNormal; - } - - public void setMessageBackToNormal(String messageBackToNormal) { - this.messageBackToNormal = messageBackToNormal; - } - - public String getMessageSuccess() { - return messageSuccess; - } - - public void setMessageSuccess(String messageSuccess) { - this.messageSuccess = messageSuccess; - } - - public String getMessageFailure() { - return messageFailure; - } - - public void setMessageFailure(String messageFailure) { - this.messageFailure = messageFailure; - } - - public String getMessageAborted() { - return messageAborted; - } - - public void setMessageAborted(String messageAborted) { - this.messageAborted = messageAborted; + public String getCompleteJobMessage() { + return completeJobMessage; } - public String getMessageNotBuilt() { - return messageNotBuilt; + public void setCompleteJobMessage(String completeJobMessage) { + this.completeJobMessage = completeJobMessage; } - public void setMessageNotBuilt(String messageNotBuilt) { - this.messageNotBuilt = messageNotBuilt; + public String getStartJobMessage() { + return startJobMessage; } - public String getMessageUnstable() { - return messageUnstable; + public void setStartJobMessage(String startJobMessage) { + this.startJobMessage = startJobMessage; } - public void setMessageUnstable(String messageUnstable) { - this.messageUnstable = messageUnstable; + public String getCompleteJobMessageDefault() { + return Messages.JobCompleted(); } - public String getMessageDefault(String enumName) { - NotificationType type = NotificationType.valueOf(enumName); - Preconditions.checkNotNull(type, "unknown NotificationType %s", enumName); - return type.getDefaultTemplate(); + public String getStartJobMessageDefault() { + return Messages.JobStarted(); } public String getRoom() { diff --git a/src/main/java/jenkins/plugins/hipchat/NotificationType.java b/src/main/java/jenkins/plugins/hipchat/NotificationType.java index f74b613..9b26a43 100644 --- a/src/main/java/jenkins/plugins/hipchat/NotificationType.java +++ b/src/main/java/jenkins/plugins/hipchat/NotificationType.java @@ -1,21 +1,14 @@ package jenkins.plugins.hipchat; import com.google.common.base.Preconditions; -import com.google.common.collect.Sets; import hudson.EnvVars; import hudson.model.AbstractBuild; import hudson.model.Result; -import hudson.scm.ChangeLogSet; import hudson.util.LogTaskListener; import hudson.util.VariableResolver; -import org.apache.commons.lang.StringUtils; import java.io.IOException; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; import java.util.Map; -import java.util.Set; import java.util.logging.Logger; import static com.google.common.base.Throwables.propagate; @@ -25,104 +18,69 @@ public enum NotificationType { - STARTED("green") { + STARTED("green", true) { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageStarted(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.Started(); } }, ABORTED("gray") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageAborted(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.Aborted(); } }, SUCCESS("green") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageSuccess(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.Success(); } }, FAILURE("red") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageFailure(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.Failure(); } }, NOT_BUILT("gray") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageNotBuilt(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.NotBuilt(); } }, BACK_TO_NORMAL("green") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageBackToNormal(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.BackToNormal(); } }, UNSTABLE("yellow") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { - return notifier.getMessageUnstable(); - } - - @Override - public String getDefaultTemplate() { + protected String getStatus() { return Messages.Unstable(); } }, UNKNOWN("purple") { @Override - protected String getConfiguredTemplateFor(HipChatNotifier notifier) { + protected String getStatus() { throw new IllegalStateException(); } - - @Override - public String getDefaultTemplate() { - return null; - } }; private static final Logger logger = Logger.getLogger(NotificationType.class.getName()); private final String color; + private final boolean isStartType; - private NotificationType(String color) { + private NotificationType(String color, boolean isStartType) { this.color = color; + this.isStartType = isStartType; + } + + private NotificationType(String color) { + this(color, false); } - protected abstract String getConfiguredTemplateFor(HipChatNotifier notifier); - public abstract String getDefaultTemplate(); + protected abstract String getStatus(); public String getColor() { return color; @@ -136,8 +94,15 @@ public final String getMessage(AbstractBuild build, HipChatNotifier notifi } private String getTemplateFor(HipChatNotifier notifier) { - String userConfig = this.getConfiguredTemplateFor(notifier); - String defaultConfig = this.getDefaultTemplate(); + String userConfig, defaultConfig; + if (this.isStartType) { + userConfig = notifier.getStartJobMessage(); + defaultConfig = Messages.JobStarted(); + } else { + userConfig = notifier.getCompleteJobMessage(); + defaultConfig = Messages.JobCompleted(); + } + if (userConfig == null || userConfig.trim().isEmpty()) { Preconditions.checkState(defaultConfig != null, "default config not set for %s", this); return defaultConfig; @@ -154,6 +119,7 @@ private Map collectParametersFor(AbstractBuild build) { String cause = NotificationTypeUtils.getCause(build); String changes = NotificationTypeUtils.getChanges(build); + merged.put("STATUS", this.getStatus()); merged.put("DURATION", build.getDurationString()); merged.put("URL", NotificationTypeUtils.getUrl(build)); merged.put("CAUSE", cause); diff --git a/src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/config.jelly b/src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/config.jelly index 152896f..9e26ff9 100644 --- a/src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/config.jelly +++ b/src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/config.jelly @@ -38,42 +38,15 @@ - - - - - Default: '${instance.getMessageDefault("STARTED")}' - - - - - Default: '${instance.getMessageDefault("BACK_TO_NORMAL")}' - - - - - Default: '${instance.getMessageDefault("SUCCESS")}' - - - - - Default: '${instance.getMessageDefault("FAILURE")}' - - - - - Default: '${instance.getMessageDefault("ABORTED")}' - - - - - Default: '${instance.getMessageDefault("NOT_BUILT")}' - + + + + Default: '${instance.getStartJobMessageDefault()}' + - - - Default: '${instance.getMessageDefault("UNSTABLE")}' - - - + + + Default: '${instance.getCompleteJobMessageDefault()}' + + diff --git a/src/main/resources/jenkins/plugins/hipchat/Messages.properties b/src/main/resources/jenkins/plugins/hipchat/Messages.properties index 6d8644f..ef770a0 100644 --- a/src/main/resources/jenkins/plugins/hipchat/Messages.properties +++ b/src/main/resources/jenkins/plugins/hipchat/Messages.properties @@ -4,11 +4,15 @@ TestNotificationSent=Test Notification Sent StartWithChanges=Started by changes from {0} ({1} file(s) changed) -Started=$JOB_NAME #$BUILD_NUMBER Starting... ($CHANGES_OR_CAUSE) (Open) -BackToNormal=$JOB_NAME #$BUILD_NUMBER Back to normal after $DURATION (Open) -Success=$JOB_NAME #$BUILD_NUMBER Success after $DURATION (Open) -Failure=$JOB_NAME #$BUILD_NUMBER FAILURE after $DURATION (Open) -Aborted=$JOB_NAME #$BUILD_NUMBER ABORTED after $DURATION (Open) -NotBuilt=$JOB_NAME #$BUILD_NUMBER Not built after $DURATION (Open) -Unstable=$JOB_NAME #$BUILD_NUMBER UNSTABLE after $DURATION (Open) +Started=Starting... +BackToNormal=Back to normal +Success=Success +Failure=FAILURE +Aborted=ABORTED +NotBuilt=Not built +Unstable=UNSTABLE + +JobStarted=$JOB_NAME #$BUILD_NUMBER $STATUS ($CHANGES_OR_CAUSE) (Open) +JobCompleted=$JOB_NAME #$BUILD_NUMBER $STATUS after $DURATION (Open) + Unknown=Unknown \ No newline at end of file diff --git a/src/test/java/jenkins/plugins/hipchat/HipChatNotifierBuilder.java b/src/test/java/jenkins/plugins/hipchat/HipChatNotifierBuilder.java index 23f8fa0..dbbbc5e 100644 --- a/src/test/java/jenkins/plugins/hipchat/HipChatNotifierBuilder.java +++ b/src/test/java/jenkins/plugins/hipchat/HipChatNotifierBuilder.java @@ -10,13 +10,8 @@ public class HipChatNotifierBuilder { private boolean notifyUnstable = true; private boolean notifyFailure = true; private boolean notifyBackToNormal = true; - private String messageStarting; - private String messageBackToNormal; - private String messageSuccess; - private String messageFailure; - private String messageAborted; - private String messageNotBuilt; - private String messageUnstable; + private String messageJobStarted; + private String messageJobCompleted; public static HipChatNotifierBuilder notifier() { return new HipChatNotifierBuilder(); @@ -67,44 +62,18 @@ public HipChatNotifierBuilder setNotifyBackToNormal(boolean notifyBackToNormal) return this; } - public HipChatNotifierBuilder setMessageStarting(String messageStarting) { - this.messageStarting = messageStarting; - return this; - } - - public HipChatNotifierBuilder setMessageBackToNormal(String messageBackToNormal) { - this.messageBackToNormal = messageBackToNormal; - return this; - } - - public HipChatNotifierBuilder setMessageSuccess(String messageSuccess) { - this.messageSuccess = messageSuccess; - return this; - } - - public HipChatNotifierBuilder setMessageFailure(String messageFailure) { - this.messageFailure = messageFailure; - return this; - } - - public HipChatNotifierBuilder setMessageAborted(String messageAborted) { - this.messageAborted = messageAborted; - return this; + public HipChatNotifier createHipChatNotifier() { + return new HipChatNotifier(token, room, startNotification, notifySuccess, notifyAborted, notifyNotBuilt, + notifyUnstable, notifyFailure, notifyBackToNormal, messageJobStarted, messageJobCompleted); } - public HipChatNotifierBuilder setMessageNotBuilt(String messageNotBuilt) { - this.messageNotBuilt = messageNotBuilt; + public HipChatNotifierBuilder setMessageJobStarted(String messageJobStarted) { + this.messageJobStarted = messageJobStarted; return this; } - public HipChatNotifierBuilder setMessageUnstable(String messageUnstable) { - this.messageUnstable = messageUnstable; + public HipChatNotifierBuilder setMessageJobCompleted(String messageJobCompleted) { + this.messageJobCompleted = messageJobCompleted; return this; } - - public HipChatNotifier createHipChatNotifier() { - return new HipChatNotifier(token, room, startNotification, notifySuccess, notifyAborted, notifyNotBuilt, - notifyUnstable, notifyFailure, notifyBackToNormal, messageStarting, messageBackToNormal, - messageSuccess, messageFailure, messageAborted, messageNotBuilt, messageUnstable); - } } \ No newline at end of file diff --git a/src/test/java/jenkins/plugins/hipchat/NotificationTypeTest.java b/src/test/java/jenkins/plugins/hipchat/NotificationTypeTest.java index 8c59fa0..0e2054c 100644 --- a/src/test/java/jenkins/plugins/hipchat/NotificationTypeTest.java +++ b/src/test/java/jenkins/plugins/hipchat/NotificationTypeTest.java @@ -56,13 +56,11 @@ public void testGetMessageWithConfig() throws Exception { mockJenkins(); HipChatNotifier notifier = notifier() - .setMessageFailure("you broke it $AUTHOR!") + .setMessageJobCompleted("i feel so $STATUS") .createHipChatNotifier(); - String url = "(Open"; - String prefix = "test-job #33"; - assertNotifierProduces(build(), notifier, SUCCESS, prefix + " Success after 42 sec " + url + ")"); - assertNotifierProduces(build(), notifier, FAILURE, "you broke it Mike!"); - assertNotifierProduces(build(), notifier, NOT_BUILT, prefix + " Not built after 42 sec " + url + ")"); + assertNotifierProduces(build(), notifier, SUCCESS, "i feel so Success"); + assertNotifierProduces(build(), notifier, FAILURE, "i feel so FAILURE"); + assertNotifierProduces(build(), notifier, NOT_BUILT, "i feel so Not built"); } @Test @@ -71,9 +69,8 @@ public void testGetMessageBlankConfiguration() throws Exception { mockJenkins(); HipChatNotifier notifier = notifier() - .setMessageFailure("") - .setMessageNotBuilt(" ") - .setMessageSuccess(null) + .setMessageJobCompleted(" ") + .setMessageJobStarted(null) .createHipChatNotifier(); testNormalConfiguration(build(), notifier); } @@ -84,21 +81,16 @@ public void testGetMessageAllOverride() throws Exception { mockJenkins(); HipChatNotifier notifier = notifier() - .setMessageStarting("MessageStarting") - .setMessageAborted("MessageAborted") - .setMessageSuccess("MessageSuccess") - .setMessageFailure("MessageFailure") - .setMessageNotBuilt("MessageNotBuilt") - .setMessageBackToNormal("MessageBackToNormal") - .setMessageUnstable("MessageUnstable") + .setMessageJobCompleted("completed") + .setMessageJobStarted("started") .createHipChatNotifier(); - assertNotifierProduces(build(), notifier, STARTED, "MessageStarting"); - assertNotifierProduces(build(), notifier, ABORTED, "MessageAborted"); - assertNotifierProduces(build(), notifier, SUCCESS, "MessageSuccess"); - assertNotifierProduces(build(), notifier, FAILURE, "MessageFailure"); - assertNotifierProduces(build(), notifier, NOT_BUILT, "MessageNotBuilt"); - assertNotifierProduces(build(), notifier, BACK_TO_NORMAL, "MessageBackToNormal"); - assertNotifierProduces(build(), notifier, UNSTABLE, "MessageUnstable"); + assertNotifierProduces(build(), notifier, STARTED, "started"); + assertNotifierProduces(build(), notifier, ABORTED, "completed"); + assertNotifierProduces(build(), notifier, SUCCESS, "completed"); + assertNotifierProduces(build(), notifier, FAILURE, "completed"); + assertNotifierProduces(build(), notifier, NOT_BUILT, "completed"); + assertNotifierProduces(build(), notifier, BACK_TO_NORMAL, "completed"); + assertNotifierProduces(build(), notifier, UNSTABLE, "completed"); } private void testNormalConfiguration(AbstractBuild build, HipChatNotifier notifier) {