Skip to content

Commit 70cdff8

Browse files
committed
implemented BranchRestrictions::update (API 2.0)
1 parent 15df546 commit 70cdff8

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

docs/repositories/branch-restrictions.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ $restrictions->create($account_name, $repo_slug, array(
3333
$restrictions->get($account_name, $repo_slug, $restrictionID);
3434
```
3535

36+
### Update a specific restriction: (API 2.0)
37+
```php
38+
$restrictions->update($account_name, $repo_slug, $restrictionID, array(
39+
'users' => array(
40+
array('username' => 'joe'),
41+
array('username' => 'mary'),
42+
array('username' => 'joe-work')
43+
)
44+
));
45+
```
46+
3647
----
3748

3849
#### Related:

lib/Bitbucket/API/Repositories/BranchRestrictions.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,34 @@ public function get($account, $repo, $id)
8989
sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id)
9090
);
9191
}
92+
93+
/**
94+
* Updates a specific branch restriction.
95+
*
96+
* @access public
97+
* @param string $account The team or individual account owning the repository.
98+
* @param string $repo The repository identifier.
99+
* @param int $id The restriction's identifier.
100+
* @param array $params Additional parameters
101+
* @return MessageInterface
102+
*
103+
* @throws \InvalidArgumentException
104+
*/
105+
public function update($account, $repo, $id, $params = array())
106+
{
107+
// allow developer to directly specify params as json if (s)he wants.
108+
if (!empty($params) && is_string($params)) {
109+
$params = $this->decodeJSON($params);
110+
}
111+
112+
if (!empty($params['kind'])) {
113+
throw new \InvalidArgumentException('You cannot change the "kind" value.');
114+
}
115+
116+
return $this->getClient()->setApiVersion('2.0')->put(
117+
sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id),
118+
json_encode($params),
119+
array('Content-Type' => 'application/json')
120+
);
121+
}
92122
}

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,60 @@ public function testGetSpecificRestriction()
106106

107107
$this->assertEquals($expectedResult, $actual);
108108
}
109+
110+
public function testUpdateRestrictionFromArray()
111+
{
112+
$endpoint = 'repositories/gentle/eof/branch-restrictions/1';
113+
$params = array(
114+
'users' => array(
115+
array('username' => 'vimishor'),
116+
array('username' => 'gtl_test1')
117+
)
118+
);
119+
120+
$client = $this->getHttpClientMock();
121+
$client->expects($this->once())
122+
->method('put')
123+
->with($endpoint, json_encode($params));
124+
125+
/** @var \Bitbucket\API\Repositories\BranchRestrictions $restriction */
126+
$restriction = $this->getClassMock('Bitbucket\API\Repositories\BranchRestrictions', $client);
127+
128+
$restriction->update('gentle', 'eof', 1, $params);
129+
}
130+
131+
public function testUpdateRestrictionFromJSON()
132+
{
133+
$endpoint = 'repositories/gentle/eof/branch-restrictions/1';
134+
$params = json_encode(array(
135+
'dummy' => array(
136+
array('username' => 'vimishor'),
137+
array('username' => 'gtl_test1')
138+
)
139+
));
140+
141+
$client = $this->getHttpClientMock();
142+
$client->expects($this->once())
143+
->method('put')
144+
->with($endpoint, $params);
145+
146+
/** @var \Bitbucket\API\Repositories\BranchRestrictions $restriction */
147+
$restriction = $this->getClassMock('Bitbucket\API\Repositories\BranchRestrictions', $client);
148+
149+
$restriction->update('gentle', 'eof', 1, $params);
150+
}
151+
152+
/**
153+
* @expectedException \InvalidArgumentException
154+
*/
155+
public function testCreateRestrictionShouldFailIfKindIsSpecified()
156+
{
157+
$params = array(
158+
'kind' => 'invalid'
159+
);
160+
161+
$restrictions = new \Bitbucket\API\Repositories\BranchRestrictions;
162+
163+
$restrictions->update('gentle', 'eof', 1, $params);
164+
}
109165
}

0 commit comments

Comments
 (0)