Skip to content

Commit c590e19

Browse files
committed
Merge branch 'release/1.1.2.0'
2 parents 178734f + 94f7761 commit c590e19

File tree

19 files changed

+387
-80
lines changed

19 files changed

+387
-80
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
report/
33
build/
44
composer.lock
5+
.idea/

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ php:
88
- 7.0
99
- 7.1
1010
- 7.2
11-
- hhvm
1211

1312
matrix:
1413
fast_finish: true

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55

6+
## [Unreleased]
7+
8+
### Added:
9+
- Create a file through API via `Src:create` (PR #40)
10+
11+
### Changed:
12+
- Updated `Api:api` in order to support class name resolution via `::class` (PR #38)
13+
- Updated `BranchRestrictions` with newly available restriction types (PR #39)
14+
15+
616
## 1.1.2 / 2018-06-18
717

818
### Fixed:
919
- Request content was not set when a json was provided (issue #74)
1020

1121

12-
1322
## 1.1.1 / 2018-06-11
1423

1524
### Fixed:

docs/examples/repositories/refs/branches.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ $branches->all($account_name, $repo_slug);
2323
$branches->get($account_name, $repo_slug, $branch_name);
2424
```
2525

26+
### Delete an individual branch:
27+
28+
```php
29+
$branches->delete($account_name, $repo_slug, $branch_name);
30+
```
31+
2632
----
2733

2834
#### Related:

docs/examples/repositories/src.md

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ title: Source
66

77
# Source
88

9-
Allows you to browse directories and view files.
10-
*NOTE:* This is a read-only resource.
9+
Allows you to browse directories and view files, create branches and commit new files.
1110

1211
### Prepare:
1312
{% include auth.md var_name="src" class_ns="Repositories\Src" %}
@@ -21,9 +20,44 @@ $src->get($account_name, $repo_slug, '1e10ffe', 'app/models/');
2120
### Get raw content of an individual file:
2221

2322
```php
24-
$src->raw($account_name, $repo_slug, '1e10ffe', 'app/models/core.php');
23+
$src->get($account_name, $repo_slug, '1e10ffe', 'app/models/core.php');
2524
```
2625

26+
### Create file in repository
27+
28+
```php
29+
$params = array();
30+
$params['parent'] = 'master'; // Optional branch to commit to
31+
$params['/path-to-file'] = 'File content'; // Can be multiple files per commit
32+
$params['author'] = 'User <my@email.com>';
33+
$params['message'] = 'Commit message';
34+
35+
$src->create($account_name, $repo_slug, $params);
36+
```
37+
38+
### Delete file in repository
39+
40+
```php
41+
$params = array();
42+
$params['parent'] = 'master'; // Optional branch to commit to
43+
$params['files'] = '/file-to-delete';
44+
$params['author'] = 'User <my@email.com>';
45+
$params['message'] = 'Commit message';
46+
47+
$src->create($account_name, $repo_slug, $params);
48+
```
49+
50+
### Create new branch in repository
51+
52+
```php
53+
$params = array();
54+
$params['parent'] = 'master'; // Optional source branch
55+
$params['branch'] = 'new-branch-name';
56+
$params['author'] = 'User <my@email.com>';
57+
$params['message'] = 'Commit message';
58+
59+
$src->create($account_name, $repo_slug, $params);
60+
```
2761
----
2862

2963
#### Related:

lib/Bitbucket/API/Api.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,15 @@ public function api($name)
207207
throw new \InvalidArgumentException('No child specified.');
208208
}
209209

210-
/** @var Api $child */
211-
$class = '\\Bitbucket\\API\\'.$name;
210+
if (class_exists($name)) {
211+
$class = $name;
212+
} else {
213+
/** @var Api $child */
214+
$class = '\\Bitbucket\\API\\'.$name;
212215

213-
if (!class_exists($class)) {
214-
throw new \InvalidArgumentException(sprintf('No such child class [%s].', $name));
216+
if (!class_exists($class)) {
217+
throw new \InvalidArgumentException(sprintf('No such child class [%s].', $name));
218+
}
215219
}
216220

217221
$child = new $class();

lib/Bitbucket/API/Http/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function get($endpoint, $params = array(), $headers = array())
8181
$params = array();
8282
}
8383

84+
$endpoint = str_replace('~=', '~', urldecode($endpoint));
85+
8486
return $this->request($endpoint, $params, 'GET', $headers);
8587
}
8688

lib/Bitbucket/API/Repositories/BranchRestrictions.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
*/
2222
class BranchRestrictions extends Api
2323
{
24+
/**
25+
* Allowed restrictions to create a new branch permission for
26+
* @var array
27+
*/
28+
protected $allowedRestrictionTypes = array(
29+
'require_tasks_to_be_completed',
30+
'require_passing_builds_to_merge',
31+
'force',
32+
'require_all_dependencies_merged',
33+
'push',
34+
'require_approvals_to_merge',
35+
'enforce_merge_checks',
36+
'restrict_merges',
37+
'reset_pullrequest_approvals_on_change',
38+
'delete'
39+
);
40+
2441
/**
2542
* Get the information associated with a repository's branch restrictions
2643
*
@@ -64,7 +81,7 @@ public function create($account, $repo, $params = array())
6481

6582
$params = array_merge($defaults, $params);
6683

67-
if (empty($params['kind']) || !in_array($params['kind'], array('push', 'delete', 'force','restrict_merges'))) {
84+
if (empty($params['kind']) || !in_array($params['kind'], $this->allowedRestrictionTypes)) {
6885
throw new \InvalidArgumentException('Invalid restriction kind.');
6986
}
7087

@@ -142,4 +159,14 @@ public function delete($account, $repo, $id)
142159
sprintf('repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id)
143160
);
144161
}
162+
163+
/**
164+
* Add allowed permission types
165+
*
166+
* @param array $restrictions
167+
*/
168+
public function addAllowedRestrictionType($restrictions = array())
169+
{
170+
$this->allowedRestrictionTypes = array_merge($this->allowedRestrictionTypes, $restrictions);
171+
}
145172
}

lib/Bitbucket/API/Repositories/PullRequests/Comments.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public function get($account, $repo, $requestID, $commentID)
6666
*/
6767
public function create($account, $repo, $requestID, $content)
6868
{
69-
return $this->requestPost(
69+
return $this->getClient()->setApiVersion('2.0')->post(
7070
sprintf('repositories/%s/%s/pullrequests/%d/comments', $account, $repo, $requestID),
71-
array('content' => $content)
71+
json_encode(array('content' => array('raw' => $content))),
72+
array('Content-Type' => 'application/json')
7273
);
7374
}
7475

@@ -85,9 +86,10 @@ public function create($account, $repo, $requestID, $content)
8586
*/
8687
public function update($account, $repo, $requestID, $commentID, $content)
8788
{
88-
return $this->requestPut(
89+
return $this->getClient()->setApiVersion('2.0')->put(
8990
sprintf('repositories/%s/%s/pullrequests/%d/comments/%d', $account, $repo, $requestID, $commentID),
90-
array('content' => $content)
91+
json_encode(array('content' => array('raw' => $content))),
92+
array('Content-Type' => 'application/json')
9193
);
9294
}
9395

@@ -103,7 +105,7 @@ public function update($account, $repo, $requestID, $commentID, $content)
103105
*/
104106
public function delete($account, $repo, $requestID, $commentID)
105107
{
106-
return $this->requestDelete(
108+
return $this->getClient()->setApiVersion('2.0')->delete(
107109
sprintf('repositories/%s/%s/pullrequests/%d/comments/%d', $account, $repo, $requestID, $commentID)
108110
);
109111
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* This file is part of the bitbucket-api package.
4+
*
5+
* (c) Alexandru Guzinschi <alex@gentle.ro>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Bitbucket\API\Repositories;
11+
12+
use Bitbucket\API;
13+
use Buzz\Message\MessageInterface;
14+
15+
/**
16+
* @author Kevin Howe <kjhowe@gmail.com>
17+
*/
18+
class Refs extends API\Api
19+
{
20+
/**
21+
* Get a list of refs
22+
*
23+
* @access public
24+
* @param string $account The team or individual account owning the repository.
25+
* @param string $repo The repository identifier.
26+
* @param string|array $params GET parameters
27+
* @return MessageInterface
28+
*
29+
* @throws \InvalidArgumentException
30+
*/
31+
public function all($account, $repo, array $params = array())
32+
{
33+
return $this->getClient()->setApiVersion('2.0')->get(
34+
sprintf('repositories/%s/%s/refs', $account, $repo),
35+
$params
36+
);
37+
}
38+
}

lib/Bitbucket/API/Repositories/Refs/Branches.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,22 @@ public function get($account, $repo, $name)
5353
sprintf('repositories/%s/%s/refs/branches/%s', $account, $repo, $name)
5454
);
5555
}
56+
57+
/**
58+
* Deletes an individual branch
59+
*
60+
* @access public
61+
* @param string $account The team or individual account owning the repository.
62+
* @param string $repo The repository identifier.
63+
* @param string $name The branch identifier.
64+
* @return MessageInterface
65+
*
66+
* @throws \InvalidArgumentException
67+
*/
68+
public function delete($account, $repo, $name)
69+
{
70+
return $this->getClient()->setApiVersion('2.0')->delete(
71+
sprintf('repositories/%s/%s/refs/branches/%s', $account, $repo, $name)
72+
);
73+
}
5674
}

lib/Bitbucket/API/Repositories/Repository.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,12 @@ public function fork($account, $repo, $name, array $params = array())
196196
{
197197
$params['name'] = $name;
198198

199-
return $this->requestPost(
200-
sprintf('repositories/%s/%s/fork', $account, $repo),
201-
$params
199+
$params = json_encode($params);
200+
201+
return $this->getClient()->setApiVersion('2.0')->post(
202+
sprintf('repositories/%s/%s/forks', $account, $repo),
203+
$params,
204+
array('Content-Type' => 'application/json')
202205
);
203206
}
204207

@@ -249,17 +252,18 @@ public function manifest($account, $repo, $revision)
249252
}
250253

251254
/**
252-
* Get a list of tags
255+
* Get a pagination list of tags or tag object by name
253256
*
254257
* @access public
255258
* @param string $account The team or individual account owning the repository.
256259
* @param string $repo The repository identifier.
260+
* @param string $name The name of the tag
257261
* @return MessageInterface
258262
*/
259-
public function tags($account, $repo)
263+
public function tags($account, $repo, $name = '')
260264
{
261-
return $this->requestGet(
262-
sprintf('repositories/%s/%s/tags', $account, $repo)
265+
return $this->getClient()->setApiVersion('2.0')->get(
266+
sprintf('repositories/%s/%s/refs/tags/%s', $account, $repo, $name)
263267
);
264268
}
265269

lib/Bitbucket/API/Repositories/Src.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class Src extends API\Api
2525
{
2626
/**
27-
* Get a list of repo source
27+
* Get raw content of an individual file, or the contents of a directory
2828
*
2929
* @access public
3030
* @param string $account The team or individual account owning the repository.
@@ -35,25 +35,38 @@ class Src extends API\Api
3535
*/
3636
public function get($account, $repo, $revision, $path)
3737
{
38-
return $this->requestGet(
38+
return $this->getClient()->setApiVersion("2.0")->get(
3939
sprintf('repositories/%s/%s/src/%s/%s', $account, $repo, $revision, $path)
4040
);
4141
}
4242

4343
/**
44-
* Get raw content of an individual file
45-
*
46-
* @access public
47-
* @param string $account The team or individual account owning the repository.
48-
* @param string $repo The repository identifier.
49-
* @param string $revision A value representing the revision or branch to list.
50-
* @param string $path The path can be a filename or a directory path.
44+
* Create file in repository
45+
* $params contains the files to create, the key is the file (with path) to create and the value is the
46+
* data that will be written to the file.
47+
* See https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/src#post
48+
* for details on what options are available
49+
* @param $account
50+
* @param $repo
51+
* @param array $params
5152
* @return MessageInterface
5253
*/
53-
public function raw($account, $repo, $revision, $path)
54+
public function create($account, $repo, array $params = array())
5455
{
55-
return $this->requestGet(
56-
sprintf('repositories/%s/%s/raw/%s/%s', $account, $repo, $revision, $path)
56+
$mandatory = array(
57+
'author' => '',
58+
'message' => '',
59+
);
60+
61+
$diff = array_diff(array_keys($mandatory), array_keys($params));
62+
63+
if (count($diff) > 0) {
64+
throw new \InvalidArgumentException('Missing parameters for creating new files.');
65+
}
66+
67+
return $this->getClient()->setApiVersion('2.0')->post(
68+
sprintf('repositories/%s/%s/src', $account, $repo),
69+
$params
5770
);
5871
}
5972
}

0 commit comments

Comments
 (0)