Skip to content

Commit

Permalink
Merge PR jenkinsci#81 Improvements to test connection
Browse files Browse the repository at this point in the history
  • Loading branch information
samrocketman committed Apr 11, 2015
2 parents 7d55122 + 7323ecf commit 58e2f4c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/main/java/jenkins/plugins/slack/SlackNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public FormValidation doTestConnection(@QueryParameter("slackTeamDomain") final
try {
SlackService testSlackService = new StandardSlackService(teamDomain, authToken, room);
String message = "Slack/Jenkins plugin: you're all set on " + buildServerUrl;
testSlackService.publish(message, "green");
return FormValidation.ok("Success");
boolean success = testSlackService.publish(message, "green");
return success ? FormValidation.ok("Success") : FormValidation.error("Failure");
} catch (Exception e) {
return FormValidation.error("Client error : " + e.getMessage());
}
Expand Down Expand Up @@ -355,8 +355,8 @@ public FormValidation doTestConnection(@QueryParameter("slackTeamDomain") final
try {
SlackService testSlackService = new StandardSlackService(teamDomain, authToken, room);
String message = "Slack/Jenkins plugin: you're all set.";
testSlackService.publish(message, "green");
return FormValidation.ok("Success");
boolean success = testSlackService.publish(message, "green");
return success ? FormValidation.ok("Success") : FormValidation.error("Failure");
} catch (Exception e) {
return FormValidation.error("Client error : " + e.getMessage());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/jenkins/plugins/slack/SlackService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jenkins.plugins.slack;

public interface SlackService {
void publish(String message);
boolean publish(String message);

void publish(String message, String color);
boolean publish(String message, String color);
}
10 changes: 7 additions & 3 deletions src/main/java/jenkins/plugins/slack/StandardSlackService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public StandardSlackService(String teamDomain, String token, String roomId) {
this.roomIds = roomId.split("[,; ]+");
}

public void publish(String message) {
publish(message, "warning");
public boolean publish(String message) {
return publish(message, "warning");
}

public void publish(String message, String color) {
public boolean publish(String message, String color) {
for (String roomId : roomIds) {
String url = "https://" + teamDomain + "." + host + "/services/hooks/jenkins-ci?token=" + token;
logger.info("Posting: to " + roomId + " on " + teamDomain + " using " + url +": " + message + " " + color);
Expand Down Expand Up @@ -67,14 +67,18 @@ public void publish(String message, String color) {
String response = post.getResponseBodyAsString();
if(responseCode != HttpStatus.SC_OK) {
logger.log(Level.WARNING, "Slack post may have failed. Response: " + response);
return false;
}
return true;
} catch (Exception e) {
logger.log(Level.WARNING, "Error posting to Slack", e);
return false;
} finally {
logger.info("Posting succeeded");
post.releaseConnection();
}
}
return false;
}

private HttpClient getHttpClient() {
Expand Down

0 comments on commit 58e2f4c

Please sign in to comment.