Skip to content

Commit a9fb9b3

Browse files
committed
Added support for System Hooks API (#117).
1 parent 3d2e518 commit a9fb9b3

File tree

6 files changed

+95
-88
lines changed

6 files changed

+95
-88
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<packaging>jar</packaging>
88
<version>4.7.12-SNAPSHOT</version>
99
<name>GitLab API Java Client</name>
10-
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API</description>
11-
<url>http://www.messners.com/#gitlab4j-api/gitlab4j-api.html</url>
10+
<description>GitLab API for Java (gitlab4j-api) provides a full featured Java API for working with GitLab repositories via the GitLab REST API.</description>
11+
<url>https://github.com/gmessner/gitlab4j-api</url>
1212

1313
<distributionManagement>
1414
<snapshotRepository>

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public String getApiNamespace() {
4848
private RepositoryFileApi repositoryFileApi;
4949
private ServicesApi servicesApi;
5050
private SessionApi sessionApi;
51+
private SystemHooksApi systemHooksApi;
5152
private UserApi userApi;
5253
private JobApi jobApi;
5354
private LabelsApi labelsApi;
@@ -950,6 +951,25 @@ public SessionApi getSessionApi() {
950951
return (sessionApi);
951952
}
952953

954+
/**
955+
* Gets the SystemHooksApi instance owned by this GitLabApi instance. All methods
956+
* require administrator authorization.
957+
*
958+
* @return the SystemHooksApi instance owned by this GitLabApi instance
959+
*/
960+
public SystemHooksApi getSystemHooksApi() {
961+
962+
if (systemHooksApi == null) {
963+
synchronized (this) {
964+
if (systemHooksApi == null) {
965+
systemHooksApi = new SystemHooksApi(this);
966+
}
967+
}
968+
}
969+
970+
return (systemHooksApi);
971+
}
972+
953973
/**
954974
* Gets the UserApi instance owned by this GitLabApi instance. The UserApi is used
955975
* to perform all user related API calls.
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.gitlab4j.api.models;
22

3+
import java.util.Date;
4+
35
import javax.xml.bind.annotation.XmlAccessType;
46
import javax.xml.bind.annotation.XmlAccessorType;
57
import javax.xml.bind.annotation.XmlRootElement;
@@ -8,58 +10,58 @@
810
@XmlAccessorType(XmlAccessType.FIELD)
911
public class SystemHook {
1012

11-
private String eventName;
12-
private String name;
13-
private String ownerEmail;
14-
private String ownerName;
15-
private String path;
16-
private Integer projectId;
13+
private Integer id;
14+
private String url;
15+
private Date createdAt;
16+
private Boolean pushEvents;
17+
private Boolean tagPushEvents;
18+
private Boolean enableSslVerification;
1719

18-
public String getEventName() {
19-
return this.eventName;
20+
public Integer getId() {
21+
return id;
2022
}
2123

22-
public void setEventName(String eventName) {
23-
this.eventName = eventName;
24+
public void setId(Integer id) {
25+
this.id = id;
2426
}
2527

26-
public String getName() {
27-
return this.name;
28+
public String getUrl() {
29+
return url;
2830
}
2931

30-
public void setName(String name) {
31-
this.name = name;
32+
public void setUrl(String url) {
33+
this.url = url;
3234
}
3335

34-
public String getOwnerEmail() {
35-
return this.ownerEmail;
36+
public Date getCreatedAt() {
37+
return createdAt;
3638
}
3739

38-
public void setOwnerEmail(String ownerEmail) {
39-
this.ownerEmail = ownerEmail;
40+
public void setCreatedAt(Date createdAt) {
41+
this.createdAt = createdAt;
4042
}
4143

42-
public String getOwnerName() {
43-
return this.ownerName;
44+
public Boolean getPushEvents() {
45+
return pushEvents;
4446
}
4547

46-
public void setOwnerName(String ownerName) {
47-
this.ownerName = ownerName;
48+
public void setPushEvents(Boolean pushEvents) {
49+
this.pushEvents = pushEvents;
4850
}
4951

50-
public String getPath() {
51-
return this.path;
52+
public Boolean getTagPushEvents() {
53+
return tagPushEvents;
5254
}
5355

54-
public void setPath(String path) {
55-
this.path = path;
56+
public void setTagPushEvents(Boolean tagPushEvents) {
57+
this.tagPushEvents = tagPushEvents;
5658
}
5759

58-
public Integer getProjectId() {
59-
return this.projectId;
60+
public Boolean getEnableSslVerification() {
61+
return enableSslVerification;
6062
}
6163

62-
public void setProjectId(Integer projectId) {
63-
this.projectId = projectId;
64+
public void setEnableSslVerification(Boolean enableSslVerification) {
65+
this.enableSslVerification = enableSslVerification;
6466
}
6567
}

src/main/java/org/gitlab4j/api/webhook/WebHookManager.java

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.servlet.http.HttpServletRequest;
1212

1313
import org.gitlab4j.api.GitLabApiException;
14+
import org.gitlab4j.api.HookManager;
1415
import org.gitlab4j.api.utils.HttpRequestUtils;
1516
import org.gitlab4j.api.utils.JacksonJson;
1617

@@ -20,70 +21,29 @@
2021
/**
2122
* This class provides a handler for processing GitLab WebHook callouts.
2223
*/
23-
public class WebHookManager {
24+
public class WebHookManager extends HookManager {
2425

2526
private final static Logger LOG = Logger.getLogger(WebHookManager.class.getName());
2627
private final JacksonJson jacksonJson = new JacksonJson();
2728

28-
private String secretToken;
29-
3029
// Collection of objects listening for WebHook events.
3130
private final List<WebHookListener> webhookListeners = new CopyOnWriteArrayList<WebHookListener>();
3231

3332
/**
34-
* Create a WebHookManager to handle GitLab webhook events.
33+
* Create a HookManager to handle GitLab webhook events.
3534
*/
3635
public WebHookManager() {
37-
this.secretToken = null;
36+
super();
3837
}
3938

4039
/**
41-
* Create a WebHookManager to handle GitLab webhook events which will be verified
40+
* Create a HookManager to handle GitLab webhook events which will be verified
4241
* against the specified secretToken.
4342
*
4443
* @param secretToken the secret token to verify against
4544
*/
4645
public WebHookManager(String secretToken) {
47-
this.secretToken = secretToken;
48-
}
49-
50-
/**
51-
* Set the secret token that received webhook events should be validated against.
52-
*
53-
* @param secretToken the secret token to verify against
54-
*/
55-
public void setSecretToken(String secretToken) {
56-
this.secretToken = secretToken;
57-
}
58-
59-
/**
60-
* Validate the provided secret token against the reference secret token. Returns true if
61-
* the secret token is valid or there is no reference secret token to validate against,
62-
* otherwise returns false.
63-
*
64-
* @param secretToken the token to validate
65-
* @return true if the secret token is valid or there is no reference secret token to validate against
66-
*/
67-
public boolean isValidSecretToken(String secretToken) {
68-
return (this.secretToken == null || this.secretToken.equals(secretToken) ? true : false);
69-
}
70-
71-
/**
72-
* Validate the provided secret token found in the HTTP header against the reference secret token.
73-
* Returns true if the secret token is valid or there is no reference secret token to validate
74-
* against, otherwise returns false.
75-
*
76-
* @param request the HTTP request to verify the secret token
77-
* @return true if the secret token is valid or there is no reference secret token to validate against
78-
*/
79-
public boolean isValidSecretToken(HttpServletRequest request) {
80-
81-
if (this.secretToken != null) {
82-
String secretToken = request.getHeader("X-Gitlab-Token");
83-
return (isValidSecretToken(secretToken));
84-
}
85-
86-
return (true);
46+
super(secretToken);
8747
}
8848

8949
/**
@@ -189,7 +149,7 @@ public void handleEvent(Event event) throws GitLabApiException {
189149
/**
190150
* Adds a WebHook event listener.
191151
*
192-
* @param listener the WebHookListener to add
152+
* @param listener the SystemHookListener to add
193153
*/
194154
public void addListener(WebHookListener listener) {
195155

@@ -201,7 +161,7 @@ public void addListener(WebHookListener listener) {
201161
/**
202162
* Removes a WebHook event listener.
203163
*
204-
* @param listener the WebHookListener to remove
164+
* @param listener the SystemHookListener to remove
205165
*/
206166
public void removeListener(WebHookListener listener) {
207167
webhookListeners.remove(listener);

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import org.gitlab4j.api.models.Tag;
6262
import org.gitlab4j.api.models.TreeItem;
6363
import org.gitlab4j.api.models.User;
64+
import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
65+
import org.gitlab4j.api.systemhooks.TeamMemberSystemHookEvent;
6466
import org.gitlab4j.api.utils.JacksonJson;
6567
import org.junit.BeforeClass;
6668
import org.junit.Test;
@@ -442,6 +444,28 @@ public void testOauthToken() {
442444
}
443445
}
444446

447+
@Test
448+
public void testProjectSystemHookEvent() {
449+
450+
try {
451+
ProjectSystemHookEvent event = makeFakeApiCall(ProjectSystemHookEvent.class, "project-system-hook-event");
452+
assertTrue(compareJson(event, "project-system-hook-event"));
453+
} catch (Exception e) {
454+
e.printStackTrace();
455+
}
456+
}
457+
458+
@Test
459+
public void testTeamMemberSystemHookEvent() {
460+
461+
try {
462+
TeamMemberSystemHookEvent event = makeFakeApiCall(TeamMemberSystemHookEvent.class, "team-member-system-hook-event");
463+
assertTrue(compareJson(event, "team-member-system-hook-event"));
464+
} catch (Exception e) {
465+
e.printStackTrace();
466+
}
467+
}
468+
445469
@Test
446470
public void testLabels() {
447471

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
{
2-
"event_name":"project_create",
3-
"name":"Ruby",
4-
"path":"ruby",
5-
"project_id":1,
6-
"owner_name":"Someone",
7-
"owner_email":"[email protected]"
8-
}
1+
{
2+
"id": 1,
3+
"url": "https://gitlab.example.com/hook",
4+
"created_at": "2016-10-31T12:32:15.192Z",
5+
"push_events": true,
6+
"tag_push_events": false,
7+
"enable_ssl_verification": true
8+
}
9+

0 commit comments

Comments
 (0)