diff --git a/src/main/java/org/gitlab4j/api/GitLabApiForm.java b/src/main/java/org/gitlab4j/api/GitLabApiForm.java index 9c0dd589551dd45dd130ec8d72bca6f800e03d64..6693e320188c9bc13c2b1d9acffe276a9b86b0f6 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApiForm.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiForm.java @@ -2,6 +2,8 @@ package org.gitlab4j.api; import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import javax.ws.rs.core.Form; import javax.ws.rs.core.MultivaluedHashMap; @@ -134,6 +136,36 @@ public class GitLabApiForm extends Form { return (this); } + /** + * Fluent method for adding an array of hash type query and form parameters to a get() or post() call. + * + * @param name the name of the field/attribute to add + * @param variables a Map containing array of hashes + * @param required the field is required flag + * @return this GitLabAPiForm instance + * @throws IllegalArgumentException if a required parameter is null or empty + */ + public GitLabApiForm withParam(String name, Map variables, boolean required) throws IllegalArgumentException { + + if (variables == null || variables.isEmpty()) { + if (required) { + throw new IllegalArgumentException(name + " cannot be empty or null"); + } + + return (this); + } + + for (Entry variable : variables.entrySet()) { + Object value = variable.getValue(); + if (value != null) { + this.param(name + "[][key]", variable.getKey()); + this.param(name + "[][value]", value.toString()); + } + } + + return (this); + } + /** * Fluent method for adding query and form parameters to a get() or post() call. * If required is true and value is null, will throw an IllegalArgumentException. diff --git a/src/main/java/org/gitlab4j/api/PipelineApi.java b/src/main/java/org/gitlab4j/api/PipelineApi.java index 6210da85e35fb0c110603ab8d37acd843c859adf..9b759d0455f8a5be1cda46d36060eb7f9b48f2d2 100644 --- a/src/main/java/org/gitlab4j/api/PipelineApi.java +++ b/src/main/java/org/gitlab4j/api/PipelineApi.java @@ -1,6 +1,7 @@ package org.gitlab4j.api; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Stream; @@ -224,7 +225,25 @@ public class PipelineApi extends AbstractApi implements Constants { * @throws GitLabApiException if any exception occurs during execution */ public Pipeline createPipeline(Object projectIdOrPath, String ref) throws GitLabApiException { - GitLabApiForm formData = new GitLabApiForm().withParam("ref", ref); + return (createPipeline(projectIdOrPath, ref, null)); + } + + /** + * Create a pipelines in a project. + * + *
GitLab Endpoint: POST /projects/:id/pipeline
+ * + * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance + * @param ref reference to commit + * @param variables a Map containing the variables available in the pipeline + * @return a Pipeline instance with the newly created pipeline info + * @throws GitLabApiException if any exception occurs during execution + */ + public Pipeline createPipeline(Object projectIdOrPath, String ref, Map variables) throws GitLabApiException { + + GitLabApiForm formData = new GitLabApiForm() + .withParam("ref", ref, true) + .withParam("variables", variables, false); Response response = post(Response.Status.CREATED, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline"); return (response.readEntity(Pipeline.class)); }