Skip to content

Commit 25b05ef

Browse files
committed
Mods to add getPipelineVariables() support (#431).
1 parent f0cf47f commit 25b05ef

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,4 +752,48 @@ public Pipeline triggerPipeline(Object projectIdOrPath, String token, String ref
752752
"projects", getProjectIdOrPath(projectIdOrPath), "trigger", "pipeline");
753753
return (response.readEntity(Pipeline.class));
754754
}
755+
756+
/**
757+
* Get List of variables of a pipeline.
758+
*
759+
* <pre><code>GET /projects/:id/pipelines/:pipeline_id/variables</code></pre>
760+
*
761+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
762+
* @param pipelineId the pipeline ID
763+
* @return a List of pipeline variables
764+
* @throws GitLabApiException if any exception occurs
765+
*/
766+
public List<Variable> getPipelineVariables(Object projectIdOrPath, Integer pipelineId) throws GitLabApiException {
767+
return (getPipelineVariables(projectIdOrPath, pipelineId, getDefaultPerPage()).all());
768+
}
769+
770+
/**
771+
* Get a Pager of variables of a pipeline.
772+
*
773+
* <pre><code>GET /projects/:id/pipelines/:pipeline_id/variables</code></pre>
774+
*
775+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
776+
* @param pipelineId the pipeline ID
777+
* @param itemsPerPage the number of Pipeline instances that will be fetched per page
778+
* @return a Pager of pipeline variables
779+
* @throws GitLabApiException if any exception occurs
780+
*/
781+
public Pager<Variable> getPipelineVariables(Object projectIdOrPath, Integer pipelineId, int itemsPerPage) throws GitLabApiException {
782+
return (new Pager<Variable>(this, Variable.class, itemsPerPage, null,
783+
"projects", getProjectIdOrPath(projectIdOrPath), "pipelines", pipelineId, "variables"));
784+
}
785+
786+
/**
787+
* Get a Stream of variables of a pipeline as a Stream.
788+
*
789+
* <pre><code>GET /projects/:id/pipelines/:pipeline_id/variables</code></pre>
790+
*
791+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
792+
* @param pipelineId the pipeline ID
793+
* @return a Stream of pipeline variables
794+
* @throws GitLabApiException if any exception occurs
795+
*/
796+
public Stream<Variable> getPipelineVariablesStream(Object projectIdOrPath, Integer pipelineId) throws GitLabApiException {
797+
return (getPipelineVariables(projectIdOrPath, pipelineId, getDefaultPerPage()).stream());
798+
}
755799
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ public void testPipelineSchedule() throws Exception {
312312
assertTrue(compareJson(pipelineSchedule, "pipeline-schedule.json"));
313313
}
314314

315+
@Test
316+
public void testPipelineVariables() throws Exception {
317+
List<Variable> variables = unmarshalResourceList(Variable.class, "pipeline-variables.json");
318+
assertTrue(compareJson(variables, "pipeline-variables.json"));
319+
}
320+
315321
@Test
316322
public void testProjectVariables() throws Exception {
317323
List<Variable> variables = unmarshalResourceList(Variable.class, "project-variables.json");

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

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

33
import static java.util.stream.Collectors.toList;
4+
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertNotNull;
67
import static org.junit.Assert.assertTrue;
@@ -290,4 +291,41 @@ public void testCreatePipelineWithMapVariables() throws GitLabApiException {
290291

291292
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
292293
}
294+
295+
@Test
296+
public void testPipelineVariables() throws GitLabApiException {
297+
298+
// Skip this test if no .gitlab-ci.yml file is in the test project
299+
assumeNotNull(gitlabCiYml);
300+
301+
// Arrange
302+
Map<String, String> variableMap = new HashMap<>();
303+
variableMap.put("VAR1", "value1");
304+
variableMap.put("VAR2", "value2");
305+
306+
// Act
307+
Pipeline pipeline = gitLabApi.getPipelineApi().createPipeline(testProject, "master", variableMap);
308+
309+
// Assert
310+
assertNotNull(pipeline);
311+
312+
try {
313+
314+
Stream<Variable> stream = gitLabApi.getPipelineApi().getPipelineVariablesStream(testProject, pipeline.getId());
315+
stream.forEach(v -> {
316+
String value = variableMap.get(v.getKey());
317+
assertEquals(value, v.getValue());
318+
});
319+
320+
List<Variable> variables = gitLabApi.getPipelineApi().getPipelineVariables(testProject, pipeline.getId());
321+
assertEquals(variableMap.size(), variables.size());
322+
variables.forEach(v -> {
323+
String value = variableMap.get(v.getKey());
324+
assertEquals(value, v.getValue());
325+
});
326+
327+
} finally {
328+
gitLabApi.getPipelineApi().deletePipeline(testProject, pipeline.getId());
329+
}
330+
}
293331
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"key": "RUN_NIGHTLY_BUILD",
4+
"variable_type": "env_var",
5+
"value": "true"
6+
},
7+
{
8+
"key": "foo",
9+
"value": "bar"
10+
}
11+
]

0 commit comments

Comments
 (0)