Skip to content

Commit

Permalink
SDK-4251: Reduce number of Release App API calls (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
vol4onok authored Nov 13, 2023
1 parent 4051946 commit 20bd1d3
Show file tree
Hide file tree
Showing 26 changed files with 229 additions and 862 deletions.
39 changes: 31 additions & 8 deletions src/Checker/SecurityChecker/SprykerSecurityChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use SprykerSdk\Evaluator\Dto\ViolationDto;
use SprykerSdk\Evaluator\Helper\SemanticVersionHelper;
use SprykerSdk\Evaluator\Reader\ComposerReaderInterface;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest;
use SprykerSdk\Evaluator\ReleaseApp\Infrastructure\Service\ReleaseAppServiceInterface;
use SprykerSdk\Evaluator\ReleaseApp\Infrastructure\Shared\Dto\ReleaseAppResponse;
use SprykerSdk\Evaluator\ReleaseApp\Infrastructure\Shared\Dto\ReleaseGroupDto;
Expand Down Expand Up @@ -75,7 +75,7 @@ public function getName(): string
public function check(CheckerInputDataDto $inputData): CheckerResponseDto
{
try {
$releaseAppResponse = $this->releaseAppService->getNewSecurityReleaseGroups($this->createDataProviderRequest());
$releaseAppResponse = $this->releaseAppService->getNewReleaseGroups($this->createDataProviderRequest());
} catch (RuntimeException $exception) {
$violation = new ViolationDto(
sprintf(
Expand Down Expand Up @@ -146,14 +146,37 @@ protected function buildViolationsByReleaseGroup(ReleaseGroupDto $releaseGroupDt
}

/**
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest
*/
protected function createDataProviderRequest(): UpgradeAnalysisRequest
protected function createDataProviderRequest(): UpgradeInstructionsRequest
{
$projectName = $this->composerReader->getProjectName();
$composerJson = $this->composerReader->getComposerData();
$composerLock = $this->composerReader->getComposerLockData();
$packages = $this->extractLockedPackages($this->composerReader->getComposerLockData());

return new UpgradeAnalysisRequest($projectName, $composerJson, $composerLock);
return new UpgradeInstructionsRequest($packages);
}

/**
* @param array<string, mixed> $composerLockArray
*
* @return array<string, string>
*/
protected function extractLockedPackages(array $composerLockArray): array
{
if (empty($composerLockArray['packages'])) {
return [];
}

$locked = [];

foreach ($composerLockArray['packages'] as $package) {
$name = (string)$package['name'];
if (!preg_match('#^spryker.*(?<!feature)/#', $name)) {
continue;
}

$locked[$name] = (string)$package['version'];
}

return $locked;
}
}
47 changes: 4 additions & 43 deletions src/ReleaseApp/Application/Service/ReleaseAppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
namespace SprykerSdk\Evaluator\ReleaseApp\Application\Service;

use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\ReleaseAppClientInterface;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeAnalysisModuleVersionCollection;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeInstructionsReleaseGroupCollection;

class ReleaseAppService implements ReleaseAppServiceInterface
Expand All @@ -31,52 +29,15 @@ public function __construct(ReleaseAppClientInterface $releaseAppClient)
}

/**
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest $upgradeAnalysisRequest
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest $upgradeInstructionsRequest
*
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeInstructionsReleaseGroupCollection
*/
public function getNewReleaseGroupsSortedByReleaseDate(
UpgradeAnalysisRequest $upgradeAnalysisRequest
UpgradeInstructionsRequest $upgradeInstructionsRequest
): UpgradeInstructionsReleaseGroupCollection {
$moduleVersionCollection = $this->getModuleVersionCollection($upgradeAnalysisRequest)->getSecurityFixes();
$releaseGroupCollection = $this->releaseAppClient->getUpgradeInstructions($upgradeInstructionsRequest)->getReleaseGroups()->getOnlyWithReleasedDate();

$releaseGroupCollection = $this->getReleaseGroupCollection($moduleVersionCollection)
->getOnlyWithReleasedDate()->getSecurityFixes()->sortByReleasedDate();

return $releaseGroupCollection;
}

/**
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeAnalysisModuleVersionCollection $moduleVersionCollection
*
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeInstructionsReleaseGroupCollection
*/
protected function getReleaseGroupCollection(
UpgradeAnalysisModuleVersionCollection $moduleVersionCollection
): UpgradeInstructionsReleaseGroupCollection {
$releaseGroupCollection = new UpgradeInstructionsReleaseGroupCollection();

foreach ($moduleVersionCollection->toArray() as $moduleVersion) {
$request = new UpgradeInstructionsRequest($moduleVersion->getId());
$response = $this->releaseAppClient->getUpgradeInstructions($request);
$releaseGroupCollection->add($response->getReleaseGroup());
}

return $releaseGroupCollection;
}

/**
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest $request
*
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeAnalysisModuleVersionCollection
*/
protected function getModuleVersionCollection(
UpgradeAnalysisRequest $request
): UpgradeAnalysisModuleVersionCollection {
$response = $this->releaseAppClient->getUpgradeAnalysis($request);

return $response->getModuleCollection()
->getModulesWithVersions()
->getModuleVersions();
return new UpgradeInstructionsReleaseGroupCollection($releaseGroupCollection->getSecurityFixes()->sortByReleasedDate()->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

namespace SprykerSdk\Evaluator\ReleaseApp\Application\Service;

use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeInstructionsReleaseGroupCollection;

interface ReleaseAppServiceInterface
{
/**
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest $upgradeAnalysisRequest
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest $upgradeInstructionsRequest
*
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\Collection\UpgradeInstructionsReleaseGroupCollection
*/
public function getNewReleaseGroupsSortedByReleaseDate(UpgradeAnalysisRequest $upgradeAnalysisRequest): UpgradeInstructionsReleaseGroupCollection;
public function getNewReleaseGroupsSortedByReleaseDate(UpgradeInstructionsRequest $upgradeInstructionsRequest): UpgradeInstructionsReleaseGroupCollection;
}
9 changes: 0 additions & 9 deletions src/ReleaseApp/Domain/Client/ReleaseAppClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@

namespace SprykerSdk\Evaluator\ReleaseApp\Domain\Client;

use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\UpgradeAnalysis;
use SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\UpgradeInstructions;

interface ReleaseAppClientInterface
{
/**
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeAnalysisRequest $upgradeAnalysisRequest
*
* @return \SprykerSdk\Evaluator\ReleaseApp\Domain\Entities\UpgradeAnalysis
*/
public function getUpgradeAnalysis(UpgradeAnalysisRequest $upgradeAnalysisRequest): UpgradeAnalysis;

/**
* @param \SprykerSdk\Evaluator\ReleaseApp\Domain\Client\Request\UpgradeInstructionsRequest $instructionsRequest
*
Expand Down
100 changes: 0 additions & 100 deletions src/ReleaseApp/Domain/Client/Request/UpgradeAnalysisRequest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,29 @@
class UpgradeInstructionsRequest implements RequestInterface
{
/**
* @var int
* @var string
*/
protected int $idModuleVersion;
protected const PACKAGES = 'packages';

/**
* @param int $moduleVersionId
* @var array<string, string>
*/
public function __construct(int $moduleVersionId)
protected array $packages;

/**
* @param array<string, string> $packages
*/
public function __construct(array $packages)
{
$this->idModuleVersion = $moduleVersionId;
$this->packages = $packages;
}

/**
* @return string|null
* @return string
*/
public function getBody(): ?string
public function getBody(): string
{
return null;
return (string)json_encode($this->getBodyArray());
}

/**
Expand All @@ -47,6 +52,16 @@ public function getResponseClass(): string
*/
public function getParameters(): ?string
{
return sprintf('%s=%s', 'module_version_id', $this->idModuleVersion);
return 'only_security=1';
}

/**
* @return array<mixed>
*/
protected function getBodyArray(): array
{
return [
static::PACKAGES => $this->packages,
];
}
}

This file was deleted.

Loading

0 comments on commit 20bd1d3

Please sign in to comment.