package org.gitlab4j.api; import java.util.List; import java.util.Optional; import java.util.stream.Stream; import javax.ws.rs.core.Form; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.Response; import org.gitlab4j.api.models.DeployKey; /** * This class implements the client side API for the GitLab Deploy Keys API calls. */ public class DeployKeysApi extends AbstractApi { public DeployKeysApi(GitLabApi gitLabApi) { super(gitLabApi); } /** * Get a list of all deploy keys across all projects of the GitLab instance. This method requires admin access. * *
GitLab Endpoint: GET /deploy_keys
*
* @return a list of DeployKey
* @throws GitLabApiException if any exception occurs
*/
public ListGitLab Endpoint: GET /deploy_keys
*
* @param page the page to get
* @param perPage the number of deploy keys per page
* @return the list of DeployKey in the specified range
* @throws GitLabApiException if any exception occurs
*/
public ListGitLab Endpoint: GET /deploy_keys
*
* @param itemsPerPage the number of DeployKey instances that will be fetched per page
* @return a Pager of DeployKey
* @throws GitLabApiException if any exception occurs
*/
public PagerGitLab Endpoint: GET /deploy_keys
*
* @return a list of DeployKey
* @throws GitLabApiException if any exception occurs
*/
public StreamGitLab Endpoint: GET /projects/:id/deploy_keys
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a list of DeployKey
* @throws GitLabApiException if any exception occurs
*/
public ListGitLab Endpoint: GET /projects/:id/deploy_keys
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param page the page to get
* @param perPage the number of deploy keys per page
* @return the list of DeployKey in the specified range
* @throws GitLabApiException if any exception occurs
*/
public ListGitLab Endpoint: GET /projects/:id/deploy_keys
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance@param projectId the ID of the project
* @param itemsPerPage the number of DeployKey instances that will be fetched per page
* @return a Pager of DeployKey
* @throws GitLabApiException if any exception occurs
*/
public PagerGitLab Endpoint: GET /projects/:id/deploy_keys
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a list of DeployKey
* @throws GitLabApiException if any exception occurs
*/
public StreamGitLab Endpoint: GET /projects/:id/deploy_keys/:key_id
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param keyId the ID of the deploy key to delete
* @return the DeployKey instance for the specified project ID and key ID
* @throws GitLabApiException if any exception occurs
*/
public DeployKey getDeployKey(Object projectIdOrPath, Integer keyId) throws GitLabApiException {
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
Response response = get(Response.Status.OK, null,
"projects", getProjectIdOrPath(projectIdOrPath), "deploy_keys", keyId);
return (response.readEntity(DeployKey.class));
}
/**
* Get a single deploy key for the specified project as an Optional instance.
*
* GitLab Endpoint: GET /projects/:id/deploy_keys/:key_id
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param keyId the ID of the deploy key to delete
* @return the DeployKey for the specified project ID and key ID as an Optional instance
*/
public OptionalGitLab Endpoint: POST /projects/:id/deploy_keys
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param title the new deploy key's title, required
* @param key the new deploy key, required
* @param canPush can deploy key push to the project's repository, optional
* @return an DeployKey instance with info on the added deploy key
* @throws GitLabApiException if any exception occurs
*/
public DeployKey addDeployKey(Object projectIdOrPath, String title, String key, Boolean canPush) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("key", key, true)
.withParam("can_push", canPush);
Response response = post(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "deploy_keys");
return (response.readEntity(DeployKey.class));
}
/**
* Removes a deploy key from the project. If the deploy key is used only for this project,it will be deleted from the system.
*
* GitLab Endpoint: DELETE /projects/:id/deploy_keys/:key_id
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param keyId the ID of the deploy key to delete
* @throws GitLabApiException if any exception occurs
*/
public void deleteDeployKey(Object projectIdOrPath, Integer keyId) throws GitLabApiException {
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "deploy_keys", keyId);
}
/**
* Enables a deploy key for a project so this can be used. Returns the enabled key when successful.
*
* GitLab Endpoint: POST /projects/:id/deploy_keys/:key_id/enable
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param keyId the ID of the deploy key to enable
* @return an DeployKey instance with info on the enabled deploy key
* @throws GitLabApiException if any exception occurs
*/
public DeployKey enableDeployKey(Object projectIdOrPath, Integer keyId) throws GitLabApiException {
if (keyId == null) {
throw new RuntimeException("keyId cannot be null");
}
Response response = post(Response.Status.CREATED, (Form)null,
"projects", getProjectIdOrPath(projectIdOrPath), "deploy_keys", keyId, "enable");
return (response.readEntity(DeployKey.class));
}
}