Skip to content

Commit d57d5ad

Browse files
jdecoolradutopala
authored andcommitted
Add deploy keys related API (#146)
* Add API to retrieve all deploy keys * Add method to enable/disable a key for a project
1 parent 041a823 commit d57d5ad

File tree

6 files changed

+155
-0
lines changed

6 files changed

+155
-0
lines changed

Diff for: lib/Gitlab/Api/DeployKeys.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php namespace Gitlab\Api;
2+
3+
class DeployKeys extends AbstractApi
4+
{
5+
const ORDER_BY = 'id';
6+
const SORT = 'asc';
7+
8+
/**
9+
* @param int $page
10+
* @param int $per_page
11+
* @param string $order_by
12+
* @param string $sort
13+
* @return mixed
14+
*/
15+
public function all($page = 1, $per_page = self::PER_PAGE, $order_by = self::ORDER_BY, $sort = self::SORT)
16+
{
17+
return $this->get('deploy_keys', array(
18+
'page' => $page,
19+
'per_page' => $per_page,
20+
'order_by' => $order_by,
21+
'sort' => $sort
22+
));
23+
}
24+
}

Diff for: lib/Gitlab/Api/Projects.php

+20
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,26 @@ public function removeKey($project_id, $key_id)
339339
return $this->delete($this->getProjectPath($project_id, 'keys/'.$this->encodePath($key_id)));
340340
}
341341

342+
/**
343+
* @param int $project_id
344+
* @param int $key_id
345+
* @return mixed
346+
*/
347+
public function enableKey($project_id, $key_id)
348+
{
349+
return $this->post($this->getProjectPath($project_id, 'keys/'.$this->encodePath($key_id).'/enable'));
350+
}
351+
352+
/**
353+
* @param int $project_id
354+
* @param int $key_id
355+
* @return mixed
356+
*/
357+
public function disableKey($project_id, $key_id)
358+
{
359+
return $this->delete($this->getProjectPath($project_id, 'keys/'.$this->encodePath($key_id).'/disable'));
360+
}
361+
342362
/**
343363
* @param int $project_id
344364
* @param int $page

Diff for: lib/Gitlab/Client.php

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public function api($name)
101101
{
102102
switch ($name) {
103103

104+
case 'deploy_keys':
105+
$api = new Api\DeployKeys($this);
106+
break;
107+
104108
case 'groups':
105109
$api = new Api\Groups($this);
106110
break;

Diff for: lib/Gitlab/Model/Project.php

+22
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,28 @@ public function removeKey($key_id)
346346
return true;
347347
}
348348

349+
/**
350+
* @param string $key_id
351+
* @return bool
352+
*/
353+
public function enableKey($key_id)
354+
{
355+
$this->api('projects')->enableKey($this->id, $key_id);
356+
357+
return true;
358+
}
359+
360+
/**
361+
* @param string $key_id
362+
* @return bool
363+
*/
364+
public function disableKey($key_id)
365+
{
366+
$this->api('projects')->disableKey($this->id, $key_id);
367+
368+
return true;
369+
}
370+
349371
/**
350372
* @param string $name
351373
* @param string $ref

Diff for: test/Gitlab/Tests/Api/DeployKeysTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace Gitlab\Tests\Api;
2+
3+
class DeployKeysTest extends ApiTestCase
4+
{
5+
/**
6+
* @test
7+
*/
8+
public function shouldGetAllDeployKeys()
9+
{
10+
$expectedArray = $this->getMultipleDeployKeysData();
11+
12+
$api = $this->getMultipleDeployKeysRequestMock('deploy_keys', $expectedArray);
13+
14+
$this->assertEquals($expectedArray, $api->all());
15+
}
16+
17+
protected function getMultipleDeployKeysRequestMock($path, $expectedArray = array(), $page = 1, $per_page = 20, $order_by = 'id', $sort = 'asc')
18+
{
19+
$api = $this->getApiMock();
20+
$api->expects($this->once())
21+
->method('get')
22+
->with($path, array('page' => $page, 'per_page' => $per_page, 'order_by' => $order_by, 'sort' => $sort))
23+
->will($this->returnValue($expectedArray))
24+
;
25+
26+
return $api;
27+
}
28+
29+
protected function getMultipleDeployKeysData()
30+
{
31+
return array(
32+
array(
33+
'id' => 1,
34+
'title' => 'Public key',
35+
'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
36+
'created_at' => '2013-10-02T10:12:29Z'
37+
),
38+
array(
39+
'id' => 3,
40+
'title' => 'Another Public key',
41+
'key' => 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
42+
'created_at' => '2013-10-02T11:12:29Z'
43+
)
44+
);
45+
}
46+
47+
protected function getApiClass()
48+
{
49+
return 'Gitlab\Api\DeployKeys';
50+
}
51+
}

Diff for: test/Gitlab/Tests/Api/ProjectsTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,40 @@ public function shouldRemoveKey()
606606
$this->assertEquals($expectedBool, $api->removeKey(1, 3));
607607
}
608608

609+
/**
610+
* @test
611+
*/
612+
public function shoudEnableKey()
613+
{
614+
$expectedBool = true;
615+
616+
$api = $this->getApiMock();
617+
$api->expects($this->once())
618+
->method('post')
619+
->with('projects/1/keys/3/enable')
620+
->will($this->returnValue($expectedBool))
621+
;
622+
623+
$this->assertEquals($expectedBool, $api->enableKey(1, 3));
624+
}
625+
626+
/**
627+
* @test
628+
*/
629+
public function shoudDisableKey()
630+
{
631+
$expectedBool = true;
632+
633+
$api = $this->getApiMock();
634+
$api->expects($this->once())
635+
->method('delete')
636+
->with('projects/1/keys/3/disable')
637+
->will($this->returnValue($expectedBool))
638+
;
639+
640+
$this->assertEquals($expectedBool, $api->disableKey(1, 3));
641+
}
642+
609643
/**
610644
* @test
611645
*/

0 commit comments

Comments
 (0)