Skip to content

Commit

Permalink
Added workflow management
Browse files Browse the repository at this point in the history
  • Loading branch information
sebprt committed Oct 24, 2023
1 parent b447227 commit 9f0191b
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 112 deletions.
15 changes: 15 additions & 0 deletions src/Cloud/Command/Workflow/RemoveWorkflowCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\Command\Workflow;

use Kiboko\Component\Satellite\Cloud\Command\Command;
use Kiboko\Component\Satellite\Cloud\DTO\WorkflowId;

final class RemoveWorkflowCommand implements Command
{
public function __construct(
public WorkflowId $workflow,
) {}
}
1 change: 1 addition & 0 deletions src/Cloud/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function withStandardHandlers(Client $client): self
Satellite\Cloud\Command\Pipeline\ReplacePipelineStepCommand::class => new Satellite\Cloud\Handler\Pipeline\ReplacePipelineStepCommandHandler($client),
Satellite\Cloud\Command\Pipeline\RemovePipelineStepCommand::class => new Satellite\Cloud\Handler\Pipeline\RemovePipelineStepCommandHandler($client),
Satellite\Cloud\Command\Workflow\DeclareWorkflowCommand::class => new Satellite\Cloud\Handler\Workflow\DeclareWorkflowCommandHandler($client),
Satellite\Cloud\Command\Workflow\RemoveWorkflowCommand::class => new Satellite\Cloud\Handler\Workflow\RemoveWorkflowCommandHandler($client),
]);
}

Expand Down
37 changes: 30 additions & 7 deletions src/Cloud/Console/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function configure(): void
$this->addOption('beta', mode: Console\Input\InputOption::VALUE_NONE, description: 'Shortcut to set the cloud instance to https://beta.gyroscops.com');
$this->addOption('ssl', mode: Console\Input\InputOption::VALUE_NEGATABLE, description: 'Enable or disable SSL');
$this->addArgument('config', Console\Input\InputArgument::REQUIRED);
$this->addArgument('type', Console\Input\InputArgument::REQUIRED, 'Type de configuration (pipeline ou workflow)');
}

protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
Expand Down Expand Up @@ -62,6 +63,12 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
}
}

$type = $input->getArgument('type');
if ($type !== 'pipeline' && $type !== 'workflow') {
$output->writeln('Le type doit être soit "pipeline" soit "workflow".');
return Console\Command\Command::FAILURE;
}

for ($directory = getcwd(); '/' !== $directory; $directory = \dirname($directory)) {
if (file_exists($directory.'/.gyro.php')) {
break;
Expand Down Expand Up @@ -121,16 +128,32 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O
}

$context = new Satellite\Cloud\Context($client, $auth, $url);
$pipeline = new Satellite\Cloud\Pipeline($context);
if (!\array_key_exists('version', $configuration)) {
foreach ($pipeline->create(Satellite\Cloud\Pipeline::fromLegacyConfiguration($configuration['satellite'])) as $command) {
$bus->push($command);

if ($type === 'pipeline') {
$pipeline = new Satellite\Cloud\Pipeline($context);
if (!\array_key_exists('version', $configuration)) {
foreach ($pipeline->create(Satellite\Cloud\Pipeline::fromLegacyConfiguration($configuration['satellite'])) as $command) {
$bus->push($command);
}
} else {
foreach ($configuration['satellites'] as $satellite) {
foreach ($pipeline->create(Satellite\Cloud\Pipeline::fromLegacyConfiguration($satellite)) as $command) {
$bus->push($command);
}
}
}
} else {
foreach ($configuration['satellites'] as $satellite) {
foreach ($pipeline->create(Satellite\Cloud\Pipeline::fromLegacyConfiguration($satellite)) as $command) {
} elseif ($type === 'workflow') {
$workflow = new Satellite\Cloud\Workflow($context);
if (!\array_key_exists('version', $configuration)) {
foreach ($workflow->create(Satellite\Cloud\Workflow::fromLegacyConfiguration($configuration['satellite'])) as $command) {
$bus->push($command);
}
} else {
foreach ($configuration['satellites'] as $satellite) {
foreach ($workflow->create(Satellite\Cloud\Workflow::fromLegacyConfiguration($satellite)) as $command) {
$bus->push($command);
}
}
}
}

Expand Down
53 changes: 53 additions & 0 deletions src/Cloud/DTO/ReferencedWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

final class ReferencedWorkflow implements WorkflowInterface
{
public function __construct(
private WorkflowId $id,
private Workflow $decorated,
) {}

public function id(): WorkflowId
{
return $this->id;
}

public function code(): string
{
return $this->decorated->code();
}

public function label(): string
{
return $this->decorated->label();
}

public function jobs(): JobList
{
return $this->decorated->jobs();
}

public function autoload(): Autoload
{
return $this->decorated->autoload();
}

public function packages(): PackageList
{
return $this->decorated->packages();
}

public function repositories(): RepositoryList
{
return $this->decorated->repositories();
}

public function auths(): AuthList
{
return $this->decorated->auths();
}
}
53 changes: 53 additions & 0 deletions src/Cloud/DTO/Workflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

final readonly class Workflow implements WorkflowInterface
{
public function __construct(
private string $label,
private string $code,
private JobList $jobs,
private Autoload $autoload,
private PackageList $packages,
private RepositoryList $repositories,
private AuthList $auths,
) {}

public function code(): string
{
return $this->code;
}

public function label(): string
{
return $this->label;
}

public function jobs(): JobList
{
return $this->jobs;
}

public function autoload(): Autoload
{
return $this->autoload;
}

public function packages(): PackageList
{
return $this->packages;
}

public function repositories(): RepositoryList
{
return $this->repositories;
}

public function auths(): AuthList
{
return $this->auths;
}
}
1 change: 0 additions & 1 deletion src/Cloud/DTO/Workflow/JobInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@

interface JobInterface
{

}
22 changes: 22 additions & 0 deletions src/Cloud/DTO/WorkflowInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\DTO;

interface WorkflowInterface
{
public function code(): string;

public function label(): string;

public function jobs(): JobList;

public function autoload(): Autoload;

public function packages(): PackageList;

public function repositories(): RepositoryList;

public function auths(): AuthList;
}
70 changes: 29 additions & 41 deletions src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,51 @@

use Gyroscops\Api;
use Kiboko\Component\Satellite\Cloud;
use Kiboko\Component\Satellite\Cloud\DTO\Probe;
use Kiboko\Component\Satellite\Cloud\DTO\PSR4AutoloadConfig;
use Kiboko\Component\Satellite\Cloud\DTO\Step;

final readonly class DeclareWorkflowCommandHandler
{
public function __construct(
private Api\Client $client,
) {}

/** TODO: update this method */
public function __invoke(Cloud\Command\Pipeline\DeclarePipelineCommand $command): Cloud\Event\PipelineDeclared
public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command): Cloud\Event\Workflow\WorkflowDeclared
{
try {
/** @var \stdClass $result */
$result = $this->client->declarePipelinePipelineCollection(
(new Api\Model\PipelineDeclarePipelineCommandInput())
/** @var Api\Model\WorkflowDeclareWorkflowCommandJsonldRead $result */
$result = $this->client->declareWorkflowWorkflowCollection(
(new Api\Model\WorkflowDeclareWorkflowCommandInput())
->setLabel($command->label)
->setCode($command->code)
->setSteps($command->steps->map(
fn (Step $step) => (new Api\Model\StepInput())
->setCode((string) $step->code)
->setLabel($step->label)
->setConfiguration($step->config)
->setProbes($step->probes->map(
fn (Probe $probe) => (new Api\Model\Probe())->setCode($probe->code)->setLabel($probe->label))
)
))
->setAutoloads($command->autoload->map(
fn (PSR4AutoloadConfig $autoloadConfig) => (new Api\Model\AutoloadInput())
->setNamespace($autoloadConfig->namespace)
->setPaths($autoloadConfig->paths)
))
->setPackages($command->packages->transform())
->setAuths($command->auths->map(
fn (Cloud\DTO\Auth $auth) => (new Api\Model\AddPipelineComposerAuthCommandInput())
->setUrl($auth->url)
->setToken($auth->token)
))
->setRepositories($command->repositories->map(
fn (Cloud\DTO\Repository $repository) => (new Api\Model\AddPipelineComposerRepositoryCommandInput())
->setName($repository->name)
->setType($repository->type)
->setUrl($repository->url)
)),
->setComposer(
(new Api\Model\Composer())
->setAutoloads($command->autoload->map(
fn (PSR4AutoloadConfig $autoloadConfig) => (new Api\Model\ComposerAutoload())
->setNamespace($autoloadConfig->namespace)
->setPaths($autoloadConfig->paths)
))
->setPackages($command->packages->transform())
->setAuthentications($command->auths->map(
fn (Cloud\DTO\Auth $auth) => (new Api\Model\ComposerAuthentication())
->setUrl($auth->url)
->setToken($auth->token)
))
->setRepositories($command->repositories->map(
fn (Cloud\DTO\Repository $repository) => (new Api\Model\ComposerRepository())
->setName($repository->name)
->setType($repository->type)
->setUrl($repository->url)
))
),
);
} catch (Api\Exception\DeclarePipelinePipelineCollectionBadRequestException $exception) {
} catch (Api\Exception\DeclareWorkflowWorkflowCollectionBadRequestException $exception) {
throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline. Maybe your client is not up to date, you may want to update your Gyroscops client.', previous: $exception);
} catch (Api\Exception\DeclarePipelinePipelineCollectionUnprocessableEntityException $exception) {
} catch (Api\Exception\DeclareWorkflowWorkflowCollectionUnprocessableEntityException $exception) {
throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline. It seems the data you sent was invalid, please check your input.', previous: $exception);
} catch (\Exception $exception) {
var_dump($exception);
}

if (null === $result) {
// TODO: change the exception message, it doesn't give enough details on how to fix the issue
throw new Cloud\DeclarePipelineFailedException('Something went wrong while declaring the pipeline.');
}

return new Cloud\Event\PipelineDeclared($result->id);
return new Cloud\Event\Workflow\WorkflowDeclared($result->getId());
}
}
20 changes: 20 additions & 0 deletions src/Cloud/Handler/Workflow/RemoveWorkflowCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Kiboko\Component\Satellite\Cloud\Handler\Workflow;

use Kiboko\Component\Satellite\Cloud;

final class RemoveWorkflowCommandHandler
{
public function __construct(
\Gyroscops\Api\Client $client,
) {
}

public function __invoke(Cloud\Command\Workflow\DeclareWorkflowCommand $command)
{

}
}
Loading

0 comments on commit 9f0191b

Please sign in to comment.