From be7104a75266c8f18e7b14550ae297f745b3df49 Mon Sep 17 00:00:00 2001 From: Jean-Michel Leclercq Date: Sun, 13 Oct 2024 21:20:14 +0200 Subject: [PATCH] feat: create group webhook #1173 --- src/main/java/org/gitlab4j/api/GroupApi.java | 34 ++- .../org/gitlab4j/api/models/GroupHook.java | 252 ++++++++++++++++++ .../gitlab4j/api/models/GroupHookParams.java | 183 +++++++++++++ .../org/gitlab4j/api/TestGitLabApiBeans.java | 7 + .../java/org/gitlab4j/api/TestGroupApi.java | 46 +++- .../org/gitlab4j/api/group-hook.json | 32 +++ 6 files changed, 529 insertions(+), 25 deletions(-) create mode 100644 src/main/java/org/gitlab4j/api/models/GroupHook.java create mode 100644 src/main/java/org/gitlab4j/api/models/GroupHookParams.java create mode 100644 src/test/resources/org/gitlab4j/api/group-hook.json diff --git a/src/main/java/org/gitlab4j/api/GroupApi.java b/src/main/java/org/gitlab4j/api/GroupApi.java index 3404e8886..3d5c7db91 100644 --- a/src/main/java/org/gitlab4j/api/GroupApi.java +++ b/src/main/java/org/gitlab4j/api/GroupApi.java @@ -16,25 +16,8 @@ import javax.ws.rs.core.Response; import org.gitlab4j.api.GitLabApi.ApiVersion; -import org.gitlab4j.api.models.AccessLevel; -import org.gitlab4j.api.models.AccessRequest; -import org.gitlab4j.api.models.AuditEvent; -import org.gitlab4j.api.models.Badge; -import org.gitlab4j.api.models.CustomAttribute; -import org.gitlab4j.api.models.Group; -import org.gitlab4j.api.models.GroupAccessToken; -import org.gitlab4j.api.models.GroupFilter; -import org.gitlab4j.api.models.GroupParams; -import org.gitlab4j.api.models.GroupProjectsFilter; +import org.gitlab4j.api.models.*; import org.gitlab4j.api.models.ImpersonationToken.Scope; -import org.gitlab4j.api.models.Iteration; -import org.gitlab4j.api.models.IterationFilter; -import org.gitlab4j.api.models.LdapGroupLink; -import org.gitlab4j.api.models.Member; -import org.gitlab4j.api.models.Project; -import org.gitlab4j.api.models.SamlGroupLink; -import org.gitlab4j.api.models.Variable; -import org.gitlab4j.api.models.Visibility; import org.gitlab4j.api.utils.ISO8601; /** @@ -2440,4 +2423,19 @@ public GroupAccessToken rotateGroupAccessToken(Object groupIdOrPath, Long tokenI public void revokeGroupAccessToken(Object groupIdOrPath, Long tokenId) throws GitLabApiException { delete(Response.Status.NO_CONTENT, null, "groups", getGroupIdOrPath(groupIdOrPath), "access_tokens", tokenId); } + + /** + * Add a group hook + * + *
GitLab Endpoint: POST /groups/:id/hooks
+ * + * @param groupIdOrPath the group in the form of an Long(ID), String(path), or Group instance + * @param groupHookParams webhook creation options + * @throws GitLabApiException if any exception occurs + */ + public GroupHook addWebhook(Object groupIdOrPath, GroupHookParams groupHookParams) throws GitLabApiException { + Response response = post( + Response.Status.CREATED, groupHookParams.getForm(), "groups", getGroupIdOrPath(groupIdOrPath), "hooks"); + return (response.readEntity(GroupHook.class)); + } } diff --git a/src/main/java/org/gitlab4j/api/models/GroupHook.java b/src/main/java/org/gitlab4j/api/models/GroupHook.java new file mode 100644 index 000000000..c50d4bae7 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/GroupHook.java @@ -0,0 +1,252 @@ +package org.gitlab4j.api.models; + +import java.io.Serializable; +import java.util.Date; + +public class GroupHook implements Serializable { + private static final long serialVersionUID = 1L; + + private Long id; + private String url; + private String name; + private String description; + private Long groupId; + private Boolean pushEvents; + private String pushEventsBranchFilter; + private String branchFilterStrategy; + private Boolean issuesEvents; + private Boolean confidentialIssuesEvents; + private Boolean mergeRequestsEvents; + private Boolean tagPushEvents; + private Boolean noteEvents; + private Boolean confidentialNoteEvents; + private Boolean jobEvents; + private Boolean pipelineEvents; + private Boolean wikiPageEvents; + private Boolean deploymentEvents; + private Boolean featureFlagEvents; + private Boolean releasesEvents; + private Boolean subgroupEvents; + private Boolean memberEvents; + private Boolean enableSslVerification; + private Boolean repositoryUpdateEvents; + private Date createdAt; + private Boolean resourceAccessTokenEvents; + private String customWebhookTemplate; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getGroupId() { + return groupId; + } + + public void setGroupId(Long groupId) { + this.groupId = groupId; + } + + public Boolean getPushEvents() { + return pushEvents; + } + + public void setPushEvents(Boolean pushEvents) { + this.pushEvents = pushEvents; + } + + public String getPushEventsBranchFilter() { + return pushEventsBranchFilter; + } + + public void setPushEventsBranchFilter(String pushEventsBranchFilter) { + this.pushEventsBranchFilter = pushEventsBranchFilter; + } + + public String getBranchFilterStrategy() { + return branchFilterStrategy; + } + + public void setBranchFilterStrategy(String branchFilterStrategy) { + this.branchFilterStrategy = branchFilterStrategy; + } + + public Boolean getIssuesEvents() { + return issuesEvents; + } + + public void setIssuesEvents(Boolean issuesEvents) { + this.issuesEvents = issuesEvents; + } + + public Boolean getConfidentialIssuesEvents() { + return confidentialIssuesEvents; + } + + public void setConfidentialIssuesEvents(Boolean confidentialIssuesEvents) { + this.confidentialIssuesEvents = confidentialIssuesEvents; + } + + public Boolean getMergeRequestsEvents() { + return mergeRequestsEvents; + } + + public void setMergeRequestsEvents(Boolean mergeRequestsEvents) { + this.mergeRequestsEvents = mergeRequestsEvents; + } + + public Boolean getTagPushEvents() { + return tagPushEvents; + } + + public void setTagPushEvents(Boolean tagPushEvents) { + this.tagPushEvents = tagPushEvents; + } + + public Boolean getNoteEvents() { + return noteEvents; + } + + public void setNoteEvents(Boolean noteEvents) { + this.noteEvents = noteEvents; + } + + public Boolean getConfidentialNoteEvents() { + return confidentialNoteEvents; + } + + public void setConfidentialNoteEvents(Boolean confidentialNoteEvents) { + this.confidentialNoteEvents = confidentialNoteEvents; + } + + public Boolean getJobEvents() { + return jobEvents; + } + + public void setJobEvents(Boolean jobEvents) { + this.jobEvents = jobEvents; + } + + public Boolean getPipelineEvents() { + return pipelineEvents; + } + + public void setPipelineEvents(Boolean pipelineEvents) { + this.pipelineEvents = pipelineEvents; + } + + public Boolean getWikiPageEvents() { + return wikiPageEvents; + } + + public void setWikiPageEvents(Boolean wikiPageEvents) { + this.wikiPageEvents = wikiPageEvents; + } + + public Boolean getDeploymentEvents() { + return deploymentEvents; + } + + public void setDeploymentEvents(Boolean deploymentEvents) { + this.deploymentEvents = deploymentEvents; + } + + public Boolean getFeatureFlagEvents() { + return featureFlagEvents; + } + + public void setFeatureFlagEvents(Boolean featureFlagEvents) { + this.featureFlagEvents = featureFlagEvents; + } + + public Boolean getReleasesEvents() { + return releasesEvents; + } + + public void setReleasesEvents(Boolean releasesEvents) { + this.releasesEvents = releasesEvents; + } + + public Boolean getSubgroupEvents() { + return subgroupEvents; + } + + public void setSubgroupEvents(Boolean subgroupEvents) { + this.subgroupEvents = subgroupEvents; + } + + public Boolean getMemberEvents() { + return memberEvents; + } + + public void setMemberEvents(Boolean memberEvents) { + this.memberEvents = memberEvents; + } + + public Boolean getEnableSslVerification() { + return enableSslVerification; + } + + public void setEnableSslVerification(Boolean enableSslVerification) { + this.enableSslVerification = enableSslVerification; + } + + public Boolean getRepositoryUpdateEvents() { + return repositoryUpdateEvents; + } + + public void setRepositoryUpdateEvents(Boolean repositoryUpdateEvents) { + this.repositoryUpdateEvents = repositoryUpdateEvents; + } + + public Date getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(Date createdAt) { + this.createdAt = createdAt; + } + + public Boolean getResourceAccessTokenEvents() { + return resourceAccessTokenEvents; + } + + public void setResourceAccessTokenEvents(Boolean resourceAccessTokenEvents) { + this.resourceAccessTokenEvents = resourceAccessTokenEvents; + } + + public String getCustomWebhookTemplate() { + return customWebhookTemplate; + } + + public void setCustomWebhookTemplate(String customWebhookTemplate) { + this.customWebhookTemplate = customWebhookTemplate; + } +} diff --git a/src/main/java/org/gitlab4j/api/models/GroupHookParams.java b/src/main/java/org/gitlab4j/api/models/GroupHookParams.java new file mode 100644 index 000000000..e2a9de494 --- /dev/null +++ b/src/main/java/org/gitlab4j/api/models/GroupHookParams.java @@ -0,0 +1,183 @@ +package org.gitlab4j.api.models; + +import java.io.Serializable; + +import org.gitlab4j.api.GitLabApiForm; + +public class GroupHookParams implements Serializable { + private static final long serialVersionUID = 1L; + + private String url; + private String name; + private String description; + private Boolean pushEvents; + private String pushEventsBranchFilter; + private String branchFilterStrategy; + private Boolean issuesEvents; + private Boolean confidentialIssuesEvents; + private Boolean mergeRequestsEvents; + private Boolean tagPushEvents; + private Boolean noteEvents; + private Boolean confidentialNoteEvents; + private Boolean jobEvents; + private Boolean pipelineEvents; + private Boolean wikiPageEvents; + private Boolean deploymentEvents; + private Boolean featureFlagEvents; + private Boolean releasesEvents; + private Boolean subgroupEvents; + private Boolean memberEvents; + private Boolean enableSslVerification; + private String token; + private Boolean resourceAccessTokenEvents; + private String customWebhookTemplate; + + public GitLabApiForm getForm() { + + return new GitLabApiForm() + .withParam("url", url, true) + .withParam("name", name) + .withParam("description", description) + .withParam("push_events", pushEvents) + .withParam("push_events_branch_filter", pushEventsBranchFilter) + .withParam("branch_filter_strategy", branchFilterStrategy) + .withParam("issues_events", issuesEvents) + .withParam("confidential_issues_events", confidentialIssuesEvents) + .withParam("merge_requests_events", mergeRequestsEvents) + .withParam("tag_push_events", tagPushEvents) + .withParam("note_events", noteEvents) + .withParam("confidential_note_events", confidentialNoteEvents) + .withParam("job_events", jobEvents) + .withParam("pipeline_events", pipelineEvents) + .withParam("wiki_page_events", wikiPageEvents) + .withParam("deployment_events", deploymentEvents) + .withParam("feature_flag_events", featureFlagEvents) + .withParam("releases_events", releasesEvents) + .withParam("subgroup_events", subgroupEvents) + .withParam("member_events", memberEvents) + .withParam("enable_ssl_verification", enableSslVerification) + .withParam("token", token) + .withParam("resource_access_token_events", resourceAccessTokenEvents) + .withParam("custom_webhook_template", customWebhookTemplate); + } + + public GroupHookParams setBranchFilterStrategy(String branchFilterStrategy) { + this.branchFilterStrategy = branchFilterStrategy; + return this; + } + + public GroupHookParams setUrl(String url) { + this.url = url; + return this; + } + + public GroupHookParams setName(String name) { + this.name = name; + return this; + } + + public GroupHookParams setDescription(String description) { + this.description = description; + return this; + } + + public GroupHookParams setPushEvents(Boolean pushEvents) { + this.pushEvents = pushEvents; + return this; + } + + public GroupHookParams setPushEventsBranchFilter(String pushEventsBranchFilter) { + this.pushEventsBranchFilter = pushEventsBranchFilter; + return this; + } + + public GroupHookParams setIssuesEvents(Boolean issuesEvents) { + this.issuesEvents = issuesEvents; + return this; + } + + public GroupHookParams setConfidentialIssuesEvents(Boolean confidentialIssuesEvents) { + this.confidentialIssuesEvents = confidentialIssuesEvents; + return this; + } + + public GroupHookParams setMergeRequestsEvents(Boolean mergeRequestsEvents) { + this.mergeRequestsEvents = mergeRequestsEvents; + return this; + } + + public GroupHookParams setTagPushEvents(Boolean tagPushEvents) { + this.tagPushEvents = tagPushEvents; + return this; + } + + public GroupHookParams setNoteEvents(Boolean noteEvents) { + this.noteEvents = noteEvents; + return this; + } + + public GroupHookParams setConfidentialNoteEvents(Boolean confidentialNoteEvents) { + this.confidentialNoteEvents = confidentialNoteEvents; + return this; + } + + public GroupHookParams setJobEvents(Boolean jobEvents) { + this.jobEvents = jobEvents; + return this; + } + + public GroupHookParams setPipelineEvents(Boolean pipelineEvents) { + this.pipelineEvents = pipelineEvents; + return this; + } + + public GroupHookParams setWikiPageEvents(Boolean wikiPageEvents) { + this.wikiPageEvents = wikiPageEvents; + return this; + } + + public GroupHookParams setDeploymentEvents(Boolean deploymentEvents) { + this.deploymentEvents = deploymentEvents; + return this; + } + + public GroupHookParams setFeatureFlagEvents(Boolean featureFlagEvents) { + this.featureFlagEvents = featureFlagEvents; + return this; + } + + public GroupHookParams setReleasesEvents(Boolean releasesEvents) { + this.releasesEvents = releasesEvents; + return this; + } + + public GroupHookParams setSubgroupEvents(Boolean subgroupEvents) { + this.subgroupEvents = subgroupEvents; + return this; + } + + public GroupHookParams setMemberEvents(Boolean memberEvents) { + this.memberEvents = memberEvents; + return this; + } + + public GroupHookParams setEnableSslVerification(Boolean enableSslVerification) { + this.enableSslVerification = enableSslVerification; + return this; + } + + public GroupHookParams setToken(String token) { + this.token = token; + return this; + } + + public GroupHookParams setResourceAccessTokenEvents(Boolean resourceAccessTokenEvents) { + this.resourceAccessTokenEvents = resourceAccessTokenEvents; + return this; + } + + public GroupHookParams setCustomWebhookTemplate(String customWebhookTemplate) { + this.customWebhookTemplate = customWebhookTemplate; + return this; + } +} diff --git a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java index 195c94d30..4f611070a 100644 --- a/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java +++ b/src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java @@ -132,6 +132,7 @@ import org.gitlab4j.api.models.Trigger; import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.Variable; +import org.gitlab4j.api.models.GroupHook; import org.gitlab4j.api.services.JiraService; import org.gitlab4j.api.services.SlackService; import org.gitlab4j.api.webhook.ExternalStatusCheckEvent; @@ -860,4 +861,10 @@ public void testMetadata() throws Exception { Metadata metadata = unmarshalResource(Metadata.class, "metadata.json"); assertTrue(compareJson(metadata, "metadata.json")); } + + @Test + public void testWebhook() throws Exception { + GroupHook groupHook = unmarshalResource(GroupHook.class, "group-hook.json"); + assertTrue(compareJson(groupHook, "group-hook.json")); + } } diff --git a/src/test/java/org/gitlab4j/api/TestGroupApi.java b/src/test/java/org/gitlab4j/api/TestGroupApi.java index c4e236dcd..68eafb099 100644 --- a/src/test/java/org/gitlab4j/api/TestGroupApi.java +++ b/src/test/java/org/gitlab4j/api/TestGroupApi.java @@ -18,13 +18,8 @@ import javax.ws.rs.core.Response; -import org.gitlab4j.api.models.AccessLevel; -import org.gitlab4j.api.models.AccessRequest; -import org.gitlab4j.api.models.Group; -import org.gitlab4j.api.models.GroupFilter; -import org.gitlab4j.api.models.GroupParams; -import org.gitlab4j.api.models.Member; -import org.gitlab4j.api.models.User; +import org.gitlab4j.api.models.*; +import org.gitlab4j.api.models.GroupHook; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -358,4 +353,41 @@ public void updateGroup() throws GitLabApiException { assertEquals(testGroup.getId(), optional.get().getId()); assertEquals(description, optional.get().getDescription()); } + + @Test + public void addGroupHook() throws GitLabApiException { + // Given + GroupHookParams groupHookParams = new GroupHookParams(); + groupHookParams + .setUrl("") + .setName("My Webhook") + .setDescription("") + .setBranchFilterStrategy("") + .setConfidentialIssuesEvents(true) + .setIssuesEvents(true) + .setConfidentialNoteEvents(true) + .setEnableSslVerification(true) + .setFeatureFlagEvents(true) + .setPushEvents(true) + .setJobEvents(true) + .setToken("token") + .setSubgroupEvents(true) + .setWikiPageEvents(true) + .setTagPushEvents(true) + .setReleasesEvents(true) + .setMemberEvents(true) + .setResourceAccessTokenEvents(true) + .setDeploymentEvents(true) + .setPipelineEvents(true) + .setNoteEvents(true) + .setMergeRequestsEvents(true) + .setPushEventsBranchFilter("wildcard") + .setCustomWebhookTemplate("{\"event\":\"{{object_kind}}\"}"); + + // When + GroupHook createdWebhook = gitLabApi.getGroupApi().addWebhook(testGroup.getId(), groupHookParams); + + // Then + assertEquals("My Webhook", createdWebhook.getName()); + } } diff --git a/src/test/resources/org/gitlab4j/api/group-hook.json b/src/test/resources/org/gitlab4j/api/group-hook.json new file mode 100644 index 000000000..8501b4f05 --- /dev/null +++ b/src/test/resources/org/gitlab4j/api/group-hook.json @@ -0,0 +1,32 @@ +{ + "id": 42, + "url": "https://example.com/hook", + "name": "My Hook", + "description": "Hook description", + "group_id": 3, + "push_events": true, + "push_events_branch_filter": "", + "branch_filter_strategy": "wildcard", + "issues_events": true, + "confidential_issues_events": true, + "merge_requests_events": true, + "tag_push_events": true, + "note_events": true, + "confidential_note_events": true, + "job_events": true, + "pipeline_events": true, + "wiki_page_events": true, + "deployment_events": true, + "feature_flag_events": true, + "releases_events": true, + "subgroup_events": true, + "member_events": true, + "enable_ssl_verification": true, + "repository_update_events": false, + "alert_status": "executable", + "disabled_until": null, + "url_variables": [ ], + "created_at": "2012-10-12T17:04:47Z", + "resource_access_token_events": true, + "custom_webhook_template": "{\"event\":\"{{object_kind}}\"}", +}