Merge pull request #335 from AvanzaBank/action-resolver-refactoring-and-tests

More tests and refactoring of ActionResolver.onPost
This commit is contained in:
omorillo 2016-05-28 16:57:44 +02:00
commit bd313432b9
2 changed files with 42 additions and 9 deletions

View File

@ -17,7 +17,6 @@ import hudson.security.ACL;
import hudson.util.HttpResponses; import hudson.util.HttpResponses;
import jenkins.model.Jenkins; import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse; import org.kohsuke.stapler.StaplerResponse;
@ -91,16 +90,22 @@ public class ActionResolver {
} }
private WebHookAction onPost(Job<?, ?> project, StaplerRequest request) { private WebHookAction onPost(Job<?, ?> project, StaplerRequest request) {
String requestBody = getRequestBody(request);
String eventHeader = request.getHeader("X-Gitlab-Event"); String eventHeader = request.getHeader("X-Gitlab-Event");
if (StringUtils.equals(eventHeader, "Merge Request Hook") ) { if (eventHeader == null) {
return new MergeRequestBuildAction(project, requestBody); LOGGER.log(Level.FINE, "Missing X-Gitlab-Event header");
} else if ( StringUtils.equals(eventHeader,"Push Hook") || StringUtils.equals(eventHeader,"Tag Push Hook")) {
return new PushBuildAction(project, requestBody);
}
LOGGER.log(Level.FINE, "Unsupported event header: {0}", eventHeader);
return new NoopAction(); return new NoopAction();
} }
switch (eventHeader) {
case "Merge Request Hook":
return new MergeRequestBuildAction(project, getRequestBody(request));
case "Push Hook":
case "Tag Push Hook":
return new PushBuildAction(project, getRequestBody(request));
default:
LOGGER.log(Level.FINE, "Unsupported X-Gitlab-Event header: {0}", eventHeader);
return new NoopAction();
}
}
private String getRequestBody(StaplerRequest request) { private String getRequestBody(StaplerRequest request) {
String requestBody; String requestBody;
@ -132,7 +137,7 @@ public class ActionResolver {
}); });
} }
private static class NoopAction implements WebHookAction { static class NoopAction implements WebHookAction {
public void execute(StaplerResponse response) { public void execute(StaplerResponse response) {
} }
} }

View File

@ -1,5 +1,6 @@
package com.dabsquared.gitlabjenkins.webhook; package com.dabsquared.gitlabjenkins.webhook;
import com.dabsquared.gitlabjenkins.webhook.ActionResolver.NoopAction;
import com.dabsquared.gitlabjenkins.webhook.build.MergeRequestBuildAction; import com.dabsquared.gitlabjenkins.webhook.build.MergeRequestBuildAction;
import com.dabsquared.gitlabjenkins.webhook.build.PushBuildAction; import com.dabsquared.gitlabjenkins.webhook.build.PushBuildAction;
import com.dabsquared.gitlabjenkins.webhook.status.BranchBuildPageRedirectAction; import com.dabsquared.gitlabjenkins.webhook.status.BranchBuildPageRedirectAction;
@ -152,6 +153,33 @@ public class ActionResolverTest {
assertThat(resolvedAction, instanceOf(PushBuildAction.class)); assertThat(resolvedAction, instanceOf(PushBuildAction.class));
} }
@Test
public void postPushMissingEventHeader() throws IOException {
String projectName = "test";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn(null);
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
assertThat(resolvedAction, instanceOf(NoopAction.class));
}
@Test
public void postPushUnsupportedEventHeader() throws IOException {
String projectName = "test";
jenkins.createFreeStyleProject(projectName);
when(request.getRestOfPath()).thenReturn("");
when(request.getMethod()).thenReturn("POST");
when(request.getHeader("X-Gitlab-Event")).thenReturn("__Not Supported Header__");
when(request.getInputStream()).thenReturn(new ResourceServletInputStream("ActionResolverTest_postPush.json"));
WebHookAction resolvedAction = new ActionResolver().resolve(projectName, request);
assertThat(resolvedAction, instanceOf(NoopAction.class));
}
private static class ResourceServletInputStream extends ServletInputStream { private static class ResourceServletInputStream extends ServletInputStream {