Skip to content

Commit 09fe8a6

Browse files
committed
add createCommit
1 parent d4459e3 commit 09fe8a6

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

lib/Gitlab/Api/Repositories.php

+65
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php namespace Gitlab\Api;
22

3+
use Symfony\Component\OptionsResolver\OptionsResolver;
4+
35
class Repositories extends AbstractApi
46
{
57
/**
@@ -168,6 +170,69 @@ public function commit($project_id, $sha)
168170
return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha)));
169171
}
170172

173+
/**
174+
* @param int $project_id
175+
* @param array $parameters (
176+
*
177+
* @var string $branch Name of the branch to commit into. To create a new branch, also provide start_branch.
178+
* @var string $commit_message Commit message.
179+
* @var string $start_branch Name of the branch to start the new commit from.
180+
* @var array $actions (
181+
*
182+
* @var string $action he action to perform, create, delete, move, update.
183+
* @var string $file_path Full path to the file.
184+
* @var string $previous_path Original full path to the file being moved.
185+
* @var string $content File content, required for all except delete. Optional for move.
186+
* @var string $encoding text or base64. text is default.
187+
* )
188+
* @var string $author_email Specify the commit author's email address.
189+
* @var string $author_name Specify the commit author's name.
190+
* )
191+
*
192+
* @return mixed
193+
*/
194+
public function createCommit($project_id, array $parameters = [])
195+
{
196+
$resolver = new OptionsResolver();
197+
$resolver->setDefined('branch')
198+
->setRequired('branch')
199+
;
200+
$resolver->setDefined('commit_message')
201+
->setRequired('commit_message')
202+
;
203+
$resolver->setDefined('start_branch');
204+
$resolver->setDefined('actions')
205+
->setRequired('actions')
206+
->setAllowedTypes('actions', 'array')
207+
->setAllowedValues('actions', function (array $actions) {
208+
return !empty($actions);
209+
})
210+
->setNormalizer('actions', function (OptionsResolver $resolver, array $actions) {
211+
$actionsOptionsResolver = new OptionsResolver();
212+
$actionsOptionsResolver->setDefined('action')
213+
->setRequired('action')
214+
->setAllowedValues('action', ['create', 'delete', 'move', 'update'])
215+
;
216+
$actionsOptionsResolver->setDefined('file_path')
217+
->setRequired('file_path')
218+
;
219+
$actionsOptionsResolver->setDefined('previous_path');
220+
$actionsOptionsResolver->setDefined('content');
221+
$actionsOptionsResolver->setDefined('encoding')
222+
->setAllowedValues('encoding', ['test', 'base64'])
223+
;
224+
225+
return array_map(function ($action) use ($actionsOptionsResolver) {
226+
return $actionsOptionsResolver->resolve($action);
227+
}, $actions);
228+
})
229+
;
230+
$resolver->setDefined('author_email');
231+
$resolver->setDefined('author_name');
232+
233+
return $this->post($this->getProjectPath($project_id, 'repository/commits'), $resolver->resolve($parameters));
234+
}
235+
171236
/**
172237
* @param int $project_id
173238
* @param string $sha

test/Gitlab/Tests/Api/RepositoriesTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,40 @@ public function shouldGetCommit()
274274
$this->assertEquals($expectedArray, $api->commit(1, 'abcd1234'));
275275
}
276276

277+
/**
278+
* @test
279+
*/
280+
public function shouldCreateCommit()
281+
{
282+
$expectedArray = array('title' => 'Initial commit.', 'author_name' => 'John Doe', 'author_email' => 'john@example.com');
283+
284+
$api = $this->getApiMock();
285+
$api->expects($this->once())
286+
->method('post')
287+
->with('projects/1/repository/commits')
288+
->will($this->returnValue($expectedArray))
289+
;
290+
291+
$this->assertEquals($expectedArray, $api->createCommit(1, [
292+
'branch' => 'master',
293+
'commit_message' => 'Initial commit.',
294+
'actions' => [
295+
[
296+
'action' => 'create',
297+
'file_path' => 'README.md',
298+
'content' => '# My new project',
299+
],
300+
[
301+
'action' => 'create',
302+
'file_path' => 'LICENSE',
303+
'content' => 'MIT License...',
304+
],
305+
],
306+
'author_name' => 'John Doe',
307+
'author_email' => 'john@example.com',
308+
]));
309+
}
310+
277311
/**
278312
* @test
279313
*/

0 commit comments

Comments
 (0)