Skip to content

Commit 1e5bae6

Browse files
committed
mmandatory parameters inside PullRequest's methods are now checked.
1 parent 1bab876 commit 1e5bae6

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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
/**

lib/Bitbucket/API/Repositories/PullRequests.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,32 @@ public function comments()
4141
* @param string $repo The repository identifier.
4242
* @param array $params Additional parameters
4343
* @return MessageInterface
44+
*
45+
* @throws \InvalidArgumentException
4446
*/
4547
public function all($account, $repo, $params = array())
4648
{
49+
$states = array('OPEN', 'MERGED', 'DECLINED');
4750
$params = array_merge(
4851
array(
4952
'state' => 'OPEN'
5053
),
5154
$params
5255
);
5356

57+
if (!is_array($params['state'])) {
58+
$params['state'] = array($params['state']);
59+
}
60+
61+
array_walk(
62+
$params['state'],
63+
function ($state) use ($states) {
64+
if (!in_array($state, $states)) {
65+
throw new \InvalidArgumentException(sprintf('Unknown `state` %s', $state));
66+
}
67+
}
68+
);
69+
5470
return $this->getClient()->setApiVersion('2.0')->get(
5571
sprintf('repositories/%s/%s/pullrequests', $account, $repo),
5672
$params
@@ -66,6 +82,7 @@ public function all($account, $repo, $params = array())
6682
* @param array $params Additional parameters
6783
* @return MessageInterface
6884
*
85+
* @throws \InvalidArgumentException
6986
* @see https://confluence.atlassian.com/x/XAZAGQ
7087
*/
7188
public function create($account, $repo, $params = array())
@@ -85,6 +102,14 @@ public function create($account, $repo, $params = array())
85102
));
86103
}
87104

105+
if (empty($params['title'])) {
106+
throw new \InvalidArgumentException('Pull request\'s title must be specified.');
107+
}
108+
109+
if (empty($params['source']['branch']['name'])) {
110+
throw new \InvalidArgumentException('Pull request\'s source branch name must be specified.');
111+
}
112+
88113
return $this->getClient()->setApiVersion('2.0')->post(
89114
sprintf('repositories/%s/%s/pullrequests', $account, $repo),
90115
$params,
@@ -101,6 +126,8 @@ public function create($account, $repo, $params = array())
101126
* @param int $id ID of the pull request that will be updated
102127
* @param array $params Additional parameters
103128
* @return MessageInterface
129+
*
130+
* @throws \InvalidArgumentException
104131
*/
105132
public function update($account, $repo, $id, $params = array())
106133
{
@@ -119,6 +146,14 @@ public function update($account, $repo, $id, $params = array())
119146
));
120147
}
121148

149+
if (empty($params['title'])) {
150+
throw new \InvalidArgumentException('Pull request\'s title must be specified.');
151+
}
152+
153+
if (empty($params['destination']['branch']['name'])) {
154+
throw new \InvalidArgumentException('Pull request\'s destination branch name must be specified.');
155+
}
156+
122157
return $this->getClient()->setApiVersion('2.0')->put(
123158
sprintf('repositories/%s/%s/pullrequests/%d', $account, $repo, $id),
124159
$params,

0 commit comments

Comments
 (0)