Skip to content

Commit 61e4a0d

Browse files
committed
MetadataUpdaterWorkflow improved.
1 parent ca89e0b commit 61e4a0d

20 files changed

+565
-442
lines changed

_metadata/src/AdapterCli/ForSynchronizingMetadata/MetadataUpdateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
use Ecotone\Messaging\Attribute\ConsoleCommand;
1919
use Ecotone\Modelling\CommandBus;
20-
use Metadata\AdapterForReadingExternalMetadataSource\UpdateModuleMetadata;
20+
use Metadata\Core\Process\StartModuleMetadataUpdate;
2121

2222
class MetadataUpdateCommand
2323
{
@@ -35,7 +35,7 @@ public function execute(CommandBus $commandBus): void
3535
// with metadata
3636
$repoUrl = 'https://github.com/MedicalMundi/oe-module-todo-list';
3737

38-
$command = new UpdateModuleMetadata($moduleId, $repoUrl);
38+
$command = new StartModuleMetadataUpdate($moduleId, $repoUrl);
3939

4040
$commandBus->send($command);
4141
}

_metadata/src/AdapterForReadingExternalMetadataSource/GithubAdapterForReadingExternalMetadataSource.php

Lines changed: 84 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,103 @@
1515

1616
namespace Metadata\AdapterForReadingExternalMetadataSource;
1717

18+
use Github\Api\Repo;
1819
use Github\Client as GithubClient;
1920
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ExternalMetadataDto;
2021
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ForReadingExternalMetadataSource;
2122
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\MetadataReaderException;
23+
use Metadata\Core\ValueObject\ComposerJsonFile;
24+
use Metadata\Core\ValueObject\Repository;
25+
use Nyholm\Psr7\Request;
26+
use Psr\Http\Client\ClientInterface;
2227

2328
class GithubAdapterForReadingExternalMetadataSource implements ForReadingExternalMetadataSource
2429
{
2530
public function __construct(
26-
private readonly GithubClient $githubClient
31+
private readonly GithubClient $githubClient,
32+
private readonly ClientInterface $httpclient,
2733
) {
2834
}
2935

30-
public function readMetadataFromExternalSource(string $moduleUrl): ExternalMetadataDto
36+
public function readMetadataFromExternalSource(string $moduleUrl): ?ExternalMetadataDto
3137
{
38+
/**
39+
* TODO: handle failure
40+
*/
41+
$repository = Repository::createFromRepositoryUrl($moduleUrl);
42+
43+
44+
$defaultBranch = $this->getDefaultBranchName($repository);
45+
46+
/**
47+
* TODO: refactor
48+
* $defaultBranch dovrebbe essere sempre un tipo string,
49+
* la funzione sopra non dovrebbe tornare null
50+
* ma eccezione di tipo http del client api
51+
* o eccezione per dati non processabili
52+
*/
53+
if (null === $defaultBranch) {
54+
return null;
55+
}
56+
57+
$composerJsonFileContent = $this->downloadComposerJsonFileContent($moduleUrl, $defaultBranch);
58+
59+
$composerFile = ComposerJsonFile::createFromJson($composerJsonFileContent);
60+
61+
if (! $composerFile->hasMetadata()) {
62+
return null;
63+
}
64+
65+
$extractedMetadata = (array) $composerFile->getMetadata();
66+
$metadataDto = new ExternalMetadataDto(
67+
enableSync: true,
68+
category: (string) $extractedMetadata['category'],
69+
tags: (array) $extractedMetadata['tags'],
70+
);
71+
72+
return $metadataDto;
73+
}
74+
75+
private function doDownloadHttpRequest(string $url): string
76+
{
77+
$request = new Request('GET', $url);
78+
79+
$response = $this->httpclient->sendRequest($request);
80+
81+
return $response->getBody()->getContents();
82+
}
83+
84+
private function downloadComposerJsonFileContent(string $url, string $reference): string
85+
{
86+
try {
87+
$repository = Repository::createFromRepositoryUrl($url);
88+
/** @var Repo $repoApi */
89+
$repoApi = $this->githubClient->api('repo');
90+
$fileInfo = (array) $repoApi
91+
->contents()
92+
->show($repository->getUsername(), $repository->getName(), 'composer.json', $reference);
93+
94+
$composerJsonFileContent = $this->doDownloadHttpRequest((string) $fileInfo['download_url']);
95+
96+
return $composerJsonFileContent;
97+
} catch (\Exception $exception) {
98+
throw new MetadataReaderException('Impossible to read metadata from: ' . $url . ' error: ' . $exception->getMessage());
99+
}
100+
}
101+
102+
private function getDefaultBranchName(Repository $repository): ? string
103+
{
104+
$data = [];
32105
try {
33-
//get composer.json
34-
// extract metadata section
35-
// create metadataDto
36-
return new ExternalMetadataDto(true, 'a category', ['tag-1', 'tag-2']);
37-
} catch (\RuntimeException $exception) {
38-
throw new MetadataReaderException($exception->getMessage());
106+
$data = $this->githubClient->repo()->show($repository->getUsername(), $repository->getName());
107+
} catch (\Exception $exception) {
108+
throw $exception;
39109
}
110+
111+
if (! \array_key_exists('default_branch', $data)) {
112+
return null;
113+
}
114+
115+
return (string) $data['default_branch'];
40116
}
41117
}

_metadata/src/AdapterForReadingExternalMetadataSource/StatelessWorkflowProcess.php

Lines changed: 0 additions & 216 deletions
This file was deleted.

_metadata/src/AdapterForReadingExternalMetadataSourceStub/StubAdapterForReadingExternalMetadataSource.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,26 @@ class StubAdapterForReadingExternalMetadataSource implements ForReadingExternalM
2222
{
2323
public function __construct(
2424
private array $metadataDtosIndexedByUrl = [],
25+
private array $exceptions = [],
2526
) {
2627
}
2728

28-
public function readMetadataFromExternalSource(string $moduleUrl): ExternalMetadataDto
29+
public function readMetadataFromExternalSource(string $moduleUrl): ?ExternalMetadataDto
2930
{
31+
if (0 < \count($this->exceptions)) {
32+
throw $this->exceptions[0];
33+
}
34+
3035
return $this->metadataDtosIndexedByUrl[$moduleUrl];
3136
}
3237

33-
public function setExternalMetadataDto(string $url, ExternalMetadataDto $externalMetadataDto): void
38+
public function setExternalMetadataDto(string $url, ?ExternalMetadataDto $externalMetadataDto): void
3439
{
3540
$this->metadataDtosIndexedByUrl[$url] = $externalMetadataDto;
3641
}
42+
43+
public function setException(object $exception): void
44+
{
45+
$this->exceptions[] = $exception;
46+
}
3747
}

_metadata/src/Core/Port/Driven/ForReadingExternalMetadataSource/ForReadingExternalMetadataSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ interface ForReadingExternalMetadataSource
2020
/**
2121
* @throws MetadataReaderException
2222
*/
23-
public function readMetadataFromExternalSource(string $moduleUrl): ExternalMetadataDto;
23+
public function readMetadataFromExternalSource(string $moduleUrl): ?ExternalMetadataDto;
2424
}

_metadata/src/AdapterForReadingExternalMetadataSource/UpdateModuleMetadata.php renamed to _metadata/src/Core/Process/Event/ModuleMetadataUpdateAbortedWithError.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT
1414
*/
1515

16-
namespace Metadata\AdapterForReadingExternalMetadataSource;
16+
namespace Metadata\Core\Process\Event;
1717

18-
class UpdateModuleMetadata
18+
class ModuleMetadataUpdateAbortedWithError
1919
{
2020
public function __construct(
2121
public readonly string $moduleId,
22-
public readonly string $repositoryUrl
22+
public readonly mixed $error,
2323
) {
2424
}
2525
}

0 commit comments

Comments
 (0)