Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for customizing hipchat host for jenkins plugin #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
46 changes: 41 additions & 5 deletions src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class HipChatNotifier extends Notifier {
private String authToken;
private String buildServerUrl;
private String room;
private String hipChatHost;
private String sendAs;

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

public String getHipChatHost() {
return hipChatHost;
}

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


@DataBoundConstructor
public HipChatNotifier(final String authToken, final String room, String buildServerUrl, final String sendAs) {
public HipChatNotifier(final String authToken, final String room, String buildServerUrl, final String sendAs, final String hipChatHost) {
super();
this.authToken = authToken;
this.buildServerUrl = buildServerUrl;
this.room = room;
this.sendAs = sendAs;
this.hipChatHost = hipChatHost;
}

public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}

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

@Override
Expand All @@ -77,6 +83,7 @@ public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
private String room;
private String buildServerUrl;
private String sendAs;
private String hipChatHost;

public DescriptorImpl() {
load();
Expand All @@ -90,6 +97,10 @@ public String getRoom() {
return room;
}

public String getHipChatHost() {
return hipChatHost;
}

public String getBuildServerUrl() {
return buildServerUrl;
}
Expand All @@ -107,8 +118,17 @@ 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");
establishHipChatHost(sr);
if (sendAs == null) sendAs = sr.getParameter("hipChatSendAs");
return new HipChatNotifier(token, room, buildServerUrl, sendAs);
return new HipChatNotifier(token, room, buildServerUrl, sendAs, hipChatHost);
}

protected void establishHipChatHost(StaplerRequest sr) {
if (null == hipChatHost) {
hipChatHost = sr.getParameter("hipChatHost");
} else {
hipChatHost = HipChatJobProperty.DEFAULT_HIPCHAT_HOST;
}
}

@Override
Expand All @@ -120,8 +140,9 @@ public boolean configure(StaplerRequest sr, JSONObject formData) throws FormExce
if (buildServerUrl != null && !buildServerUrl.endsWith("/")) {
buildServerUrl = buildServerUrl + "/";
}
establishHipChatHost(sr);
try {
new HipChatNotifier(token, room, buildServerUrl, sendAs);
new HipChatNotifier(token, room, buildServerUrl, sendAs, hipChatHost);
} catch (Exception e) {
throw new FormException("Failed to initialize notifier - check your global notifier configuration settings", e, "");
}
Expand All @@ -136,7 +157,12 @@ public String getDisplayName() {
}

public static class HipChatJobProperty extends hudson.model.JobProperty<AbstractProject<?, ?>> {

public static final String DEFAULT_HIPCHAT_HOST = "api.hipchat.com";

private String room;
private String hipChatHost;

private boolean startNotification;
private boolean notifySuccess;
private boolean notifyAborted;
Expand All @@ -145,9 +171,9 @@ public static class HipChatJobProperty extends hudson.model.JobProperty<Abstract
private boolean notifyFailure;
private boolean notifyBackToNormal;


@DataBoundConstructor
public HipChatJobProperty(String room,
String hipChatHost,
boolean startNotification,
boolean notifyAborted,
boolean notifyFailure,
Expand All @@ -156,6 +182,10 @@ public HipChatJobProperty(String room,
boolean notifyUnstable,
boolean notifyBackToNormal) {
this.room = room;
this.hipChatHost = hipChatHost;
if (null == this.hipChatHost) {
this.hipChatHost = DEFAULT_HIPCHAT_HOST;
}
this.startNotification = startNotification;
this.notifyAborted = notifyAborted;
this.notifyFailure = notifyFailure;
Expand All @@ -170,6 +200,11 @@ public String getRoom() {
return room;
}

@Exported
public String getHipChatHost() {
return hipChatHost;
}

@Exported
public boolean getStartNotification() {
return startNotification;
Expand Down Expand Up @@ -233,6 +268,7 @@ public boolean isApplicable(Class<? extends Job> jobType) {
@Override
public HipChatJobProperty newInstance(StaplerRequest sr, JSONObject formData) throws hudson.model.Descriptor.FormException {
return new HipChatJobProperty(sr.getParameter("hipChatProjectRoom"),
sr.getParameter("hipChatProjectRoom"),
sr.getParameter("hipChatStartNotification") != null,
sr.getParameter("hipChatNotifyAborted") != null,
sr.getParameter("hipChatNotifyFailure") != null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ public class StandardHipChatService implements HipChatService {

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

private String host = "api.hipchat.com";
private String host;
private String token;
private String[] roomIds;
private String from;

public StandardHipChatService(String token, String roomId, String from) {
public StandardHipChatService(String token, String roomId, String from, String hipChatHost) {
super();
this.token = token;
this.host = hipChatHost;
this.roomIds = roomId.split(",");
this.from = from;
}
Expand Down Expand Up @@ -56,7 +57,7 @@ public void publish(String message, String color) {
}
}
}

private HttpClient getHttpClient() {
HttpClient client = new HttpClient();
if (Jenkins.getInstance() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<f:textbox name="hipChatProjectRoom" value="${instance.getRoom()}"/>
</f:entry>

<f:entry title="HipChat Host">
<f:textbox name="hipChatHost" value="${instance.getHipChatHost()}"/>
</f:entry>

<f:entry title="Notify Build Start">
<f:checkbox name="hipChatStartNotification" value="true" checked="${instance.getStartNotification()}"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class StandardHipChatServiceTest {
*/
@Test
public void publishWithBadHostShouldNotRethrowExceptions() {
StandardHipChatService service = new StandardHipChatService("token", "room", "from");
StandardHipChatService service = new StandardHipChatService("token", "room", "from", "hipchat.example.com");
service.setHost("hostvaluethatwillcausepublishtofail");
service.publish("message");
}
Expand Down