Skip to content

Commit

Permalink
Fix gitlab4j#649 : Add missing properties to SystemHook
Browse files Browse the repository at this point in the history
  • Loading branch information
jabby committed Jun 14, 2021
1 parent 0286422 commit 6941314
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
32 changes: 27 additions & 5 deletions src/main/java/org/gitlab4j/api/SystemHooksApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,32 @@ public Stream<SystemHook> getSystemHookStream() throws GitLabApiException {
* @param token secret token to validate received payloads, optional
* @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 enablSsslVerification do SSL verification when triggering the hook, optional
* @param enableSslVerification do SSL verification when triggering the hook, optional
* @return an SystemHookEvent instance with info on the added system hook
* @throws GitLabApiException if any exception occurs
*/
public SystemHook addSystemHook(String url, String token, Boolean pushEvents,
Boolean tagPushEvents, Boolean enablSsslVerification) throws GitLabApiException {
Boolean tagPushEvents, Boolean enableSslVerification) throws GitLabApiException {

SystemHook systemHook = new SystemHook().withPushEvents(pushEvents)
.withTagPushEvents(tagPushEvents)
.withEnableSslVerification(enableSslVerification);

return addSystemHook(url, token, systemHook);
}

/**
* Add a new system hook. This method requires admin access.
*
* <pre><code>GitLab Endpoint: POST /hooks</code></pre>
*
* @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
* @throws GitLabApiException if any exception occurs
*/
public SystemHook addSystemHook(String url, String token, SystemHook systemHook) throws GitLabApiException {

if (url == null) {
throw new RuntimeException("url cannot be null");
Expand All @@ -94,9 +114,11 @@ public SystemHook addSystemHook(String url, String token, Boolean pushEvents,
GitLabApiForm formData = new GitLabApiForm()
.withParam("url", url, true)
.withParam("token", token)
.withParam("push_events", pushEvents)
.withParam("tag_push_events", tagPushEvents)
.withParam("enable_ssl_verification", enablSsslVerification);
.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 = post(Response.Status.CREATED, formData, "hooks");
return (response.readEntity(SystemHook.class));
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/gitlab4j/api/models/SystemHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SystemHook {
private Boolean tagPushEvents;
private Boolean enableSslVerification;
private Boolean repositoryUpdateEvents;
private Boolean mergeRequestsEvents;

public Integer getId() {
return id;
Expand Down Expand Up @@ -70,6 +71,14 @@ public Boolean getRepositoryUpdateEvents() {
return repositoryUpdateEvents;
}

public void setMergeRequestsEvents(Boolean mergeRequestsEvents) {
this.mergeRequestsEvents = mergeRequestsEvents;
}

public Boolean getMergeRequestsEvents() {
return mergeRequestsEvents;
}

public SystemHook withId(Integer id) {
this.id = id;
return (this);
Expand Down Expand Up @@ -105,6 +114,11 @@ public SystemHook withRepositoryUpdateEvents(Boolean repositoryUpdateEvents) {
return (this);
}

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

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
Expand Down
26 changes: 23 additions & 3 deletions src/test/java/org/gitlab4j/api/TestSystemHooksApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,41 @@ public void testAddSystemHook() throws GitLabApiException {
assertEquals(TEST_HOOK_URL, hook.getUrl());
assertTrue(hook.getPushEvents());
assertFalse(hook.getTagPushEvents());
assertFalse(hook.getMergeRequestsEvents());
assertFalse(hook.getRepositoryUpdateEvents());
assertTrue(hook.getEnableSslVerification());


gitLabApi.getSystemHooksApi().deleteSystemHook(hook);

hook.withPushEvents(false)
.withTagPushEvents(true)
.withMergeRequestsEvents(true)
.withRepositoryUpdateEvents(true)
.withEnableSslVerification(false);

SystemHook updatedHook = gitLabApi.getSystemHooksApi().addSystemHook(TEST_HOOK_URL, TEST_SECRET_TOKEN, hook);
assertNotNull(updatedHook);
assertEquals(TEST_HOOK_URL, updatedHook.getUrl());
assertFalse(hook.getPushEvents());
assertTrue(hook.getTagPushEvents());
assertTrue(hook.getMergeRequestsEvents());
assertTrue(hook.getRepositoryUpdateEvents());
assertFalse(hook.getEnableSslVerification());

gitLabApi.getSystemHooksApi().deleteSystemHook(hook);

}

@Test
public void testGerSystemHooks() throws GitLabApiException {
public void testGetSystemHooks() throws GitLabApiException {

SystemHook hook = gitLabApi.getSystemHooksApi().addSystemHook(TEST_HOOK_URL, TEST_SECRET_TOKEN, true, false, true);
assertNotNull(hook);

List<SystemHook> hooks = gitLabApi.getSystemHooksApi().getSystemHooks();
assertNotNull(hooks);
assertFalse(hooks.isEmpty());

gitLabApi.getSystemHooksApi().deleteSystemHook(hook);
}
}

0 comments on commit 6941314

Please sign in to comment.