Skip to content

Commit

Permalink
Merge pull request #234 from fbourigault/multi-files-commit
Browse files Browse the repository at this point in the history
Multi files commit
  • Loading branch information
fbourigault committed Aug 18, 2017
2 parents 9dd701c + 09fe8a6 commit 8339237
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Gitlab/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar

$body = null;
if (empty($files) && !empty($parameters)) {
$body = $this->streamFactory->createStream(http_build_query($parameters));
$body = $this->streamFactory->createStream(QueryStringBuilder::build($parameters));
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
} elseif (!empty($files)) {
$builder = new MultipartStreamBuilder($this->streamFactory);
Expand Down
65 changes: 65 additions & 0 deletions lib/Gitlab/Api/Repositories.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php namespace Gitlab\Api;

use Symfony\Component\OptionsResolver\OptionsResolver;

class Repositories extends AbstractApi
{
/**
Expand Down Expand Up @@ -168,6 +170,69 @@ public function commit($project_id, $sha)
return $this->get($this->getProjectPath($project_id, 'repository/commits/'.$this->encodePath($sha)));
}

/**
* @param int $project_id
* @param array $parameters (
*
* @var string $branch Name of the branch to commit into. To create a new branch, also provide start_branch.
* @var string $commit_message Commit message.
* @var string $start_branch Name of the branch to start the new commit from.
* @var array $actions (
*
* @var string $action he action to perform, create, delete, move, update.
* @var string $file_path Full path to the file.
* @var string $previous_path Original full path to the file being moved.
* @var string $content File content, required for all except delete. Optional for move.
* @var string $encoding text or base64. text is default.
* )
* @var string $author_email Specify the commit author's email address.
* @var string $author_name Specify the commit author's name.
* )
*
* @return mixed
*/
public function createCommit($project_id, array $parameters = [])
{
$resolver = new OptionsResolver();
$resolver->setDefined('branch')
->setRequired('branch')
;
$resolver->setDefined('commit_message')
->setRequired('commit_message')
;
$resolver->setDefined('start_branch');
$resolver->setDefined('actions')
->setRequired('actions')
->setAllowedTypes('actions', 'array')
->setAllowedValues('actions', function (array $actions) {
return !empty($actions);
})
->setNormalizer('actions', function (OptionsResolver $resolver, array $actions) {
$actionsOptionsResolver = new OptionsResolver();
$actionsOptionsResolver->setDefined('action')
->setRequired('action')
->setAllowedValues('action', ['create', 'delete', 'move', 'update'])
;
$actionsOptionsResolver->setDefined('file_path')
->setRequired('file_path')
;
$actionsOptionsResolver->setDefined('previous_path');
$actionsOptionsResolver->setDefined('content');
$actionsOptionsResolver->setDefined('encoding')
->setAllowedValues('encoding', ['test', 'base64'])
;

return array_map(function ($action) use ($actionsOptionsResolver) {
return $actionsOptionsResolver->resolve($action);
}, $actions);
})
;
$resolver->setDefined('author_email');
$resolver->setDefined('author_name');

return $this->post($this->getProjectPath($project_id, 'repository/commits'), $resolver->resolve($parameters));
}

/**
* @param int $project_id
* @param string $sha
Expand Down
34 changes: 34 additions & 0 deletions test/Gitlab/Tests/Api/RepositoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@ public function shouldGetCommit()
$this->assertEquals($expectedArray, $api->commit(1, 'abcd1234'));
}

/**
* @test
*/
public function shouldCreateCommit()
{
$expectedArray = array('title' => 'Initial commit.', 'author_name' => 'John Doe', 'author_email' => 'john@example.com');

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('projects/1/repository/commits')
->will($this->returnValue($expectedArray))
;

$this->assertEquals($expectedArray, $api->createCommit(1, [
'branch' => 'master',
'commit_message' => 'Initial commit.',
'actions' => [
[
'action' => 'create',
'file_path' => 'README.md',
'content' => '# My new project',
],
[
'action' => 'create',
'file_path' => 'LICENSE',
'content' => 'MIT License...',
],
],
'author_name' => 'John Doe',
'author_email' => 'john@example.com',
]));
}

/**
* @test
*/
Expand Down

0 comments on commit 8339237

Please sign in to comment.