Skip to content

Commit 6e91cb3

Browse files
authored
System hook improvements (#1234)
1 parent 398d357 commit 6e91cb3

File tree

3 files changed

+156
-19
lines changed

3 files changed

+156
-19
lines changed

gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java

+82-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public SystemHooksApi(GitLabApi gitLabApi) {
2323
*
2424
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
2525
*
26-
* @return a list of SystemHookEvent
26+
* @return a list of SystemHook
2727
* @throws GitLabApiException if any exception occurs
2828
*/
2929
public List<SystemHook> getSystemHooks() throws GitLabApiException {
@@ -38,7 +38,7 @@ public List<SystemHook> getSystemHooks() throws GitLabApiException {
3838
*
3939
* @param page the page to get
4040
* @param perPage the number of deploy keys per page
41-
* @return the list of SystemHookEvent in the specified range
41+
* @return the list of SystemHook in the specified range
4242
* @throws GitLabApiException if any exception occurs
4343
*/
4444
public List<SystemHook> getSystemHooks(int page, int perPage) throws GitLabApiException {
@@ -51,8 +51,8 @@ public List<SystemHook> getSystemHooks(int page, int perPage) throws GitLabApiEx
5151
*
5252
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
5353
*
54-
* @param itemsPerPage the number of SystemHookEvent instances that will be fetched per page
55-
* @return a Pager of SystemHookEvent
54+
* @param itemsPerPage the number of SystemHook instances that will be fetched per page
55+
* @return a Pager of SystemHook
5656
* @throws GitLabApiException if any exception occurs
5757
*/
5858
public Pager<SystemHook> getSystemHooks(int itemsPerPage) throws GitLabApiException {
@@ -64,13 +64,27 @@ public Pager<SystemHook> getSystemHooks(int itemsPerPage) throws GitLabApiExcept
6464
*
6565
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
6666
*
67-
* @return a Stream of SystemHookEvent
67+
* @return a Stream of SystemHook
6868
* @throws GitLabApiException if any exception occurs
6969
*/
7070
public Stream<SystemHook> getSystemHookStream() throws GitLabApiException {
7171
return (getSystemHooks(getDefaultPerPage()).stream());
7272
}
7373

74+
/**
75+
* Get a list of all system hooks. This method requires admin access.
76+
*
77+
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
78+
*
79+
* @param hookId the ID of the system hook.
80+
* @return the SystemHook
81+
* @throws GitLabApiException if any exception occurs
82+
*/
83+
public SystemHook getSystemHook(Long hookId) throws GitLabApiException {
84+
Response response = get(Response.Status.OK, null, "hooks", hookId);
85+
return response.readEntity(SystemHook.class);
86+
}
87+
7488
/**
7589
* Add a new system hook. This method requires admin access.
7690
*
@@ -81,7 +95,7 @@ public Stream<SystemHook> getSystemHookStream() throws GitLabApiException {
8195
* @param pushEvents when true, the hook will fire on push events, optional
8296
* @param tagPushEvents when true, the hook will fire on new tags being pushed, optional
8397
* @param enableSslVerification do SSL verification when triggering the hook, optional
84-
* @return an SystemHookEvent instance with info on the added system hook
98+
* @return an SystemHook instance with info on the added system hook
8599
* @throws GitLabApiException if any exception occurs
86100
*/
87101
public SystemHook addSystemHook(
@@ -104,7 +118,7 @@ public SystemHook addSystemHook(
104118
* @param url the hook URL, required
105119
* @param token secret token to validate received payloads, optional
106120
* @param systemHook the systemHook to create
107-
* @return an SystemHookEvent instance with info on the added system hook
121+
* @return an SystemHook instance with info on the added system hook
108122
* @throws GitLabApiException if any exception occurs
109123
*/
110124
public SystemHook addSystemHook(String url, String token, SystemHook systemHook) throws GitLabApiException {
@@ -116,6 +130,8 @@ public SystemHook addSystemHook(String url, String token, SystemHook systemHook)
116130
GitLabApiForm formData = new GitLabApiForm()
117131
.withParam("url", url, true)
118132
.withParam("token", token)
133+
.withParam("name", systemHook.getName())
134+
.withParam("description", systemHook.getDescription())
119135
.withParam("push_events", systemHook.getPushEvents())
120136
.withParam("tag_push_events", systemHook.getTagPushEvents())
121137
.withParam("merge_requests_events", systemHook.getMergeRequestsEvents())
@@ -125,6 +141,36 @@ public SystemHook addSystemHook(String url, String token, SystemHook systemHook)
125141
return (response.readEntity(SystemHook.class));
126142
}
127143

144+
/**
145+
* Add a new system hook. This method requires admin access.
146+
*
147+
* <pre><code>GitLab Endpoint: PUT /hooks/:hook_id</code></pre>
148+
*
149+
* @param systemHook the systemHook to update
150+
* @param token secret token to validate received payloads, optional
151+
* @return an SystemHook instance with info on the added system hook
152+
* @throws GitLabApiException if any exception occurs
153+
*/
154+
public SystemHook updateSystemHook(SystemHook systemHook, String token) throws GitLabApiException {
155+
156+
if (systemHook.getId() == null) {
157+
throw new RuntimeException("systemHook id cannot be null");
158+
}
159+
160+
GitLabApiForm formData = new GitLabApiForm()
161+
.withParam("url", systemHook.getUrl())
162+
.withParam("token", token)
163+
.withParam("name", systemHook.getName())
164+
.withParam("description", systemHook.getDescription())
165+
.withParam("push_events", systemHook.getPushEvents())
166+
.withParam("tag_push_events", systemHook.getTagPushEvents())
167+
.withParam("merge_requests_events", systemHook.getMergeRequestsEvents())
168+
.withParam("repository_update_events", systemHook.getRepositoryUpdateEvents())
169+
.withParam("enable_ssl_verification", systemHook.getEnableSslVerification());
170+
Response response = putWithFormData(Response.Status.OK, formData, "hooks", systemHook.getId());
171+
return (response.readEntity(SystemHook.class));
172+
}
173+
128174
/**
129175
* Deletes a system hook. This method requires admin access.
130176
*
@@ -166,7 +212,7 @@ public void deleteSystemHook(Long hookId) throws GitLabApiException {
166212
*
167213
* <pre><code>GitLab Endpoint: GET /hooks/:hook_id</code></pre>
168214
*
169-
* @param hook the SystemHookEvent instance to test
215+
* @param hook the SystemHook instance to test
170216
* @throws GitLabApiException if any exception occurs
171217
*/
172218
public void testSystemHook(SystemHook hook) throws GitLabApiException {
@@ -194,4 +240,32 @@ public void testSystemHook(Long hookId) throws GitLabApiException {
194240

195241
get(Response.Status.OK, null, "hooks", hookId);
196242
}
243+
244+
/**
245+
* Add a new URL variable.
246+
*
247+
* <pre><code>GitLab Endpoint: PUT /hooks/:hook_id/url_variables/:key</code></pre>
248+
*
249+
* @param hookId the ID of the system hook
250+
* @param key Key of the URL variable
251+
* @param value Value of the URL variable.
252+
* @throws GitLabApiException if any exception occurs
253+
*/
254+
public void addSystemHookUrlVariable(Long hookId, String key, String value) throws GitLabApiException {
255+
GitLabApiForm formData = new GitLabApiForm().withParam("value", value, true);
256+
put(Response.Status.CREATED, formData.asMap(), "hooks", hookId, "url_variables", key);
257+
}
258+
259+
/**
260+
* Delete a URL variable.
261+
*
262+
* <pre><code>GitLab Endpoint: DELETE /hooks/:hook_id/url_variables/:key</code></pre>
263+
*
264+
* @param hookId the ID of the system hook
265+
* @param key Key of the URL variable
266+
* @throws GitLabApiException if any exception occurs
267+
*/
268+
public void deleteSystemHookUrlVariable(Long hookId, String key) throws GitLabApiException {
269+
delete(Response.Status.NO_CONTENT, null, "hooks", hookId, "url_variables", key);
270+
}
197271
}

gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java

+65-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
import java.io.Serializable;
44
import java.util.Date;
5+
import java.util.List;
56

67
import org.gitlab4j.models.utils.JacksonJson;
78

89
public class SystemHook implements Serializable {
910
private static final long serialVersionUID = 1L;
1011

1112
private Long id;
13+
private String name;
14+
private String description;
1215
private String url;
1316
private Date createdAt;
1417
private Boolean pushEvents;
1518
private Boolean tagPushEvents;
1619
private Boolean enableSslVerification;
1720
private Boolean repositoryUpdateEvents;
1821
private Boolean mergeRequestsEvents;
22+
private List<SystemHook.UrlVariable> urlVariables;
1923

2024
public Long getId() {
2125
return id;
@@ -25,6 +29,22 @@ public void setId(Long id) {
2529
this.id = id;
2630
}
2731

32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public String getDescription() {
41+
return description;
42+
}
43+
44+
public void setDescription(String description) {
45+
this.description = description;
46+
}
47+
2848
public String getUrl() {
2949
return url;
3050
}
@@ -81,48 +101,85 @@ public Boolean getMergeRequestsEvents() {
81101
return mergeRequestsEvents;
82102
}
83103

104+
public List<SystemHook.UrlVariable> getUrlVariables() {
105+
return urlVariables;
106+
}
107+
108+
public void setUrlVariables(List<SystemHook.UrlVariable> urlVariables) {
109+
this.urlVariables = urlVariables;
110+
}
111+
84112
public SystemHook withId(Long id) {
85113
this.id = id;
86-
return (this);
114+
return this;
115+
}
116+
117+
public SystemHook withName(String name) {
118+
this.name = name;
119+
return this;
120+
}
121+
122+
public SystemHook withDescription(String description) {
123+
this.description = description;
124+
return this;
87125
}
88126

89127
public SystemHook withUrl(String url) {
90128
this.url = url;
91-
return (this);
129+
return this;
92130
}
93131

94132
public SystemHook withCreatedAt(Date createdAt) {
95133
this.createdAt = createdAt;
96-
return (this);
134+
return this;
97135
}
98136

99137
public SystemHook withPushEvents(Boolean pushEvents) {
100138
this.pushEvents = pushEvents;
101-
return (this);
139+
return this;
102140
}
103141

104142
public SystemHook withTagPushEvents(Boolean tagPushEvents) {
105143
this.tagPushEvents = tagPushEvents;
106-
return (this);
144+
return this;
107145
}
108146

109147
public SystemHook withEnableSslVerification(Boolean enableSslVerification) {
110148
this.enableSslVerification = enableSslVerification;
111-
return (this);
149+
return this;
112150
}
113151

114152
public SystemHook withRepositoryUpdateEvents(Boolean repositoryUpdateEvents) {
115153
this.repositoryUpdateEvents = repositoryUpdateEvents;
116-
return (this);
154+
return this;
117155
}
118156

119157
public SystemHook withMergeRequestsEvents(Boolean mergeRequestsEvents) {
120158
this.mergeRequestsEvents = mergeRequestsEvents;
121-
return (this);
159+
return this;
122160
}
123161

124162
@Override
125163
public String toString() {
126164
return (JacksonJson.toJsonString(this));
127165
}
166+
167+
public static class UrlVariable implements Serializable {
168+
private static final long serialVersionUID = 1L;
169+
170+
private String key;
171+
172+
public String getKey() {
173+
return key;
174+
}
175+
176+
public void setKey(String key) {
177+
this.key = key;
178+
}
179+
180+
@Override
181+
public String toString() {
182+
return (JacksonJson.toJsonString(this));
183+
}
184+
}
128185
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{
22
"id": 1,
33
"url": "https://gitlab.example.com/hook",
4+
"name": "Hook name",
5+
"description": "Hook description",
46
"created_at": "2016-10-31T12:32:15.192Z",
57
"push_events": true,
68
"tag_push_events": false,
7-
"enable_ssl_verification": true
8-
}
9-
9+
"merge_requests_events": true,
10+
"repository_update_events": true,
11+
"enable_ssl_verification": true,
12+
"url_variables": [{
13+
"key": "example"
14+
}]
15+
}

0 commit comments

Comments
 (0)