Skip to content

Commit d089a2b

Browse files
committed
implemented PullRequests::create (API 2.0)
1 parent 62c1030 commit d089a2b

File tree

4 files changed

+162
-32
lines changed

4 files changed

+162
-32
lines changed

docs/repositories/pullrequests.md

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,39 @@ $pull = new Bitbucket\API\Repositories\PullRequests();
99
$pull->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
1010
```
1111

12-
### Get all pull requests
12+
### Get all pull requests:
1313
```php
1414
$pull->all($account_name, $repo_slug);
1515
```
1616

17-
### Get all merged pull requests
17+
### Get all merged pull requests:
1818
```php
1919
$pull->all($account_name, $repo_slug, array('state' => 'merged'));
2020
```
2121

22-
### Get a list of a pull request comments:
23-
```php
24-
$pull->comments()->all($account_name, $repo_slug, 1)
25-
```
26-
27-
### Get an individual pull request comment:
28-
```php
29-
$pull->comments()->get($account_name, $repo_slug, 1, 2)
30-
```
31-
32-
### Add a new comment:
33-
```php
34-
$pull->comments()->create($account_name, $repo_slug, 41, "dummy content");
22+
### Create a new pull request:
23+
```php
24+
$pull->create('gentle', 'secret-repo', array(
25+
'title' => 'Test PR',
26+
'description' => 'Fixed readme',
27+
'source' => array(
28+
'branch' => array(
29+
'name' => 'quickfix-1'
30+
),
31+
'repository' => array(
32+
'full_name' => 'vimishor/secret-repo'
33+
)
34+
),
35+
'destination' => array(
36+
'branch' => array(
37+
'name' => 'master'
38+
)
39+
)
40+
));
3541
```
36-
37-
### Update an existing comment:
38-
```php
39-
$pull->comments()->update($account_name, $repo_slug, 41, 4, "dummy content [edited]");
40-
```
41-
42-
### Delete a pull request comment
43-
```php
44-
$pull->comments()->delete($account_name, $repo_slug, 41, 4);
45-
```
46-
4742
----
4843

4944
#### Related:
5045
* [Authentication](../authentication.md)
46+
* [PullRequests comments](pullrequests/comments.md)
5147
* [BB Wiki](https://confluence.atlassian.com/display/BITBUCKET/pullrequests+Resource#pullrequestsResource-Overview)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Pull requests comments
2+
3+
---
4+
Manage pull requests comments.
5+
6+
### Prepare:
7+
```php
8+
$pull = new Bitbucket\API\Repositories\PullRequests();
9+
$pull->setCredentials( new Bitbucket\API\Authentication\Basic($bb_user, $bb_pass) );
10+
```
11+
12+
### Get a list of a pull request comments:
13+
```php
14+
$pull->comments()->all($account_name, $repo_slug, 1)
15+
```
16+
17+
### Get an individual pull request comment:
18+
```php
19+
$pull->comments()->get($account_name, $repo_slug, 1, 2)
20+
```
21+
22+
### Add a new comment:
23+
```php
24+
$pull->comments()->create($account_name, $repo_slug, 41, "dummy content");
25+
```
26+
27+
### Update an existing comment:
28+
```php
29+
$pull->comments()->update($account_name, $repo_slug, 41, 4, "dummy content [edited]");
30+
```
31+
32+
### Delete a pull request comment
33+
```php
34+
$pull->comments()->delete($account_name, $repo_slug, 41, 4);
35+
```

lib/Bitbucket/API/Repositories/PullRequests.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Bitbucket\API\Repositories;
1313

1414
use Bitbucket\API;
15+
use Bitbucket\API\Utils;
16+
use Buzz\Message\MessageInterface;
1517

1618
/**
1719
* PullRequests class
@@ -38,10 +40,10 @@ public function comments()
3840
* Get a list of pull requests
3941
*
4042
* @access public
41-
* @param string $account The team or individual account owning the repository.
42-
* @param string $repo The repository identifier.
43-
* @param array $params Additional parameters
44-
* @return mixed
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
4547
*/
4648
public function all($account, $repo, $params = array())
4749
{
@@ -59,4 +61,39 @@ public function all($account, $repo, $params = array())
5961
$params
6062
);
6163
}
64+
65+
/**
66+
* Create a new pull request
67+
*
68+
* @access public
69+
* @param string $account The team or individual account owning the repository.
70+
* @param string $repo The repository identifier.
71+
* @param array $params Additional parameters
72+
* @return MessageInterface
73+
*
74+
* @see https://confluence.atlassian.com/x/XAZAGQ
75+
*/
76+
public function create($account, $repo, $params = array())
77+
{
78+
// allow developer to directly specify params as json if (s)he wants.
79+
if (!empty($params) && is_array($params)) {
80+
$params = json_encode(array_merge(
81+
array(
82+
'title' => 'New pull request',
83+
'source' => array(
84+
'branch' => array(
85+
'name' => 'develop'
86+
)
87+
)
88+
),
89+
$params
90+
));
91+
}
92+
93+
return $this->getClient()->setApiVersion('2.0')->post(
94+
sprintf('repositories/%s/%s/pullrequests', $account, $repo),
95+
$params,
96+
array('Content-Type' => 'application/json')
97+
);
98+
}
6299
}

test/Bitbucket/Tests/API/Repositories/PullRequestsTest.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,73 @@ public function testGetAllPullRequests()
1919
$pullRequests->expects($this->once())
2020
->method('requestGet')
2121
->with($endpoint)
22-
->will( $this->returnValue($expectedResult) );
22+
->will($this->returnValue($expectedResult));
2323

2424
/** @var $pullRequests \Bitbucket\API\Repositories\PullRequests */
2525
$actual = $pullRequests->all('gentle', 'eof');
2626

2727
$this->assertEquals($expectedResult, $actual);
2828
}
29-
}
29+
30+
public function testCreateNewPullRequestFromJSON()
31+
{
32+
$endpoint = 'repositories/gentle/eof/pullrequests';
33+
$params = json_encode(array(
34+
'title' => 'Test PR',
35+
'source' => array(
36+
'branch' => array(
37+
'name' => 'quickfix-1'
38+
),
39+
'repository' => array(
40+
'full_name' => 'vimishor/secret-repo'
41+
)
42+
),
43+
'destination' => array(
44+
'branch' => array(
45+
'name' => 'master'
46+
)
47+
)
48+
));
49+
50+
$client = $this->getHttpClientMock();
51+
$client->expects($this->once())
52+
->method('post')
53+
->with($endpoint, $params);
54+
55+
/** @var \Bitbucket\API\Repositories\PullRequests $pull */
56+
$pull = $this->getClassMock('Bitbucket\API\Repositories\PullRequests', $client);
57+
58+
$pull->create('gentle', 'eof', $params);
59+
}
60+
61+
public function testCreateNewPullRequestFromArray()
62+
{
63+
$endpoint = 'repositories/gentle/eof/pullrequests';
64+
$params = array(
65+
'title' => 'Test PR',
66+
'source' => array(
67+
'branch' => array(
68+
'name' => 'quickfix-1'
69+
),
70+
'repository' => array(
71+
'full_name' => 'vimishor/secret-repo'
72+
)
73+
),
74+
'destination' => array(
75+
'branch' => array(
76+
'name' => 'master'
77+
)
78+
),
79+
);
80+
81+
$client = $this->getHttpClientMock();
82+
$client->expects($this->once())
83+
->method('post')
84+
->with($endpoint, json_encode($params));
85+
86+
/** @var \Bitbucket\API\Repositories\PullRequests $pull */
87+
$pull = $this->getClassMock('Bitbucket\API\Repositories\PullRequests', $client);
88+
89+
$pull->create('gentle', 'eof', $params);
90+
}
91+
}

0 commit comments

Comments
 (0)