Skip to content

Commit 875a1fc

Browse files
committed
Add methods
1 parent 4cd5a69 commit 875a1fc

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,36 @@ public SystemHook addSystemHook(String url, String token, SystemHook systemHook)
141141
return (response.readEntity(SystemHook.class));
142142
}
143143

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+
144174
/**
145175
* Deletes a system hook. This method requires admin access.
146176
*
@@ -212,7 +242,7 @@ public void testSystemHook(Long hookId) throws GitLabApiException {
212242
}
213243

214244
/**
215-
* Add a new system hook. This method requires admin access.
245+
* Add a new URL variable.
216246
*
217247
* <pre><code>GitLab Endpoint: PUT /hooks/:hook_id/url_variables/:key</code></pre>
218248
*
@@ -225,4 +255,17 @@ public void addSystemHookUrlVariable(Long hookId, String key, String value) thro
225255
GitLabApiForm formData = new GitLabApiForm().withParam("value", value, true);
226256
put(Response.Status.CREATED, formData.asMap(), "hooks", hookId, "url_variables", key);
227257
}
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+
}
228271
}

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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

@@ -18,6 +19,7 @@ public class SystemHook implements Serializable {
1819
private Boolean enableSslVerification;
1920
private Boolean repositoryUpdateEvents;
2021
private Boolean mergeRequestsEvents;
22+
private List<SystemHook.UrlVariable> urlVariables;
2123

2224
public Long getId() {
2325
return id;
@@ -99,48 +101,85 @@ public Boolean getMergeRequestsEvents() {
99101
return mergeRequestsEvents;
100102
}
101103

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+
102112
public SystemHook withId(Long id) {
103113
this.id = id;
104-
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;
105125
}
106126

107127
public SystemHook withUrl(String url) {
108128
this.url = url;
109-
return (this);
129+
return this;
110130
}
111131

112132
public SystemHook withCreatedAt(Date createdAt) {
113133
this.createdAt = createdAt;
114-
return (this);
134+
return this;
115135
}
116136

117137
public SystemHook withPushEvents(Boolean pushEvents) {
118138
this.pushEvents = pushEvents;
119-
return (this);
139+
return this;
120140
}
121141

122142
public SystemHook withTagPushEvents(Boolean tagPushEvents) {
123143
this.tagPushEvents = tagPushEvents;
124-
return (this);
144+
return this;
125145
}
126146

127147
public SystemHook withEnableSslVerification(Boolean enableSslVerification) {
128148
this.enableSslVerification = enableSslVerification;
129-
return (this);
149+
return this;
130150
}
131151

132152
public SystemHook withRepositoryUpdateEvents(Boolean repositoryUpdateEvents) {
133153
this.repositoryUpdateEvents = repositoryUpdateEvents;
134-
return (this);
154+
return this;
135155
}
136156

137157
public SystemHook withMergeRequestsEvents(Boolean mergeRequestsEvents) {
138158
this.mergeRequestsEvents = mergeRequestsEvents;
139-
return (this);
159+
return this;
140160
}
141161

142162
@Override
143163
public String toString() {
144164
return (JacksonJson.toJsonString(this));
145165
}
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+
}
146185
}
Lines changed: 9 additions & 3 deletions
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)