Skip to content

Commit 93b554f

Browse files
committed
Merge branch 'release/0.7.0'
2 parents 3916ff8 + 1cb92cf commit 93b554f

File tree

25 files changed

+963
-69
lines changed

25 files changed

+963
-69
lines changed

.travis.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ php:
77
- hhvm
88

99
matrix:
10+
fast_finish: true
1011
allow_failures:
1112
- php: 5.3
1213

1314
before_script:
14-
- curl -s http://getcomposer.org/installer | php
15-
- php composer.phar install --prefer-source
15+
- travis_retry composer self-update
16+
- composer --version
17+
- travis_retry composer install --no-interaction --prefer-source
1618

1719
script:
18-
- mkdir -p build/logs
19-
- php vendor/bin/phpunit
20+
- mkdir -p ./build/logs
21+
- php vendor/bin/phpunit --coverage-clover=./build/logs/coverage.clover
2022

2123
after_script:
24+
- travis_retry wget https://scrutinizer-ci.com/ocular.phar -O ocular.phar
25+
- travis_retry php ocular.phar self-update
26+
- php ocular.phar --version
27+
- travis_retry php ocular.phar code-coverage:upload --repository=b/gentlero/bitbucket-api --format=php-clover ./build/logs/coverage.clover

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5-
## [Unreleased][unreleased]
5+
## [0.7.0] / 2015-09-08
6+
7+
### Added:
8+
- Implemented webhooks endpoints.
9+
- Toggle spam on a changeset comment. (issue #2)
10+
- Support for OAuth2. (issue #34)
11+
12+
### Changed:
13+
- Marked Repositories/Services as deprecated in favor of Repositories/Hooks. (issue #29)
14+
- [DOCS] Added example on how to use this library in combination with a 3rd party OAuth1 client in a 3-legged flow.
15+
16+
### Fixed:
17+
- `forking_policy` parameter renamed to `fork_policy` on repository endpoint. (issue #32)
618

719
## 0.6.2 / 2015-05-18
820

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
[![Latest Version](https://img.shields.io/packagist/v/gentle/bitbucket-api.svg?style=flat-square)](https://packagist.org/packages/gentle/bitbucket-api)
44
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/gentlero/bitbucket-api/blob/master/LICENSE)
5-
[![Build Status](http://img.shields.io/travis/gentlero/bitbucket-api/master.svg?style=flat-square)](https://travis-ci.org/gentlero/bitbucket-api)
6-
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/b/gentlero/bitbucket-api/master.svg?style=flat-square)](https://scrutinizer-ci.com/b/gentlero/bitbucket-api/?branch=master)
7-
[![Code quality](http://img.shields.io/scrutinizer/b/gentlero/bitbucket-api/master.svg?style=flat-square)](https://scrutinizer-ci.com/b/gentlero/bitbucket-api/?branch=master)
5+
[![Build Status](https://img.shields.io/travis/gentlero/bitbucket-api/master.svg?style=flat-square)](https://travis-ci.org/gentlero/bitbucket-api)
6+
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/b/gentlero/bitbucket-api.svg?style=flat-square)](https://scrutinizer-ci.com/b/gentlero/bitbucket-api/?branch=develop)
7+
[![Code quality](https://img.shields.io/scrutinizer/b/gentlero/bitbucket-api.svg?style=flat-square)](https://scrutinizer-ci.com/b/gentlero/bitbucket-api/?branch=develop)
88

99
Simple Bitbucket API wrapper for PHP >= 5.3.2.
1010

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
],
1616
"require": {
17-
"php": "~5.3",
17+
"php": ">=5.3",
1818
"kriswallsmith/buzz": "~0.13",
1919
"jacobkiers/oauth": "~1.0.10"
2020
},
@@ -29,5 +29,8 @@
2929
},
3030
"autoload": {
3131
"psr-0": { "Bitbucket\\": "lib/" }
32+
},
33+
"scripts": {
34+
"test": "vendor/bin/phpunit"
3235
}
3336
}

composer.lock

Lines changed: 25 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/examples/authentication.md

Lines changed: 168 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ To use basic authentication, you need to attach `BasicAuthListener` to http clie
2323
$response = $user->get();
2424
```
2525

26-
### OAuth authorization
26+
### OAuth1 authorization
2727
This library comes with a `OAuthListener` which will sign all requests for you. All you need to do is to attach the listener to
2828
http client with oauth credentials before making a request.
2929

30+
#### OAuth1 1-legged
3031
```php
3132
// OAuth 1-legged example
3233
// You can create a new consumer at: https://bitbucket.org/account/user/<username or team>/api
@@ -44,8 +45,174 @@ http client with oauth credentials before making a request.
4445
$response = $user->get();
4546
```
4647

48+
#### OAuth1 3-legged
49+
50+
You can use any 3rd party library to complete this [flow][3] and set OAuth credentials when you instantiate `OAuthListener`.
51+
52+
In the following example [PHP League's OAuth 1.0 Client][4] is used.
53+
54+
```php
55+
session_start();
56+
57+
// @see: https://bitbucket.org/account/user/<username>/api
58+
$oauth_params = array(
59+
'identifier' => 'aaa',
60+
'secret' => 'bbb',
61+
'callback_uri' => 'http://example.com/oauth1_3legged.php'
62+
);
63+
64+
$server = new League\OAuth1\Client\Server\Bitbucket($oauth_params);
65+
66+
if (array_key_exists('profile', $_GET)) {
67+
if (false === array_key_exists('bb_credentials', $_SESSION)) {
68+
header('Location: ' . $oauth_params['callback_uri']);
69+
exit;
70+
}
71+
72+
$oauth_params = array_merge(unserialize($_SESSION['bb_credentials']), array(
73+
'oauth_consumer_key' => $oauth_params['identifier'],
74+
'oauth_consumer_secret' => $oauth_params['secret'],
75+
'oauth_callback' => $oauth_params['callback_uri'],
76+
));
77+
78+
79+
$bitbucket = new \Bitbucket\API\Api();
80+
$bitbucket->getClient()->addListener(
81+
new \Bitbucket\API\Http\Listener\OAuthListener($oauth_params)
82+
);
83+
84+
/** @var \Bitbucket\API\User $user */
85+
$user = $bitbucket->api('User');
86+
87+
$profile = json_decode($user->get()->getContent(), true);
88+
echo sprintf('<a href="?logout">Logout %s</a>', $profile['user']['username']);
89+
90+
// show all user repositories
91+
echo '<h3>My repositories:</h3><ul>';
92+
array_walk($profile['repositories'], function($repository) {
93+
$repositoryUrl = str_replace('/1.0/repositories/', '', $repository['resource_uri']);
94+
echo sprintf(
95+
'<li><a href="http://bitbucket.org/%s">%s</a></li>', $repositoryUrl, $repository['name']
96+
);
97+
});
98+
echo '</ul>';
99+
exit;
100+
} elseif (array_key_exists('login', $_GET)) {
101+
// Retrieve temporary credentials
102+
$temporaryCredentials = $server->getTemporaryCredentials();
103+
104+
// Store credentials in the session, we'll need them later
105+
$_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
106+
session_write_close();
107+
108+
// Second part of OAuth 1.0 authentication is to redirect the
109+
// resource owner to the login screen on the server.
110+
$server->authorize($temporaryCredentials);
111+
exit;
112+
} elseif (array_key_exists('oauth_token', $_GET) && array_key_exists('oauth_verifier', $_GET)) {
113+
// Retrieve the temporary credentials we saved before
114+
$temporaryCredentials = unserialize($_SESSION['temporary_credentials']);
115+
116+
// We will now retrieve token credentials from the server
117+
$tokenCredentials = $server->getTokenCredentials(
118+
$temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']
119+
);
120+
121+
$oauth_params = array(
122+
'oauth_token' => $tokenCredentials->getIdentifier(),
123+
'oauth_token_secret' => $tokenCredentials->getSecret()
124+
);
125+
126+
unset($_SESSION['temporary_credentials'], $_SESSION['token_credentials']);
127+
$_SESSION['bb_credentials'] = serialize($oauth_params);
128+
session_write_close();
129+
130+
// redirect the user to the profile page, in order to fetch his/her information.
131+
header('Location: '.$oauth_params['callback_uri'].'?profile');
132+
exit;
133+
} elseif (array_key_exists('logout', $_GET)) {
134+
unset($_SESSION['bb_credentials']);
135+
session_write_close();
136+
}
137+
138+
echo '<a href="?login">Login with BitBucket!</a>';
139+
```
140+
141+
### OAuth2 authorization
142+
143+
You can use `OAuth2Listener` in order to make authorized requests using version 2 of OAuth protocol.
144+
145+
#### OAuth2 client credentials (_2-legged flow_)
146+
147+
```php
148+
// @see: https://bitbucket.org/account/user/<username or team>/api
149+
$oauth_params = array(
150+
'client_id' => 'aaa',
151+
'client_secret' => 'bbb'
152+
);
153+
154+
$bitbucket = new \Bitbucket\API\Api();
155+
$bitbucket->getClient()->addListener(
156+
new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
157+
);
158+
159+
$repositories = $bitbucket->api('Repositories');
160+
$response = $repositories->all('my_account'); // should include private repositories
161+
```
162+
163+
#### OAuth2 Authorization code (_3-legged flow_)
164+
165+
You can use any 3rd party library to complete this [flow][3] and set `access_token` option when you instantiate `OAuth2Listener`.
166+
167+
In the following example [PHP League's OAuth 2.0 Client][1] is used with [Bitbucket Provider][2].
168+
169+
```php
170+
session_start();
171+
172+
$provider = new Stevenmaguire\OAuth2\Client\Provider\Bitbucket([
173+
'clientId' => $_ENV['bitbucket_consumer_key'],
174+
'clientSecret' => $_ENV['bitbucket_consumer_secret'],
175+
'redirectUri' => 'http://example.com/bitbucket_login.php'
176+
]);
177+
if (!isset($_GET['code'])) {
178+
179+
// If we don't have an authorization code then get one
180+
$authUrl = $provider->getAuthorizationUrl();
181+
$_SESSION['oauth2state'] = $provider->getState();
182+
header('Location: '.$authUrl);
183+
exit;
184+
185+
// Check given state against previously stored one to mitigate CSRF attack
186+
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
187+
188+
unset($_SESSION['oauth2state']);
189+
exit('Invalid state');
190+
191+
} else {
192+
193+
// Try to get an access token (using the authorization code grant)
194+
$token = $provider->getAccessToken('authorization_code', [
195+
'code' => $_GET['code']
196+
]);
197+
198+
$bitbucket = new Bitbucket\API\Repositories();
199+
$bitbucket->getClient()->addListener(
200+
new \Bitbucket\API\Http\Listener\OAuth2Listener(
201+
array('access_token' => $token->getToken())
202+
)
203+
);
204+
205+
echo $bitbucket->all('my_account')->getContent(); // should include private repositories
206+
}
207+
```
208+
47209
----
48210

49211
#### Related:
50212
* [Authentication @ BB Wiki](https://confluence.atlassian.com/display/BITBUCKET/Use+the+Bitbucket+REST+APIs#UsetheBitbucketRESTAPIs-Authentication)
51213
* [OAuth on Bitbucket @ BB Wiki](https://confluence.atlassian.com/display/BITBUCKET/OAuth+on+Bitbucket)
214+
215+
[1]: http://oauth2-client.thephpleague.com/
216+
[2]: https://github.com/stevenmaguire/oauth2-bitbucket
217+
[3]: http://oauthbible.com/#oauth-2-three-legged
218+
[4]: https://github.com/thephpleague/oauth1-client

docs/examples/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ title: Examples
4646
- [Pull requests](repositories/pull-requests.html)
4747
- [Comments](repositories/pull-requests/comments.html)
4848
- [Repository](repositories/repository.html)
49-
- [Services](repositories/services.html)
49+
- ~~[Services](repositories/services.html)~~ (deprecated since 0.7.0 -- @see [WebHooks](repositories/webhooks.html))
50+
- [Hooks](repositories/webhooks.html)
5051
- [Src](repositories/src.html)
5152
- [Wiki](repositories/wiki.html)
5253
- [Teams](teams.html)

docs/examples/repositories/commits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Retrieve and compare information about commits.
1111
### Prepare:
1212

1313
```php
14-
$commits = new Bitbucket\API\Repositories\PullRequests();
14+
$commits = new Bitbucket\API\Repositories\Commits();
1515
$commits->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
1616
```
1717

0 commit comments

Comments
 (0)