From 545f0180079221dae3a5d66e8095371963b4a5f1 Mon Sep 17 00:00:00 2001 From: Adrian David Castro Tenemaya Date: Wed, 16 Feb 2022 12:53:42 +0100 Subject: [PATCH 1/3] [11.8] Added search API --- src/Api/Projects.php | 2 ++ src/Api/Search.php | 67 ++++++++++++++++++++++++++++++++++++++++ src/Client.php | 9 ++++++ tests/Api/SearchTest.php | 57 ++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 src/Api/Search.php create mode 100644 tests/Api/SearchTest.php diff --git a/src/Api/Projects.php b/src/Api/Projects.php index e96ee96c..420fefd1 100644 --- a/src/Api/Projects.php +++ b/src/Api/Projects.php @@ -78,6 +78,8 @@ public function all(array $parameters = []) 'id', 'name', 'path', 'created_at', 'updated_at', 'last_activity_at', 'repository_size', 'storage_size', 'packages_size', 'wiki_size', ]; + $resolver->setDefined('pagination'); + $resolver->setDefined('per_page'); $resolver->setDefined('order_by') ->setAllowedValues('order_by', $orderBy) ; diff --git a/src/Api/Search.php b/src/Api/Search.php new file mode 100644 index 00000000..e1d0fb54 --- /dev/null +++ b/src/Api/Search.php @@ -0,0 +1,67 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Api; + +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; +use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; +use Symfony\Component\OptionsResolver\Options; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class Search extends AbstractApi +{ + /** + * @param array $parameters { + * + * @var string $scope The scope to search in + * @var string $search The search query + * @var string $state Filter by state. Issues and merge requests are supported; it is ignored for other scopes. + * @var bool $confidential Filter by confidentiality. Issues scope is supported; it is ignored for other scopes. + * @var string $order_by Allowed values are created_at only. If this is not set, the results are either sorted by created_at in descending order for basic search, or by the most relevant documents when using advanced search. + * @var string $sort Return projects sorted in asc or desc order (default is desc) + * } + * + * @throws UndefinedOptionsException If an option name is undefined + * @throws InvalidOptionsException If an option doesn't fulfill the + * specified validation rules + * + * @return mixed + */ + public function all(array $parameters = []) + { + $resolver = $this->createOptionsResolver(); + $booleanNormalizer = function (Options $resolver, $value): string { + return $value ? 'true' : 'false'; + }; + $resolver->setDefined('confidential') + ->setAllowedTypes('confidential', 'bool') + ->setNormalizer('confidential', $booleanNormalizer); + $scope = [ + 'projects', + 'issues', + 'merge_requests', + 'milestones', + 'snippet_titles', + 'users' + ]; + $resolver->setDefined('scope') + ->setAllowedValues('scope', $scope); + $resolver->setDefined('search'); + $resolver->setDefined('order_by') + ->setAllowedValues('order_by', ['created_at']); + $resolver->setDefined('sort') + ->setAllowedValues('sort', ['asc', 'desc']); + return $this->get('search', $resolver->resolve($parameters)); + } +} diff --git a/src/Client.php b/src/Client.php index 872d8369..153c630e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -38,6 +38,7 @@ use Gitlab\Api\ResourceMilestoneEvents; use Gitlab\Api\ResourceStateEvents; use Gitlab\Api\ResourceWeightEvents; +use Gitlab\Api\Search; use Gitlab\Api\Schedules; use Gitlab\Api\Snippets; use Gitlab\Api\SystemHooks; @@ -335,6 +336,14 @@ public function repositoryFiles(): RepositoryFiles return new RepositoryFiles($this); } + /** + * @return Search + */ + public function search(): Search + { + return new Search($this); + } + /** * @return Schedules */ diff --git a/tests/Api/SearchTest.php b/tests/Api/SearchTest.php new file mode 100644 index 00000000..44a9ebbd --- /dev/null +++ b/tests/Api/SearchTest.php @@ -0,0 +1,57 @@ + + * (c) Graham Campbell + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gitlab\Tests\Api; + +use Gitlab\Api\Search; + +class SearchTest extends TestCase +{ + /** + * @test + */ + public function shouldGetAll(): void + { + $expectedArray = [ + ['id' => 6, 'name' => 'Project 6 bla'], + ['id' => 7, 'name' => 'Project 7 bla'], + ['id' => 8, 'name' => 'Project 8 bla'], + ]; + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('get') + ->with('search', [ + 'scope' => 'projects', + 'confidential' => 'false', + 'search' => 'bla', + 'order_by' => 'created_at', + 'sort' => 'desc' + ]) + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->all([ + 'scope' => 'projects', + 'confidential' => false, + 'search' => 'bla', + 'order_by' => 'created_at', + 'sort' => 'desc' + ])); + } + + protected function getApiClass() + { + return Search::class; + } +} From 1bb7456a79ee9cac2c6b4d73c38db4e28affbf57 Mon Sep 17 00:00:00 2001 From: Adrian David Castro Tenemaya Date: Wed, 16 Feb 2022 12:58:03 +0100 Subject: [PATCH 2/3] Fixed lint issues --- src/Api/Search.php | 3 +-- src/Client.php | 2 +- tests/Api/SearchTest.php | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Api/Search.php b/src/Api/Search.php index e1d0fb54..3fd2c4bf 100644 --- a/src/Api/Search.php +++ b/src/Api/Search.php @@ -17,7 +17,6 @@ use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException; use Symfony\Component\OptionsResolver\Options; -use Symfony\Component\OptionsResolver\OptionsResolver; class Search extends AbstractApi { @@ -53,7 +52,7 @@ public function all(array $parameters = []) 'merge_requests', 'milestones', 'snippet_titles', - 'users' + 'users', ]; $resolver->setDefined('scope') ->setAllowedValues('scope', $scope); diff --git a/src/Client.php b/src/Client.php index 153c630e..9b2ece76 100644 --- a/src/Client.php +++ b/src/Client.php @@ -38,8 +38,8 @@ use Gitlab\Api\ResourceMilestoneEvents; use Gitlab\Api\ResourceStateEvents; use Gitlab\Api\ResourceWeightEvents; -use Gitlab\Api\Search; use Gitlab\Api\Schedules; +use Gitlab\Api\Search; use Gitlab\Api\Snippets; use Gitlab\Api\SystemHooks; use Gitlab\Api\Tags; diff --git a/tests/Api/SearchTest.php b/tests/Api/SearchTest.php index 44a9ebbd..e1e7c991 100644 --- a/tests/Api/SearchTest.php +++ b/tests/Api/SearchTest.php @@ -37,7 +37,7 @@ public function shouldGetAll(): void 'confidential' => 'false', 'search' => 'bla', 'order_by' => 'created_at', - 'sort' => 'desc' + 'sort' => 'desc', ]) ->will($this->returnValue($expectedArray)); @@ -46,7 +46,7 @@ public function shouldGetAll(): void 'confidential' => false, 'search' => 'bla', 'order_by' => 'created_at', - 'sort' => 'desc' + 'sort' => 'desc', ])); } From ac4b11a36f936b602616bf8fcfe5f9990592a352 Mon Sep 17 00:00:00 2001 From: Adrian David Castro Tenemaya Date: Wed, 16 Feb 2022 12:58:41 +0100 Subject: [PATCH 3/3] Fixed last issue with Search API --- src/Api/Search.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Api/Search.php b/src/Api/Search.php index 3fd2c4bf..aea4b7fe 100644 --- a/src/Api/Search.php +++ b/src/Api/Search.php @@ -61,6 +61,7 @@ public function all(array $parameters = []) ->setAllowedValues('order_by', ['created_at']); $resolver->setDefined('sort') ->setAllowedValues('sort', ['asc', 'desc']); + return $this->get('search', $resolver->resolve($parameters)); } }