Skip to content

Commit 31c4219

Browse files
authored
Merge pull request #133 from EdwinHoksberg/buildVariables
Add support for build variables
2 parents 6fb0b9c + 4b72e72 commit 31c4219

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

lib/Gitlab/Api/Projects.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,60 @@ public function removeService($project_id, $service_name)
417417
{
418418
return $this->delete($this->getProjectPath($project_id, 'services/'.$this->encodePath($service_name)));
419419
}
420+
421+
/**
422+
* @param int $project_id
423+
* @return mixed
424+
*/
425+
public function variables($project_id)
426+
{
427+
return $this->get($this->getProjectPath($project_id, 'variables'));
428+
}
429+
430+
/**
431+
* @param int $project_id
432+
* @param string $key
433+
* @return mixed
434+
*/
435+
public function variable($project_id, $key)
436+
{
437+
return $this->get($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)));
438+
}
439+
440+
/**
441+
* @param int $project_id
442+
* @param string $key
443+
* @param string $value
444+
* @return mixed
445+
*/
446+
public function addVariable($project_id, $key, $value)
447+
{
448+
return $this->post($this->getProjectPath($project_id, 'variables'), array(
449+
'key' => $key,
450+
'value' => $value
451+
));
452+
}
453+
454+
/**
455+
* @param int $project_id
456+
* @param string $key
457+
* @param string $value
458+
* @return mixed
459+
*/
460+
public function updateVariable($project_id, $key, $value)
461+
{
462+
return $this->put($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)), array(
463+
'value' => $value,
464+
));
465+
}
466+
467+
/**
468+
* @param int $project_id
469+
* @param string $key
470+
* @return mixed
471+
*/
472+
public function removeVariable($project_id, $key)
473+
{
474+
return $this->delete($this->getProjectPath($project_id, 'variables/'.$this->encodePath($key)));
475+
}
420476
}

test/Gitlab/Tests/Api/ProjectsTest.php

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function shouldGetBuild()
254254

255255
$this->assertEquals($expectedArray, $api->build(1, 2));
256256
}
257-
257+
258258
/**
259259
* @test
260260
*/
@@ -757,6 +757,106 @@ public function shouldRemoveService()
757757
$this->assertEquals($expectedBool, $api->removeService(1, 'hipchat'));
758758
}
759759

760+
/**
761+
* @test
762+
*/
763+
public function shouldGetVariables()
764+
{
765+
$expectedArray = array(
766+
array('key' => 'ftp_username', 'value' => 'ftp'),
767+
array('key' => 'ftp_password', 'value' => 'somepassword')
768+
);
769+
770+
$api = $this->getApiMock();
771+
$api->expects($this->once())
772+
->method('get')
773+
->with('projects/1/variables')
774+
->will($this->returnValue($expectedArray))
775+
;
776+
777+
$this->assertEquals($expectedArray, $api->variables(1));
778+
}
779+
780+
/**
781+
* @test
782+
*/
783+
public function shouldGetVariable()
784+
{
785+
$expectedArray = array('key' => 'ftp_username', 'value' => 'ftp');
786+
787+
$api = $this->getApiMock();
788+
$api->expects($this->once())
789+
->method('get')
790+
->with('projects/1/variables/ftp_username')
791+
->will($this->returnValue($expectedArray))
792+
;
793+
794+
$this->assertEquals($expectedArray, $api->variable(1, 'ftp_username'));
795+
}
796+
797+
/**
798+
* @test
799+
*/
800+
public function shouldAddVariable()
801+
{
802+
$expectedKey = 'ftp_port';
803+
$expectedValue = '21';
804+
805+
$expectedArray = array(
806+
'key' => $expectedKey,
807+
'value' => $expectedValue,
808+
);
809+
810+
$api = $this->getApiMock();
811+
$api->expects($this->once())
812+
->method('post')
813+
->with('projects/1/variables', $expectedArray)
814+
->will($this->returnValue($expectedArray))
815+
;
816+
817+
$this->assertEquals($expectedArray, $api->addVariable(1, $expectedKey, $expectedValue));
818+
}
819+
820+
/**
821+
* @test
822+
*/
823+
public function shouldUpdateVariable()
824+
{
825+
$expectedKey = 'ftp_port';
826+
$expectedValue = '22';
827+
828+
$expectedArray = array(
829+
'key' => 'ftp_port',
830+
'value' => '22',
831+
);
832+
833+
$api = $this->getApiMock();
834+
$api->expects($this->once())
835+
->method('put')
836+
->with('projects/1/variables/'.$expectedKey, array('value' => $expectedValue))
837+
->will($this->returnValue($expectedArray))
838+
;
839+
840+
$this->assertEquals($expectedArray, $api->updateVariable(1, $expectedKey, $expectedValue));
841+
}
842+
843+
/**
844+
* @test
845+
*/
846+
public function shouldRemoveVariable()
847+
{
848+
$expectedBool = true;
849+
850+
$api = $this->getApiMock();
851+
$api->expects($this->once())
852+
->method('delete')
853+
->with('projects/1/variables/ftp_password')
854+
->will($this->returnValue($expectedBool))
855+
;
856+
857+
$this->assertEquals($expectedBool, $api->removeVariable(1, 'ftp_password'));
858+
}
859+
760860
protected function getMultipleProjectsRequestMock($path, $expectedArray = array(), $page = 1, $per_page = 20, $order_by = 'created_at', $sort = 'asc')
761861
{
762862
$api = $this->getApiMock();

0 commit comments

Comments
 (0)