From 74588bc739c2507097650d528419e1a6cb8f41d7 Mon Sep 17 00:00:00 2001 From: Greg Messner Date: Tue, 19 Mar 2019 23:46:17 -0700 Subject: [PATCH] Added createPipeline() method that takes the variables param (#321). --- .../java/org/gitlab4j/api/GitLabApiForm.java | 32 +++++++++++++++++++ .../java/org/gitlab4j/api/PipelineApi.java | 21 +++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/gitlab4j/api/GitLabApiForm.java b/src/main/java/org/gitlab4j/api/GitLabApiForm.java index 9c0dd589..6693e320 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 6210da85..9b759d04 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)); } -- GitLab