separate out compatibility step

This commit is contained in:
Xinran Xiao 2016-05-17 14:40:24 -07:00
parent d9a66ae5ba
commit 88d0f92ad2
3 changed files with 45 additions and 27 deletions

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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;
}
}