Skip to content

Commit 68226cc

Browse files
committed
Added branch protection permissions. Fixes #156
1 parent 7173d00 commit 68226cc

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

lib/Gitlab/Api/Repositories.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ public function deleteBranch($project_id, $branch_name)
4848
/**
4949
* @param int $project_id
5050
* @param string $branch_name
51+
* @param bool $devPush
52+
* @param bool $devMerge
5153
* @return mixed
5254
*/
53-
public function protectBranch($project_id, $branch_name)
55+
public function protectBranch($project_id, $branch_name, $devPush = false, $devMerge = false)
5456
{
55-
return $this->put($this->getProjectPath($project_id, 'repository/branches/'.$this->encodeBranch($branch_name).'/protect'));
57+
return $this->put($this->getProjectPath($project_id, 'repository/branches/'.$this->encodeBranch($branch_name).'/protect'), array(
58+
'developers_can_push' => $devPush,
59+
'developers_can_merge' => $devMerge
60+
));
5661
}
5762

5863
/**

lib/Gitlab/Model/Branch.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ public function show()
6363
}
6464

6565
/**
66+
* @param bool $devPush
67+
* @param bool $devMerge
6668
* @return Branch
6769
*/
68-
public function protect()
70+
public function protect($devPush = false, $devMerge = false)
6971
{
70-
$data = $this->api('repositories')->protectBranch($this->project->id, $this->name);
72+
$data = $this->api('repositories')->protectBranch($this->project->id, $this->name, $devPush, $devMerge);
7173

7274
return static::fromArray($this->getClient(), $this->project, $data);
7375
}

lib/Gitlab/Model/Project.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,16 @@ public function branch($branch_name)
420420

421421
/**
422422
* @param string $branch_name
423+
* @param bool $devPush
424+
* @param bool $devMerge
423425
* @return Branch
424426
*/
425-
public function protectBranch($branch_name)
427+
public function protectBranch($branch_name, $devPush = false, $devMerge = false)
426428
{
427429
$branch = new Branch($this, $branch_name);
428430
$branch->setClient($this->getClient());
429431

430-
return $branch->protect();
432+
return $branch->protect($devPush, $devMerge);
431433
}
432434

433435
/**

test/Gitlab/Tests/Api/RepositoriesTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,30 @@ public function shouldProtectBranch()
8585
$api = $this->getApiMock();
8686
$api->expects($this->once())
8787
->method('put')
88-
->with('projects/1/repository/branches/master/protect')
88+
->with('projects/1/repository/branches/master/protect', array('developers_can_push' => false, 'developers_can_merge' => false))
8989
->will($this->returnValue($expectedArray))
9090
;
9191

9292
$this->assertEquals($expectedArray, $api->protectBranch(1, 'master'));
9393
}
9494

95+
/**
96+
* @test
97+
*/
98+
public function shouldProtectBranchWithPermissions()
99+
{
100+
$expectedArray = array('name' => 'master');
101+
102+
$api = $this->getApiMock();
103+
$api->expects($this->once())
104+
->method('put')
105+
->with('projects/1/repository/branches/master/protect', array('developers_can_push' => true, 'developers_can_merge' => true))
106+
->will($this->returnValue($expectedArray))
107+
;
108+
109+
$this->assertEquals($expectedArray, $api->protectBranch(1, 'master', true, true));
110+
}
111+
95112
/**
96113
* @test
97114
*/

0 commit comments

Comments
 (0)