Add some logging for the web hook processing

This commit is contained in:
Robin Müller 2016-03-13 17:49:06 +01:00
parent 5179494b92
commit e3e29a34c5
6 changed files with 56 additions and 0 deletions

View File

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

View File

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

View File

@ -1,6 +1,7 @@
package com.dabsquared.gitlabjenkins.webhook; package com.dabsquared.gitlabjenkins.webhook;
import com.dabsquared.gitlabjenkins.util.ACLUtil; 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.MergeRequestBuildAction;
import com.dabsquared.gitlabjenkins.webhook.build.PushBuildAction; import com.dabsquared.gitlabjenkins.webhook.build.PushBuildAction;
import com.dabsquared.gitlabjenkins.webhook.status.BranchBuildPageRedirectAction; import com.dabsquared.gitlabjenkins.webhook.status.BranchBuildPageRedirectAction;
@ -22,14 +23,19 @@ import org.kohsuke.stapler.StaplerResponse;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.dabsquared.gitlabjenkins.util.LoggerUtil.toArray;
/** /**
* @author Robin Müller * @author Robin Müller
*/ */
public class ActionResolver { public class ActionResolver {
private static final Logger LOGGER = Logger.getLogger(ActionResolver.class.getName());
private static final Pattern COMMIT_STATUS_PATTERN = private static final Pattern COMMIT_STATUS_PATTERN =
Pattern.compile("^(refs/[^/]+/)?(commits|builds)/(?<sha1>[0-9a-fA-F]+)(?<statusJson>/status.json)?$"); Pattern.compile("^(refs/[^/]+/)?(commits|builds)/(?<sha1>[0-9a-fA-F]+)(?<statusJson>/status.json)?$");
@ -49,6 +55,7 @@ public class ActionResolver {
} else if (method.equals("GET")) { } else if (method.equals("GET")) {
return onGet(project, restOfPath, request); return onGet(project, restOfPath, request);
} }
LOGGER.log(Level.FINE, "Unsupported HTTP method: {0}", method);
return new NoopAction(); return new NoopAction();
} }
@ -61,6 +68,7 @@ public class ActionResolver {
} else if (commitMatcher.matches()) { } else if (commitMatcher.matches()) {
return onGetCommitStatus(project, commitMatcher.group("sha1"), commitMatcher.group("statusJson")); return onGetCommitStatus(project, commitMatcher.group("sha1"), commitMatcher.group("statusJson"));
} }
LOGGER.log(Level.FINE, "Unknown GET request: {0}", restOfPath);
return new NoopAction(); return new NoopAction();
} }
@ -88,6 +96,7 @@ public class ActionResolver {
} else if (eventHeader.equals("Push Hook")) { } else if (eventHeader.equals("Push Hook")) {
return new PushBuildAction(project, requestBody); return new PushBuildAction(project, requestBody);
} }
LOGGER.log(Level.FINE, "Unsupported event header: {0}", eventHeader);
return new NoopAction(); return new NoopAction();
} }
@ -114,6 +123,7 @@ public class ActionResolver {
return (AbstractProject<?, ?>) item; return (AbstractProject<?, ?>) item;
} }
} }
LOGGER.log(Level.FINE, "No project found: {0}, {1}", toArray(projectName, Joiner.on('/').join(restOfPathParts)));
return null; return null;
} }
}); });

View File

@ -11,6 +11,8 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/** /**
* *
@ -20,6 +22,8 @@ import java.io.IOException;
@Extension @Extension
public class GitLabWebHook implements UnprotectedRootAction { public class GitLabWebHook implements UnprotectedRootAction {
private static final Logger LOGGER = Logger.getLogger(GitLabWebHook.class.getName());
public static final String WEBHOOK_URL = "project"; public static final String WEBHOOK_URL = "project";
private transient final ActionResolver actionResolver = new ActionResolver(); 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) { 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); actionResolver.resolve(projectName, request).execute(response);
} }

View File

@ -15,6 +15,8 @@ import org.kohsuke.stapler.StaplerResponse;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.util.GsonUtil.toPrettyPrint;
/** /**
* @author Robin Müller * @author Robin Müller
*/ */
@ -25,6 +27,7 @@ public class MergeRequestBuildAction implements WebHookAction {
private GitLabMergeRequest mergeRequest; private GitLabMergeRequest mergeRequest;
public MergeRequestBuildAction(AbstractProject<?, ?> project, String json) { public MergeRequestBuildAction(AbstractProject<?, ?> project, String json) {
LOGGER.log(Level.FINE, "MergeRequest: {0}", toPrettyPrint(json));
this.project = project; this.project = project;
this.mergeRequest = GitLabMergeRequest.create(json); this.mergeRequest = GitLabMergeRequest.create(json);
} }

View File

@ -20,6 +20,8 @@ import org.kohsuke.stapler.StaplerResponse;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import static com.dabsquared.gitlabjenkins.util.GsonUtil.toPrettyPrint;
/** /**
* @author Robin Müller * @author Robin Müller
*/ */
@ -31,6 +33,7 @@ public class PushBuildAction implements WebHookAction {
private GitLabPushRequest pushRequest; private GitLabPushRequest pushRequest;
public PushBuildAction(AbstractProject<?, ?> project, String json) { public PushBuildAction(AbstractProject<?, ?> project, String json) {
LOGGER.log(Level.FINE, "Push: {0}", toPrettyPrint(json));
this.project = project; this.project = project;
this.pushRequest = GitLabPushRequest.create(json); this.pushRequest = GitLabPushRequest.create(json);
} }