-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Command; | ||
|
||
use Buddy\Repman\Entity\Organization; | ||
use Buddy\Repman\Message\Organization\AddPackage; | ||
use Buddy\Repman\Message\Organization\SynchronizePackage; | ||
use Buddy\Repman\Repository\OrganizationRepository; | ||
use Ramsey\Uuid\Uuid; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
final class AddPackageCommand extends Command | ||
{ | ||
protected static $defaultName = 'repman:package:add'; | ||
|
||
private MessageBusInterface $bus; | ||
|
||
private OrganizationRepository $organizations; | ||
|
||
public function __construct(MessageBusInterface $bus, OrganizationRepository $organizations) | ||
{ | ||
$this->bus = $bus; | ||
$this->organizations = $organizations; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setDescription('Add an artifact package to an organization') | ||
->addArgument('organization', InputArgument::REQUIRED, 'Organization alias') | ||
->addArgument('type', InputArgument::REQUIRED, 'Type of the project. Just artifact available for now.') | ||
->addArgument('url', InputArgument::REQUIRED, 'Artifact path') | ||
->addOption('keep-last-releases', 'k', InputOption::VALUE_OPTIONAL, 'Artifact path', '0') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
if ($input->getArgument('type') !== 'artifact') { | ||
$io->error('Just artifact available for now.'); | ||
|
||
return 1; | ||
} | ||
|
||
try { | ||
$organization = $this->getOrganizationFromAlias($input->getArgument('organization')); | ||
} catch (\InvalidArgumentException $e) { | ||
$io->error($e->getMessage()); | ||
|
||
return 1; | ||
} | ||
|
||
$this->bus->dispatch(new AddPackage( | ||
$id = Uuid::uuid4()->toString(), | ||
$organization->id()->toString(), | ||
$input->getArgument('url'), | ||
'artifact', | ||
[], | ||
intval($input->getOption('keep-last-releases')) | ||
)); | ||
$this->bus->dispatch(new SynchronizePackage($id)); | ||
|
||
$output->writeln('Package has been added and will be synchronized in the background'); | ||
|
||
return 0; | ||
} | ||
|
||
protected function getOrganizationFromAlias(string $organizationAlias): Organization | ||
{ | ||
$organization = $this->organizations->findOneBy(['alias' => $organizationAlias]); | ||
|
||
if (!$organization instanceof Organization) { | ||
throw new \InvalidArgumentException(sprintf('Organization with alias %s not found.', $organizationAlias)); | ||
} | ||
|
||
return $organization; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buddy\Repman\Tests\Functional\Command; | ||
|
||
use Buddy\Repman\Command\AddPackageCommand; | ||
use Buddy\Repman\Message\Security\ScanPackage; | ||
use Buddy\Repman\Tests\Functional\FunctionalTestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Messenger\Transport\InMemoryTransport; | ||
|
||
final class AddPackageCommandTest extends FunctionalTestCase | ||
{ | ||
private string $userId; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->userId = $this->createAndLoginAdmin(); | ||
$this->fixtures->createOrganization('buddy', $this->userId); | ||
} | ||
|
||
public function testAddSuccess(): void | ||
{ | ||
/** @var InMemoryTransport $transport */ | ||
$transport = $this->container()->get('messenger.transport.async'); | ||
$transport->reset(); | ||
|
||
$commandTester = new CommandTester($this->container()->get(AddPackageCommand::class)); | ||
$result = $commandTester->execute([ | ||
'organization' => 'buddy', | ||
'type' => 'artifact', | ||
'url' => '/path/to/package', | ||
]); | ||
|
||
self::assertEquals($result, 0); | ||
self::assertCount(1, $transport->getSent()); | ||
self::assertInstanceOf(ScanPackage::class, $transport->getSent()[0]->getMessage()); | ||
} | ||
|
||
public function testInvalidOrganization(): void | ||
{ | ||
$commandTester = new CommandTester($this->container()->get(AddPackageCommand::class)); | ||
$result = $commandTester->execute([ | ||
'organization' => 'vendor', | ||
'type' => 'artifact', | ||
'url' => '/path/to/package', | ||
]); | ||
|
||
self::assertEquals($result, 1); | ||
} | ||
|
||
public function testInvalidType(): void | ||
{ | ||
$commandTester = new CommandTester($this->container()->get(AddPackageCommand::class)); | ||
$result = $commandTester->execute([ | ||
'organization' => 'vendor', | ||
'type' => 'vcs', | ||
'url' => '/path/to/package', | ||
]); | ||
|
||
self::assertEquals($result, 1); | ||
} | ||
} |