Skip to content

Commit 7a02af0

Browse files
jdecoolm1guelpf
authored andcommitted
Add method to create group variable
1 parent 7474433 commit 7a02af0

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

lib/Gitlab/Api/Groups.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,28 @@ public function variable($group_id, $key)
242242
return $this->get($this->getGroupPath($group_id, 'variables/'.$this->encodePath($key)));
243243
}
244244

245+
/**
246+
* @param int $group_id
247+
* @param string $key
248+
* @param string $value
249+
* @param bool $protected
250+
*
251+
* @return mixed
252+
*/
253+
public function addVariable($group_id, $key, $value, $protected = null)
254+
{
255+
$payload = array(
256+
'key' => $key,
257+
'value' => $value,
258+
);
259+
260+
if ($protected) {
261+
$payload['protected'] = $protected;
262+
}
263+
264+
return $this->post($this->getGroupPath($group_id, 'variables'), $payload);
265+
}
266+
245267
private function getGroupSearchResolver()
246268
{
247269
$resolver = $this->createOptionsResolver();

test/Gitlab/Tests/Api/GroupsTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,47 @@ public function shouldGetVariable()
328328
$this->assertEquals($expectedArray, $api->variable(1, 'ftp_username'));
329329
}
330330

331+
public function shouldAddVariable()
332+
{
333+
$expectedKey = 'ftp_port';
334+
$expectedValue = '21';
335+
336+
$expectedArray = array(
337+
'key' => $expectedKey,
338+
'value' => $expectedValue,
339+
);
340+
341+
$api = $this->getApiMock();
342+
$api->expects($this->once())
343+
->method('post')
344+
->with('groups/1/variables', $expectedArray)
345+
->will($this->returnValue($expectedArray))
346+
;
347+
348+
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue));
349+
}
350+
351+
/**
352+
* @test
353+
*/
354+
public function shouldAddVariableWithProtected()
355+
{
356+
$expectedArray = array(
357+
'key' => 'DEPLOY_SERVER',
358+
'value' => 'stage.example.com',
359+
'protected' => true,
360+
);
361+
362+
$api = $this->getApiMock();
363+
$api->expects($this->once())
364+
->method('post')
365+
->with('groups/1/variables', $expectedArray)
366+
->will($this->returnValue($expectedArray))
367+
;
368+
369+
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
370+
}
371+
331372
protected function getApiClass()
332373
{
333374
return 'Gitlab\Api\Groups';

0 commit comments

Comments
 (0)