Skip to content

Commit 8ba69aa

Browse files
jdecoolm1guelpf
authored andcommitted
Add method to update group variable
1 parent 7a02af0 commit 8ba69aa

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/Gitlab/Api/Groups.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,27 @@ public function addVariable($group_id, $key, $value, $protected = null)
264264
return $this->post($this->getGroupPath($group_id, 'variables'), $payload);
265265
}
266266

267+
/**
268+
* @param int $group_id
269+
* @param string $key
270+
* @param string $value
271+
* @param bool $protected
272+
*
273+
* @return mixed
274+
*/
275+
public function updateVariable($group_id, $key, $value, $protected = null)
276+
{
277+
$payload = array(
278+
'value' => $value,
279+
);
280+
281+
if ($protected) {
282+
$payload['protected'] = $protected;
283+
}
284+
285+
return $this->put($this->getGroupPath($group_id, 'variables/'.$this->encodePath($key)), $payload);
286+
}
287+
267288
private function getGroupSearchResolver()
268289
{
269290
$resolver = $this->createOptionsResolver();

test/Gitlab/Tests/Api/GroupsTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,50 @@ public function shouldAddVariableWithProtected()
369369
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
370370
}
371371

372+
/**
373+
* @test
374+
*/
375+
public function shouldUpdateVariable()
376+
{
377+
$expectedKey = 'ftp_port';
378+
$expectedValue = '22';
379+
380+
$expectedArray = array(
381+
'key' => 'ftp_port',
382+
'value' => '22',
383+
);
384+
385+
$api = $this->getApiMock();
386+
$api->expects($this->once())
387+
->method('put')
388+
->with('groups/1/variables/'.$expectedKey, array('value' => $expectedValue))
389+
->will($this->returnValue($expectedArray))
390+
;
391+
392+
$this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue));
393+
}
394+
395+
/**
396+
* @test
397+
*/
398+
public function shouldUpdateVariableWithProtected()
399+
{
400+
$expectedArray = array(
401+
'key' => 'DEPLOY_SERVER',
402+
'value' => 'stage.example.com',
403+
'protected' => true,
404+
);
405+
406+
$api = $this->getApiMock();
407+
$api->expects($this->once())
408+
->method('put')
409+
->with('groups/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true))
410+
->will($this->returnValue($expectedArray))
411+
;
412+
413+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
414+
}
415+
372416
protected function getApiClass()
373417
{
374418
return 'Gitlab\Api\Groups';

0 commit comments

Comments
 (0)