From 3adad94bd6e391c40421d6129af16f2296666bda Mon Sep 17 00:00:00 2001 From: Piotr Rogowski Date: Fri, 26 Jun 2020 12:49:04 +0200 Subject: [PATCH] Add support for private packages --- src/Controller/ProxyController.php | 7 +++- src/Controller/RepoController.php | 20 +++++++++++ .../Controller/RepoControllerTest.php | 33 ++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Controller/ProxyController.php b/src/Controller/ProxyController.php index 343b4efc..1ad9b135 100644 --- a/src/Controller/ProxyController.php +++ b/src/Controller/ProxyController.php @@ -60,7 +60,12 @@ public function provider(string $package): JsonResponse } /** - * @Route("/p2/{package}.json", name="package_provider_v2", requirements={"package"="%package_name_pattern%"}, methods={"GET"}) + * @Route("/p2/{package}.json", + * name="package_provider_v2", + * host="repo.{domain}", + * defaults={"domain"="%domain%"}, + * requirements={"package"="%package_name_pattern%","domain"="%domain%"}, + * methods={"GET"}) */ public function providerV2(string $package): JsonResponse { diff --git a/src/Controller/RepoController.php b/src/Controller/RepoController.php index 58582537..f729ceba 100644 --- a/src/Controller/RepoController.php +++ b/src/Controller/RepoController.php @@ -6,6 +6,7 @@ use Buddy\Repman\Message\Organization\AddDownload; use Buddy\Repman\Query\User\Model\Organization; +use Buddy\Repman\Query\User\Model\PackageName; use Buddy\Repman\Query\User\PackageQuery; use Buddy\Repman\Service\Organization\PackageManager; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; @@ -36,6 +37,7 @@ public function packages(Organization $organization): JsonResponse { return new JsonResponse([ 'packages' => $this->packageManager->findProviders($organization->alias(), $this->packageQuery->getAllNames($organization->id())), + 'metadata-url' => '/p2/%package%.json', 'notify-batch' => $this->generateUrl('repo_package_downloads', [ 'organization' => $organization->alias(), ], UrlGeneratorInterface::ABSOLUTE_URL), @@ -108,6 +110,24 @@ public function downloads(Request $request, Organization $organization): JsonRes return new JsonResponse(['status' => 'success'], JsonResponse::HTTP_CREATED); } + /** + * @Route("/p2/{package}.json", + * host="{organization}.repo.{domain}", + * name="repo_package_provider_v2", + * methods={"GET"}, + * defaults={"domain":"%domain%"}, + * requirements={"domain"="%domain%","package"="%package_name_pattern%"}) + */ + public function providerV2(Organization $organization, string $package): JsonResponse + { + $providerData = $this->packageManager->findProviders( + $organization->alias(), + [new PackageName('', $package)] + ); + + return new JsonResponse($providerData === [] ? new \stdClass() : $providerData); + } + /** * @return array */ diff --git a/tests/Functional/Controller/RepoControllerTest.php b/tests/Functional/Controller/RepoControllerTest.php index 625a8593..38ee1f12 100644 --- a/tests/Functional/Controller/RepoControllerTest.php +++ b/tests/Functional/Controller/RepoControllerTest.php @@ -77,7 +77,8 @@ public function testOrganizationPackagesAction(): void self::assertMatchesPattern(' { - "packages": {}, + "packages": [], + "metadata-url": "/p2/%package%.json", "notify-batch": "http://buddy.repo.repman.wip/downloads", "search": "https://packagist.org/search.json?q=%query%&type=%type%", "mirrors": [ @@ -160,4 +161,34 @@ public function testAnonymousUserAccess(): void self::assertTrue($this->client->getResponse()->isOk()); } + + public function testProviderV2Action(): void + { + $adminId = $this->createAndLoginAdmin('test@buddy.works', 'secret'); + $this->fixtures->createToken($this->fixtures->createOrganization('buddy', $adminId), 'secret-org-token'); + + $this->client->request('GET', '/p2/buddy-works/repman.json', [], [], [ + 'HTTP_HOST' => 'buddy.repo.repman.wip', + 'PHP_AUTH_USER' => 'token', + 'PHP_AUTH_PW' => 'secret-org-token', + ]); + + self::assertTrue($this->client->getResponse()->isOk()); + + self::assertMatchesPattern(' + { + "buddy-works/repman": { + "1.2.3": { + "version": "1.2.3", + "version_normalized": "1.2.3.0", + "dist": { + "type": "zip", + "url": "/path/to/reference.zip", + "reference": "ac7dcaf888af2324cd14200769362129c8dd8550" + } + } + } + } + ', $this->client->getResponse()->getContent()); + } }