Skip to content

Commit

Permalink
MetadataUpdaterWorkflow improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerai committed Sep 24, 2024
1 parent ca89e0b commit 1a12a60
Show file tree
Hide file tree
Showing 20 changed files with 565 additions and 442 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

use Ecotone\Messaging\Attribute\ConsoleCommand;
use Ecotone\Modelling\CommandBus;
use Metadata\AdapterForReadingExternalMetadataSource\UpdateModuleMetadata;
use Metadata\Core\Process\StartModuleMetadataUpdate;

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

$command = new UpdateModuleMetadata($moduleId, $repoUrl);
$command = new StartModuleMetadataUpdate($moduleId, $repoUrl);

$commandBus->send($command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,102 @@

namespace Metadata\AdapterForReadingExternalMetadataSource;

use Github\Api\Repo;
use Github\Client as GithubClient;
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ExternalMetadataDto;
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ForReadingExternalMetadataSource;
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\MetadataReaderException;
use Metadata\Core\ValueObject\Repository;
use Nyholm\Psr7\Request;
use Psr\Http\Client\ClientInterface;

class GithubAdapterForReadingExternalMetadataSource implements ForReadingExternalMetadataSource
{
public function __construct(
private readonly GithubClient $githubClient
private readonly GithubClient $githubClient,
private readonly ClientInterface $httpclient,
) {
}

public function readMetadataFromExternalSource(string $moduleUrl): ExternalMetadataDto
public function readMetadataFromExternalSource(string $moduleUrl): ?ExternalMetadataDto

Check failure on line 35 in _metadata/src/AdapterForReadingExternalMetadataSource/GithubAdapterForReadingExternalMetadataSource.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

MixedInferredReturnType

_metadata/src/AdapterForReadingExternalMetadataSource/GithubAdapterForReadingExternalMetadataSource.php:35:72: MixedInferredReturnType: Could not verify return type 'Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ExternalMetadataDto|null' for Metadata\AdapterForReadingExternalMetadataSource\GithubAdapterForReadingExternalMetadataSource::readMetadataFromExternalSource (see https://psalm.dev/047)
{
/**
* TODO: handle failure
*/
$repository = Repository::createFromRepositoryUrl($moduleUrl);


$defaultBranch = $this->getDefaultBranchName($repository);

/**
* TODO: refactor
* $defaultBranch dovrebbe essere sempre un tipo string,
* la funzione sopra non dovrebbe tornare null
* ma eccezione di tipo http del client api
* o eccezione per dati non processabili
*/
if (null === $defaultBranch) {
return null;
}

$composerJsonFileContent = $this->downloadComposerJsonFileContent($moduleUrl, $defaultBranch);

$composerFile = ComposerJsonFile::createFromJson($composerJsonFileContent);

Check failure on line 58 in _metadata/src/AdapterForReadingExternalMetadataSource/GithubAdapterForReadingExternalMetadataSource.php

View workflow job for this annotation

GitHub Actions / Commit Stage / Commit checks (8.1)

UndefinedClass

_metadata/src/AdapterForReadingExternalMetadataSource/GithubAdapterForReadingExternalMetadataSource.php:58:25: UndefinedClass: Class, interface or enum named Metadata\AdapterForReadingExternalMetadataSource\ComposerJsonFile does not exist (see https://psalm.dev/019)

if (! $composerFile->hasMetadata()) {
return null;
}

$extractedMetadata = $composerFile->getMetadata();
$metadataDto = new ExternalMetadataDto(
enableSync: true,
category: (string) $extractedMetadata['category'],
tags: (array) $extractedMetadata['tags'],
);

return $metadataDto;
}

private function doDownloadHttpRequest(string $url): string
{
$request = new Request('GET', $url);

$response = $this->httpclient->sendRequest($request);

return $response->getBody()->getContents();
}

private function downloadComposerJsonFileContent(string $url, string $reference): string
{
try {
$repository = Repository::createFromRepositoryUrl($url);
/** @var Repo $repoApi */
$repoApi = $this->githubClient->api('repo');
$fileInfo = (array) $repoApi
->contents()
->show($repository->getUsername(), $repository->getName(), 'composer.json', $reference);

$composerJsonFileContent = $this->doDownloadHttpRequest((string) $fileInfo['download_url']);

return $composerJsonFileContent;
} catch (\Exception $exception) {
throw new MetadataReaderException('Impossible to read metadata from: ' . $url . ' error: ' . $exception->getMessage());
}
}

private function getDefaultBranchName(Repository $repository): ? string
{
$data = [];
try {
//get composer.json
// extract metadata section
// create metadataDto
return new ExternalMetadataDto(true, 'a category', ['tag-1', 'tag-2']);
} catch (\RuntimeException $exception) {
throw new MetadataReaderException($exception->getMessage());
$data = $this->githubClient->repo()->show($repository->getUsername(), $repository->getName());
} catch (\Exception $exception) {
throw $exception;
}

if (! \array_key_exists('default_branch', $data)) {
return null;
}

return (string) $data['default_branch'];
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ class StubAdapterForReadingExternalMetadataSource implements ForReadingExternalM
{
public function __construct(
private array $metadataDtosIndexedByUrl = [],
private array $exceptions = [],
) {
}

public function readMetadataFromExternalSource(string $moduleUrl): ExternalMetadataDto
public function readMetadataFromExternalSource(string $moduleUrl): ?ExternalMetadataDto
{
if (0 < \count($this->exceptions)) {
throw $this->exceptions[0];
}

return $this->metadataDtosIndexedByUrl[$moduleUrl];
}

public function setExternalMetadataDto(string $url, ExternalMetadataDto $externalMetadataDto): void
public function setExternalMetadataDto(string $url, ?ExternalMetadataDto $externalMetadataDto): void
{
$this->metadataDtosIndexedByUrl[$url] = $externalMetadataDto;
}

public function setException(object $exception): void
{
$this->exceptions[] = $exception;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ interface ForReadingExternalMetadataSource
/**
* @throws MetadataReaderException
*/
public function readMetadataFromExternalSource(string $moduleUrl): ExternalMetadataDto;
public function readMetadataFromExternalSource(string $moduleUrl): ?ExternalMetadataDto;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT
*/

namespace Metadata\AdapterForReadingExternalMetadataSource;
namespace Metadata\Core\Process\Event;

class UpdateModuleMetadata
class ModuleMetadataUpdateAbortedWithError
{
public function __construct(
public readonly string $moduleId,
public readonly string $repositoryUrl
public readonly mixed $error,
) {
}
}
Loading

0 comments on commit 1a12a60

Please sign in to comment.