From 88d0f92ad2d800f8a6d5aca88dd31424a358f2b0 Mon Sep 17 00:00:00 2001 From: Xinran Xiao Date: Tue, 17 May 2016 14:40:24 -0700 Subject: [PATCH] separate out compatibility step --- .../webhook/build/BuildWebHookAction.java | 17 +++++++ .../build/MergeRequestBuildAction.java | 9 +++- .../webhook/build/PushBuildAction.java | 46 +++++++++---------- 3 files changed, 45 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java new file mode 100644 index 0000000..6bfdc7b --- /dev/null +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java @@ -0,0 +1,17 @@ +package com.dabsquared.gitlabjenkins.webhook.build; + +import com.dabsquared.gitlabjenkins.webhook.WebHookAction; +import org.kohsuke.stapler.StaplerResponse; + +/** + * @author Xinran Xiao + */ +abstract class BuildWebHookAction implements WebHookAction { + abstract void processForCompatibility(); + abstract void execute(); + + public final void execute(StaplerResponse response) { + processForCompatibility(); + execute(); + } +} diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java index 8979ba5..582939f 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java @@ -17,7 +17,7 @@ import static com.dabsquared.gitlabjenkins.util.JsonUtil.toPrettyPrint; /** * @author Robin Müller */ -public class MergeRequestBuildAction implements WebHookAction { +public class MergeRequestBuildAction extends BuildWebHookAction { private final static Logger LOGGER = Logger.getLogger(MergeRequestBuildAction.class.getName()); private Job project; @@ -29,7 +29,12 @@ public class MergeRequestBuildAction implements WebHookAction { this.mergeRequestHook = JsonUtil.read(json, MergeRequestHook.class); } - public void execute(StaplerResponse response) { + /** + * Noop for merge request hooks. + */ + void processForCompatibility() {} + + public void execute() { ACL.impersonate(ACL.SYSTEM, new Runnable() { public void run() { GitLabPushTrigger trigger = GitLabPushTrigger.getFromJob(project); diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java index 995bfb8..1eb41be 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java @@ -21,7 +21,7 @@ import static com.dabsquared.gitlabjenkins.util.JsonUtil.toPrettyPrint; /** * @author Robin Müller */ -public class PushBuildAction implements WebHookAction { +public class PushBuildAction extends BuildWebHookAction { private final static Logger LOGGER = Logger.getLogger(PushBuildAction.class.getName()); private final Job project; @@ -31,10 +31,28 @@ public class PushBuildAction implements WebHookAction { public PushBuildAction(Job project, String json) { LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json)); this.project = project; - this.pushHook = processForCompatibility(JsonUtil.read(json, PushHook.class)); + this.pushHook = JsonUtil.read(json, PushHook.class); } - public void execute(StaplerResponse response) { + void processForCompatibility() { + // Fill in project if it's not defined. + if (this.pushHook.getProject() == null && this.pushHook.getRepository() != null) { + try { + String path = new URL(this.pushHook.getRepository().getGitHttpUrl()).getPath(); + if (StringUtils.isNotBlank(path)) { + Project project = new Project(); + project.setNamespace(path.replaceFirst("/", "").substring(0, path.lastIndexOf("/"))); + this.pushHook.setProject(project); + } else { + LOGGER.log(Level.WARNING, "Could not find suitable namespace."); + } + } catch (MalformedURLException ignored) { + LOGGER.log(Level.WARNING, "Invalid repository url found while building namespace."); + } + } + } + + public void execute() { if (pushHook.getRepository() != null && pushHook.getRepository().getUrl() == null) { LOGGER.log(Level.WARNING, "No repository url found."); return; @@ -50,26 +68,4 @@ public class PushBuildAction implements WebHookAction { }); throw HttpResponses.ok(); } - - private static PushHook processForCompatibility(PushHook pushHook) { - // Fill in project if it's not defined. - if (pushHook.getProject() == null) { - if (pushHook.getRepository() != null) { - try { - String path = new URL(pushHook.getRepository().getGitHttpUrl()).getPath(); - if (StringUtils.isNotBlank(path)) { - Project project = new Project(); - project.setNamespace(path.replaceFirst("/", "").substring(0, path.lastIndexOf("/"))); - pushHook.setProject(project); - } else { - LOGGER.log(Level.WARNING, "Could not find suitable namespace."); - } - } catch (MalformedURLException ignored) { - LOGGER.log(Level.WARNING, "Invalid repository url found while building namespace."); - } - } - } - - return pushHook; - } }