Commit 74588bc7 authored by Greg Messner's avatar Greg Messner
Browse files

Added createPipeline() method that takes the variables param (#321).

parent 87ea8f6d
......@@ -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<String, ?> 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<String, ?> 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.
......
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.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/pipeline</code></pre>
*
* @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<String, String> 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));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment