Skip to content

Commit 7ee6c65

Browse files
committed
implemented BranchRestrictions::create (API 2.0)
1 parent e255a2b commit 7ee6c65

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

docs/repositories/branch-restrictions.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ $restrictions->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user,
1414
$restrictions->all($account_name, $repo_slug);
1515
```
1616

17+
### Creates restrictions: (API 2.0)
18+
19+
Restrict push access to any branches starting with `joe-and-mary-` only to users `joe` and `mary`:
20+
```php
21+
$restrictions->create($account_name, $repo_slug, array(
22+
'kind' => 'push',
23+
'pattern' => 'joe-and-mary-*',
24+
'users' => array(
25+
array('username' => 'joe'),
26+
array('username' => 'mary')
27+
)
28+
));
29+
```
30+
1731
----
1832

1933
#### Related:

lib/Bitbucket/API/Repositories/BranchRestrictions.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,42 @@ public function all($account, $repo)
3535
sprintf('repositories/%s/%s/branch-restrictions', $account, $repo)
3636
);
3737
}
38+
39+
/**
40+
* Creates restrictions for the specified repository.
41+
*
42+
* @access public
43+
* @param string $account The team or individual account owning the repository.
44+
* @param string $repo The repository identifier.
45+
* @param array $params Additional parameters
46+
* @return MessageInterface
47+
*
48+
* @throws \InvalidArgumentException
49+
*/
50+
public function create($account, $repo, $params = array())
51+
{
52+
// allow developer to directly specify params as json if (s)he wants.
53+
if (!empty($params) && is_string($params)) {
54+
$params = $this->decodeJSON($params);
55+
}
56+
57+
if (!empty($params) && is_array($params)) {
58+
$params = array_merge(
59+
array(
60+
'kind' => 'push'
61+
),
62+
$params
63+
);
64+
}
65+
66+
if (empty($params['kind']) or !in_array($params['kind'], array('push', 'delete', 'force'))) {
67+
throw new \InvalidArgumentException('Invalid restriction kind.');
68+
}
69+
70+
return $this->getClient()->setApiVersion('2.0')->post(
71+
sprintf('repositories/%s/%s/branch-restrictions', $account, $repo),
72+
json_encode($params),
73+
array('Content-Type' => 'application/json')
74+
);
75+
}
3876
}

test/Bitbucket/Tests/API/Repositories/BranchRestrictionsTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,68 @@ public function testGetAllRestrictions()
2424

2525
$this->assertEquals($expectedResult, $actual);
2626
}
27+
28+
public function testCreateRestrictionFromArray()
29+
{
30+
$endpoint = 'repositories/gentle/eof/branch-restrictions';
31+
$params = array(
32+
'kind' => 'push'
33+
);
34+
35+
$client = $this->getHttpClientMock();
36+
$client->expects($this->once())
37+
->method('post')
38+
->with($endpoint, json_encode($params));
39+
40+
/** @var \Bitbucket\API\Repositories\BranchRestrictions $restrictions */
41+
$restrictions = $this->getClassMock('Bitbucket\API\Repositories\BranchRestrictions', $client);
42+
43+
$restrictions->create('gentle', 'eof', $params);
44+
}
45+
46+
public function testCreateRestrictionFromJSON()
47+
{
48+
$endpoint = 'repositories/gentle/eof/branch-restrictions';
49+
$params = json_encode(array(
50+
'kind' => 'push'
51+
));
52+
53+
$client = $this->getHttpClientMock();
54+
$client->expects($this->once())
55+
->method('post')
56+
->with($endpoint, $params);
57+
58+
/** @var \Bitbucket\API\Repositories\BranchRestrictions $restrictions */
59+
$restrictions = $this->getClassMock('Bitbucket\API\Repositories\BranchRestrictions', $client);
60+
61+
$restrictions->create('gentle', 'eof', $params);
62+
}
63+
64+
/**
65+
* @expectedException \InvalidArgumentException
66+
*/
67+
public function testCreateRestrictionFromArrayShouldFailWithInvalidRestrictionKind()
68+
{
69+
$params = array(
70+
'kind' => 'invalid'
71+
);
72+
73+
$restrictions = new \Bitbucket\API\Repositories\BranchRestrictions;
74+
75+
$restrictions->create('gentle', 'eof', $params);
76+
}
77+
78+
/**
79+
* @expectedException \InvalidArgumentException
80+
*/
81+
public function testCreateRestrictionFromJSONShouldFailWithInvalidRestrictionKind()
82+
{
83+
$params = json_encode(array(
84+
'kind' => 'invalid'
85+
));
86+
87+
$restrictions = new \Bitbucket\API\Repositories\BranchRestrictions;
88+
89+
$restrictions->create('gentle', 'eof', $params);
90+
}
2791
}

0 commit comments

Comments
 (0)