Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

System hook improvements #1234

Merged
merged 4 commits into from
Mar 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 82 additions & 8 deletions gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ public SystemHooksApi(GitLabApi gitLabApi) {
*
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
*
* @return a list of SystemHookEvent
* @return a list of SystemHook
* @throws GitLabApiException if any exception occurs
*/
public List<SystemHook> getSystemHooks() throws GitLabApiException {
@@ -38,7 +38,7 @@ public List<SystemHook> getSystemHooks() throws GitLabApiException {
*
* @param page the page to get
* @param perPage the number of deploy keys per page
* @return the list of SystemHookEvent in the specified range
* @return the list of SystemHook in the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<SystemHook> getSystemHooks(int page, int perPage) throws GitLabApiException {
@@ -51,8 +51,8 @@ public List<SystemHook> getSystemHooks(int page, int perPage) throws GitLabApiEx
*
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
*
* @param itemsPerPage the number of SystemHookEvent instances that will be fetched per page
* @return a Pager of SystemHookEvent
* @param itemsPerPage the number of SystemHook instances that will be fetched per page
* @return a Pager of SystemHook
* @throws GitLabApiException if any exception occurs
*/
public Pager<SystemHook> getSystemHooks(int itemsPerPage) throws GitLabApiException {
@@ -64,13 +64,27 @@ public Pager<SystemHook> getSystemHooks(int itemsPerPage) throws GitLabApiExcept
*
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
*
* @return a Stream of SystemHookEvent
* @return a Stream of SystemHook
* @throws GitLabApiException if any exception occurs
*/
public Stream<SystemHook> getSystemHookStream() throws GitLabApiException {
return (getSystemHooks(getDefaultPerPage()).stream());
}

/**
* Get a list of all system hooks. This method requires admin access.
*
* <pre><code>GitLab Endpoint: GET /hooks</code></pre>
*
* @param hookId the ID of the system hook.
* @return the SystemHook
* @throws GitLabApiException if any exception occurs
*/
public SystemHook getSystemHook(Long hookId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "hooks", hookId);
return response.readEntity(SystemHook.class);
}

/**
* Add a new system hook. This method requires admin access.
*
@@ -81,7 +95,7 @@ public Stream<SystemHook> getSystemHookStream() throws GitLabApiException {
* @param pushEvents when true, the hook will fire on push events, optional
* @param tagPushEvents when true, the hook will fire on new tags being pushed, optional
* @param enableSslVerification do SSL verification when triggering the hook, optional
* @return an SystemHookEvent instance with info on the added system hook
* @return an SystemHook instance with info on the added system hook
* @throws GitLabApiException if any exception occurs
*/
public SystemHook addSystemHook(
@@ -104,7 +118,7 @@ public SystemHook addSystemHook(
* @param url the hook URL, required
* @param token secret token to validate received payloads, optional
* @param systemHook the systemHook to create
* @return an SystemHookEvent instance with info on the added system hook
* @return an SystemHook instance with info on the added system hook
* @throws GitLabApiException if any exception occurs
*/
public SystemHook addSystemHook(String url, String token, SystemHook systemHook) throws GitLabApiException {
@@ -116,6 +130,8 @@ public SystemHook addSystemHook(String url, String token, SystemHook systemHook)
GitLabApiForm formData = new GitLabApiForm()
.withParam("url", url, true)
.withParam("token", token)
.withParam("name", systemHook.getName())
.withParam("description", systemHook.getDescription())
.withParam("push_events", systemHook.getPushEvents())
.withParam("tag_push_events", systemHook.getTagPushEvents())
.withParam("merge_requests_events", systemHook.getMergeRequestsEvents())
@@ -125,6 +141,36 @@ public SystemHook addSystemHook(String url, String token, SystemHook systemHook)
return (response.readEntity(SystemHook.class));
}

/**
* Add a new system hook. This method requires admin access.
*
* <pre><code>GitLab Endpoint: PUT /hooks/:hook_id</code></pre>
*
* @param systemHook the systemHook to update
* @param token secret token to validate received payloads, optional
* @return an SystemHook instance with info on the added system hook
* @throws GitLabApiException if any exception occurs
*/
public SystemHook updateSystemHook(SystemHook systemHook, String token) throws GitLabApiException {

if (systemHook.getId() == null) {
throw new RuntimeException("systemHook id cannot be null");
}

GitLabApiForm formData = new GitLabApiForm()
.withParam("url", systemHook.getUrl())
.withParam("token", token)
.withParam("name", systemHook.getName())
.withParam("description", systemHook.getDescription())
.withParam("push_events", systemHook.getPushEvents())
.withParam("tag_push_events", systemHook.getTagPushEvents())
.withParam("merge_requests_events", systemHook.getMergeRequestsEvents())
.withParam("repository_update_events", systemHook.getRepositoryUpdateEvents())
.withParam("enable_ssl_verification", systemHook.getEnableSslVerification());
Response response = putWithFormData(Response.Status.OK, formData, "hooks", systemHook.getId());
return (response.readEntity(SystemHook.class));
}

/**
* Deletes a system hook. This method requires admin access.
*
@@ -166,7 +212,7 @@ public void deleteSystemHook(Long hookId) throws GitLabApiException {
*
* <pre><code>GitLab Endpoint: GET /hooks/:hook_id</code></pre>
*
* @param hook the SystemHookEvent instance to test
* @param hook the SystemHook instance to test
* @throws GitLabApiException if any exception occurs
*/
public void testSystemHook(SystemHook hook) throws GitLabApiException {
@@ -194,4 +240,32 @@ public void testSystemHook(Long hookId) throws GitLabApiException {

get(Response.Status.OK, null, "hooks", hookId);
}

/**
* Add a new URL variable.
*
* <pre><code>GitLab Endpoint: PUT /hooks/:hook_id/url_variables/:key</code></pre>
*
* @param hookId the ID of the system hook
* @param key Key of the URL variable
* @param value Value of the URL variable.
* @throws GitLabApiException if any exception occurs
*/
public void addSystemHookUrlVariable(Long hookId, String key, String value) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("value", value, true);
put(Response.Status.CREATED, formData.asMap(), "hooks", hookId, "url_variables", key);
}

/**
* Delete a URL variable.
*
* <pre><code>GitLab Endpoint: DELETE /hooks/:hook_id/url_variables/:key</code></pre>
*
* @param hookId the ID of the system hook
* @param key Key of the URL variable
* @throws GitLabApiException if any exception occurs
*/
public void deleteSystemHookUrlVariable(Long hookId, String key) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "hooks", hookId, "url_variables", key);
}
}
Original file line number Diff line number Diff line change
@@ -2,20 +2,24 @@

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.gitlab4j.models.utils.JacksonJson;

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

private Long id;
private String name;
private String description;
private String url;
private Date createdAt;
private Boolean pushEvents;
private Boolean tagPushEvents;
private Boolean enableSslVerification;
private Boolean repositoryUpdateEvents;
private Boolean mergeRequestsEvents;
private List<SystemHook.UrlVariable> urlVariables;

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getUrl() {
return url;
}
@@ -81,48 +101,85 @@ public Boolean getMergeRequestsEvents() {
return mergeRequestsEvents;
}

public List<SystemHook.UrlVariable> getUrlVariables() {
return urlVariables;
}

public void setUrlVariables(List<SystemHook.UrlVariable> urlVariables) {
this.urlVariables = urlVariables;
}

public SystemHook withId(Long id) {
this.id = id;
return (this);
return this;
}

public SystemHook withName(String name) {
this.name = name;
return this;
}

public SystemHook withDescription(String description) {
this.description = description;
return this;
}

public SystemHook withUrl(String url) {
this.url = url;
return (this);
return this;
}

public SystemHook withCreatedAt(Date createdAt) {
this.createdAt = createdAt;
return (this);
return this;
}

public SystemHook withPushEvents(Boolean pushEvents) {
this.pushEvents = pushEvents;
return (this);
return this;
}

public SystemHook withTagPushEvents(Boolean tagPushEvents) {
this.tagPushEvents = tagPushEvents;
return (this);
return this;
}

public SystemHook withEnableSslVerification(Boolean enableSslVerification) {
this.enableSslVerification = enableSslVerification;
return (this);
return this;
}

public SystemHook withRepositoryUpdateEvents(Boolean repositoryUpdateEvents) {
this.repositoryUpdateEvents = repositoryUpdateEvents;
return (this);
return this;
}

public SystemHook withMergeRequestsEvents(Boolean mergeRequestsEvents) {
this.mergeRequestsEvents = mergeRequestsEvents;
return (this);
return this;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}

public static class UrlVariable implements Serializable {
private static final long serialVersionUID = 1L;

private String key;

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
{
"id": 1,
"url": "https://gitlab.example.com/hook",
"name": "Hook name",
"description": "Hook description",
"created_at": "2016-10-31T12:32:15.192Z",
"push_events": true,
"tag_push_events": false,
"enable_ssl_verification": true
}

"merge_requests_events": true,
"repository_update_events": true,
"enable_ssl_verification": true,
"url_variables": [{
"key": "example"
}]
}