From 128c5673c7c3921e30b9b16e35175eaa816f3485 Mon Sep 17 00:00:00 2001 From: yashin Date: Thu, 28 Jun 2018 13:03:38 +0800 Subject: [PATCH] =?UTF-8?q?webhook=E6=8E=A5=E5=8F=97=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=90=8E=E5=9B=9E=E4=BC=A0=E5=B7=B2=E6=8E=A5=E5=8F=97=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitlab/hook/model/MergeRequestHook.java | 5 +++++ .../gitlab/hook/model/PushHook.java | 4 ++++ .../gitlabjenkins/gitlab/hook/model/WebHook.java | 5 +++++ .../webhook/build/BuildWebHookAction.java | 16 ++++++++++++++++ .../webhook/build/MergeRequestBuildAction.java | 2 +- .../webhook/build/PushBuildAction.java | 5 ++--- 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/MergeRequestHook.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/MergeRequestHook.java index 511edd8..db8972d 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/MergeRequestHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/MergeRequestHook.java @@ -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) { diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java index 45d1b28..43725f8 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/PushHook.java @@ -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) { diff --git a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/WebHook.java b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/WebHook.java index 50fda4f..b55538e 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/WebHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/WebHook.java @@ -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) { diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java index 98e59c6..7ba6edc 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/BuildWebHookAction.java @@ -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; 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 e4e8f4a..24f1a1c 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java @@ -64,6 +64,6 @@ public class MergeRequestBuildAction extends BuildWebHookAction { trigger.onPost(mergeRequestHook); } }); - throw HttpResponses.ok(); + throw responseWithHook(mergeRequestHook); } } 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 ad083a3..1e2c094 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java @@ -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"); }