Skip to content

Commit

Permalink
Adding configurable webhook endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dpires committed Jan 8, 2016
1 parent 5cf2087 commit 27cc45a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/main/java/jenkins/plugins/slack/webhook/GlobalConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
public class GlobalConfig extends GlobalConfiguration {

private String slackOutgoingWebhookToken;
private String slackOutgoingWebhookURL;

public GlobalConfig() {
load();
Expand All @@ -42,6 +43,21 @@ public FormValidation doCheckSlackOutgoingWebhookToken(@QueryParameter String va
return FormValidation.ok();
}

public String getSlackOutgoingWebhookURL() {
return slackOutgoingWebhookURL;
}

public void setSlackOutgoingWebhookURL(String slackOutgoingWebhookURL) {
this.slackOutgoingWebhookURL = slackOutgoingWebhookURL;
}

public FormValidation doCheckSlackOutgoingWebhookURL(@QueryParameter String value) {
if (value == null || value.trim().isEmpty())
return FormValidation.warning("Please set a url endpoint");

return FormValidation.ok();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
req.bindJSON(this, json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ public WebhookEndpoint() {

@Override
public String getUrlName() {
return "webhook";
return "/"+globalConfig.getSlackOutgoingWebhookURL();
}

@RequirePOST
public HttpResponse doIndex(StaplerRequest req) throws IOException,
ServletException {

if (globalConfig.getSlackOutgoingWebhookToken() == null) {
if (globalConfig.getSlackOutgoingWebhookToken() == null ||
globalConfig.getSlackOutgoingWebhookToken().equals("")) {
return new JsonResponse(new SlackTextMessage("Slack token not set"),
StaplerResponse.SC_OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ f.section(title: _('Slack Webhook Settings')) {
f.entry(field: 'slackOutgoingWebhookToken', title: _('Outgoing Webhook Token')) {
f.textbox()
}
f.entry(field: 'slackOutgoingWebhookURL', title: _('Outgoing Webhook URL Endpoint')) {
f.textbox()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
This is the webhook endpoint to send requests to.
<br />
Example: "webook", "slackwebook". This endpoint will be mapped to your Jenkins root address at JENKINS_HOST/ENDPOINT
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import static java.net.HttpURLConnection.HTTP_OK;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;

import org.apache.commons.httpclient.NameValuePair;

Expand All @@ -45,7 +46,8 @@ public class WebhookEndpointTest {

private JenkinsRule.WebClient client;

private final String ENDPOINT = "webhook/";
private final String URL = "webook";
private final String ENDPOINT = URL+"/";

private List<NameValuePair> data;

Expand All @@ -61,8 +63,18 @@ public void setUp() throws Exception {
data.add(new NameValuePair("trigger_word", "jenkins"));
}

@Test
public void testUnconfiguredSlackURL() throws Exception {
WebResponse response = makeRequest(null);
assertThat(response.getStatusCode(), is(HTTP_NOT_FOUND));
}

@Test
public void testUnconfiguredSlackToken() throws Exception {
HtmlForm form = jenkinsRule.createWebClient().goTo("configure").getFormByName("config");
form.getInputByName("_.slackOutgoingWebhookURL").setValueAttribute(URL);
jenkinsRule.submit(form);

WebResponse response = makeRequest(null);

assertThat(response.getStatusCode(), is(HTTP_OK));
Expand Down Expand Up @@ -163,6 +175,7 @@ public void testGetProjectBuildLog() throws Exception {

private void setConfigSettings() throws Exception {
HtmlForm form = jenkinsRule.createWebClient().goTo("configure").getFormByName("config");
form.getInputByName("_.slackOutgoingWebhookURL").setValueAttribute(URL);
form.getInputByName("_.slackOutgoingWebhookToken").setValueAttribute("GOOD_TOKEN");
jenkinsRule.submit(form);
}
Expand Down

0 comments on commit 27cc45a

Please sign in to comment.