From d4459e3a0948001db57ecd3c678c21f7a8fdd213 Mon Sep 17 00:00:00 2001 From: Fabien Bourigault Date: Wed, 16 Aug 2017 16:32:56 +0200 Subject: [PATCH 1/2] use QueryStringBuilder for POST requests --- lib/Gitlab/Api/AbstractApi.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Gitlab/Api/AbstractApi.php b/lib/Gitlab/Api/AbstractApi.php index 7c5cfe65d..ef9120a20 100644 --- a/lib/Gitlab/Api/AbstractApi.php +++ b/lib/Gitlab/Api/AbstractApi.php @@ -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); From 09fe8a644adff2c6794d2e4080fb406d2c99922b Mon Sep 17 00:00:00 2001 From: Fabien Bourigault Date: Wed, 16 Aug 2017 16:44:30 +0200 Subject: [PATCH 2/2] add createCommit --- lib/Gitlab/Api/Repositories.php | 65 ++++++++++++++++++++++ test/Gitlab/Tests/Api/RepositoriesTest.php | 34 +++++++++++ 2 files changed, 99 insertions(+) diff --git a/lib/Gitlab/Api/Repositories.php b/lib/Gitlab/Api/Repositories.php index 752e909a0..e67d79f4a 100644 --- a/lib/Gitlab/Api/Repositories.php +++ b/lib/Gitlab/Api/Repositories.php @@ -1,5 +1,7 @@ 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 diff --git a/test/Gitlab/Tests/Api/RepositoriesTest.php b/test/Gitlab/Tests/Api/RepositoriesTest.php index fcad85309..64cc8bc16 100644 --- a/test/Gitlab/Tests/Api/RepositoriesTest.php +++ b/test/Gitlab/Tests/Api/RepositoriesTest.php @@ -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 */