Skip to content

Commit e37b78f

Browse files
authored
Merge pull request #251 from c33s/feature/variables-enhancement
Updated addVariable() and updateVariable()
2 parents 08acb86 + cadc605 commit e37b78f

File tree

2 files changed

+159
-7
lines changed

2 files changed

+159
-7
lines changed

lib/Gitlab/Api/Projects.php

+31-7
Original file line numberDiff line numberDiff line change
@@ -564,27 +564,51 @@ public function variable($project_id, $key)
564564
* @param int $project_id
565565
* @param string $key
566566
* @param string $value
567+
* @param bool $protected
568+
* @param string $environment_scope
567569
* @return mixed
568570
*/
569-
public function addVariable($project_id, $key, $value)
571+
public function addVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
570572
{
571-
return $this->post($this->getProjectPath($project_id, 'variables'), array(
573+
$payload = array(
572574
'key' => $key,
573-
'value' => $value
574-
));
575+
'value' => $value,
576+
);
577+
578+
if ($protected) {
579+
$payload['protected'] = $protected;
580+
}
581+
582+
if ($environment_scope) {
583+
$payload['environment_scope'] = $environment_scope;
584+
}
585+
586+
return $this->post($this->getProjectPath($project_id, 'variables'), $payload);
575587
}
576588

577589
/**
578590
* @param int $project_id
579591
* @param string $key
580592
* @param string $value
593+
* @param bool $protected
594+
* @param string $environment_scope
581595
* @return mixed
582596
*/
583-
public function updateVariable($project_id, $key, $value)
597+
public function updateVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
584598
{
585-
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), array(
599+
$payload = array(
586600
'value' => $value,
587-
));
601+
);
602+
603+
if ($protected) {
604+
$payload['protected'] = $protected;
605+
}
606+
607+
if ($environment_scope) {
608+
$payload['environment_scope'] = $environment_scope;
609+
}
610+
611+
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), $payload);
588612
}
589613

590614
/**

test/Gitlab/Tests/Api/ProjectsTest.php

+128
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,70 @@ public function shouldAddVariable()
865865
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue));
866866
}
867867

868+
/**
869+
* @test
870+
*/
871+
public function shouldAddVariableWithProtected()
872+
{
873+
$expectedArray = array(
874+
'key' => 'DEPLOY_SERVER',
875+
'value' => 'stage.example.com',
876+
'protected' => true,
877+
);
878+
879+
$api = $this->getApiMock();
880+
$api->expects($this->once())
881+
->method('post')
882+
->with('projects/1/variables', $expectedArray)
883+
->will($this->returnValue($expectedArray))
884+
;
885+
886+
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
887+
}
888+
889+
/**
890+
* @test
891+
*/
892+
public function shouldAddVariableWithEnvironment()
893+
{
894+
$expectedArray = array(
895+
'key' => 'DEPLOY_SERVER',
896+
'value' => 'stage.example.com',
897+
'environment_scope' => 'staging',
898+
);
899+
900+
$api = $this->getApiMock();
901+
$api->expects($this->once())
902+
->method('post')
903+
->with('projects/1/variables', $expectedArray)
904+
->will($this->returnValue($expectedArray))
905+
;
906+
907+
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
908+
}
909+
910+
/**
911+
* @test
912+
*/
913+
public function shouldAddVariableWithProtectionAndEnvironment()
914+
{
915+
$expectedArray = array(
916+
'key' => 'DEPLOY_SERVER',
917+
'value' => 'stage.example.com',
918+
'protected' => true,
919+
'environment_scope' => 'staging',
920+
);
921+
922+
$api = $this->getApiMock();
923+
$api->expects($this->once())
924+
->method('post')
925+
->with('projects/1/variables', $expectedArray)
926+
->will($this->returnValue($expectedArray))
927+
;
928+
929+
$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
930+
}
931+
868932
/**
869933
* @test
870934
*/
@@ -888,6 +952,70 @@ public function shouldUpdateVariable()
888952
$this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue));
889953
}
890954

955+
/**
956+
* @test
957+
*/
958+
public function shouldUpdateVariableWithProtected()
959+
{
960+
$expectedArray = array(
961+
'key' => 'DEPLOY_SERVER',
962+
'value' => 'stage.example.com',
963+
'protected' => true,
964+
);
965+
966+
$api = $this->getApiMock();
967+
$api->expects($this->once())
968+
->method('put')
969+
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true))
970+
->will($this->returnValue($expectedArray))
971+
;
972+
973+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
974+
}
975+
976+
/**
977+
* @test
978+
*/
979+
public function shouldUpdateVariableWithEnvironment()
980+
{
981+
$expectedArray = array(
982+
'key' => 'DEPLOY_SERVER',
983+
'value' => 'stage.example.com',
984+
'environment_scope' => 'staging',
985+
);
986+
987+
$api = $this->getApiMock();
988+
$api->expects($this->once())
989+
->method('put')
990+
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'environment_scope' => 'staging'))
991+
->will($this->returnValue($expectedArray))
992+
;
993+
994+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
995+
}
996+
997+
/**
998+
* @test
999+
*/
1000+
public function shouldUpdateVariableWithProtectedAndEnvironment()
1001+
{
1002+
$expectedArray = array(
1003+
'key' => 'DEPLOY_SERVER',
1004+
'value' => 'stage.example.com',
1005+
'protected' => true,
1006+
'environment_scope' => 'staging',
1007+
);
1008+
1009+
$api = $this->getApiMock();
1010+
$api->expects($this->once())
1011+
->method('put')
1012+
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging'))
1013+
->will($this->returnValue($expectedArray))
1014+
;
1015+
1016+
$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
1017+
}
1018+
8911019
/**
8921020
* @test
8931021
*/

0 commit comments

Comments
 (0)