Skip to content

Commit 74588bc

Browse files
committed
Added createPipeline() method that takes the variables param (#321).
1 parent 87ea8f6 commit 74588bc

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/main/java/org/gitlab4j/api/GitLabApiForm.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Date;
44
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Map.Entry;
57

68
import javax.ws.rs.core.Form;
79
import javax.ws.rs.core.MultivaluedHashMap;
@@ -134,6 +136,36 @@ public <T> GitLabApiForm withParam(String name, List<T> values, boolean required
134136
return (this);
135137
}
136138

139+
/**
140+
* Fluent method for adding an array of hash type query and form parameters to a get() or post() call.
141+
*
142+
* @param name the name of the field/attribute to add
143+
* @param variables a Map containing array of hashes
144+
* @param required the field is required flag
145+
* @return this GitLabAPiForm instance
146+
* @throws IllegalArgumentException if a required parameter is null or empty
147+
*/
148+
public GitLabApiForm withParam(String name, Map<String, ?> variables, boolean required) throws IllegalArgumentException {
149+
150+
if (variables == null || variables.isEmpty()) {
151+
if (required) {
152+
throw new IllegalArgumentException(name + " cannot be empty or null");
153+
}
154+
155+
return (this);
156+
}
157+
158+
for (Entry<String, ?> variable : variables.entrySet()) {
159+
Object value = variable.getValue();
160+
if (value != null) {
161+
this.param(name + "[][key]", variable.getKey());
162+
this.param(name + "[][value]", value.toString());
163+
}
164+
}
165+
166+
return (this);
167+
}
168+
137169
/**
138170
* Fluent method for adding query and form parameters to a get() or post() call.
139171
* If required is true and value is null, will throw an IllegalArgumentException.

src/main/java/org/gitlab4j/api/PipelineApi.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.util.List;
4+
import java.util.Map;
45
import java.util.Optional;
56
import java.util.stream.Stream;
67

@@ -224,7 +225,25 @@ public Pipeline getPipeline(Object projectIdOrPath, int pipelineId) throws GitLa
224225
* @throws GitLabApiException if any exception occurs during execution
225226
*/
226227
public Pipeline createPipeline(Object projectIdOrPath, String ref) throws GitLabApiException {
227-
GitLabApiForm formData = new GitLabApiForm().withParam("ref", ref);
228+
return (createPipeline(projectIdOrPath, ref, null));
229+
}
230+
231+
/**
232+
* Create a pipelines in a project.
233+
*
234+
* <pre><code>GitLab Endpoint: POST /projects/:id/pipeline</code></pre>
235+
*
236+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
237+
* @param ref reference to commit
238+
* @param variables a Map containing the variables available in the pipeline
239+
* @return a Pipeline instance with the newly created pipeline info
240+
* @throws GitLabApiException if any exception occurs during execution
241+
*/
242+
public Pipeline createPipeline(Object projectIdOrPath, String ref, Map<String, String> variables) throws GitLabApiException {
243+
244+
GitLabApiForm formData = new GitLabApiForm()
245+
.withParam("ref", ref, true)
246+
.withParam("variables", variables, false);
228247
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline");
229248
return (response.readEntity(Pipeline.class));
230249
}

0 commit comments

Comments
 (0)