|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javax.ws.rs.core.GenericType; |
| 6 | +import javax.ws.rs.core.Response; |
| 7 | + |
| 8 | +import org.gitlab4j.api.GitLabApi.ApiVersion; |
| 9 | +import org.gitlab4j.api.models.SystemHook; |
| 10 | + |
| 11 | +/** |
| 12 | + * This class implements the client side API for the GitLab System Hooks Keys API calls. |
| 13 | + */ |
| 14 | +public class SystemHooksApi extends AbstractApi { |
| 15 | + |
| 16 | + public SystemHooksApi(GitLabApi gitLabApi) { |
| 17 | + super(gitLabApi); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Get a list of all system hooks. This method requires admin access. |
| 22 | + * Only returns the first page. This method requires admin access. |
| 23 | + * |
| 24 | + * <code>GET /hooks</code> |
| 25 | + * |
| 26 | + * @return a list of SystemHookEvent |
| 27 | + * @throws GitLabApiException if any exception occurs |
| 28 | + */ |
| 29 | + public List<SystemHook> getSystemHooks() throws GitLabApiException { |
| 30 | + return (getSystemHooks(1, getDefaultPerPage())); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Get a list of all system hooks using the specified page and per page settings. |
| 35 | + * This method requires admin access. |
| 36 | + * |
| 37 | + * <code>GET /hooks</code> |
| 38 | + * |
| 39 | + * @param page the page to get |
| 40 | + * @param perPage the number of deploy keys per page |
| 41 | + * @return the list of SystemHookEvent in the specified range |
| 42 | + * @throws GitLabApiException if any exception occurs |
| 43 | + */ |
| 44 | + public List<SystemHook> getSystemHooks(int page, int perPage) throws GitLabApiException { |
| 45 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "hooks"); |
| 46 | + return (response.readEntity(new GenericType<List<SystemHook>>() {})); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get a Pager of all system hooks. This method requires admin access. |
| 51 | + * |
| 52 | + * <code>GET /hooks</code> |
| 53 | + * |
| 54 | + * @param itemsPerPage the number of SystemHookEvent instances that will be fetched per page |
| 55 | + * @return a Pager of SystemHookEvent |
| 56 | + * @throws GitLabApiException if any exception occurs |
| 57 | + */ |
| 58 | + public Pager<SystemHook> getSystemHooks(int itemsPerPage) throws GitLabApiException { |
| 59 | + return (new Pager<SystemHook>(this, SystemHook.class, itemsPerPage, null, "hooks")); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Add a new system hook. This method requires admin access. |
| 64 | + * |
| 65 | + * <code>POST /hooks</code> |
| 66 | + * |
| 67 | + * @param url the hook URL, required |
| 68 | + * @param token secret token to validate received payloads, optional |
| 69 | + * @param pushEvents when true, the hook will fire on push events, optional |
| 70 | + * @param tagPushEvents when true, the hook will fire on new tags being pushed, optional |
| 71 | + * @param enablSsslVerification do SSL verification when triggering the hook, optional |
| 72 | + * @return an SystemHookEvent instance with info on the added system hook |
| 73 | + * @throws GitLabApiException if any exception occurs |
| 74 | + */ |
| 75 | + public SystemHook addSystemHook(String url, String token, Boolean pushEvents, |
| 76 | + Boolean tagPushEvents, Boolean enablSsslVerification) throws GitLabApiException { |
| 77 | + |
| 78 | + if (url == null) { |
| 79 | + throw new RuntimeException("url cannot be null"); |
| 80 | + } |
| 81 | + |
| 82 | + GitLabApiForm formData = new GitLabApiForm() |
| 83 | + .withParam("url", url, true) |
| 84 | + .withParam("token", token) |
| 85 | + .withParam("push_events", pushEvents) |
| 86 | + .withParam("tag_push_events", tagPushEvents) |
| 87 | + .withParam("enable_ssl_verification", enablSsslVerification); |
| 88 | + Response response = post(Response.Status.CREATED, formData, "hooks"); |
| 89 | + return (response.readEntity(SystemHook.class)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Deletes a system hook. This method requires admin access. |
| 94 | + * |
| 95 | + * <code>DELETE /hooks/:hook_id</code> |
| 96 | + * |
| 97 | + * @param hook the SystemHook instance to delete |
| 98 | + * @throws GitLabApiException if any exception occurs |
| 99 | + */ |
| 100 | + public void deleteSystemHook(SystemHook hook) throws GitLabApiException { |
| 101 | + |
| 102 | + if (hook == null) { |
| 103 | + throw new RuntimeException("hook cannot be null"); |
| 104 | + } |
| 105 | + |
| 106 | + deleteSystemHook(hook.getId()); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Deletes a system hook. This method requires admin access. |
| 111 | + * |
| 112 | + * <code>DELETE /hooks/:hook_id</code> |
| 113 | + * |
| 114 | + * @param hookId the ID of the system hook to delete |
| 115 | + * @throws GitLabApiException if any exception occurs |
| 116 | + */ |
| 117 | + public void deleteSystemHook(Integer hookId) throws GitLabApiException { |
| 118 | + |
| 119 | + if (hookId == null) { |
| 120 | + throw new RuntimeException("hookId cannot be null"); |
| 121 | + } |
| 122 | + |
| 123 | + Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); |
| 124 | + delete(expectedStatus, null, "hooks", hookId); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Test a system hook. This method requires admin access. |
| 129 | + * |
| 130 | + * <code>GET /hooks/:hook_id</code> |
| 131 | + * |
| 132 | + * @param hook the SystemHookEvent instance to test |
| 133 | + * @throws GitLabApiException if any exception occurs |
| 134 | + */ |
| 135 | + public void testSystemHook(SystemHook hook) throws GitLabApiException { |
| 136 | + |
| 137 | + if (hook == null) { |
| 138 | + throw new RuntimeException("hook cannot be null"); |
| 139 | + } |
| 140 | + |
| 141 | + testSystemHook(hook.getId()); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Test a system hook. This method requires admin access. |
| 146 | + * |
| 147 | + * <code>GET /hooks/:hook_id</code> |
| 148 | + * |
| 149 | + * @param hookId the ID of the system hook to test |
| 150 | + * @throws GitLabApiException if any exception occurs |
| 151 | + */ |
| 152 | + public void testSystemHook(Integer hookId) throws GitLabApiException { |
| 153 | + |
| 154 | + if (hookId == null) { |
| 155 | + throw new RuntimeException("hookId cannot be null"); |
| 156 | + } |
| 157 | + |
| 158 | + get(Response.Status.OK, null, "hooks", hookId); |
| 159 | + } |
| 160 | +} |
0 commit comments