-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started to create classes to send workflow configurations to the cloud
- Loading branch information
Showing
12 changed files
with
508 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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; | ||
|
||
final class DeclareWorkflowCommand implements Command | ||
{ | ||
public function __construct( | ||
public string $code, | ||
public string $label, | ||
public DTO\JobList $jobs, | ||
public DTO\Autoload $autoload, | ||
public DTO\PackageList $packages, | ||
public DTO\RepositoryList $repositories, | ||
public DTO\AuthList $auths, | ||
public DTO\OrganizationId $organizationId, | ||
public DTO\WorkspaceId $project, | ||
) {} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\DTO; | ||
|
||
final readonly class JobCode implements \Stringable | ||
{ | ||
public function __construct( | ||
private string $reference, | ||
) {} | ||
|
||
public function asString(): string | ||
{ | ||
return $this->reference; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->reference; | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\DTO; | ||
|
||
use Kiboko\Component\Satellite\Cloud\DTO\Workflow\JobInterface; | ||
|
||
readonly class JobList implements \Countable, \IteratorAggregate | ||
{ | ||
/** @var list<JobInterface> */ | ||
private array $jobs; | ||
|
||
public function __construct( | ||
JobInterface ...$job, | ||
) { | ||
$this->jobs = $job; | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
$jobs = $this->jobs; | ||
usort($jobs, fn (JobInterface $left, JobInterface $right) => $left->order <=> $right->order); | ||
Check failure on line 23 in src/Cloud/DTO/JobList.php GitHub Actions / phpstan
|
||
|
||
return new \ArrayIterator($jobs); | ||
} | ||
|
||
public function codes(): array | ||
{ | ||
$jobs = $this->jobs; | ||
usort($jobs, fn (JobInterface $left, JobInterface $right) => $left->order <=> $right->order); | ||
Check failure on line 31 in src/Cloud/DTO/JobList.php GitHub Actions / phpstan
|
||
|
||
return array_map(fn (JobInterface $job) => $job->code->asString(), $jobs); | ||
} | ||
|
||
public function get(string $code): JobInterface | ||
{ | ||
foreach ($this->jobs as $job) { | ||
if ($job->code->asString() === $code) { | ||
return $job; | ||
} | ||
} | ||
|
||
throw new \OutOfBoundsException('There was no job found matching the provided code'); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return \count($this->jobs); | ||
} | ||
|
||
public function map(callable $callback): array | ||
{ | ||
return array_map($callback, $this->jobs); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\DTO\Workflow; | ||
|
||
use Kiboko\Component\Satellite\Cloud\DTO\JobCode; | ||
|
||
final readonly class Action implements JobInterface | ||
{ | ||
public function __construct( | ||
public string $label, | ||
public JobCode $code, | ||
public array $configuration, | ||
public int $order, | ||
) { | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\DTO\Workflow; | ||
|
||
use Kiboko\Component\Satellite\Cloud\DTO\JobCode; | ||
|
||
interface JobInterface | ||
{ | ||
|
||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\DTO\Workflow; | ||
|
||
use Kiboko\Component\Satellite\Cloud\DTO\JobCode; | ||
use Kiboko\Component\Satellite\Cloud\DTO\StepList; | ||
|
||
final readonly class Pipeline implements JobInterface | ||
{ | ||
public function __construct( | ||
public string $label, | ||
public JobCode $code, | ||
public StepList $stepList, | ||
public int $order, | ||
) { | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\DTO; | ||
|
||
final readonly class WorkflowId implements \Stringable | ||
{ | ||
public function __construct( | ||
private string $reference, | ||
) {} | ||
|
||
public function asString(): string | ||
{ | ||
return $this->reference; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->reference; | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\Event\Workflow; | ||
|
||
final readonly class WorkflowDeclared | ||
{ | ||
public function __construct( | ||
private string $id, | ||
) {} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Cloud/Handler/Workflow/DeclareWorkflowCommandHandler.php
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kiboko\Component\Satellite\Cloud\Handler\Workflow; | ||
|
||
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 | ||
{ | ||
try { | ||
/** @var \stdClass $result */ | ||
$result = $this->client->declarePipelinePipelineCollection( | ||
(new Api\Model\PipelineDeclarePipelineCommandInput()) | ||
->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) | ||
)), | ||
); | ||
} catch (Api\Exception\DeclarePipelinePipelineCollectionBadRequestException $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) { | ||
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); | ||
} | ||
|
||
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); | ||
} | ||
} |
Oops, something went wrong.