Skip to content

Commit 7f3dad4

Browse files
committed
- Implemented Commits::all (API 2.0)
- Implemented Commits::get (API 2.0) - Created Http RequestListener which transforms ( *via regex* ) PHP array from Request body in a different array format which is accepted by remote API.
1 parent 8da2564 commit 7f3dad4

File tree

7 files changed

+241
-0
lines changed

7 files changed

+241
-0
lines changed

docs/repositories.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Only public repositories are returned.
2323
$repositories->all();
2424
```
2525

26+
* [Commit(s)](repositories/commits.md) (API 2.0)
2627
* [Changesets](repositories/changesets.md)
2728
* [Deploykeys](repositories/deploykeys.md)
2829
* [Events](repositories/events.md)

docs/repositories/commits.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Commits
2+
3+
----
4+
Retrieve and compare information about commits.
5+
6+
### Prepare:
7+
```php
8+
$commits = new Bitbucket\API\Repositories\PullRequests();
9+
$commits->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
10+
```
11+
12+
### Get all commits for a repository: (API 2.0)
13+
```php
14+
$commits->all($account_name, $repo_slug);
15+
```
16+
17+
### Get all commits for a single branch: (API 2.0)
18+
```php
19+
$commits->all($account_name, $repo_slug, array(
20+
'branch' => 'master' // this can also be a tag
21+
));
22+
```
23+
24+
----
25+
26+
#### Related:
27+
* [Authentication](../authentication.md)
28+
* [BB Wiki](https://confluence.atlassian.com/x/doA7Fw)

lib/Bitbucket/API/Api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Bitbucket\API;
1313

14+
use Bitbucket\API\Http\Listener\RequestListener;
1415
use Buzz\Message\MessageInterface;
1516
use Buzz\Message\RequestInterface;
1617
use Buzz\Client\ClientInterface as BuzzClientInterface;
@@ -63,6 +64,8 @@ public function __construct(BuzzClientInterface $client = null)
6364
$this->client = (is_null($client)) ? new Curl : $client;
6465
$this->httpClient = new Client(array(), $client);
6566

67+
$this->httpClient->addListener(new RequestListener());
68+
6669
return $this;
6770
}
6871

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Http\Listener;
13+
14+
use Buzz\Message\MessageInterface;
15+
use Buzz\Message\RequestInterface;
16+
17+
/**
18+
* @author Alexandru G. <alex@gentle.ro>
19+
*/
20+
class RequestListener implements ListenerInterface
21+
{
22+
/**
23+
* {@inheritDoc}
24+
*/
25+
public function getName()
26+
{
27+
return 'request';
28+
}
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public function preSend(RequestInterface $request)
34+
{
35+
$request->setContent(
36+
// Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
37+
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getContent())
38+
);
39+
}
40+
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function postSend(RequestInterface $request, MessageInterface $response)
45+
{
46+
}
47+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
* @author Alexandru G. <alex@gentle.ro>
19+
*/
20+
class Commits extends Api
21+
{
22+
/**
23+
* Get a list of pull requests
24+
*
25+
* @access public
26+
* @param string $account The team or individual account owning the repository.
27+
* @param string $repo The repository identifier.
28+
* @param array $params Additional parameters
29+
* @return MessageInterface
30+
*/
31+
public function all($account, $repo, $params = array())
32+
{
33+
$endpoint = sprintf('repositories/%s/%s/commits', $account, $repo);
34+
35+
if (!empty($params['branch'])) {
36+
$endpoint .= '/'.$params['branch']; // can also be a tag
37+
unset($params['branch']);
38+
}
39+
40+
return $this->getClient()->setApiVersion('2.0')->post(
41+
$endpoint,
42+
$params
43+
);
44+
}
45+
46+
/**
47+
* Get an individual commit
48+
*
49+
* @access public
50+
* @param string $account The team or individual account owning the repository.
51+
* @param string $repo The repository identifier.
52+
* @param string $revision A SHA1 value for the commit.
53+
* @return MessageInterface
54+
*/
55+
public function get($account, $repo, $revision)
56+
{
57+
return $this->getClient()->setApiVersion('2.0')->get(
58+
sprintf('repositories/%s/%s/commit/%s', $account, $repo, $revision)
59+
);
60+
}
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Bitbucket\Tests\API\Http\Listener;
4+
5+
use Bitbucket\API\Http\Listener\RequestListener;
6+
use Bitbucket\Tests\API as Tests;
7+
use Buzz\Message\Request;
8+
9+
/**
10+
* @author Alexandru G. <alex@gentle.ro>
11+
*/
12+
class RequestListenerTest extends Tests\TestCase
13+
{
14+
public function testPHPArrayToApiArrayConversion()
15+
{
16+
$listener = new RequestListener();
17+
$request = new Request('POST', '/dummy');
18+
19+
$request->setContent(
20+
http_build_query(
21+
array(
22+
'branch' => 'master',
23+
'exclude' => array(
24+
'aaa',
25+
'ccc'
26+
),
27+
'include' => array(
28+
'bbb'
29+
)
30+
)
31+
)
32+
);
33+
34+
$listener->preSend($request);
35+
36+
$this->assertEquals('branch=master&exclude=aaa&exclude=ccc&include=bbb', $request->getContent());
37+
}
38+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Bitbucket\Tests\API\Repositories;
4+
5+
use Bitbucket\Tests\API as Tests;
6+
use Bitbucket\API;
7+
8+
class CommitsTest extends Tests\TestCase
9+
{
10+
public function testGetAllRepositoryCommits()
11+
{
12+
$endpoint = 'repositories/gentle/eof/commits';
13+
$expectedResult = $this->fakeResponse(array('dummy'));
14+
15+
$client = $this->getHttpClientMock();
16+
$client->expects($this->once())
17+
->method('post')
18+
->with($endpoint)
19+
->will($this->returnValue($expectedResult));
20+
21+
/** @var \Bitbucket\API\Repositories\Commits $commits */
22+
$commits = $this->getClassMock('Bitbucket\API\Repositories\Commits', $client);
23+
$actual = $commits->all('gentle', 'eof', array('dummy'));
24+
25+
$this->assertEquals($expectedResult, $actual);
26+
}
27+
28+
public function testGetAllRepositoryCommitsFromSpecificBranch()
29+
{
30+
$endpoint = 'repositories/gentle/eof/commits/master';
31+
$expectedResult = $this->fakeResponse(array('dummy', 'branch' => 'master'));
32+
33+
$client = $this->getHttpClientMock();
34+
$client->expects($this->once())
35+
->method('post')
36+
->with($endpoint)
37+
->will($this->returnValue($expectedResult));
38+
39+
/** @var \Bitbucket\API\Repositories\Commits $commits */
40+
$commits = $this->getClassMock('Bitbucket\API\Repositories\Commits', $client);
41+
$actual = $commits->all('gentle', 'eof', array('dummy', 'branch' => 'master'));
42+
43+
$this->assertEquals($expectedResult, $actual);
44+
}
45+
46+
public function testGetSingleCommitInfo()
47+
{
48+
$endpoint = 'repositories/gentle/eof/commit/SHA';
49+
$expectedResult = $this->fakeResponse(array('dummy'));
50+
51+
$client = $this->getHttpClientMock();
52+
$client->expects($this->once())
53+
->method('get')
54+
->with($endpoint)
55+
->will($this->returnValue($expectedResult));
56+
57+
/** @var \Bitbucket\API\Repositories\Commits $commits */
58+
$commits = $this->getClassMock('Bitbucket\API\Repositories\Commits', $client);
59+
$actual = $commits->get('gentle', 'eof', 'SHA');
60+
61+
$this->assertEquals($expectedResult, $actual);
62+
}
63+
}

0 commit comments

Comments
 (0)