Skip to content

Commit 17e83fe

Browse files
committed
Fixed issue with variables when creating a pipeline (#354).
1 parent 22bffd0 commit 17e83fe

File tree

3 files changed

+123
-5
lines changed

3 files changed

+123
-5
lines changed

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/org/gitlab4j/api/models/Variable.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package org.gitlab4j.api.models;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Map;
6+
37
import org.gitlab4j.api.utils.JacksonJson;
48

59
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -12,6 +16,14 @@ public class Variable {
1216
private Boolean isProtected;
1317
private String environmentScope;
1418

19+
public Variable() {
20+
}
21+
22+
public Variable(String key, String value) {
23+
this.key = key;
24+
this.value = value;
25+
}
26+
1527
public String getKey() {
1628
return key;
1729
}
@@ -48,4 +60,21 @@ public void setEnvironmentScope(String environmentScope) {
4860
public String toString() {
4961
return (JacksonJson.toJsonString(this));
5062
}
63+
64+
/**
65+
* Create a List of Variable from the provided Map.
66+
*
67+
* @param variables the Map to convert to a List of Variable
68+
* @return the List of Variable containing the keys and values from the Map, or null if the Map is null
69+
*/
70+
public static final List<Variable> convertMapToList(Map<String, String> variables) {
71+
72+
if (variables == null) {
73+
return null;
74+
}
75+
76+
List<Variable> varList = new ArrayList<>(variables.size());
77+
variables.forEach((k, v) -> varList.add(new Variable(k, v)));
78+
return varList;
79+
}
5180
}

src/test/java/org/gitlab4j/api/TestPipelineApi.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
import static org.junit.Assert.assertTrue;
77
import static org.junit.Assume.assumeNotNull;
88

9+
import java.util.ArrayList;
10+
import java.util.HashMap;
911
import java.util.List;
12+
import java.util.Map;
1013
import java.util.stream.Stream;
1114

1215
import org.gitlab4j.api.models.Pipeline;
1316
import org.gitlab4j.api.models.PipelineSchedule;
1417
import org.gitlab4j.api.models.Project;
1518
import org.gitlab4j.api.models.RepositoryFile;
1619
import org.gitlab4j.api.models.Trigger;
20+
import org.gitlab4j.api.models.Variable;
1721
import org.junit.AfterClass;
1822
import org.junit.Before;
1923
import org.junit.BeforeClass;
@@ -205,4 +209,53 @@ public void testTriggerAndCancelPipeline() throws GitLabApiException {
205209
assertNotNull(pipelineTriggers);
206210
assertFalse(pipelineTriggers.map(Trigger::getDescription).collect(toList()).contains(triggerDescription));
207211
}
212+
213+
@Test
214+
public void testCreatePipelineNoVariables() throws GitLabApiException {
215+
assumeNotNull(testProject);
216+
217+
// Act
218+
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master");
219+
220+
// Assert
221+
assertNotNull(pipeline);
222+
223+
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
224+
}
225+
226+
@Test
227+
public void testCreatePipelineWithVariables() throws GitLabApiException {
228+
assumeNotNull(testProject);
229+
230+
// Arrange
231+
List<Variable> variableList = new ArrayList<>();
232+
variableList.add(new Variable("VAR1", "value1"));
233+
variableList.add(new Variable("VAR2", "value2"));
234+
235+
// Act
236+
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master", variableList);
237+
238+
// Assert
239+
assertNotNull(pipeline);
240+
241+
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
242+
}
243+
244+
@Test
245+
public void testCreatePipelineWithMapVariables() throws GitLabApiException {
246+
assumeNotNull(testProject);
247+
248+
// Arrange
249+
Map<String, String> variableMap = new HashMap<>();
250+
variableMap.put("VAR1", "value1");
251+
variableMap.put("VAR2", "value2");
252+
253+
// Act
254+
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master", variableMap);
255+
256+
// Assert
257+
assertNotNull(pipeline);
258+
259+
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
260+
}
208261
}

0 commit comments

Comments
 (0)