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 @@
<packaging>jar</packaging>
<version>4.7.12-SNAPSHOT</version>
<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>
<url>http://www.messners.com/#gitlab4j-api/gitlab4j-api.html</url>
<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>https://github.com/gmessner/gitlab4j-api</url>
<distributionManagement>
<snapshotRepository>
......
......@@ -48,6 +48,7 @@ public class GitLabApi {
private RepositoryFileApi repositoryFileApi;
private ServicesApi servicesApi;
private SessionApi sessionApi;
private SystemHooksApi systemHooksApi;
private UserApi userApi;
private JobApi jobApi;
private LabelsApi labelsApi;
......@@ -950,6 +951,25 @@ public class GitLabApi {
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
* to perform all user related API calls.
......
package org.gitlab4j.api.models;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
......@@ -8,58 +10,58 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class SystemHook {
private String eventName;
private String name;
private String ownerEmail;
private String ownerName;
private String path;
private Integer projectId;
private Integer id;
private String url;
private Date createdAt;
private Boolean pushEvents;
private Boolean tagPushEvents;
private Boolean enableSslVerification;
public String getEventName() {
return this.eventName;
public Integer getId() {
return id;
}
public void setEventName(String eventName) {
this.eventName = eventName;
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
public String getUrl() {
return url;
}
public void setName(String name) {
this.name = name;
public void setUrl(String url) {
this.url = url;
}
public String getOwnerEmail() {
return this.ownerEmail;
public Date getCreatedAt() {
return createdAt;
}
public void setOwnerEmail(String ownerEmail) {
this.ownerEmail = ownerEmail;
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public String getOwnerName() {
return this.ownerName;
public Boolean getPushEvents() {
return pushEvents;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
public void setPushEvents(Boolean pushEvents) {
this.pushEvents = pushEvents;
}
public String getPath() {
return this.path;
public Boolean getTagPushEvents() {
return tagPushEvents;
}
public void setPath(String path) {
this.path = path;
public void setTagPushEvents(Boolean tagPushEvents) {
this.tagPushEvents = tagPushEvents;
}
public Integer getProjectId() {
return this.projectId;
public Boolean getEnableSslVerification() {
return enableSslVerification;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
public void setEnableSslVerification(Boolean enableSslVerification) {
this.enableSslVerification = enableSslVerification;
}
}
......@@ -11,6 +11,7 @@ import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.HookManager;
import org.gitlab4j.api.utils.HttpRequestUtils;
import org.gitlab4j.api.utils.JacksonJson;
......@@ -20,70 +21,29 @@ import com.fasterxml.jackson.databind.JsonMappingException;
/**
* 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 JacksonJson jacksonJson = new JacksonJson();
private String secretToken;
// Collection of objects listening for WebHook events.
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() {
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.
*
* @param secretToken the secret token to verify against
*/
public WebHookManager(String secretToken) {
this.secretToken = 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);
super(secretToken);
}
/**
......@@ -189,7 +149,7 @@ public class WebHookManager {
/**
* Adds a WebHook event listener.
*
* @param listener the WebHookListener to add
* @param listener the SystemHookListener to add
*/
public void addListener(WebHookListener listener) {
......@@ -201,7 +161,7 @@ public class WebHookManager {
/**
* Removes a WebHook event listener.
*
* @param listener the WebHookListener to remove
* @param listener the SystemHookListener to remove
*/
public void removeListener(WebHookListener listener) {
webhookListeners.remove(listener);
......
......@@ -61,6 +61,8 @@ import org.gitlab4j.api.models.SystemHook;
import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.models.TreeItem;
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.junit.BeforeClass;
import org.junit.Test;
......@@ -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
public void testLabels() {
......
{
"event_name":"project_create",
"name":"Ruby",
"path":"ruby",
"project_id":1,
"owner_name":"Someone",
"owner_email":"example@gitlabhq.com"
}
{
"id": 1,
"url": "https://gitlab.example.com/hook",
"created_at": "2016-10-31T12:32:15.192Z",
"push_events": true,
"tag_push_events": false,
"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