Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Define WebhookRequest which wraps StaplerRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
ikikko committed Jan 24, 2015
1 parent 3db418b commit 109748d
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import hudson.model.RootAction;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutorFactory;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookParser;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
Expand Down Expand Up @@ -44,9 +44,7 @@ public HttpResponse doNotify() {
return new HttpResponse() {
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
throws IOException, ServletException {
String message = new WebhookParser(req).parse();
WebhookExecutor executor = WebhookExecutorFactory.create(req, rsp, message);

WebhookExecutor executor = WebhookExecutorFactory.create(new WebhookRequest(req), rsp);
executor.execute();
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.typetalk.webhookaction;

import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.http.HttpServletResponse;
Expand All @@ -12,11 +11,11 @@ public abstract class WebhookExecutor {

protected Logger logger = Logger.getLogger(getClass().getName());

protected StaplerRequest req;
protected WebhookRequest req;
protected StaplerResponse rsp;
protected String command;

protected WebhookExecutor(StaplerRequest req, StaplerResponse rsp, String command) {
protected WebhookExecutor(WebhookRequest req, StaplerResponse rsp, String command) {
this.req = req;
this.rsp = rsp;
this.command = command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.HelpExecutor;
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.ListExecutor;
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.UndefinedExecutor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import java.util.Arrays;
import java.util.LinkedList;

public class WebhookExecutorFactory {
public static WebhookExecutor create(StaplerRequest req, StaplerResponse rsp, String message) {
LinkedList<String> messageList = new LinkedList<>(Arrays.asList(message.split("\\s+")));
public static WebhookExecutor create(WebhookRequest req, StaplerResponse rsp) {
LinkedList<String> messageList = new LinkedList<>(Arrays.asList(req.getPostMessage().split("\\s+")));
String botUser = messageList.poll(); // not used
String command = messageList.poll();

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.jenkinsci.plugins.typetalk.webhookaction;

import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

import java.io.BufferedReader;
import java.io.IOException;

public class WebhookRequest {

private StaplerRequest req;
private JSONObject json;

WebhookRequest() {
// for test
}

public WebhookRequest(StaplerRequest req) {
this.req = req;
parseBodyToJson();
}

private void parseBodyToJson() {
try {
StringBuilder sb = new StringBuilder();
BufferedReader reader = req.getReader();

String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}

json = JSONObject.fromObject(sb.toString());
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}

public String getRemoteAddr() {
return req.getRemoteAddr();
}

public int getPostId() {
return json.getJSONObject("post").getInt("id");
}

public String getPostMessage() {
return json.getJSONObject("post").getString("message");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hudson.model.*;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

Expand All @@ -20,7 +21,7 @@ public class BuildExecutor extends WebhookExecutor {

private Map<String, String> parameterMap = new HashMap<>();

public BuildExecutor(StaplerRequest req, StaplerResponse rsp, String job, List<String> parameters) {
public BuildExecutor(WebhookRequest req, StaplerResponse rsp, String job, List<String> parameters) {
super(req, rsp, "build");
this.job = job;
this.parameters = parameters;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jenkinsci.plugins.typetalk.webhookaction.executorimpl;

import org.kohsuke.stapler.StaplerRequest;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest;
import org.kohsuke.stapler.StaplerResponse;

// TODO add help executor
public class HelpExecutor extends UndefinedExecutor {

public HelpExecutor(StaplerRequest req, StaplerResponse rsp, String command) {
public HelpExecutor(WebhookRequest req, StaplerResponse rsp, String command) {
super(req, rsp, command);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jenkinsci.plugins.typetalk.webhookaction.executorimpl;

import org.kohsuke.stapler.StaplerRequest;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest;
import org.kohsuke.stapler.StaplerResponse;

// TODO add list executor ( with regexp filter )
public class ListExecutor extends UndefinedExecutor {

public ListExecutor(StaplerRequest req, StaplerResponse rsp, String command) {
public ListExecutor(WebhookRequest req, StaplerResponse rsp, String command) {
super(req, rsp, command);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.jenkinsci.plugins.typetalk.webhookaction.executorimpl;

import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor;
import org.kohsuke.stapler.StaplerRequest;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest;
import org.kohsuke.stapler.StaplerResponse;

import java.util.logging.Logger;

public class UndefinedExecutor extends WebhookExecutor {

public UndefinedExecutor(StaplerRequest req, StaplerResponse rsp, String command) {
public UndefinedExecutor(WebhookRequest req, StaplerResponse rsp, String command) {
super(req, rsp, command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.BuildExecutor
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.HelpExecutor
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.ListExecutor
import org.jenkinsci.plugins.typetalk.webhookaction.executorimpl.UndefinedExecutor
import org.kohsuke.stapler.StaplerRequest
import org.kohsuke.stapler.StaplerResponse
import spock.lang.Specification
import spock.lang.Unroll

class WebhookExecutorFactorySpec extends Specification {

def req = Mock(StaplerRequest)
def req = Mock(WebhookRequest)
def res = Mock(StaplerResponse)

@Unroll
def "create BuildExecutor : #message"() {
setup:
req.postMessage >> message

when:
def executor = WebhookExecutorFactory.create(req, res, message)
def executor = WebhookExecutorFactory.create(req, res)

then:
executor.class == BuildExecutor
Expand All @@ -33,24 +35,33 @@ class WebhookExecutorFactorySpec extends Specification {
}

def "create ListExecutor"() {
setup:
req.postMessage >> "@jenkins+ list"

when:
def executor = WebhookExecutorFactory.create(req, res, "@jenkins+ list")
def executor = WebhookExecutorFactory.create(req, res)

then:
executor.class == ListExecutor
}

def "create HelpExecutor"() {
setup:
req.postMessage >> "@jenkins+ help"

when:
def executor = WebhookExecutorFactory.create(req, res, "@jenkins+ help")
def executor = WebhookExecutorFactory.create(req, res)

then:
executor.class == HelpExecutor
}

def "create UndefinedExecutor"() {
setup:
req.postMessage >> "@jenkins+ dummy"

when:
def executor = WebhookExecutorFactory.create(req, res, "@jenkins+ dummy")
def executor = WebhookExecutorFactory.create(req, res)

then:
executor.class == UndefinedExecutor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.jenkinsci.plugins.typetalk.webhookaction

import net.sf.json.JSONObject
import spock.lang.Specification

class WebhookParserSpec extends Specification {

def parseJson() {
setup:
def parser = new WebhookParser()
def json = """
class WebhookRequestSpec extends Specification {
def json = """
{
"topic": {
"id": 9526,
Expand Down Expand Up @@ -42,8 +39,14 @@ class WebhookParserSpec extends Specification {
}
"""

def parseJson() {
setup:
def request = new WebhookRequest()
request.json = JSONObject.fromObject(json)

expect:
parser.parseJson(json) == "@ikikkobot+ build hello-typetalk-plugin"
request.getPostMessage() == "@ikikkobot+ build hello-typetalk-plugin"
request.getPostId() == 754399
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import org.acegisecurity.Authentication
import org.acegisecurity.context.SecurityContextHolder
import org.acegisecurity.providers.TestingAuthenticationToken
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest
import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule
import org.jvnet.hudson.test.TestBuilder
import org.kohsuke.stapler.StaplerRequest
import org.kohsuke.stapler.StaplerResponse
import spock.lang.Specification
import spock.lang.Unroll
Expand All @@ -24,7 +24,7 @@ class BuildExecutorSpec extends Specification {

@Rule JenkinsRule j = new JenkinsRule()

def req = Mock(StaplerRequest)
def req = Mock(WebhookRequest)
def res = Mock(StaplerResponse)

AbstractProject project
Expand Down

0 comments on commit 109748d

Please sign in to comment.