Permalink
Browse files

Merge pull request #24 from peterlynch/2012-updates

2012 updates
  • Loading branch information...
2 parents 6cb438d + ee4c7ca commit ae4fa4e7023314526f0e8782a561ebdc73754816 @jlewallen committed May 2, 2012
View
72 pom.xml
@@ -1,20 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
+
+ <!-- change to this parent to compare building this plugin for Hudson -->
+ <!--
+ <parent>
<groupId>org.jvnet.hudson.plugins</groupId>
- <artifactId>plugin</artifactId>
- <version>1.319</version>
+ <artifactId>hudson-plugin-parent</artifactId>
+ <version>2.0.0</version>
</parent>
+ -->
+
+ <parent>
+ <groupId>org.jenkins-ci.plugins</groupId>
+ <artifactId>plugin</artifactId>
+ <version>1.447</version>
+ </parent>
+ <!-- keeping original groupId -->
+ <groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>hipchat</artifactId>
<packaging>hpi</packaging>
<version>0.1.2-SNAPSHOT</version>
<name>Jenkins HipChat Plugin</name>
<description>A Build status publisher that notifies channels on a HipChat server</description>
<url>http://wiki.jenkins-ci.org/display/JENKINS/HipChat+Plugin</url>
- <licenses>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ </properties>
+
+ <licenses>
<license>
<name>MIT license</name>
<comments>All source code is under the MIT license.</comments>
@@ -36,35 +53,44 @@
<dependencies>
<dependency>
- <groupId>org.jvnet.hudson.main</groupId>
- <artifactId>maven-plugin</artifactId>
- </dependency>
- <dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
- <version>3.1-rc1</version>
- </dependency>
- <dependency>
- <groupId>org.jvnet.hudson</groupId>
- <artifactId>htmlunit</artifactId>
- <version>2.2-hudson-10</version>
+ <version>3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
- </dependencies>
+ <!-- only here to prevent from being included inside hpi for hudson parent, not needed by project at all -->
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.9</version>
+ <scope>provided</scope>
+ </dependency>
+ <!-- only here to prevent from being included inside hpi for hudson parent, not needed by project at all -->
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.0.4</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
<build>
- <plugins>
- <plugin>
- <artifactId>maven-release-plugin</artifactId>
- <configuration>
- <goals>deploy</goals>
- </configuration>
- </plugin>
- </plugins>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.jenkins-ci.tools</groupId>
+ <artifactId>maven-hpi-plugin</artifactId>
+ <configuration>
+ <disabledTestInjection>true</disabledTestInjection>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
</build>
+
</project>
View
331 src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java
@@ -1,181 +1,190 @@
package jenkins.plugins.hipchat;
import hudson.Util;
-import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.CauseAction;
+import hudson.model.Result;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.AffectedFile;
import hudson.scm.ChangeLogSet.Entry;
+import org.apache.commons.lang.StringUtils;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
-import org.apache.commons.lang.StringUtils;
-
@SuppressWarnings("rawtypes")
public class ActiveNotifier implements FineGrainedNotifier {
- private static final Logger logger = Logger.getLogger(HipChatListener.class.getName());
-
- HipChatNotifier notifier;
-
- public ActiveNotifier(HipChatNotifier notifier) {
- super();
- this.notifier = notifier;
- }
-
- private HipChatService getHipChat(AbstractBuild r) {
- AbstractProject<?, ?> project = r.getProject();
- String projectRoom = Util.fixEmpty(project.getProperty(HipChatNotifier.HipChatJobProperty.class).getRoom());
- return notifier.newHipChatService(projectRoom);
- }
-
- public void deleted(AbstractBuild r) {}
-
- public void started(AbstractBuild build) {
- String changes = getChanges(build);
- CauseAction cause = build.getAction(CauseAction.class);
- if(changes != null) {
- notifyStart(build, changes);
- }
- else if(cause != null) {
- MessageBuilder message = new MessageBuilder(notifier, build);
- message.append(cause.getShortDescription());
- notifyStart(build, message.appendOpenLink().toString());
- }
- else {
- notifyStart(build, getBuildStatusMessage(build));
- }
- }
-
- private void notifyStart(AbstractBuild build, String message) {
- getHipChat(build).publish(message, "green");
- }
-
- public void finalized(AbstractBuild r) {}
-
- public void completed(AbstractBuild r) {
- getHipChat(r).publish(getBuildStatusMessage(r), getBuildColor(r));
- }
-
- String getChanges(AbstractBuild r) {
- if(!r.hasChangeSetComputed()) {
- logger.info("No change set computed...");
- return null;
- }
- ChangeLogSet changeSet = r.getChangeSet();
- List<Entry> entries = new LinkedList<Entry>();
- Set<AffectedFile> files = new HashSet<AffectedFile>();
- for(Object o : changeSet.getItems()) {
- Entry entry = (Entry)o;
- logger.info("Entry " + o);
- entries.add(entry);
- files.addAll(entry.getAffectedFiles());
- }
- if(entries.isEmpty()) {
- logger.info("Empty change...");
- return null;
- }
- Set<String> authors = new HashSet<String>();
- for(Entry entry : entries) {
- authors.add(entry.getAuthor().getDisplayName());
- }
- MessageBuilder message = new MessageBuilder(notifier, r);
- message.append("Started by changes from ");
- message.append(StringUtils.join(authors, ", "));
- message.append(" (");
- message.append(files.size());
- message.append(" file(s) changed)");
- return message.appendOpenLink().toString();
- }
-
- static String getBuildColor(AbstractBuild r) {
- Result result = r.getResult();
- if(result == Result.SUCCESS) {
- return "green";
- }
- else if(result == Result.FAILURE) {
- return "red";
- }
- else {
- return "yellow";
- }
- }
-
- String getBuildStatusMessage(AbstractBuild r) {
- MessageBuilder message = new MessageBuilder(notifier, r);
- message.appendStatusMessage();
- message.appendDuration();
- return message.appendOpenLink().toString();
- }
-
- public static class MessageBuilder {
- private StringBuffer message;
- private HipChatNotifier notifier;
- private AbstractBuild build;
-
- public MessageBuilder(HipChatNotifier notifier, AbstractBuild build) {
- this.notifier = notifier;
- this.message = new StringBuffer();
- this.build = build;
- startMessage();
- }
-
- public MessageBuilder appendStatusMessage() {
- message.append(getStatusMessage(build));
- return this;
- }
-
- static String getStatusMessage(AbstractBuild r) {
- if(r.isBuilding()) {
- return "Starting...";
- }
- Result result = r.getResult();
- if(result == Result.SUCCESS) return "Success";
- if(result == Result.FAILURE) return "<b>FAILURE</b>";
- if(result == Result.ABORTED) return "ABORTED";
- if(result == Result.NOT_BUILT) return "Not built";
- if(result == Result.UNSTABLE) return "Unstable";
- return "Unknown";
- }
-
- public MessageBuilder append(String string) {
- message.append(string);
- return this;
- }
-
- public MessageBuilder append(Object string) {
- message.append(string.toString());
- return this;
- }
-
- private MessageBuilder startMessage() {
- message.append(build.getProject().getDisplayName());
- message.append(" - ");
- message.append(build.getDisplayName());
- message.append(" ");
- return this;
- }
-
- public MessageBuilder appendOpenLink() {
- String url = notifier.getJenkinsUrl() + build.getUrl();
- message.append(" (<a href='").append(url).append("'>Open</a>)");
- return this;
- }
-
- public MessageBuilder appendDuration() {
- message.append(" after ");
- message.append(build.getDurationString());
- return this;
- }
-
- public String toString() {
- return message.toString();
- }
- }
+ private static final Logger logger = Logger.getLogger(HipChatListener.class.getName());
+
+ HipChatNotifier notifier;
+
+ public ActiveNotifier(HipChatNotifier notifier) {
+ super();
+ this.notifier = notifier;
+ }
+
+ private HipChatService getHipChat(AbstractBuild r) {
+ AbstractProject<?, ?> project = r.getProject();
+ String projectRoom = Util.fixEmpty(project.getProperty(HipChatNotifier.HipChatJobProperty.class).getRoom());
+ return notifier.newHipChatService(projectRoom);
+ }
+
+ public void deleted(AbstractBuild r) {
+ }
+
+ public void started(AbstractBuild build) {
+ String changes = getChanges(build);
+ CauseAction cause = build.getAction(CauseAction.class);
+
+ if (changes != null) {
+ notifyStart(build, changes);
+ } else if (cause != null) {
+ MessageBuilder message = new MessageBuilder(notifier, build);
+ message.append(cause.getShortDescription());
+ notifyStart(build, message.appendOpenLink().toString());
+ } else {
+ notifyStart(build, getBuildStatusMessage(build));
+ }
+ }
+
+ private void notifyStart(AbstractBuild build, String message) {
+ getHipChat(build).publish(message, "green");
+ }
+
+ public void finalized(AbstractBuild r) {
+ }
+
+ public void completed(AbstractBuild r) {
+
+ AbstractProject<?, ?> project = r.getProject();
+ HipChatNotifier.HipChatJobProperty jobProperty = project.getProperty(HipChatNotifier.HipChatJobProperty.class);
+ Result result = r.getResult();
+ if ((result == Result.ABORTED && jobProperty.getNotifyAborted())
+ || (result == Result.FAILURE && jobProperty.getNotifyFailure())
+ || (result == Result.NOT_BUILT && jobProperty.getNotifyNotBuilt())
+ || (result == Result.SUCCESS && jobProperty.getNotifySuccess())
+ || (result == Result.UNSTABLE && jobProperty.getNotifyUnstable())) {
+ getHipChat(r).publish(getBuildStatusMessage(r), getBuildColor(r));
+ }
+
+ }
+
+ String getChanges(AbstractBuild r) {
+ if (!r.hasChangeSetComputed()) {
+ logger.info("No change set computed...");
+ return null;
+ }
+ ChangeLogSet changeSet = r.getChangeSet();
+ List<Entry> entries = new LinkedList<Entry>();
+ Set<AffectedFile> files = new HashSet<AffectedFile>();
+ for (Object o : changeSet.getItems()) {
+ Entry entry = (Entry) o;
+ logger.info("Entry " + o);
+ entries.add(entry);
+ files.addAll(entry.getAffectedFiles());
+ }
+ if (entries.isEmpty()) {
+ logger.info("Empty change...");
+ return null;
+ }
+ Set<String> authors = new HashSet<String>();
+ for (Entry entry : entries) {
+ authors.add(entry.getAuthor().getDisplayName());
+ }
+ MessageBuilder message = new MessageBuilder(notifier, r);
+ message.append("Started by changes from ");
+ message.append(StringUtils.join(authors, ", "));
+ message.append(" (");
+ message.append(files.size());
+ message.append(" file(s) changed)");
+ return message.appendOpenLink().toString();
+ }
+
+ static String getBuildColor(AbstractBuild r) {
+ Result result = r.getResult();
+ if (result == Result.SUCCESS) {
+ return "green";
+ } else if (result == Result.FAILURE) {
+ return "red";
+ } else {
+ return "yellow";
+ }
+ }
+
+ String getBuildStatusMessage(AbstractBuild r) {
+ MessageBuilder message = new MessageBuilder(notifier, r);
+ message.appendStatusMessage();
+ message.appendDuration();
+ return message.appendOpenLink().toString();
+ }
+
+ public static class MessageBuilder {
+ private StringBuffer message;
+ private HipChatNotifier notifier;
+ private AbstractBuild build;
+
+ public MessageBuilder(HipChatNotifier notifier, AbstractBuild build) {
+ this.notifier = notifier;
+ this.message = new StringBuffer();
+ this.build = build;
+ startMessage();
+ }
+
+ public MessageBuilder appendStatusMessage() {
+ message.append(getStatusMessage(build));
+ return this;
+ }
+
+ static String getStatusMessage(AbstractBuild r) {
+ if (r.isBuilding()) {
+ return "Starting...";
+ }
+ Result result = r.getResult();
+ if (result == Result.SUCCESS) return "Success";
+ if (result == Result.FAILURE) return "<b>FAILURE</b>";
+ if (result == Result.ABORTED) return "ABORTED";
+ if (result == Result.NOT_BUILT) return "Not built";
+ if (result == Result.UNSTABLE) return "Unstable";
+ return "Unknown";
+ }
+
+ public MessageBuilder append(String string) {
+ message.append(string);
+ return this;
+ }
+
+ public MessageBuilder append(Object string) {
+ message.append(string.toString());
+ return this;
+ }
+
+ private MessageBuilder startMessage() {
+ message.append(build.getProject().getDisplayName());
+ message.append(" - ");
+ message.append(build.getDisplayName());
+ message.append(" ");
+ return this;
+ }
+
+ public MessageBuilder appendOpenLink() {
+ String url = notifier.getBuildServerUrl() + build.getUrl();
+ message.append(" (<a href='").append(url).append("'>Open</a>)");
+ return this;
+ }
+
+ public MessageBuilder appendDuration() {
+ message.append(" after ");
+ message.append(build.getDurationString());
+ return this;
+ }
+
+ public String toString() {
+ return message.toString();
+ }
+ }
}
View
12 src/main/java/jenkins/plugins/hipchat/DisabledNotifier.java
@@ -4,11 +4,15 @@
@SuppressWarnings("rawtypes")
public class DisabledNotifier implements FineGrainedNotifier {
- public void started(AbstractBuild r) {}
+ public void started(AbstractBuild r) {
+ }
- public void deleted(AbstractBuild r) {}
+ public void deleted(AbstractBuild r) {
+ }
- public void finalized(AbstractBuild r) {}
+ public void finalized(AbstractBuild r) {
+ }
- public void completed(AbstractBuild r) {}
+ public void completed(AbstractBuild r) {
+ }
}
View
16 src/main/java/jenkins/plugins/hipchat/FineGrainedNotifier.java
@@ -4,16 +4,16 @@
public interface FineGrainedNotifier {
- @SuppressWarnings("rawtypes")
- void started(AbstractBuild r);
+ @SuppressWarnings("rawtypes")
+ void started(AbstractBuild r);
- @SuppressWarnings("rawtypes")
- void deleted(AbstractBuild r);
+ @SuppressWarnings("rawtypes")
+ void deleted(AbstractBuild r);
- @SuppressWarnings("rawtypes")
- void finalized(AbstractBuild r);
+ @SuppressWarnings("rawtypes")
+ void finalized(AbstractBuild r);
- @SuppressWarnings("rawtypes")
- void completed(AbstractBuild r);
+ @SuppressWarnings("rawtypes")
+ void completed(AbstractBuild r);
}
View
84 src/main/java/jenkins/plugins/hipchat/HipChatListener.java
@@ -1,10 +1,10 @@
package jenkins.plugins.hipchat;
import hudson.Extension;
-import hudson.model.Descriptor;
-import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
+import hudson.model.Descriptor;
+import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import hudson.tasks.Publisher;
@@ -15,45 +15,45 @@
@SuppressWarnings("rawtypes")
public class HipChatListener extends RunListener<AbstractBuild> {
- private static final Logger logger = Logger.getLogger(HipChatListener.class.getName());
-
- public HipChatListener() {
- super(AbstractBuild.class);
- }
-
- @Override
- public void onCompleted(AbstractBuild r, TaskListener listener) {
- getNotifier(r.getProject()).completed(r);
- super.onCompleted(r, listener);
- }
-
- @Override
- public void onStarted(AbstractBuild r, TaskListener listener) {
- // getNotifier(r.getProject()).started(r);
- // super.onStarted(r, listener);
- }
-
- @Override
- public void onDeleted(AbstractBuild r) {
- // getNotifier(r.getProject()).deleted(r);
- // super.onDeleted(r);
- }
-
- @Override
- public void onFinalized(AbstractBuild r) {
- // getNotifier(r.getProject()).finalized(r);
- // super.onFinalized(r);
- }
-
- @SuppressWarnings("unchecked")
- FineGrainedNotifier getNotifier(AbstractProject project) {
- Map<Descriptor<Publisher>, Publisher> map = project.getPublishersList().toMap();
- for(Publisher publisher : map.values()) {
- if(publisher instanceof HipChatNotifier) {
- return new ActiveNotifier((HipChatNotifier)publisher);
- }
- }
- return new DisabledNotifier();
- }
+ private static final Logger logger = Logger.getLogger(HipChatListener.class.getName());
+
+ public HipChatListener() {
+ super(AbstractBuild.class);
+ }
+
+ @Override
+ public void onCompleted(AbstractBuild r, TaskListener listener) {
+ getNotifier(r.getProject()).completed(r);
+ super.onCompleted(r, listener);
+ }
+
+ @Override
+ public void onStarted(AbstractBuild r, TaskListener listener) {
+ // getNotifier(r.getProject()).started(r);
+ // super.onStarted(r, listener);
+ }
+
+ @Override
+ public void onDeleted(AbstractBuild r) {
+ // getNotifier(r.getProject()).deleted(r);
+ // super.onDeleted(r);
+ }
+
+ @Override
+ public void onFinalized(AbstractBuild r) {
+ // getNotifier(r.getProject()).finalized(r);
+ // super.onFinalized(r);
+ }
+
+ @SuppressWarnings("unchecked")
+ FineGrainedNotifier getNotifier(AbstractProject project) {
+ Map<Descriptor<Publisher>, Publisher> map = project.getPublishersList().toMap();
+ for (Publisher publisher : map.values()) {
+ if (publisher instanceof HipChatNotifier) {
+ return new ActiveNotifier((HipChatNotifier) publisher);
+ }
+ }
+ return new DisabledNotifier();
+ }
}
View
399 src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
@@ -2,193 +2,244 @@
import hudson.Extension;
import hudson.Launcher;
-import hudson.model.BuildListener;
-import hudson.model.Descriptor;
-import hudson.model.JobPropertyDescriptor;
-import hudson.model.AbstractBuild;
-import hudson.model.AbstractProject;
-import hudson.model.Job;
+import hudson.model.*;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
-
-import java.io.IOException;
-import java.util.Map;
-import java.util.logging.Logger;
-
import net.sf.json.JSONObject;
-
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
-@SuppressWarnings({ "unchecked" })
+import java.io.IOException;
+import java.util.Map;
+import java.util.logging.Logger;
+
+@SuppressWarnings({"unchecked"})
public class HipChatNotifier extends Notifier {
- private static final Logger logger = Logger.getLogger(HipChatNotifier.class.getName());
-
- private String jenkinsUrl;
- private String authToken;
- private String room;
-
- @Override
- public DescriptorImpl getDescriptor() {
- return (DescriptorImpl)super.getDescriptor();
- }
-
- public String getRoom() {
- return room;
- }
-
- public String getAuthToken() {
- return authToken;
- }
-
- public String getJenkinsUrl() {
- return jenkinsUrl;
- }
-
- public void setJenkinsUrl(String jenkinsUrl) {
- this.jenkinsUrl = jenkinsUrl;
- }
-
- public void setAuthToken(String authToken) {
- this.authToken = authToken;
- }
-
- public void setRoom(String room) {
- this.room = room;
- }
-
- @DataBoundConstructor
- public HipChatNotifier(String authToken, String room, String jenkinsUrl) {
- super();
- this.authToken = authToken;
- this.jenkinsUrl = jenkinsUrl;
- this.room = room;
- }
-
- public BuildStepMonitor getRequiredMonitorService() {
- return BuildStepMonitor.BUILD;
- }
-
- public HipChatService newHipChatService(String room) {
- return new StandardHipChatService(getAuthToken(), room == null ? getRoom() : room, "Jenkins");
- }
-
- @Override
- public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
- return true;
- }
-
- @Extension
- public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
- private String token;
- private String room;
- private String jenkinsUrl;
-
- public DescriptorImpl() {
- load();
- }
-
- public String getToken() {
- return token;
- }
-
- public String getRoom() {
- return room;
- }
-
- public String getJenkinsUrl() {
- return jenkinsUrl;
- }
-
- public boolean isApplicable(Class<? extends AbstractProject> aClass) {
- return true;
- }
-
- @Override
- public HipChatNotifier newInstance(StaplerRequest sr) {
- if(token == null) token = sr.getParameter("hipChatToken");
- if(jenkinsUrl == null) jenkinsUrl = sr.getParameter("hipChatJenkinsUrl");
- if(room == null) room = sr.getParameter("hipChatRoom");
- return new HipChatNotifier(token, room, jenkinsUrl);
- }
-
- @Override
- public boolean configure(StaplerRequest sr, JSONObject formData) throws FormException {
- token = sr.getParameter("hipChatToken");
- room = sr.getParameter("hipChatRoom");
- jenkinsUrl = sr.getParameter("hipChatJenkinsUrl");
- if(jenkinsUrl != null && !jenkinsUrl.endsWith("/")) {
- jenkinsUrl = jenkinsUrl + "/";
- }
- try {
- new HipChatNotifier(token, room, jenkinsUrl);
- }
- catch(Exception e) {
- throw new FormException("Failed to initialize notifier - check your global notifier configuration settings", e, "");
- }
- save();
- return super.configure(sr, formData);
- }
-
- @Override
- public String getDisplayName() {
- return "HipChat Notifications";
- }
- }
-
- public static class HipChatJobProperty extends hudson.model.JobProperty<AbstractProject<?, ?>> {
- private String room;
- private boolean startNotification;
-
- @DataBoundConstructor
- public HipChatJobProperty(String room, boolean startNotification) {
- this.room = room;
- this.startNotification = startNotification;
- }
-
- @Exported
- public String getRoom() {
- return room;
- }
-
- @Exported
- public boolean getStartNotification() {
- return startNotification;
- }
-
- @Override
- public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
- if(startNotification) {
- Map<Descriptor<Publisher>, Publisher> map = build.getProject().getPublishersList().toMap();
- for(Publisher publisher : map.values()) {
- if(publisher instanceof HipChatNotifier) {
- logger.info("Invoking Started...");
- new ActiveNotifier((HipChatNotifier)publisher).started(build);
- }
+ private static final Logger logger = Logger.getLogger(HipChatNotifier.class.getName());
+
+ private String authToken;
+ private String buildServerUrl;
+ private String room;
+ private String sendAs;
+
+ @Override
+ public DescriptorImpl getDescriptor() {
+ return (DescriptorImpl) super.getDescriptor();
+ }
+
+ public String getRoom() {
+ return room;
+ }
+
+ public String getAuthToken() {
+ return authToken;
+ }
+
+ public String getBuildServerUrl() {
+ return buildServerUrl;
+ }
+
+ public String getSendAs() {
+ return sendAs;
+ }
+
+ public void setBuildServerUrl(final String buildServerUrl) {
+ this.buildServerUrl = buildServerUrl;
+ }
+
+ public void setAuthToken(final String authToken) {
+ this.authToken = authToken;
+ }
+
+ public void setRoom(final String room) {
+ this.room = room;
+ }
+
+ public void setSendAs(final String sendAs) {
+ this.sendAs = sendAs;
+ }
+
+ @DataBoundConstructor
+ public HipChatNotifier(final String authToken, final String room, String buildServerUrl, final String sendAs) {
+ super();
+ this.authToken = authToken;
+ this.buildServerUrl = buildServerUrl;
+ this.room = room;
+ this.sendAs = sendAs;
+ }
+
+ public BuildStepMonitor getRequiredMonitorService() {
+ return BuildStepMonitor.BUILD;
+ }
+
+ public HipChatService newHipChatService(final String room) {
+ return new StandardHipChatService(getAuthToken(), room == null ? getRoom() : room, getSendAs() == null ? "Build Server" : getSendAs());
+ }
+
+ @Override
+ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
+ return true;
+ }
+
+ @Extension
+ public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
+ private String token;
+ private String room;
+ private String buildServerUrl;
+ private String sendAs;
+
+ public DescriptorImpl() {
+ load();
+ }
+
+ public String getToken() {
+ return token;
+ }
+
+ public String getRoom() {
+ return room;
+ }
+
+ public String getBuildServerUrl() {
+ return buildServerUrl;
+ }
+
+ public String getSendAs() {
+ return sendAs;
+ }
+
+ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
+ return true;
+ }
+
+ @Override
+ public HipChatNotifier newInstance(StaplerRequest sr) {
+ if (token == null) token = sr.getParameter("hipChatToken");
+ if (buildServerUrl == null) buildServerUrl = sr.getParameter("hipChatBuildServerUrl");
+ if (room == null) room = sr.getParameter("hipChatRoom");
+ if (sendAs == null) sendAs = sr.getParameter("hipChatSendAs");
+ return new HipChatNotifier(token, room, buildServerUrl, sendAs);
+ }
+
+ @Override
+ public boolean configure(StaplerRequest sr, JSONObject formData) throws FormException {
+ token = sr.getParameter("hipChatToken");
+ room = sr.getParameter("hipChatRoom");
+ buildServerUrl = sr.getParameter("hipChatBuildServerUrl");
+ sendAs = sr.getParameter("hipChatSendAs");
+ if (buildServerUrl != null && !buildServerUrl.endsWith("/")) {
+ buildServerUrl = buildServerUrl + "/";
+ }
+ try {
+ new HipChatNotifier(token, room, buildServerUrl, sendAs);
+ } catch (Exception e) {
+ throw new FormException("Failed to initialize notifier - check your global notifier configuration settings", e, "");
}
- }
- return super.prebuild(build, listener);
- }
+ save();
+ return super.configure(sr, formData);
+ }
- @Extension
- public static final class DescriptorImpl extends JobPropertyDescriptor {
- public String getDisplayName() {
+ @Override
+ public String getDisplayName() {
return "HipChat Notifications";
- }
+ }
+ }
+
+ public static class HipChatJobProperty extends hudson.model.JobProperty<AbstractProject<?, ?>> {
+ private String room;
+ private boolean startNotification;
+ private boolean notifySuccess;
+ private boolean notifyAborted;
+ private boolean notifyNotBuilt;
+ private boolean notifyUnstable;
+ private boolean notifyFailure;
+
+
+ @DataBoundConstructor
+ public HipChatJobProperty(String room, boolean startNotification, boolean notifyAborted, boolean notifyFailure, boolean notifyNotBuilt, boolean notifySuccess, boolean notifyUnstable) {
+ this.room = room;
+ this.startNotification = startNotification;
+ this.notifyAborted = notifyAborted;
+ this.notifyFailure = notifyFailure;
+ this.notifyNotBuilt = notifyNotBuilt;
+ this.notifySuccess = notifySuccess;
+ this.notifyUnstable = notifyUnstable;
+ }
+
+ @Exported
+ public String getRoom() {
+ return room;
+ }
+
+ @Exported
+ public boolean getStartNotification() {
+ return startNotification;
+ }
+
+ @Exported
+ public boolean getNotifySuccess() {
+ return notifySuccess;
+ }
+
+ @Override
+ public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
+ if (startNotification) {
+ Map<Descriptor<Publisher>, Publisher> map = build.getProject().getPublishersList().toMap();
+ for (Publisher publisher : map.values()) {
+ if (publisher instanceof HipChatNotifier) {
+ logger.info("Invoking Started...");
+ new ActiveNotifier((HipChatNotifier) publisher).started(build);
+ }
+ }
+ }
+ return super.prebuild(build, listener);
+ }
+
+ @Exported
+ public boolean getNotifyAborted() {
+ return notifyAborted;
+ }
+
+ @Exported
+ public boolean getNotifyFailure() {
+ return notifyFailure;
+ }
+
+ @Exported
+ public boolean getNotifyNotBuilt() {
+ return notifyNotBuilt;
+ }
+
+ @Exported
+ public boolean getNotifyUnstable() {
+ return notifyUnstable;
+ }
+
+ @Extension
+ public static final class DescriptorImpl extends JobPropertyDescriptor {
+ public String getDisplayName() {
+ return "HipChat Notifications";
+ }
- @Override
- public boolean isApplicable(Class<? extends Job> jobType) {
- return true;
- }
-
- @Override
- public HipChatJobProperty newInstance(StaplerRequest sr, JSONObject formData) throws hudson.model.Descriptor.FormException {
- return new HipChatJobProperty(sr.getParameter("hipChatProjectRoom"), sr.getParameter("hipChatStartNotification") != null);
- }
- }
- }
+ @Override
+ public boolean isApplicable(Class<? extends Job> jobType) {
+ return true;
+ }
+
+ @Override
+ public HipChatJobProperty newInstance(StaplerRequest sr, JSONObject formData) throws hudson.model.Descriptor.FormException {
+ return new HipChatJobProperty(sr.getParameter("hipChatProjectRoom"),
+ sr.getParameter("hipChatStartNotification") != null,
+ sr.getParameter("hipChatNotifyAborted") != null,
+ sr.getParameter("hipChatNotifyFailure") != null,
+ sr.getParameter("hipChatNotifyNotBuilt") != null,
+ sr.getParameter("hipChatNotifySuccess") != null,
+ sr.getParameter("hipChatNotifyUnstable") != null);
+ }
+ }
+ }
}
View
5 src/main/java/jenkins/plugins/hipchat/HipChatService.java
@@ -1,6 +1,7 @@
package jenkins.plugins.hipchat;
public interface HipChatService {
- void publish(String message);
- void publish(String message, String color);
+ void publish(String message);
+
+ void publish(String message, String color);
}
View
98 src/main/java/jenkins/plugins/hipchat/StandardHipChatService.java
@@ -8,54 +8,52 @@
public class StandardHipChatService implements HipChatService {
- private static final Logger logger = Logger.getLogger(StandardHipChatService.class.getName());
-
- private String host = "api.hipchat.com";
- private String token;
- private String[] roomIds;
- private String from;
-
- public StandardHipChatService(String token, String roomId, String from) {
- super();
- this.token = token;
- this.roomIds = roomId.split(",");
- this.from = from;
- }
-
- public void publish(String message) {
- publish(message, "yellow");
- }
-
- public void publish(String message, String color) {
- for(String roomId : roomIds) {
- logger.info("Posting: " + from + " to " + roomId + ": " + message + " " + color);
- HttpClient client = new HttpClient();
- String url = "https://" + host + "/v1/rooms/message?auth_token=" + token;
- PostMethod post = new PostMethod(url);
-
- try {
- post.addParameter("from", from);
- post.addParameter("room_id", roomId);
- post.addParameter("message", message);
- post.addParameter("color", color);
- post.addParameter("notify", shouldNotify(color));
- post.getParams().setContentCharset("UTF-8");
- client.executeMethod(post);
- }
- catch(Exception e) {
- logger.log(Level.WARNING, "Error posting to HipChat", e);
- }
- finally {
- post.releaseConnection();
- }
- }
- }
-
- private String shouldNotify(String color) {
- return color.equalsIgnoreCase("green") ? "0" : "1";
- }
-
- void setHost(String host) {
- this.host = host;
- }
+ private static final Logger logger = Logger.getLogger(StandardHipChatService.class.getName());
+
+ private String host = "api.hipchat.com";
+ private String token;
+ private String[] roomIds;
+ private String from;
+
+ public StandardHipChatService(String token, String roomId, String from) {
+ super();
+ this.token = token;
+ this.roomIds = roomId.split(",");
+ this.from = from;
+ }
+
+ public void publish(String message) {
+ publish(message, "yellow");
+ }
+
+ public void publish(String message, String color) {
+ for (String roomId : roomIds) {
+ logger.info("Posting: " + from + " to " + roomId + ": " + message + " " + color);
+ HttpClient client = new HttpClient();
+ String url = "https://" + host + "/v1/rooms/message?auth_token=" + token;
+ PostMethod post = new PostMethod(url);
+
+ try {
+ post.addParameter("from", from);
+ post.addParameter("room_id", roomId);
+ post.addParameter("message", message);
+ post.addParameter("color", color);
+ post.addParameter("notify", shouldNotify(color));
+ post.getParams().setContentCharset("UTF-8");
+ client.executeMethod(post);
+ } catch (Exception e) {
+ logger.log(Level.WARNING, "Error posting to HipChat", e);
+ } finally {
+ post.releaseConnection();
+ }
+ }
+ }
+
+ private String shouldNotify(String color) {
+ return color.equalsIgnoreCase("green") ? "0" : "1";
+ }
+
+ void setHost(String host) {
+ this.host = host;
+ }
}
View
25 src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/HipChatJobProperty/config.jelly
@@ -4,10 +4,31 @@
<f:entry title="Project Room">
<f:textbox name="hipChatProjectRoom" value="${instance.getRoom()}"/>
</f:entry>
-
- <f:entry title="Start Notification">
+
+ <f:entry title="Notify Build Start">
<f:checkbox name="hipChatStartNotification" value="true" checked="${instance.getStartNotification()}"/>
</f:entry>
+
+ <f:entry title="Notify Aborted">
+ <f:checkbox name="hipChatNotifyAborted" value="true" checked="${instance.getNotifyAborted()}"/>
+ </f:entry>
+
+ <f:entry title="Notify Failure">
+ <f:checkbox name="hipChatNotifyFailure" value="true" checked="${instance.getNotifyFailure()}"/>
+ </f:entry>
+
+ <f:entry title="Notify Not Built">
+ <f:checkbox name="hipChatNotifyNotBuilt" value="true" checked="${instance.getNotifyNotBuilt()}"/>
+ </f:entry>
+
+ <f:entry title="Notify Success">
+ <f:checkbox name="hipChatNotifySuccess" value="true" checked="${instance.getNotifySuccess()}"/>
+ </f:entry>
+
+ <f:entry title="Notify Unstable">
+ <f:checkbox name="hipChatNotifyUnstable" value="true" checked="${instance.getNotifyUnstable()}"/>
+ </f:entry>
+
</f:section>
</j:jelly>
View
1 src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/HipChatJobProperty/index.jelly
@@ -0,0 +1 @@
+<div></div>
View
7 src/main/resources/jenkins/plugins/hipchat/HipChatNotifier/global.jelly
@@ -18,8 +18,11 @@
<f:entry title="Room" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatRoom.html">
<f:textbox name="hipChatRoom" value="${descriptor.getRoom()}" />
</f:entry>
- <f:entry title="Jenkins URL" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatJenkinsUrl.html">
- <f:textbox name="hipChatJenkinsUrl" value="${descriptor.getJenkinsUrl()}" />
+ <f:entry title="Build Server URL" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatBuildServerUrl.html">
+ <f:textbox name="hipChatBuildServerUrl" value="${descriptor.getBuildServerUrl()}" />
+ </f:entry>
+ <f:entry title="Send As" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatSendAs.html">
+ <f:textbox name="hipChatSendAs" value="${descriptor.getSendAs()}" />
</f:entry>
</f:section>
</j:jelly>
View
5 src/main/webapp/help-globalConfig-hipChatBuildServerUrl.html
@@ -0,0 +1,5 @@
+<div>
+ <p>Optionally specify the URL for your Server installation, e.g. http://yourhost.yourdomain/hudson/.
+ This value will be used to generate links back to your Server from HipChat and should end with a forward slash.</p>
+ <p>This is necessary because the Server cannot reliably detect such a URL from within itself.</p>
+</div>
View
5 src/main/webapp/help-globalConfig-hipChatJenkinsUrl.html
@@ -1,5 +0,0 @@
-<div>
- <p>Optionally specify the URL for your Jenkins installation, e.g. http://yourhost.yourdomain/hudson/.
- This value will be used to generate links back to Jenkins from HipChat and should end with a forward slash.</p>
- <p>This is necessary because Jenkins cannot reliably detect such a URL from within itself.</p>
-</div>
View
4 src/main/webapp/help-globalConfig-hipChatSendAs.html
@@ -0,0 +1,4 @@
+<div xmlns="http://www.w3.org/1999/html">
+ <p>Optionally specify a name to send the notifications as.</p>
+ <p>If not specified, messages will be sent as "Build Server".</p>
+</div>
View
4 src/main/webapp/help-projectConfig-hipChatRoom.html
@@ -1,7 +1,7 @@
<div>
<p>
- Enter the name of the room to which notifications should be sent. Note that this can be either the name of the
- room OR the id number, e.g. "Dev Team", and that multiple names may appear comma separated.
+ Enter the room names to which notifications should be sent. Note that this can include names of
+ rooms OR room id numbers, e.g. "Dev Team", and that multiple values may appear comma separated.
While names are more readable, room numbers will not change over time and are therefore more resilient.
Use your <b>admin</b> token to get a list of room ids <a href="https://api.hipchat.com/v1/rooms/list?auth_token=ADMINTOKEN">here</a>.
</p>
View
2 src/main/webapp/help.html
@@ -1,4 +1,4 @@
<div>
Send build notifications to a HipChat chat room.<br />
- The HipChat notifier is configured globally, but you can customize the room name per-project.
+ The HipChat notifier is configured globally, but you can customize the room names per-project.
</div>
View
15 src/test/java/jenkins/plugins/hipchat/StandardHipChatServiceTest.java
@@ -1,19 +1,18 @@
package jenkins.plugins.hipchat;
+import jenkins.plugins.hipchat.StandardHipChatService;
import org.junit.Before;
import org.junit.Test;
public class StandardHipChatServiceTest {
- private StandardHipChatService service;
-
- @Before
- public void setUp() throws Exception {
- service = new StandardHipChatService("token", "room", "from");
- service.setHost("localhost");
- }
+ /**
+ * Publish should generally not rethrow exceptions, or it will cause a build job to fail at end.
+ */
@Test
- public void publishShouldNotRethrowExceptions() {
+ public void publishWithBadHostShouldNotRethrowExceptions() {
+ StandardHipChatService service = new StandardHipChatService("token", "room", "from");
+ service.setHost("hostvaluethatwillcausepublishtofail");
service.publish("message");
}
}

0 comments on commit ae4fa4e

Please sign in to comment.