Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>hipchat</artifactId>
<packaging>hpi</packaging>
<version>0.1.5-SNAPSHOT</version>
<version>0.1.5.1-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>
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HipChatNotifier extends Notifier {

private static final Logger logger = Logger.getLogger(HipChatNotifier.class.getName());

private String host;
private String authToken;
private String buildServerUrl;
private String room;
Expand All @@ -36,6 +37,10 @@ public String getRoom() {
return room;
}

public String getHost() {
return host;
}

public String getAuthToken() {
return authToken;
}
Expand All @@ -50,8 +55,9 @@ public String getSendAs() {


@DataBoundConstructor
public HipChatNotifier(final String authToken, final String room, String buildServerUrl, final String sendAs) {
public HipChatNotifier(final String host, final String authToken, final String room, String buildServerUrl, final String sendAs) {
super();
this.host = host;
this.authToken = authToken;
this.buildServerUrl = buildServerUrl;
this.room = room;
Expand All @@ -63,7 +69,7 @@ public BuildStepMonitor getRequiredMonitorService() {
}

public HipChatService newHipChatService(final String room) {
return new StandardHipChatService(getAuthToken(), room == null ? getRoom() : room, StringUtils.isBlank(getSendAs()) ? "Build Server" : getSendAs());
return new StandardHipChatService(getHost(), getAuthToken(), room == null ? getRoom() : room, StringUtils.isBlank(getSendAs()) ? "Build Server" : getSendAs());
}

@Override
Expand All @@ -73,6 +79,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
private String host;
private String token;
private String room;
private String buildServerUrl;
Expand All @@ -81,6 +88,10 @@ public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public DescriptorImpl() {
load();
}

public String getHost() {
return host;
}

public String getToken() {
return token;
Expand All @@ -104,15 +115,17 @@ public boolean isApplicable(Class<? extends AbstractProject> aClass) {

@Override
public HipChatNotifier newInstance(StaplerRequest sr) {
if (host == null) host = sr.getParameter("hipChatHost");
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);
return new HipChatNotifier(host, token, room, buildServerUrl, sendAs);
}

@Override
public boolean configure(StaplerRequest sr, JSONObject formData) throws FormException {
host = sr.getParameter("hipChatHost");
token = sr.getParameter("hipChatToken");
room = sr.getParameter("hipChatRoom");
buildServerUrl = sr.getParameter("hipChatBuildServerUrl");
Expand All @@ -121,7 +134,7 @@ public boolean configure(StaplerRequest sr, JSONObject formData) throws FormExce
buildServerUrl = buildServerUrl + "/";
}
try {
new HipChatNotifier(token, room, buildServerUrl, sendAs);
new HipChatNotifier(host, token, room, buildServerUrl, sendAs);
} catch (Exception e) {
throw new FormException("Failed to initialize notifier - check your global notifier configuration settings", e, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ public class StandardHipChatService implements HipChatService {
private String[] roomIds;
private String from;

public StandardHipChatService(String token, String roomId, String from) {
public StandardHipChatService(String host, String token, String roomId, String from) {
super();
if (host != null && !String.trim().isEmpty()) {
this.host = host;
}
this.token = token;
this.roomIds = roomId.split(",");
this.from = from;
Expand All @@ -31,8 +34,9 @@ public void publish(String message) {
}

public void publish(String message, String color) {
logger.info("Publishing messages to HipChat server [" + host + "]...");
for (String roomId : roomIds) {
logger.info("Posting: " + from + " to " + roomId + ": " + message + " " + color);
logger.info("`" + from + "` says to room `" + roomId + "`: `" + message + "` in the color `" + color + "`");
HttpClient client = getHttpClient();
String url = "https://" + host + "/v1/rooms/message?auth_token=" + token;
PostMethod post = new PostMethod(url);
Expand All @@ -55,6 +59,8 @@ public void publish(String message, String color) {
post.releaseConnection();
}
}

logger.info("Done publishing messages to HipChat server");
}

private HttpClient getHttpClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
so it should be straightforward to find them.
-->
<f:section title="Global HipChat Notifier Settings">
<f:entry title="HipChat Server" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatHost.html">
<f:textbox name="hipChatHost" value="${descriptor.getHost()}" />
</f:entry>
<f:entry title="API Token" help="${rootURL}/plugin/hipchat/help-globalConfig-hipChatToken.html">
<f:textbox name="hipChatToken" value="${descriptor.getToken()}" />
</f:entry>
Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/help-globalConfig-hipChatHost.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<p>This is the full URL to the HipChat server to communicate with.</p>
<p>The default is the public server of `api.hipchat.com`</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class StandardHipChatServiceTest {
*/
@Test
public void publishWithBadHostShouldNotRethrowExceptions() {
StandardHipChatService service = new StandardHipChatService("token", "room", "from");
service.setHost("hostvaluethatwillcausepublishtofail");
StandardHipChatService service = new StandardHipChatService("hostvaluethatwillcausepublishtofail", "token", "room", "from");
service.publish("message");
}
}