From b7de09cfc37eda2c32c623c938453782dd46e3f3 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson <jeremie.bresson@unblu.com> Date: Fri, 7 Mar 2025 10:19:44 +0100 Subject: [PATCH 1/4] System hook improvements --- .../java/org/gitlab4j/api/SystemHooksApi.java | 47 +++++++++++++++---- .../org/gitlab4j/api/models/SystemHook.java | 18 +++++++ 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java index f82c34baa..9732fd9b2 100644 --- a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java +++ b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java @@ -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()) @@ -166,7 +182,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 +210,19 @@ public void testSystemHook(Long hookId) throws GitLabApiException { get(Response.Status.OK, null, "hooks", hookId); } + + /** + * Add a new system hook. This method requires admin access. + * + * <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, "hooks", hookId, "url_variables", key); + } } diff --git a/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java b/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java index c04e51054..3daa423cb 100644 --- a/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java +++ b/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java @@ -9,6 +9,8 @@ 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; @@ -25,6 +27,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; } From 0f0dcbc0cab4af85f2604246e6acedceec86baf1 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson <jeremie.bresson@unblu.com> Date: Fri, 7 Mar 2025 10:33:29 +0100 Subject: [PATCH 2/4] Fixup --- gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java index 9732fd9b2..a9ec1a1fe 100644 --- a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java +++ b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java @@ -223,6 +223,6 @@ public void testSystemHook(Long hookId) throws GitLabApiException { */ public void addSystemHookUrlVariable(Long hookId, String key, String value) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm().withParam("value", value, true); - put(Response.Status.CREATED, formData, "hooks", hookId, "url_variables", key); + putWithFormData(Response.Status.CREATED, formData, "hooks", hookId, "url_variables", key); } } From 4cd5a693ab4cc03c86e4913ea9c5f9b70346fb74 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson <jeremie.bresson@unblu.com> Date: Fri, 7 Mar 2025 11:19:31 +0100 Subject: [PATCH 3/4] Fixup 2 --- gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java index a9ec1a1fe..ed977b81b 100644 --- a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java +++ b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java @@ -223,6 +223,6 @@ public void testSystemHook(Long hookId) throws GitLabApiException { */ public void addSystemHookUrlVariable(Long hookId, String key, String value) throws GitLabApiException { GitLabApiForm formData = new GitLabApiForm().withParam("value", value, true); - putWithFormData(Response.Status.CREATED, formData, "hooks", hookId, "url_variables", key); + put(Response.Status.CREATED, formData.asMap(), "hooks", hookId, "url_variables", key); } } From 875a1fc5c4d6e1a7513a97884d2c034c24184590 Mon Sep 17 00:00:00 2001 From: Jeremie Bresson <jeremie.bresson@unblu.com> Date: Fri, 7 Mar 2025 14:03:29 +0100 Subject: [PATCH 4/4] Add methods --- .../java/org/gitlab4j/api/SystemHooksApi.java | 45 ++++++++++++++- .../org/gitlab4j/api/models/SystemHook.java | 55 ++++++++++++++++--- .../org/gitlab4j/models/system-hook.json | 12 +++- 3 files changed, 100 insertions(+), 12 deletions(-) diff --git a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java index ed977b81b..539b6e303 100644 --- a/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java +++ b/gitlab4j-api/src/main/java/org/gitlab4j/api/SystemHooksApi.java @@ -141,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. * @@ -212,7 +242,7 @@ public void testSystemHook(Long hookId) throws GitLabApiException { } /** - * Add a new system hook. This method requires admin access. + * Add a new URL variable. * * <pre><code>GitLab Endpoint: PUT /hooks/:hook_id/url_variables/:key</code></pre> * @@ -225,4 +255,17 @@ public void addSystemHookUrlVariable(Long hookId, String key, String value) thro 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); + } } diff --git a/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java b/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java index 3daa423cb..8b2baa186 100644 --- a/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java +++ b/gitlab4j-models/src/main/java/org/gitlab4j/api/models/SystemHook.java @@ -2,6 +2,7 @@ import java.io.Serializable; import java.util.Date; +import java.util.List; import org.gitlab4j.models.utils.JacksonJson; @@ -18,6 +19,7 @@ public class SystemHook implements Serializable { private Boolean enableSslVerification; private Boolean repositoryUpdateEvents; private Boolean mergeRequestsEvents; + private List<SystemHook.UrlVariable> urlVariables; public Long getId() { return id; @@ -99,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)); + } + } } diff --git a/gitlab4j-models/src/test/resources/org/gitlab4j/models/system-hook.json b/gitlab4j-models/src/test/resources/org/gitlab4j/models/system-hook.json index d1cc6ea9a..5af118b12 100644 --- a/gitlab4j-models/src/test/resources/org/gitlab4j/models/system-hook.json +++ b/gitlab4j-models/src/test/resources/org/gitlab4j/models/system-hook.json @@ -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" + }] +} \ No newline at end of file