Skip to content

Commit 4ff7bb3

Browse files
committed
Merge branch 'feature/api-v2' into develop
Implemented branch-restrictions endpoint.
2 parents a7f0cd0 + d8203c8 commit 4ff7bb3

File tree

6 files changed

+411
-6
lines changed

6 files changed

+411
-6
lines changed

docs/Home.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A simple PHP wrapper for Bitbucket API.
1111
* [Invitations](invitations.md)
1212
* [Privileges](privileges.md)
1313
* [Repositories](repositories.md) (API 2.0)
14+
* [Branch Restrictions](repositories/branch-restrictions.md) (API 2.0)
1415
* [Changesets](repositories/changesets.md)
1516
* [Commit(s)](repositories/commits.md) (API 2.0)
1617
* [Deploykeys](repositories/deploykeys.md)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Branch restrictions
2+
3+
----
4+
Manage branch restrictions on a repository
5+
6+
### Prepare:
7+
```php
8+
$restrictions = new Bitbucket\API\Repositories\PullRequests();
9+
$restrictions->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
10+
```
11+
12+
### Get the information associated with a repository's branch restrictions: (API 2.0)
13+
```php
14+
$restrictions->all($account_name, $repo_slug);
15+
```
16+
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+
31+
### Get a specific restriction: (API 2.0)
32+
```php
33+
$restrictions->get($account_name, $repo_slug, $restrictionID);
34+
```
35+
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+
47+
### Delete a specific restriction: (API 2.0)
48+
```php
49+
$restrictions->delete($account_name, $repo_slug, $restrictionID);
50+
```
51+
52+
----
53+
54+
#### Related:
55+
* [Authentication](../authentication.md)
56+
* [BB Wiki](https://confluence.atlassian.com/x/XQEYFw)

lib/Bitbucket/API/Api.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,24 @@ protected function childFactory($name)
298298

299299
return $child;
300300
}
301+
302+
/**
303+
* Convert JSON to array with error check
304+
*
305+
* @access protected
306+
* @param string $body JSON data
307+
* @return array
308+
*
309+
* @throws \InvalidArgumentException
310+
*/
311+
protected function decodeJSON($body)
312+
{
313+
$params = json_decode($body, true);
314+
315+
if (json_last_error() !== JSON_ERROR_NONE) {
316+
throw new \InvalidArgumentException('Invalid JSON data provided.');
317+
}
318+
319+
return $params;
320+
}
301321
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the bitbucket-api package.
5+
*
6+
* (c) Alexandru G. <alex@gentle.ro>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Bitbucket\API\Repositories;
13+
14+
use Bitbucket\API\Api;
15+
use Buzz\Message\MessageInterface;
16+
17+
/**
18+
* Manage branch restrictions on a repository
19+
*
20+
* @author Alexandru G. <alex@gentle.ro>
21+
*/
22+
class BranchRestrictions extends Api
23+
{
24+
/**
25+
* Get the information associated with a repository's branch restrictions
26+
*
27+
* @access public
28+
* @param string $account The team or individual account owning the repository.
29+
* @param string $repo The repository identifier.
30+
* @return MessageInterface
31+
*/
32+
public function all($account, $repo)
33+
{
34+
return $this->getClient()->setApiVersion('2.0')->get(
35+
sprintf('repositories/%s/%s/branch-restrictions', $account, $repo)
36+
);
37+
}
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+
}
76+
77+
/**
78+
* Get a specific restriction
79+
*
80+
* @access public
81+
* @param string $account The team or individual account owning the repository.
82+
* @param string $repo The repository identifier.
83+
* @param int $id The restriction's identifier.
84+
* @return MessageInterface
85+
*/
86+
public function get($account, $repo, $id)
87+
{
88+
return $this->getClient()->setApiVersion('2.0')->get(
89+
sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id)
90+
);
91+
}
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+
}
122+
123+
/**
124+
* Delete a specific branch restriction.
125+
*
126+
* @access public
127+
* @param string $account The team or individual account owning the repository.
128+
* @param string $repo The repository identifier.
129+
* @param int $id The restriction's identifier.
130+
* @return MessageInterface
131+
*
132+
* @throws \InvalidArgumentException
133+
*/
134+
public function delete($account, $repo, $id)
135+
{
136+
return $this->getClient()->setApiVersion('2.0')->delete(
137+
sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id)
138+
);
139+
}
140+
}

lib/Bitbucket/API/Repositories/PullRequests.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ function ($state) use ($states) {
8888
public function create($account, $repo, $params = array())
8989
{
9090
// allow developer to directly specify params as json if (s)he wants.
91+
if (!empty($params) && is_string($params)) {
92+
$params = $this->decodeJSON($params);
93+
}
94+
9195
if (!empty($params) && is_array($params)) {
92-
$params = json_encode(array_merge(
96+
$params = array_merge(
9397
array(
9498
'title' => 'New pull request',
9599
'source' => array(
@@ -99,7 +103,7 @@ public function create($account, $repo, $params = array())
99103
)
100104
),
101105
$params
102-
));
106+
);
103107
}
104108

105109
if (empty($params['title'])) {
@@ -112,7 +116,7 @@ public function create($account, $repo, $params = array())
112116

113117
return $this->getClient()->setApiVersion('2.0')->post(
114118
sprintf('repositories/%s/%s/pullrequests', $account, $repo),
115-
$params,
119+
json_encode($params),
116120
array('Content-Type' => 'application/json')
117121
);
118122
}
@@ -132,8 +136,12 @@ public function create($account, $repo, $params = array())
132136
public function update($account, $repo, $id, $params = array())
133137
{
134138
// allow developer to directly specify params as json if (s)he wants.
139+
if (!empty($params) && is_string($params)) {
140+
$params = $this->decodeJSON($params);
141+
}
142+
135143
if (!empty($params) && is_array($params)) {
136-
$params = json_encode(array_merge(
144+
$params = array_merge(
137145
array(
138146
'title' => 'Updated pull request',
139147
'destination' => array(
@@ -143,7 +151,7 @@ public function update($account, $repo, $id, $params = array())
143151
)
144152
),
145153
$params
146-
));
154+
);
147155
}
148156

149157
if (empty($params['title'])) {
@@ -156,7 +164,7 @@ public function update($account, $repo, $id, $params = array())
156164

157165
return $this->getClient()->setApiVersion('2.0')->put(
158166
sprintf('repositories/%s/%s/pullrequests/%d', $account, $repo, $id),
159-
$params,
167+
json_encode($params),
160168
array('Content-Type' => 'application/json')
161169
);
162170
}

0 commit comments

Comments
 (0)