Skip to content

Commit e0b6320

Browse files
committed
Fixes #26
Request body was build the wrong way when no additional params where passed to Repository::create() Added additional test which should cover this issue.
1 parent 19d6e2a commit e0b6320

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

lib/Bitbucket/API/Repositories/Repository.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function get($account, $repo)
4848
* @param array $params Additional parameters
4949
* @return MessageInterface
5050
*
51+
* @throws \InvalidArgumentException If invalid JSON is provided.
52+
*
5153
* @see https://confluence.atlassian.com/x/WwZAGQ
5254
*/
5355
public function create($account, $repo, $params = array())
@@ -58,20 +60,25 @@ public function create($account, $repo, $params = array())
5860
return $this->createLegacy($account, $repo);
5961
}
6062

63+
$defaults = array(
64+
'scm' => 'git',
65+
'name' => $repo,
66+
'is_private' => true,
67+
'description' => 'My secret repo',
68+
'forking_policy' => 'no_forks',
69+
);
70+
6171
// allow developer to directly specify params as json if (s)he wants.
62-
if (!empty($params) && is_array($params)) {
63-
$params = json_encode(array_merge(
64-
array(
65-
'scm' => 'git',
66-
'name' => $repo,
67-
'is_private' => true,
68-
'description' => 'My secret repo',
69-
'forking_policy' => 'no_forks',
70-
),
71-
$params
72-
));
72+
if ('string' === gettype($params)) {
73+
$params = json_decode($params, true);
74+
75+
if (JSON_ERROR_NONE !== json_last_error()) {
76+
throw new \InvalidArgumentException('Invalid JSON provided.');
77+
}
7378
}
7479

80+
$params = json_encode(array_merge($defaults, $params));
81+
7582
return $this->getClient()->setApiVersion('2.0')->post(
7683
sprintf('repositories/%s/%s', $account, $repo),
7784
$params,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,31 @@ public function testCreateRepositoryFromArray()
6969
$repo->create('gentle', 'new-repo', $params);
7070
}
7171

72+
/**
73+
* @ticket 26
74+
*/
75+
public function testCreateRepositoryWithDefaultParams()
76+
{
77+
$endpoint = 'repositories/gentle/new-repo';
78+
$params = array(
79+
'scm' => 'git',
80+
'name' => 'new-repo',
81+
'is_private' => true,
82+
'description' => 'My secret repo',
83+
'forking_policy' => 'no_forks',
84+
);
85+
86+
$client = $this->getHttpClientMock();
87+
$client->expects($this->once())
88+
->method('post')
89+
->with($endpoint, json_encode($params));
90+
91+
/** @var \Bitbucket\API\Repositories\Repository $repo */
92+
$repo = $this->getClassMock('Bitbucket\API\Repositories\Repository', $client);
93+
94+
$repo->create('gentle', 'new-repo', array());
95+
}
96+
7297
public function testCreateRepositorySuccess()
7398
{
7499
$endpoint = 'repositories';

0 commit comments

Comments
 (0)