All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gitlab4j.api.webhook.WebHookManager Maven / Gradle / Ivy

Go to download

GitLab4J-API (gitlab4j-api) provides a full featured Java client library for working with GitLab repositories and servers via the GitLab REST API.

There is a newer version: 6.0.0-rc.5
Show newest version

package org.gitlab4j.api.webhook;

import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.HookManager;
import org.gitlab4j.api.utils.HttpRequestUtils;
import org.gitlab4j.api.utils.JacksonJson;

/**
 * This class provides a handler for processing GitLab WebHook callouts.
 */
public class WebHookManager extends HookManager {

    private final static Logger LOGGER = GitLabApi.getLogger();
    private final JacksonJson jacksonJson = new JacksonJson();

    // Collection of objects listening for WebHook events.
    private final List webhookListeners = new CopyOnWriteArrayList();

    /**
     * Create a HookManager to handle GitLab webhook events.
     */
    public WebHookManager() {
        super();
    }

    /**
     * Create a HookManager to handle GitLab webhook events which will be verified
     * against the specified secretToken.
     * 
     * @param secretToken the secret token to verify against
     */
    public WebHookManager(String secretToken) {
        super(secretToken);
    }

    /**
     * Parses and verifies an Event instance from the HTTP request and
     * fires it off to the registered listeners.
     * 
     * @param request the HttpServletRequest to read the Event instance from
     * @throws GitLabApiException if the parsed event is not supported
     */
    public void handleEvent(HttpServletRequest request) throws GitLabApiException {

        String eventName = request.getHeader("X-Gitlab-Event");
        if (eventName == null || eventName.trim().isEmpty()) {
            LOGGER.warning("X-Gitlab-Event header is missing!");
            return;
        }

        if (!isValidSecretToken(request)) {
            String message = "X-Gitlab-Token mismatch!";
            LOGGER.warning(message);
            throw new GitLabApiException(message);
        }

        LOGGER.info("handleEvent: X-Gitlab-Event=" + eventName);
        switch (eventName) {

        case BuildEvent.BUILD_HOOK_X_GITLAB_EVENT:
        case BuildEvent.JOB_HOOK_X_GITLAB_EVENT:
        case IssueEvent.X_GITLAB_EVENT:
        case MergeRequestEvent.X_GITLAB_EVENT:
        case NoteEvent.X_GITLAB_EVENT:
        case PipelineEvent.X_GITLAB_EVENT:
        case PushEvent.X_GITLAB_EVENT:
        case TagPushEvent.X_GITLAB_EVENT:
        case WikiPageEvent.X_GITLAB_EVENT:
            break;

        default:
            String message = "Unsupported X-Gitlab-Event, event Name=" + eventName;
            LOGGER.warning(message);
            throw new GitLabApiException(message);
        }

        try {

            Event event;
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine(HttpRequestUtils.getShortRequestDump(eventName + " webhook", true, request));
                String postData = HttpRequestUtils.getPostDataAsString(request);
                LOGGER.fine("Raw POST data:\n" + postData);
                event = jacksonJson.unmarshal(Event.class, postData);
                LOGGER.fine(event.getObjectKind() + " event:\n" + jacksonJson.marshal(event) + "\n");
            } else {
                InputStreamReader reader = new InputStreamReader(request.getInputStream());
                event = jacksonJson.unmarshal(Event.class, reader);
            }

            event.setRequestUrl(request.getRequestURL().toString());
            event.setRequestQueryString(request.getQueryString());
            fireEvent(event);

        } catch (Exception e) {
            LOGGER.warning("Error parsing JSON data, exception=" + e.getClass().getSimpleName() + ", error=" + e.getMessage());
            throw new GitLabApiException(e);
        }
    }

    /**
     * Verifies the provided Event and fires it off to the registered listeners.
     * 
     * @param event the Event instance to handle
     * @throws GitLabApiException if the event is not supported
     */
    public void handleEvent(Event event) throws GitLabApiException {

        LOGGER.info("handleEvent: object_kind=" + event.getObjectKind());

        switch (event.getObjectKind()) {
        case BuildEvent.OBJECT_KIND:
        case IssueEvent.OBJECT_KIND:
        case MergeRequestEvent.OBJECT_KIND:
        case NoteEvent.OBJECT_KIND:
        case PipelineEvent.OBJECT_KIND:
        case PushEvent.OBJECT_KIND:
        case TagPushEvent.OBJECT_KIND:
        case WikiPageEvent.OBJECT_KIND:
            break;

        default:
            String message = "Unsupported event object_kind, object_kind=" + event.getObjectKind();
            LOGGER.warning(message);
            throw new GitLabApiException(message);
        }

        fireEvent(event);
    }

    /**
     * Adds a WebHook event listener.
     *
     * @param listener the SystemHookListener to add
     */
    public void addListener(WebHookListener listener) {

        if (!webhookListeners.contains(listener)) {
            webhookListeners.add(listener);
        }
    }

    /**
     * Removes a WebHook event listener.
     *
     * @param listener the SystemHookListener to remove
     */
    public void removeListener(WebHookListener listener) {
        webhookListeners.remove(listener);
    }

    /**
     * Fire the event to the registered listeners.
     * 
     * @param event the Event instance to fire to the registered event listeners
     * @throws GitLabApiException if the event is not supported
     */
    public void fireEvent(Event event) throws GitLabApiException {

        switch (event.getObjectKind()) {
        case BuildEvent.OBJECT_KIND:
            fireBuildEvent((BuildEvent) event);
            break;

        case IssueEvent.OBJECT_KIND:
            fireIssueEvent((IssueEvent) event);
            break;

        case MergeRequestEvent.OBJECT_KIND:
            fireMergeRequestEvent((MergeRequestEvent) event);
            break;

        case NoteEvent.OBJECT_KIND:
            fireNoteEvent((NoteEvent) event);
            break;

        case PipelineEvent.OBJECT_KIND:
            firePipelineEvent((PipelineEvent) event);
            break;

        case PushEvent.OBJECT_KIND:
            firePushEvent((PushEvent) event);
            break;

        case TagPushEvent.OBJECT_KIND:
            fireTagPushEvent((TagPushEvent) event);
            break;

        case WikiPageEvent.OBJECT_KIND:
            fireWikiPageEvent((WikiPageEvent) event);
            break;

        default:
            String message = "Unsupported event object_kind, object_kind=" + event.getObjectKind();
            LOGGER.warning(message);
            throw new GitLabApiException(message);
        }
    }

    protected void fireBuildEvent(BuildEvent buildEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onBuildEvent(buildEvent);
        }
    }

    protected void fireIssueEvent(IssueEvent issueEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onIssueEvent(issueEvent);
        }
    }

    protected void fireMergeRequestEvent(MergeRequestEvent mergeRequestEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onMergeRequestEvent(mergeRequestEvent);
        }
    }

    protected void fireNoteEvent(NoteEvent noteEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onNoteEvent(noteEvent);
        }
    }

    protected void firePipelineEvent(PipelineEvent pipelineEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onPipelineEvent(pipelineEvent);
        }
    }

    protected void firePushEvent(PushEvent pushEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onPushEvent(pushEvent);
        }
    }

    protected void fireTagPushEvent(TagPushEvent tagPushEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onTagPushEvent(tagPushEvent);
        }
    }

    protected void fireWikiPageEvent(WikiPageEvent wikiPageEvent) {
        for (WebHookListener listener : webhookListeners) {
            listener.onWikiPageEvent(wikiPageEvent);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy