Skip to content

Commit bf37870

Browse files
ampersandNyholm
authored andcommitted
Add branch protection (#520)
* Added branch protection * Updated method comments
1 parent 0bef07b commit bf37870

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

lib/Github/Api/Repo.php

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Github\Api\Repository\DeployKeys;
1010
use Github\Api\Repository\Downloads;
1111
use Github\Api\Repository\Projects;
12+
use Github\Api\Repository\Protection;
1213
use Github\Api\Repository\Releases;
1314
use Github\Api\Repository\Forks;
1415
use Github\Api\Repository\Hooks;
@@ -413,6 +414,18 @@ public function branches($username, $repository, $branch = null)
413414
return $this->get($url);
414415
}
415416

417+
/**
418+
* Manage the protection of a repository branch.
419+
*
420+
* @link https://developer.github.com/v3/repos/branches/#get-branch-protection
421+
*
422+
* @return Protection
423+
*/
424+
public function protection()
425+
{
426+
return new Protection($this->client);
427+
}
428+
416429
/**
417430
* Get the contributors of a repository.
418431
*
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Github\Api\Repository;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* @link https://developer.github.com/v3/repos/branches/
9+
* @author Brandon Bloodgood <[email protected]>
10+
*/
11+
class Protection extends AbstractApi
12+
{
13+
/**
14+
* Retrieves configured protection for the provided branch
15+
*
16+
* @link https://developer.github.com/v3/repos/branches/#get-branch-protection
17+
*
18+
* @param string $username The user who owns the repository
19+
* @param string $repository The name of the repo
20+
* @param string $branch The name of the branch
21+
*
22+
* @return array The branch protection information
23+
*/
24+
public function show($username, $repository, $branch)
25+
{
26+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/branches/'.rawurlencode($branch).'/protection');
27+
}
28+
29+
/**
30+
* Updates the repo's branch protection
31+
*
32+
* @link https://developer.github.com/v3/repos/branches/#update-branch-protection
33+
*
34+
* @param string $username The user who owns the repository
35+
* @param string $repository The name of the repo
36+
* @param string $branch The name of the branch
37+
* @param array $params The branch protection information
38+
*
39+
* @return array The updated branch protection information
40+
*/
41+
public function update($username, $repository, $branch, array $params = array())
42+
{
43+
return $this->put('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/branches/'.rawurlencode($branch).'/protection', $params);
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Github\Tests\Api\Repository;
4+
5+
use Github\Tests\Api\TestCase;
6+
7+
class ProtectionTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
*/
12+
public function shouldShowProtection()
13+
{
14+
$expectedValue = array('required_status_checks', 'required_pull_reqeust_reviews', 'restrictions');
15+
16+
$api = $this->getApiMock();
17+
$api->expects($this->once())
18+
->method('get')
19+
->with('/repos/KnpLabs/php-github-api/branches/master/protection')
20+
->will($this->returnValue($expectedValue));
21+
22+
$this->assertEquals($expectedValue, $api->show('KnpLabs', 'php-github-api', 'master'));
23+
}
24+
25+
/**
26+
* @test
27+
*/
28+
public function shouldUpdateProtection()
29+
{
30+
$expectedValue = array('required_status_checks', 'required_pull_reqeust_reviews', 'restrictions');
31+
$data = array('required_status_checks' => null);
32+
33+
$api = $this->getApiMock();
34+
$api->expects($this->once())
35+
->method('put')
36+
->with('/repos/KnpLabs/php-github-api/branches/master/protection', $data)
37+
->will($this->returnValue($expectedValue));
38+
39+
$this->assertEquals($expectedValue, $api->update('KnpLabs', 'php-github-api', 'master', $data));
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
protected function getApiClass()
46+
{
47+
return \Github\Api\Repository\Protection::class;
48+
}
49+
}

0 commit comments

Comments
 (0)