Commit 17e83fee authored by Greg Messner's avatar Greg Messner
Browse files

Fixed issue with variables when creating a pipeline (#354).

parent 22bffd04
...@@ -232,7 +232,7 @@ public class PipelineApi extends AbstractApi implements Constants { ...@@ -232,7 +232,7 @@ public class PipelineApi extends AbstractApi implements Constants {
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Pipeline createPipeline(Object projectIdOrPath, String ref) throws GitLabApiException { public Pipeline createPipeline(Object projectIdOrPath, String ref) throws GitLabApiException {
return (createPipeline(projectIdOrPath, ref, null)); return (createPipeline(projectIdOrPath, ref, Variable.convertMapToList(null)));
} }
/** /**
...@@ -247,11 +247,47 @@ public class PipelineApi extends AbstractApi implements Constants { ...@@ -247,11 +247,47 @@ public class PipelineApi extends AbstractApi implements Constants {
* @throws GitLabApiException if any exception occurs during execution * @throws GitLabApiException if any exception occurs during execution
*/ */
public Pipeline createPipeline(Object projectIdOrPath, String ref, Map<String, String> variables) throws GitLabApiException { public Pipeline createPipeline(Object projectIdOrPath, String ref, Map<String, String> variables) throws GitLabApiException {
return (createPipeline(projectIdOrPath, ref, Variable.convertMapToList(variables)));
}
GitLabApiForm formData = new GitLabApiForm() /**
.withParam("ref", ref, true) * Create a pipelines in a project.
.withParam("variables", variables, false); *
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "pipeline"); * <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, List<Variable> variables) throws GitLabApiException {
if (ref == null || ref.trim().isEmpty()) {
throw new GitLabApiException("ref cannot be null or empty");
}
if (variables == null || variables.isEmpty()) {
GitLabApiForm formData = new GitLabApiForm().withParam("ref", ref, true);
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline");
return (response.readEntity(Pipeline.class));
}
// The create pipeline REST API expects the variable data in an unusual format, this
// class is used to create the JSON for the POST data.
class CreatePipelineForm {
@SuppressWarnings("unused")
public String ref;
@SuppressWarnings("unused")
public List<Variable> variables;
CreatePipelineForm(String ref, List<Variable> variables) {
this.ref = ref;
this.variables = variables;
}
}
CreatePipelineForm pipelineForm = new CreatePipelineForm(ref, variables);
Response response = post(Response.Status.CREATED, pipelineForm, "projects", getProjectIdOrPath(projectIdOrPath), "pipeline");
return (response.readEntity(Pipeline.class)); return (response.readEntity(Pipeline.class));
} }
......
package org.gitlab4j.api.models; package org.gitlab4j.api.models;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gitlab4j.api.utils.JacksonJson; import org.gitlab4j.api.utils.JacksonJson;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
...@@ -12,6 +16,14 @@ public class Variable { ...@@ -12,6 +16,14 @@ public class Variable {
private Boolean isProtected; private Boolean isProtected;
private String environmentScope; private String environmentScope;
public Variable() {
}
public Variable(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() { public String getKey() {
return key; return key;
} }
...@@ -48,4 +60,21 @@ public class Variable { ...@@ -48,4 +60,21 @@ public class Variable {
public String toString() { public String toString() {
return (JacksonJson.toJsonString(this)); return (JacksonJson.toJsonString(this));
} }
/**
* Create a List of Variable from the provided Map.
*
* @param variables the Map to convert to a List of Variable
* @return the List of Variable containing the keys and values from the Map, or null if the Map is null
*/
public static final List<Variable> convertMapToList(Map<String, String> variables) {
if (variables == null) {
return null;
}
List<Variable> varList = new ArrayList<>(variables.size());
variables.forEach((k, v) -> varList.add(new Variable(k, v)));
return varList;
}
} }
...@@ -6,7 +6,10 @@ import static org.junit.Assert.assertNotNull; ...@@ -6,7 +6,10 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull; import static org.junit.Assume.assumeNotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.gitlab4j.api.models.Pipeline; import org.gitlab4j.api.models.Pipeline;
...@@ -14,6 +17,7 @@ import org.gitlab4j.api.models.PipelineSchedule; ...@@ -14,6 +17,7 @@ import org.gitlab4j.api.models.PipelineSchedule;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile; import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.Trigger; import org.gitlab4j.api.models.Trigger;
import org.gitlab4j.api.models.Variable;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
...@@ -205,4 +209,53 @@ public class TestPipelineApi extends AbstractIntegrationTest { ...@@ -205,4 +209,53 @@ public class TestPipelineApi extends AbstractIntegrationTest {
assertNotNull(pipelineTriggers); assertNotNull(pipelineTriggers);
assertFalse(pipelineTriggers.map(Trigger::getDescription).collect(toList()).contains(triggerDescription)); assertFalse(pipelineTriggers.map(Trigger::getDescription).collect(toList()).contains(triggerDescription));
} }
@Test
public void testCreatePipelineNoVariables() throws GitLabApiException {
assumeNotNull(testProject);
// Act
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master");
// Assert
assertNotNull(pipeline);
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
}
@Test
public void testCreatePipelineWithVariables() throws GitLabApiException {
assumeNotNull(testProject);
// Arrange
List<Variable> variableList = new ArrayList<>();
variableList.add(new Variable("VAR1", "value1"));
variableList.add(new Variable("VAR2", "value2"));
// Act
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master", variableList);
// Assert
assertNotNull(pipeline);
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
}
@Test
public void testCreatePipelineWithMapVariables() throws GitLabApiException {
assumeNotNull(testProject);
// Arrange
Map<String, String> variableMap = new HashMap<>();
variableMap.put("VAR1", "value1");
variableMap.put("VAR2", "value2");
// Act
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master", variableMap);
// Assert
assertNotNull(pipeline);
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
}
} }
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