webhook接受请求后回传已接受信息

This commit is contained in:
yashin 2018-06-28 13:03:38 +08:00
parent 6dc5f9de2c
commit 128c5673c7
6 changed files with 33 additions and 4 deletions

View File

@ -10,6 +10,7 @@ import java.util.List;
/**
* @author Robin Müller
* @author Yashin Luo
*/
@GeneratePojoBuilder(intoPackage = "*.builder.generated", withFactoryMethod = "*")
public class MergeRequestHook extends WebHook {
@ -88,6 +89,10 @@ public class MergeRequestHook extends WebHook {
this.labels = labels;
}
public String getWebHookDescription() {
return getHookName() + " iid = " + objectAttributes.getIid() + " merge commit sha = " + objectAttributes.getMergeCommitSha();
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -112,6 +112,10 @@ public class PushHook extends WebHook {
this.totalCommitsCount = totalCommitsCount;
}
public String getWebHookDescription() {
return getHookName() + " ref = " + ref + " commit sha = " + after;
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -6,6 +6,7 @@ import org.apache.commons.lang.builder.ToStringBuilder;
/**
* @author Robin Müller
* @author Yashin Luo
*/
public abstract class WebHook {
@ -37,6 +38,10 @@ public abstract class WebHook {
this.repository = repository;
}
public String getWebHookDescription() {
return hookName;
}
@Override
public boolean equals(Object o) {
if (this == o) {

View File

@ -1,7 +1,9 @@
package com.dabsquared.gitlabjenkins.webhook.build;
import java.io.IOException;
import java.util.logging.Logger;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.WebHook;
import hudson.model.Item;
import hudson.model.Job;
import hudson.security.Messages;
@ -10,13 +12,17 @@ import hudson.util.HttpResponses;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import com.dabsquared.gitlabjenkins.GitLabPushTrigger;
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig;
import com.dabsquared.gitlabjenkins.webhook.WebHookAction;
import javax.servlet.ServletException;
/**
* @author Xinran Xiao
* @author Yashin Luo
*/
abstract class BuildWebHookAction implements WebHookAction {
@ -31,6 +37,16 @@ abstract class BuildWebHookAction implements WebHookAction {
execute();
}
public static HttpResponses.HttpResponseException responseWithHook(final WebHook webHook) {
return new HttpResponses.HttpResponseException() {
public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
String text = webHook.getWebHookDescription() + " has been accepted.";
rsp.setContentType("text/plain;charset=UTF-8");
rsp.getWriter().println(text);
}
};
}
protected abstract static class TriggerNotifier implements Runnable {
private final Item project;

View File

@ -64,6 +64,6 @@ public class MergeRequestBuildAction extends BuildWebHookAction {
trigger.onPost(mergeRequestHook);
}
});
throw HttpResponses.ok();
throw responseWithHook(mergeRequestHook);
}
}

View File

@ -14,7 +14,6 @@ import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.URIish;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
@ -72,11 +71,11 @@ public class PushBuildAction extends BuildWebHookAction {
trigger.onPost(pushHook);
}
});
throw HttpResponses.ok();
throw responseWithHook(pushHook);
}
if (project instanceof SCMSourceOwner) {
ACL.impersonate(ACL.SYSTEM, new SCMSourceOwnerNotifier());
throw HttpResponses.ok();
throw responseWithHook(pushHook);
}
throw HttpResponses.errorWithoutStack(409, "Push Hook is not supported for this project");
}