Skip to content

Commit 34f7b94

Browse files
committed
Merge branch 'release/0.4.0'
2 parents 0ba6f9b + 40886c0 commit 34f7b94

File tree

16 files changed

+866
-11
lines changed

16 files changed

+866
-11
lines changed

docs/Home.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ 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)
16+
* [Commit(s)](repositories/commits.md) (API 2.0)
1517
* [Deploykeys](repositories/deploykeys.md)
1618
* [Events](repositories/events.md)
1719
* [Followers](repositories/followers.md)
@@ -22,9 +24,10 @@ A simple PHP wrapper for Bitbucket API.
2224
* [Services](repositories/services.md)
2325
* [Src](repositories/src.md)
2426
* [Wiki](repositories/wiki.md)
27+
* [Teams](teams.md) (API 2.0)
2528
* [User](user.md)
2629
* [Repositories](user/repositories.md)
27-
* [Users](users.md)
30+
* [Users](users.md) (API 2.0)
2831
* [Account](users/account.md)
2932
* [Emails](users/emails.md)
3033
* [Invitations](users/invitations.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)

docs/repositories/pullrequests.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ $pull->all($account_name, $repo_slug);
1919
$pull->all($account_name, $repo_slug, array('state' => 'merged'));
2020
```
2121

22+
### Get all merged and declined pull requests: (API 2.0)
23+
```php
24+
$pull->all($account_name, $repo_slug, array('state' => array('merged', 'declined')));
25+
```
26+
2227
### Create a new pull request: (API 2.0)
2328
```php
2429
$pull->create('gentle', 'secret-repo', array(

docs/teams.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Teams
2+
3+
----
4+
Get Team related information.
5+
6+
### Prepare:
7+
```php
8+
$team = new Bitbucket\API\Teams();
9+
$team->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
10+
```
11+
12+
### Get the team profile: (API 2.0)
13+
```php
14+
$team->profile($team_name);
15+
```
16+
17+
### Get the team members: (API 2.0)
18+
```php
19+
$team->members($team_name);
20+
```
21+
22+
### Get the team followers: (API 2.0)
23+
```php
24+
$team->followers($team_name);
25+
```
26+
27+
### Get a list of accounts the team is following: (API 2.0)
28+
```php
29+
$team->following($team_name);
30+
```
31+
32+
### Get the team's repositories: (API 2.0)
33+
```php
34+
$team->repositories($team_name);
35+
```
36+
37+
38+
----
39+
40+
#### Related:
41+
* [Authentication](authentication.md)
42+
* [BB Wiki](https://confluence.atlassian.com/x/XwZAGQ)

docs/users.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,28 @@ Get information related to an individual or team account.
55

66
### Prepare:
77
```php
8-
$users = new Bitbucket\API\Users();
9-
$users->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
8+
$user = new Bitbucket\API\Users();
9+
$user->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
10+
```
11+
12+
### Get the public information associated with a user: (API 2.0)
13+
```php
14+
$user->get($username);
15+
```
16+
17+
### Get the list of followers: (API 2.0)
18+
```php
19+
$user->followers($username);
20+
```
21+
22+
### Get a list of accounts the user is following: (API 2.0)
23+
```php
24+
$user->following($username);
25+
```
26+
27+
### Get the list of the user's repositories: (API 2.0)
28+
```php
29+
$user->repositories($username);
1030
```
1131

1232
----

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
}

lib/Bitbucket/API/Http/Client.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Client implements ClientInterface
3333
'api_versions' => array('1.0', '2.0'), // supported versions
3434
'format' => 'json',
3535
'formats' => array('json', 'xml'), // supported response formats
36-
'user_agent' => 'bitbucket-api-php/0.3.0 (https://bitbucket.org/gentlero/bitbucket-api)',
36+
'user_agent' => 'bitbucket-api-php/0.4.0 (https://bitbucket.org/gentlero/bitbucket-api)',
3737
'timeout' => 10,
3838
'verify_peer' => false
3939
);
@@ -77,6 +77,22 @@ public function addListener(ListenerInterface $listener)
7777
return $this;
7878
}
7979

80+
/**
81+
* {@inheritDoc}
82+
*/
83+
public function delListener($name)
84+
{
85+
if ($name instanceof ListenerInterface) {
86+
$name = $name->getName();
87+
}
88+
89+
if ($this->isListener($name) === true) {
90+
unset($this->listeners[$name]);
91+
}
92+
93+
return $this;
94+
}
95+
8096
/**
8197
* {@inheritDoc}
8298
*/

lib/Bitbucket/API/Http/ClientInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ public function getApiBaseUrl();
132132
*/
133133
public function addListener(ListenerInterface $listener);
134134

135+
/**
136+
* @access public
137+
* @param ListenerInterface|string $name
138+
* @return $this
139+
*/
140+
public function delListener($name);
141+
135142
/**
136143
* Get listener interface
137144
*

lib/Bitbucket/API/Http/Listener/RequestListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function preSend(RequestInterface $request)
3636
// Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
3737
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getContent())
3838
);
39+
40+
$request->setResource(
41+
// Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
42+
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getResource())
43+
);
3944
}
4045

4146
/**
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+
}

0 commit comments

Comments
 (0)