Skip to content

Add deploy keys related API #146

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 2 commits into from
Sep 19, 2016
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
24 changes: 24 additions & 0 deletions lib/Gitlab/Api/DeployKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace Gitlab\Api;

class DeployKeys extends AbstractApi
{
const ORDER_BY = 'id';
const SORT = 'asc';

/**
* @param int $page
* @param int $per_page
* @param string $order_by
* @param string $sort
* @return mixed
*/
public function all($page = 1, $per_page = self::PER_PAGE, $order_by = self::ORDER_BY, $sort = self::SORT)
{
return $this->get('deploy_keys', array(
'page' => $page,
'per_page' => $per_page,
'order_by' => $order_by,
'sort' => $sort
));
}
}
20 changes: 20 additions & 0 deletions lib/Gitlab/Api/Projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ public function removeKey($project_id, $key_id)
return $this->delete($this->getProjectPath($project_id, 'keys/'.$this->encodePath($key_id)));
}

/**
* @param int $project_id
* @param int $key_id
* @return mixed
*/
public function enableKey($project_id, $key_id)
{
return $this->post($this->getProjectPath($project_id, 'keys/'.$this->encodePath($key_id).'/enable'));
}

/**
* @param int $project_id
* @param int $key_id
* @return mixed
*/
public function disableKey($project_id, $key_id)
{
return $this->delete($this->getProjectPath($project_id, 'keys/'.$this->encodePath($key_id).'/disable'));
}

/**
* @param int $project_id
* @param int $page
Expand Down
4 changes: 4 additions & 0 deletions lib/Gitlab/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public function api($name)
{
switch ($name) {

case 'deploy_keys':
$api = new Api\DeployKeys($this);
break;

case 'groups':
$api = new Api\Groups($this);
break;
Expand Down
22 changes: 22 additions & 0 deletions lib/Gitlab/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,28 @@ public function removeKey($key_id)
return true;
}

/**
* @param string $key_id
* @return bool
*/
public function enableKey($key_id)
{
$this->api('projects')->enableKey($this->id, $key_id);

return true;
}

/**
* @param string $key_id
* @return bool
*/
public function disableKey($key_id)
{
$this->api('projects')->disableKey($this->id, $key_id);

return true;
}

/**
* @param string $name
* @param string $ref
Expand Down
51 changes: 51 additions & 0 deletions test/Gitlab/Tests/Api/DeployKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php namespace Gitlab\Tests\Api;

class DeployKeysTest extends ApiTestCase
{
/**
* @test
*/
public function shouldGetAllDeployKeys()
{
$expectedArray = $this->getMultipleDeployKeysData();

$api = $this->getMultipleDeployKeysRequestMock('deploy_keys', $expectedArray);

$this->assertEquals($expectedArray, $api->all());
}

protected function getMultipleDeployKeysRequestMock($path, $expectedArray = array(), $page = 1, $per_page = 20, $order_by = 'id', $sort = 'asc')
{
$api = $this->getApiMock();
$api->expects($this->once())
->method('get')
->with($path, array('page' => $page, 'per_page' => $per_page, 'order_by' => $order_by, 'sort' => $sort))
->will($this->returnValue($expectedArray))
;

return $api;
}

protected function getMultipleDeployKeysData()
{
return array(
array(
'id' => 1,
'title' => 'Public key',
'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
'created_at' => '2013-10-02T10:12:29Z'
),
array(
'id' => 3,
'title' => 'Another Public key',
'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
'created_at' => '2013-10-02T11:12:29Z'
)
);
}

protected function getApiClass()
{
return 'Gitlab\Api\DeployKeys';
}
}
34 changes: 34 additions & 0 deletions test/Gitlab/Tests/Api/ProjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,40 @@ public function shouldRemoveKey()
$this->assertEquals($expectedBool, $api->removeKey(1, 3));
}

/**
* @test
*/
public function shoudEnableKey()
{
$expectedBool = true;

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('projects/1/keys/3/enable')
->will($this->returnValue($expectedBool))
;

$this->assertEquals($expectedBool, $api->enableKey(1, 3));
}

/**
* @test
*/
public function shoudDisableKey()
{
$expectedBool = true;

$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('projects/1/keys/3/disable')
->will($this->returnValue($expectedBool))
;

$this->assertEquals($expectedBool, $api->disableKey(1, 3));
}

/**
* @test
*/
Expand Down