Skip to content

Multi files commit #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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