Skip to content

Commit 3d75413

Browse files
committed
Merge branch 'feature/api-v2' into develop
2 parents 8da2564 + 739fbfe commit 3d75413

File tree

10 files changed

+458
-0
lines changed

10 files changed

+458
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
### Get an individual commit: (API 2.0)
25+
```php
26+
$commits->get($account_name, $repo_slug, $commitSHA1);
27+
```
28+
29+
### Approve a commit: (API 2.0)
30+
```php
31+
$commits->approve($account_name, $repo_slug, $commitSHA1);
32+
```
33+
34+
### Delete a commit approval: (API 2.0)
35+
```php
36+
$commits->deleteApproval($account_name, $repo_slug, $commitSHA1);
37+
```
38+
39+
----
40+
41+
#### Related:
42+
* [Authentication](../authentication.md)
43+
* [Commit(s) comments](commits/comments.md)
44+
* [BB Wiki](https://confluence.atlassian.com/x/doA7Fw)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Commits comments
2+
3+
---
4+
Manage commits comments.
5+
6+
### Prepare:
7+
```php
8+
$commit = new Bitbucket\API\Repositories\Commits();
9+
$commit->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
10+
```
11+
12+
### Get a list of a commit comments: (API 2.0)
13+
```php
14+
$commit->comments()->all($account_name, $repo_slug, $commitSHA1)
15+
```
16+
17+
### Get an individual commit comment: (API 2.0)
18+
```php
19+
$commit->comments()->get($account_name, $repo_slug, $commitSHA1, $commentID)
20+
```
21+
22+
----
23+
24+
#### Related:
25+
* [Authentication](../../authentication.md)
26+
* [Commit(s)](../commits.md)

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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
62+
/**
63+
* Approve a commit
64+
*
65+
* @access public
66+
* @param string $account The team or individual account owning the repository.
67+
* @param string $repo The repository identifier.
68+
* @param string $revision A SHA1 value for the commit.
69+
* @return MessageInterface
70+
*/
71+
public function approve($account, $repo, $revision)
72+
{
73+
return $this->getClient()->setApiVersion('2.0')->post(
74+
sprintf('repositories/%s/%s/commit/%s/approve', $account, $repo, $revision)
75+
);
76+
}
77+
78+
/**
79+
* Delete a commit approval
80+
*
81+
* NOTE: On success returns `HTTP/1.1 204 NO CONTENT`
82+
*
83+
* @access public
84+
* @param string $account The team or individual account owning the repository.
85+
* @param string $repo The repository identifier.
86+
* @param string $revision A SHA1 value for the commit.
87+
* @return MessageInterface
88+
*/
89+
public function deleteApproval($account, $repo, $revision)
90+
{
91+
return $this->getClient()->setApiVersion('2.0')->delete(
92+
sprintf('repositories/%s/%s/commit/%s/approve', $account, $repo, $revision)
93+
);
94+
}
95+
96+
/**
97+
* Get comments
98+
*
99+
* @access public
100+
* @return Commits\Comments
101+
* @codeCoverageIgnore
102+
*/
103+
public function comments()
104+
{
105+
return $this->childFactory('Repositories\\Commits\\Comments');
106+
}
107+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Commits;
13+
14+
use Bitbucket\API\Api;
15+
use Buzz\Message\MessageInterface;
16+
17+
/**
18+
* @author Alexandru G. <alex@gentle.ro>
19+
*/
20+
class Comments extends Api
21+
{
22+
/**
23+
* Get a list of a commit comments
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 string $revision A SHA1 value for the commit.
29+
* @return MessageInterface
30+
*/
31+
public function all($account, $repo, $revision)
32+
{
33+
return $this->getClient()->setApiVersion('2.0')->get(
34+
sprintf('repositories/%s/%s/commit/%s/comments', $account, $repo, $revision)
35+
);
36+
}
37+
38+
/**
39+
* Get an individual commit comment
40+
*
41+
* @access public
42+
* @param string $account The team or individual account owning the repository.
43+
* @param string $repo The repository identifier.
44+
* @param string $revision A SHA1 value for the commit.
45+
* @param int $commentID The comment identifier.
46+
* @return MessageInterface
47+
*/
48+
public function get($account, $repo, $revision, $commentID)
49+
{
50+
return $this->getClient()->setApiVersion('2.0')->get(
51+
sprintf('repositories/%s/%s/commit/%s/comments/%d', $account, $repo, $revision, $commentID)
52+
);
53+
}
54+
}
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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Bitbucket\Tests\API\Repositories\PullRequest;
4+
5+
use Bitbucket\Tests\API as Tests;
6+
use Bitbucket\API;
7+
8+
class CommitsTest extends Tests\TestCase
9+
{
10+
public function testGetAllComments()
11+
{
12+
$endpoint = 'repositories/gentle/eof/commit/SHA1/comments';
13+
$expectedResult = $this->fakeResponse(array('dummy'));
14+
15+
$client = $this->getHttpClientMock();
16+
$client->expects($this->once())
17+
->method('get')
18+
->with($endpoint)
19+
->will($this->returnValue($expectedResult));
20+
21+
/** @var \Bitbucket\API\Repositories\Commits\Comments $comments */
22+
$comments = $this->getClassMock('Bitbucket\API\Repositories\Commits\Comments', $client);
23+
$actual = $comments->all('gentle', 'eof', 'SHA1');
24+
25+
$this->assertEquals($expectedResult, $actual);
26+
}
27+
28+
public function testGetSingleComment()
29+
{
30+
$endpoint = 'repositories/gentle/eof/commit/SHA1/comments/1';
31+
$expectedResult = $this->fakeResponse(array('dummy'));
32+
33+
$client = $this->getHttpClientMock();
34+
$client->expects($this->once())
35+
->method('get')
36+
->with($endpoint)
37+
->will($this->returnValue($expectedResult));
38+
39+
/** @var \Bitbucket\API\Repositories\Commits\Comments $comments */
40+
$comments = $this->getClassMock('Bitbucket\API\Repositories\Commits\Comments', $client);
41+
$actual = $comments->get('gentle', 'eof', 'SHA1', 1);
42+
43+
$this->assertEquals($expectedResult, $actual);
44+
}
45+
}

0 commit comments

Comments
 (0)