diff --git a/src/main/java/com/dabsquared/gitlabjenkins/util/GsonUtil.java b/src/main/java/com/dabsquared/gitlabjenkins/util/GsonUtil.java new file mode 100644 index 0000000..310736d --- /dev/null +++ b/src/main/java/com/dabsquared/gitlabjenkins/util/GsonUtil.java @@ -0,0 +1,22 @@ +package com.dabsquared.gitlabjenkins.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +/** + * @author Robin Müller + */ +public final class GsonUtil { + + private static final Gson prettyPrint = new GsonBuilder().setPrettyPrinting().create(); + + private GsonUtil() { } + + public static String toPrettyPrint(String json) { + JsonParser parser = new JsonParser(); + JsonObject object = parser.parse(json).getAsJsonObject(); + return prettyPrint.toJson(object); + } +} diff --git a/src/main/java/com/dabsquared/gitlabjenkins/util/LoggerUtil.java b/src/main/java/com/dabsquared/gitlabjenkins/util/LoggerUtil.java new file mode 100644 index 0000000..2b19b34 --- /dev/null +++ b/src/main/java/com/dabsquared/gitlabjenkins/util/LoggerUtil.java @@ -0,0 +1,13 @@ +package com.dabsquared.gitlabjenkins.util; + +/** + * @author Robin Müller + */ +public final class LoggerUtil { + + private LoggerUtil() {} + + public static Object[] toArray(Object... objects) { + return objects; + } +} diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java index bf178d1..03b3c4f 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/ActionResolver.java @@ -1,6 +1,7 @@ package com.dabsquared.gitlabjenkins.webhook; import com.dabsquared.gitlabjenkins.util.ACLUtil; +import com.dabsquared.gitlabjenkins.util.LoggerUtil; import com.dabsquared.gitlabjenkins.webhook.build.MergeRequestBuildAction; import com.dabsquared.gitlabjenkins.webhook.build.PushBuildAction; import com.dabsquared.gitlabjenkins.webhook.status.BranchBuildPageRedirectAction; @@ -22,14 +23,19 @@ import org.kohsuke.stapler.StaplerResponse; import java.io.IOException; import java.util.Iterator; +import java.util.logging.Level; +import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.dabsquared.gitlabjenkins.util.LoggerUtil.toArray; + /** * @author Robin Müller */ public class ActionResolver { + private static final Logger LOGGER = Logger.getLogger(ActionResolver.class.getName()); private static final Pattern COMMIT_STATUS_PATTERN = Pattern.compile("^(refs/[^/]+/)?(commits|builds)/(?[0-9a-fA-F]+)(?/status.json)?$"); @@ -49,6 +55,7 @@ public class ActionResolver { } else if (method.equals("GET")) { return onGet(project, restOfPath, request); } + LOGGER.log(Level.FINE, "Unsupported HTTP method: {0}", method); return new NoopAction(); } @@ -61,6 +68,7 @@ public class ActionResolver { } else if (commitMatcher.matches()) { return onGetCommitStatus(project, commitMatcher.group("sha1"), commitMatcher.group("statusJson")); } + LOGGER.log(Level.FINE, "Unknown GET request: {0}", restOfPath); return new NoopAction(); } @@ -88,6 +96,7 @@ public class ActionResolver { } else if (eventHeader.equals("Push Hook")) { return new PushBuildAction(project, requestBody); } + LOGGER.log(Level.FINE, "Unsupported event header: {0}", eventHeader); return new NoopAction(); } @@ -114,6 +123,7 @@ public class ActionResolver { return (AbstractProject) item; } } + LOGGER.log(Level.FINE, "No project found: {0}, {1}", toArray(projectName, Joiner.on('/').join(restOfPathParts))); return null; } }); diff --git a/src/main/java/com/dabsquared/gitlabjenkins/webhook/GitLabWebHook.java b/src/main/java/com/dabsquared/gitlabjenkins/webhook/GitLabWebHook.java index c1a77b1..15b9f2f 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/GitLabWebHook.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/GitLabWebHook.java @@ -11,6 +11,8 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; /** * @@ -20,6 +22,8 @@ import java.io.IOException; @Extension public class GitLabWebHook implements UnprotectedRootAction { + private static final Logger LOGGER = Logger.getLogger(GitLabWebHook.class.getName()); + public static final String WEBHOOK_URL = "project"; private transient final ActionResolver actionResolver = new ActionResolver(); @@ -37,6 +41,7 @@ public class GitLabWebHook implements UnprotectedRootAction { } public void getDynamic(final String projectName, final StaplerRequest request, StaplerResponse response) { + LOGGER.log(Level.INFO, "WebHook called with url: {0}", request.getRequestURIWithQueryString()); actionResolver.resolve(projectName, request).execute(response); } 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 2e63eaa..b742bea 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/MergeRequestBuildAction.java @@ -15,6 +15,8 @@ import org.kohsuke.stapler.StaplerResponse; import java.util.logging.Level; import java.util.logging.Logger; +import static com.dabsquared.gitlabjenkins.util.GsonUtil.toPrettyPrint; + /** * @author Robin Müller */ @@ -25,6 +27,7 @@ public class MergeRequestBuildAction implements WebHookAction { private GitLabMergeRequest mergeRequest; public MergeRequestBuildAction(AbstractProject project, String json) { + LOGGER.log(Level.FINE, "MergeRequest: {0}", toPrettyPrint(json)); this.project = project; this.mergeRequest = GitLabMergeRequest.create(json); } 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 eb20a9e..560dfd4 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/webhook/build/PushBuildAction.java @@ -20,6 +20,8 @@ import org.kohsuke.stapler.StaplerResponse; import java.util.logging.Level; import java.util.logging.Logger; +import static com.dabsquared.gitlabjenkins.util.GsonUtil.toPrettyPrint; + /** * @author Robin Müller */ @@ -31,6 +33,7 @@ public class PushBuildAction implements WebHookAction { private GitLabPushRequest pushRequest; public PushBuildAction(AbstractProject project, String json) { + LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json)); this.project = project; this.pushRequest = GitLabPushRequest.create(json); }