Commit a9fb9b33 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for System Hooks API (#117).

parent 3d2e5184
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
<packaging>jar</packaging> <packaging>jar</packaging>
<version>4.7.12-SNAPSHOT</version> <version>4.7.12-SNAPSHOT</version>
<name>GitLab API Java Client</name> <name>GitLab API Java Client</name>
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API</description> <description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API.</description>
<url>http://www.messners.com/#gitlab4j-api/gitlab4j-api.html</url> <url>https://github.com/gmessner/gitlab4j-api</url>
<distributionManagement> <distributionManagement>
<snapshotRepository> <snapshotRepository>
......
...@@ -48,6 +48,7 @@ public class GitLabApi { ...@@ -48,6 +48,7 @@ public class GitLabApi {
private RepositoryFileApi repositoryFileApi; private RepositoryFileApi repositoryFileApi;
private ServicesApi servicesApi; private ServicesApi servicesApi;
private SessionApi sessionApi; private SessionApi sessionApi;
private SystemHooksApi systemHooksApi;
private UserApi userApi; private UserApi userApi;
private JobApi jobApi; private JobApi jobApi;
private LabelsApi labelsApi; private LabelsApi labelsApi;
...@@ -950,6 +951,25 @@ public class GitLabApi { ...@@ -950,6 +951,25 @@ public class GitLabApi {
return (sessionApi); return (sessionApi);
} }
/**
* Gets the SystemHooksApi instance owned by this GitLabApi instance. All methods
* require administrator authorization.
*
* @return the SystemHooksApi instance owned by this GitLabApi instance
*/
public SystemHooksApi getSystemHooksApi() {
if (systemHooksApi == null) {
synchronized (this) {
if (systemHooksApi == null) {
systemHooksApi = new SystemHooksApi(this);
}
}
}
return (systemHooksApi);
}
/** /**
* Gets the UserApi instance owned by this GitLabApi instance. The UserApi is used * Gets the UserApi instance owned by this GitLabApi instance. The UserApi is used
* to perform all user related API calls. * to perform all user related API calls.
......
package org.gitlab4j.api.models; package org.gitlab4j.api.models;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
...@@ -8,58 +10,58 @@ import javax.xml.bind.annotation.XmlRootElement; ...@@ -8,58 +10,58 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class SystemHook { public class SystemHook {
private String eventName; private Integer id;
private String name; private String url;
private String ownerEmail; private Date createdAt;
private String ownerName; private Boolean pushEvents;
private String path; private Boolean tagPushEvents;
private Integer projectId; private Boolean enableSslVerification;
public String getEventName() { public Integer getId() {
return this.eventName; return id;
} }
public void setEventName(String eventName) { public void setId(Integer id) {
this.eventName = eventName; this.id = id;
} }
public String getName() { public String getUrl() {
return this.name; return url;
} }
public void setName(String name) { public void setUrl(String url) {
this.name = name; this.url = url;
} }
public String getOwnerEmail() { public Date getCreatedAt() {
return this.ownerEmail; return createdAt;
} }
public void setOwnerEmail(String ownerEmail) { public void setCreatedAt(Date createdAt) {
this.ownerEmail = ownerEmail; this.createdAt = createdAt;
} }
public String getOwnerName() { public Boolean getPushEvents() {
return this.ownerName; return pushEvents;
} }
public void setOwnerName(String ownerName) { public void setPushEvents(Boolean pushEvents) {
this.ownerName = ownerName; this.pushEvents = pushEvents;
} }
public String getPath() { public Boolean getTagPushEvents() {
return this.path; return tagPushEvents;
} }
public void setPath(String path) { public void setTagPushEvents(Boolean tagPushEvents) {
this.path = path; this.tagPushEvents = tagPushEvents;
} }
public Integer getProjectId() { public Boolean getEnableSslVerification() {
return this.projectId; return enableSslVerification;
} }
public void setProjectId(Integer projectId) { public void setEnableSslVerification(Boolean enableSslVerification) {
this.projectId = projectId; this.enableSslVerification = enableSslVerification;
} }
} }
...@@ -11,6 +11,7 @@ import java.util.logging.Logger; ...@@ -11,6 +11,7 @@ import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.gitlab4j.api.GitLabApiException; import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.HookManager;
import org.gitlab4j.api.utils.HttpRequestUtils; import org.gitlab4j.api.utils.HttpRequestUtils;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
...@@ -20,70 +21,29 @@ import com.fasterxml.jackson.databind.JsonMappingException; ...@@ -20,70 +21,29 @@ import com.fasterxml.jackson.databind.JsonMappingException;
/** /**
* This class provides a handler for processing GitLab WebHook callouts. * This class provides a handler for processing GitLab WebHook callouts.
*/ */
public class WebHookManager { public class WebHookManager extends HookManager {
private final static Logger LOG = Logger.getLogger(WebHookManager.class.getName()); private final static Logger LOG = Logger.getLogger(WebHookManager.class.getName());
private final JacksonJson jacksonJson = new JacksonJson(); private final JacksonJson jacksonJson = new JacksonJson();
private String secretToken;
// Collection of objects listening for WebHook events. // Collection of objects listening for WebHook events.
private final List<WebHookListener> webhookListeners = new CopyOnWriteArrayList<WebHookListener>(); private final List<WebHookListener> webhookListeners = new CopyOnWriteArrayList<WebHookListener>();
/** /**
* Create a WebHookManager to handle GitLab webhook events. * Create a HookManager to handle GitLab webhook events.
*/ */
public WebHookManager() { public WebHookManager() {
this.secretToken = null; super();
} }
/** /**
* Create a WebHookManager to handle GitLab webhook events which will be verified * Create a HookManager to handle GitLab webhook events which will be verified
* against the specified secretToken. * against the specified secretToken.
* *
* @param secretToken the secret token to verify against * @param secretToken the secret token to verify against
*/ */
public WebHookManager(String secretToken) { public WebHookManager(String secretToken) {
this.secretToken = secretToken; super(secretToken);
}
/**
* Set the secret token that received webhook events should be validated against.
*
* @param secretToken the secret token to verify against
*/
public void setSecretToken(String secretToken) {
this.secretToken = secretToken;
}
/**
* Validate the provided secret token against the reference secret token. Returns true if
* the secret token is valid or there is no reference secret token to validate against,
* otherwise returns false.
*
* @param secretToken the token to validate
* @return true if the secret token is valid or there is no reference secret token to validate against
*/
public boolean isValidSecretToken(String secretToken) {
return (this.secretToken == null || this.secretToken.equals(secretToken) ? true : false);
}
/**
* Validate the provided secret token found in the HTTP header against the reference secret token.
* Returns true if the secret token is valid or there is no reference secret token to validate
* against, otherwise returns false.
*
* @param request the HTTP request to verify the secret token
* @return true if the secret token is valid or there is no reference secret token to validate against
*/
public boolean isValidSecretToken(HttpServletRequest request) {
if (this.secretToken != null) {
String secretToken = request.getHeader("X-Gitlab-Token");
return (isValidSecretToken(secretToken));
}
return (true);
} }
/** /**
...@@ -189,7 +149,7 @@ public class WebHookManager { ...@@ -189,7 +149,7 @@ public class WebHookManager {
/** /**
* Adds a WebHook event listener. * Adds a WebHook event listener.
* *
* @param listener the WebHookListener to add * @param listener the SystemHookListener to add
*/ */
public void addListener(WebHookListener listener) { public void addListener(WebHookListener listener) {
...@@ -201,7 +161,7 @@ public class WebHookManager { ...@@ -201,7 +161,7 @@ public class WebHookManager {
/** /**
* Removes a WebHook event listener. * Removes a WebHook event listener.
* *
* @param listener the WebHookListener to remove * @param listener the SystemHookListener to remove
*/ */
public void removeListener(WebHookListener listener) { public void removeListener(WebHookListener listener) {
webhookListeners.remove(listener); webhookListeners.remove(listener);
......
...@@ -61,6 +61,8 @@ import org.gitlab4j.api.models.SystemHook; ...@@ -61,6 +61,8 @@ import org.gitlab4j.api.models.SystemHook;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.models.TreeItem; import org.gitlab4j.api.models.TreeItem;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
...@@ -442,6 +444,28 @@ public class TestGitLabApiBeans { ...@@ -442,6 +444,28 @@ public class TestGitLabApiBeans {
} }
} }
@Test
public void testProjectSystemHookEvent() {
try {
ProjectSystemHookEvent event = makeFakeApiCall(ProjectSystemHookEvent.class, "project-system-hook-event");
assertTrue(compareJson(event, "project-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testTeamMemberSystemHookEvent() {
try {
TeamMemberSystemHookEvent event = makeFakeApiCall(TeamMemberSystemHookEvent.class, "team-member-system-hook-event");
assertTrue(compareJson(event, "team-member-system-hook-event"));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test @Test
public void testLabels() { public void testLabels() {
......
{ {
"event_name":"project_create", "id": 1,
"name":"Ruby", "url": "https://gitlab.example.com/hook",
"path":"ruby", "created_at": "2016-10-31T12:32:15.192Z",
"project_id":1, "push_events": true,
"owner_name":"Someone", "tag_push_events": false,
"owner_email":"example@gitlabhq.com" "enable_ssl_verification": true
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment