Skip to content

Commit

Permalink
Merge pull request #251 from c33s/feature/variables-enhancement
Browse files Browse the repository at this point in the history
Updated addVariable() and updateVariable()
  • Loading branch information
fbourigault committed Sep 29, 2017
2 parents 08acb86 + cadc605 commit e37b78f
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 7 deletions.
38 changes: 31 additions & 7 deletions lib/Gitlab/Api/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -564,27 +564,51 @@ public function variable($project_id, $key)
* @param int $project_id
* @param string $key
* @param string $value
* @param bool $protected
* @param string $environment_scope
* @return mixed
*/
public function addVariable($project_id, $key, $value)
public function addVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
{
return $this->post($this->getProjectPath($project_id, 'variables'), array(
$payload = array(
'key' => $key,
'value' => $value
));
'value' => $value,
);

if ($protected) {
$payload['protected'] = $protected;
}

if ($environment_scope) {
$payload['environment_scope'] = $environment_scope;
}

return $this->post($this->getProjectPath($project_id, 'variables'), $payload);
}

/**
* @param int $project_id
* @param string $key
* @param string $value
* @param bool $protected
* @param string $environment_scope
* @return mixed
*/
public function updateVariable($project_id, $key, $value)
public function updateVariable($project_id, $key, $value, $protected = null, $environment_scope = null)
{
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), array(
$payload = array(
'value' => $value,
));
);

if ($protected) {
$payload['protected'] = $protected;
}

if ($environment_scope) {
$payload['environment_scope'] = $environment_scope;
}

return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), $payload);
}

/**
Expand Down
128 changes: 128 additions & 0 deletions test/Gitlab/Tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,70 @@ public function shouldAddVariable()
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue));
}

/**
* @test
*/
public function shouldAddVariableWithProtected()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('projects/1/variables', $expectedArray)
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
}

/**
* @test
*/
public function shouldAddVariableWithEnvironment()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('projects/1/variables', $expectedArray)
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
}

/**
* @test
*/
public function shouldAddVariableWithProtectionAndEnvironment()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('projects/1/variables', $expectedArray)
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->addVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
}

/**
* @test
*/
Expand All @@ -888,6 +952,70 @@ public function shouldUpdateVariable()
$this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue));
}

/**
* @test
*/
public function shouldUpdateVariableWithProtected()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true))
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true));
}

/**
* @test
*/
public function shouldUpdateVariableWithEnvironment()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'environment_scope' => 'staging'))
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', null, 'staging'));
}

/**
* @test
*/
public function shouldUpdateVariableWithProtectedAndEnvironment()
{
$expectedArray = array(
'key' => 'DEPLOY_SERVER',
'value' => 'stage.example.com',
'protected' => true,
'environment_scope' => 'staging',
);

$api = $this->getApiMock();
$api->expects($this->once())
->method('put')
->with('projects/1/variables/DEPLOY_SERVER', array('value' => 'stage.example.com', 'protected' => true, 'environment_scope' => 'staging'))
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->updateVariable(1, 'DEPLOY_SERVER', 'stage.example.com', true, 'staging'));
}

/**
* @test
*/
Expand Down

0 comments on commit e37b78f

Please sign in to comment.