Skip to content

Commit f61e8bb

Browse files
vkerkhoffponvimishor
authored andcommitted
Create file thru Src api
The Repositories\Src Api had no option to create a file, the $params array holds the files that needs to be created. With the create method it's also possible to create branches. ref: Merged in vkerkhoffpon/bitbucket-api/feature/create-files-thru-api (pull request #40) closes #69
1 parent 3011661 commit f61e8bb

File tree

3 files changed

+118
-2
lines changed

3 files changed

+118
-2
lines changed

docs/examples/repositories/src.md

Lines changed: 36 additions & 2 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" %}
@@ -24,6 +23,41 @@ $src->get($account_name, $repo_slug, '1e10ffe', 'app/models/');
2423
$src->raw($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/Repositories/Src.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,34 @@ public function raw($account, $repo, $revision, $path)
5656
sprintf('repositories/%s/%s/raw/%s/%s', $account, $repo, $revision, $path)
5757
);
5858
}
59+
60+
/**
61+
* Create file in repository
62+
* $params contains the files to create, the key is the file (with path) to create and the value is the
63+
* data that will be written to the file.
64+
* See https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/src#post
65+
* for details on what options are available
66+
* @param $account
67+
* @param $repo
68+
* @param array $params
69+
* @return MessageInterface
70+
*/
71+
public function create($account, $repo, array $params = array())
72+
{
73+
$mandatory = array(
74+
'author' => '',
75+
'message' => '',
76+
);
77+
78+
$diff = array_diff(array_keys($mandatory), array_keys($params));
79+
80+
if (count($diff) > 0) {
81+
throw new \InvalidArgumentException('Missing parameters for creating new files.');
82+
}
83+
84+
return $this->getClient()->setApiVersion('2.0')->post(
85+
sprintf('repositories/%s/%s/src', $account, $repo),
86+
$params
87+
);
88+
}
5989
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,56 @@ public function testSrcGetRawContent()
4040

4141
$this->assertEquals($expectedResult, $actual);
4242
}
43+
44+
/**
45+
* @expectedException \InvalidArgumentException
46+
*/
47+
public function testSrcCreateWithInvalidParams()
48+
{
49+
/** @var \Bitbucket\API\Repositories\Src $src */
50+
$src = $this->getApiMock('Bitbucket\API\Repositories\Src');
51+
52+
$src->create('gentle', 'eof', array());
53+
$src->create('gentle', 'eof', array(3));
54+
}
55+
56+
public function testSrcCreateFile()
57+
{
58+
$endpoint = 'repositories/gentle/eof/src';
59+
$params = array(
60+
'/testfile' => 'dummy',
61+
'author' => 'Gentle <noreply@gentle.com>',
62+
'message' => 'Test commit'
63+
);
64+
65+
$client = $this->getHttpClientMock();
66+
$client->expects($this->once())
67+
->method('post')
68+
->with($endpoint, $params);
69+
70+
/** @var \Bitbucket\API\Repositories\Src $src */
71+
$src = $this->getClassMock('Bitbucket\API\Repositories\Src', $client);
72+
73+
$src->create('gentle', 'eof', $params);
74+
}
75+
76+
public function testSrcCreateBranch()
77+
{
78+
$endpoint = 'repositories/gentle/eof/src';
79+
$params = array(
80+
'branch' => 'new-branch',
81+
'author' => 'Gentle <noreply@gentle.com>',
82+
'message' => 'Test create branch'
83+
);
84+
85+
$client = $this->getHttpClientMock();
86+
$client->expects($this->once())
87+
->method('post')
88+
->with($endpoint, $params);
89+
90+
/** @var \Bitbucket\API\Repositories\Src $src */
91+
$src = $this->getClassMock('Bitbucket\API\Repositories\Src', $client);
92+
93+
$src->create('gentle', 'eof', $params);
94+
}
4395
}

0 commit comments

Comments
 (0)