Merge pull request #322 from xinranxiao/project_field_compatibility
Git push hook namespace compatibility for Gitlab pre ~v8.5
This commit is contained in:
commit
821cb3fd0e
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -2,6 +2,8 @@ package com.dabsquared.gitlabjenkins.webhook.build;
|
|||
|
||||
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestHook;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.hook.model.ObjectAttributes;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.hook.model.Project;
|
||||
import com.dabsquared.gitlabjenkins.util.JsonUtil;
|
||||
import com.dabsquared.gitlabjenkins.webhook.WebHookAction;
|
||||
import hudson.model.Job;
|
||||
|
@ -17,7 +19,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 +31,23 @@ public class MergeRequestBuildAction implements WebHookAction {
|
|||
this.mergeRequestHook = JsonUtil.read(json, MergeRequestHook.class);
|
||||
}
|
||||
|
||||
public void execute(StaplerResponse response) {
|
||||
void processForCompatibility() {
|
||||
// url and homepage are introduced in 8.x versions of Gitlab
|
||||
final ObjectAttributes attributes = this.mergeRequestHook.getObjectAttributes();
|
||||
if (attributes != null) {
|
||||
final Project source = attributes.getSource();
|
||||
if (source != null && source.getHttpUrl() != null) {
|
||||
if (source.getUrl() == null) {
|
||||
source.setUrl(source.getHttpUrl());
|
||||
}
|
||||
if (source.getHomepage() == null) {
|
||||
source.setHomepage(source.getHttpUrl().substring(0, source.getHttpUrl().lastIndexOf(".git")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
ACL.impersonate(ACL.SYSTEM, new Runnable() {
|
||||
public void run() {
|
||||
GitLabPushTrigger trigger = GitLabPushTrigger.getFromJob(project);
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package com.dabsquared.gitlabjenkins.webhook.build;
|
||||
|
||||
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.hook.model.Project;
|
||||
import com.dabsquared.gitlabjenkins.gitlab.hook.model.PushHook;
|
||||
import com.dabsquared.gitlabjenkins.util.JsonUtil;
|
||||
import com.dabsquared.gitlabjenkins.webhook.WebHookAction;
|
||||
import hudson.model.Job;
|
||||
import hudson.security.ACL;
|
||||
import hudson.util.HttpResponses;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
@ -17,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;
|
||||
|
@ -30,7 +34,25 @@ public class PushBuildAction implements WebHookAction {
|
|||
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;
|
||||
|
|
Loading…
Reference in New Issue