From d5752a70e0b06ae5d91971fcf42e8e1a0962ae56 Mon Sep 17 00:00:00 2001 From: Kristoffer Erlandsson Date: Thu, 26 May 2016 10:48:04 +0200 Subject: [PATCH] More tests and refactoring of ActionResolver.onPost --- .../gitlabjenkins/webhook/ActionResolver.java | 23 +++++++++------ .../webhook/ActionResolverTest.java | 28 +++++++++++++++++++ 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java index aa55668..d93f289 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java @@ -17,7 +17,6 @@ import hudson.security.ACL; import hudson.util.HttpResponses; import jenkins.model.Jenkins; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; @@ -91,15 +90,21 @@ public class ActionResolver { } private WebHookAction onPost(Job project, StaplerRequest request) { - String requestBody = getRequestBody(request); String eventHeader = request.getHeader("X-Gitlab-Event"); - if (StringUtils.equals(eventHeader, "Merge Request Hook") ) { - return new MergeRequestBuildAction(project, requestBody); - } else if ( StringUtils.equals(eventHeader,"Push Hook") || StringUtils.equals(eventHeader,"Tag Push Hook")) { - return new PushBuildAction(project, requestBody); + if (eventHeader == null) { + LOGGER.log(Level.FINE, "Missing X-Gitlab-Event header"); + 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(); } - LOGGER.log(Level.FINE, "Unsupported event header: {0}", eventHeader); - return new NoopAction(); } private String getRequestBody(StaplerRequest request) { @@ -132,7 +137,7 @@ public class ActionResolver { }); } - private static class NoopAction implements WebHookAction { + static class NoopAction implements WebHookAction { public void execute(StaplerResponse response) { } } diff --git a/src/test/java/com/dabsquared/gitlabjenkins/webhook/ActionResolverTest.java b/src/test/java/com/dabsquared/gitlabjenkins/webhook/ActionResolverTest.java index 602b914..2b3ae95 100644 --- a/src/test/java/com/dabsquared/gitlabjenkins/webhook/ActionResolverTest.java +++ b/src/test/java/com/dabsquared/gitlabjenkins/webhook/ActionResolverTest.java @@ -1,5 +1,6 @@ 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.PushBuildAction; import com.dabsquared.gitlabjenkins.webhook.status.BranchBuildPageRedirectAction; @@ -152,6 +153,33 @@ public class ActionResolverTest { 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 {