Skip to content

Updated addVariable() and updateVariable() #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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