Skip to content

Commit

Permalink
Customizable messages: simplify structure again
Browse files Browse the repository at this point in the history
  • Loading branch information
retoo authored and aldaris committed Mar 9, 2015
1 parent 228d6ac commit c4f0a1d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 238 deletions.
92 changes: 22 additions & 70 deletions src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jenkins.plugins.hipchat;

import com.google.common.base.Preconditions;
import hudson.Extension;
import hudson.Launcher;
import hudson.Util;
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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() {
Expand Down
88 changes: 27 additions & 61 deletions src/main/java/jenkins/plugins/hipchat/NotificationType.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -154,6 +119,7 @@ private Map<String, String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,15 @@
</f:entry>
</f:section>

<f:advanced>
<f:section title="${%Message Templates}">
<f:entry title="${%Started}" help="/plugin/hipchat/help-projectConfig-hipChatMessages.html">
<f:textbox name="hipchat.messageStarted" value="${instance.messageStarted}"/>
Default: '${instance.getMessageDefault("STARTED")}'
</f:entry>

<f:entry title="${%Back To Normal}">
<f:textbox name="hipchat.messageBackToNormal" value="${instance.messageBackToNormal}"/>
Default: '${instance.getMessageDefault("BACK_TO_NORMAL")}'
</f:entry>

<f:entry title="${%Success}">
<f:textbox name="hipchat.messageSuccess" value="${instance.messageSuccess}"/>
Default: '${instance.getMessageDefault("SUCCESS")}'
</f:entry>

<f:entry title="${%Failure}">
<f:textbox name="hipchat.messageFailure" value="${instance.messageFailure}"/>
Default: '${instance.getMessageDefault("FAILURE")}'
</f:entry>

<f:entry title="${%Aborted}">
<f:textbox name="hipchat.messageAborted" value="${instance.messageAborted}"/>
Default: '${instance.getMessageDefault("ABORTED")}'
</f:entry>

<f:entry title="${%Not Built}">
<f:textbox name="hipchat.messageNotBuilt" value="${instance.messageNotBuilt}"/>
Default: '${instance.getMessageDefault("NOT_BUILT")}'
</f:entry>
<f:section title="${%Message Templates}">
<f:entry title="${%Job started}" help="/plugin/hipchat/help-projectConfig-hipChatMessages.html">
<f:textbox name="hipchat.startJobMessage" value="${instance.startJobMessage}"/>
Default: '${instance.getStartJobMessageDefault()}'
</f:entry>

<f:entry title="${%Unstable}">
<f:textbox name="hipchat.messageUnstable" value="${instance.messageUnstable}"/>
Default: '${instance.getMessageDefault("UNSTABLE")}'
</f:entry>
</f:section>
</f:advanced>
<f:entry title="${%Job completed}">
<f:textbox name="hipchat.completeJobMessage" value="${instance.completeJobMessage}"/>
Default: '${instance.getCompleteJobMessageDefault()}'
</f:entry>
</f:section>
</j:jelly>
18 changes: 11 additions & 7 deletions src/main/resources/jenkins/plugins/hipchat/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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) (<a href="$URL">Open</a>)
BackToNormal=$JOB_NAME #$BUILD_NUMBER Back to normal after $DURATION (<a href="$URL">Open</a>)
Success=$JOB_NAME #$BUILD_NUMBER Success after $DURATION (<a href="$URL">Open</a>)
Failure=$JOB_NAME #$BUILD_NUMBER <b>FAILURE</b> after $DURATION (<a href="$URL">Open</a>)
Aborted=$JOB_NAME #$BUILD_NUMBER ABORTED after $DURATION (<a href="$URL">Open</a>)
NotBuilt=$JOB_NAME #$BUILD_NUMBER Not built after $DURATION (<a href="$URL">Open</a>)
Unstable=$JOB_NAME #$BUILD_NUMBER UNSTABLE after $DURATION (<a href="$URL">Open</a>)
Started=Starting...
BackToNormal=Back to normal
Success=Success
Failure=<b>FAILURE</b>
Aborted=ABORTED
NotBuilt=Not built
Unstable=UNSTABLE

JobStarted=$JOB_NAME #$BUILD_NUMBER $STATUS ($CHANGES_OR_CAUSE) (<a href="$URL">Open</a>)
JobCompleted=$JOB_NAME #$BUILD_NUMBER $STATUS after $DURATION (<a href="$URL">Open</a>)

Unknown=Unknown
Loading

0 comments on commit c4f0a1d

Please sign in to comment.