@@ -232,7 +232,7 @@ public Pipeline getPipeline(Object projectIdOrPath, int pipelineId) throws GitLa
232232 * @throws GitLabApiException if any exception occurs during execution
233233 */
234234 public Pipeline createPipeline (Object projectIdOrPath , String ref ) throws GitLabApiException {
235- return (createPipeline (projectIdOrPath , ref , null ));
235+ return (createPipeline (projectIdOrPath , ref , Variable . convertMapToList ( null ) ));
236236 }
237237
238238 /**
@@ -247,11 +247,47 @@ public Pipeline createPipeline(Object projectIdOrPath, String ref) throws GitLab
247247 * @throws GitLabApiException if any exception occurs during execution
248248 */
249249 public Pipeline createPipeline (Object projectIdOrPath , String ref , Map <String , String > variables ) throws GitLabApiException {
250+ return (createPipeline (projectIdOrPath , ref , Variable .convertMapToList (variables )));
251+ }
250252
251- GitLabApiForm formData = new GitLabApiForm ()
252- .withParam ("ref" , ref , true )
253- .withParam ("variables" , variables , false );
254- Response response = post (Response .Status .CREATED , formData .asMap (), "projects" , getProjectIdOrPath (projectIdOrPath ), "pipeline" );
253+ /**
254+ * Create a pipelines in a project.
255+ *
256+ * <pre><code>GitLab Endpoint: POST /projects/:id/pipeline</code></pre>
257+ *
258+ * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
259+ * @param ref reference to commit
260+ * @param variables a Map containing the variables available in the pipeline
261+ * @return a Pipeline instance with the newly created pipeline info
262+ * @throws GitLabApiException if any exception occurs during execution
263+ */
264+ public Pipeline createPipeline (Object projectIdOrPath , String ref , List <Variable > variables ) throws GitLabApiException {
265+
266+ if (ref == null || ref .trim ().isEmpty ()) {
267+ throw new GitLabApiException ("ref cannot be null or empty" );
268+ }
269+
270+ if (variables == null || variables .isEmpty ()) {
271+ GitLabApiForm formData = new GitLabApiForm ().withParam ("ref" , ref , true );
272+ Response response = post (Response .Status .CREATED , formData , "projects" , getProjectIdOrPath (projectIdOrPath ), "pipeline" );
273+ return (response .readEntity (Pipeline .class ));
274+ }
275+
276+ // The create pipeline REST API expects the variable data in an unusual format, this
277+ // class is used to create the JSON for the POST data.
278+ class CreatePipelineForm {
279+ @ SuppressWarnings ("unused" )
280+ public String ref ;
281+ @ SuppressWarnings ("unused" )
282+ public List <Variable > variables ;
283+ CreatePipelineForm (String ref , List <Variable > variables ) {
284+ this .ref = ref ;
285+ this .variables = variables ;
286+ }
287+ }
288+
289+ CreatePipelineForm pipelineForm = new CreatePipelineForm (ref , variables );
290+ Response response = post (Response .Status .CREATED , pipelineForm , "projects" , getProjectIdOrPath (projectIdOrPath ), "pipeline" );
255291 return (response .readEntity (Pipeline .class ));
256292 }
257293
0 commit comments