Add some logging for the web hook processing
This commit is contained in:
parent
5179494b92
commit
e3e29a34c5
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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)/(?<sha1>[0-9a-fA-F]+)(?<statusJson>/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;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue