Skip to content

Commit 1a12a60

Browse files
committed
MetadataUpdaterWorkflow improved.
1 parent ca89e0b commit 1a12a60

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: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,102 @@
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\Repository;
24+
use Nyholm\Psr7\Request;
25+
use Psr\Http\Client\ClientInterface;
2226

2327
class GithubAdapterForReadingExternalMetadataSource implements ForReadingExternalMetadataSource
2428
{
2529
public function __construct(
26-
private readonly GithubClient $githubClient
30+
private readonly GithubClient $githubClient,
31+
private readonly ClientInterface $httpclient,
2732
) {
2833
}
2934

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

_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)