Skip to content

Commit

Permalink
Remove Ignored Packages Based on Config Values
Browse files Browse the repository at this point in the history
  • Loading branch information
pykettk committed May 17, 2023
1 parent f210297 commit 5960815
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
12 changes: 10 additions & 2 deletions Cron/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ public function execute(): void
$this->integrityResultsRegistry->setResults($results);

if ($this->moduleConfig->isSansecComposerIntegrityEmailNotificationEnabled()
&& $failedChecks = $this->integrityResultsRegistry->getFailedMatches($results)
&& $failedMatches = $this->integrityResultsRegistry->getFailedMatches($results)
) {
$this->emailNotifier->sendErrorNotification($failedChecks);
if ($this->moduleConfig->isIgnoreListEnabled()
&& $this->moduleConfig->shouldRemoveIgnoredPackagesFromEmail()
) {
$failedMatches = $this->integrityResultsRegistry->removeIgnoredPackages($failedMatches);
}

if ($failedMatches) {
$this->emailNotifier->sendErrorNotification($failedMatches);
}
}
}
}
Expand Down
33 changes: 30 additions & 3 deletions Model/IntegrityResultsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public function __construct(

public function getHyvaGridData(): array
{
return $this->getLastResults();
$packageData = $this->getLastResults();

return $this->moduleConfig->isIgnoreListEnabled() && $this->moduleConfig->shouldRemoveIgnoredPackagesFromAdminGrid()
? $this->removeIgnoredPackages($packageData)
: $packageData;
}

public function getLastResults(): ?array
Expand All @@ -55,7 +59,13 @@ public function getLastResults(): ?array
*/
public function setResults(array $data): self
{
$this->loadSelf()->setFlagData($data);
$packages = [];

foreach ($data as $package) {
$packages[$package->name] = $package;
}

$this->loadSelf()->setFlagData($packages);

return $this->save();
}
Expand All @@ -69,10 +79,27 @@ public function getFailedMatches(array $packages = []): array
$package = (array)$package;

if ((int)$package['percentage'] < $threshold) {
$failures[] = $package;
$failures[$package['name']] = $package;
}
}

return $failures;
}

public function removeIgnoredPackages(array &$packages): array
{
if (!$packages || !($ignoredPackages = $this->moduleConfig->getIgnoredPackageList())) {
return $packages;
}

$allPackageNames = array_keys($packages);

foreach ($ignoredPackages as $ignoredPackage) {
if (in_array($ignoredPackage, $allPackageNames)) {
unset($packages[$ignoredPackage]);
}
}

return $packages;
}
}
15 changes: 13 additions & 2 deletions Model/System/Message/PackageBelowThreshold.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
namespace Element119\SansecComposerIntegrityChecker\Model\System\Message;

use Element119\SansecComposerIntegrityChecker\Model\IntegrityResultsRegistry;
use Element119\SansecComposerIntegrityChecker\Scope\Config as ModuleConfig;
use Magento\Framework\Notification\MessageInterface;
use Magento\Framework\Phrase;
use Magento\Framework\UrlInterface;

class PackageBelowThreshold implements MessageInterface
{
public function __construct(
private readonly ModuleConfig $moduleConfig,
private readonly IntegrityResultsRegistry $integrityResultsRegistry,
private readonly UrlInterface $urlBuilder
) {}
Expand All @@ -23,14 +25,14 @@ public function getText(): Phrase
{
return __(
'Sansec Composer Integrity checker found %1 package(s) that did not meet the match threshold. <a href="%2">View scan results.</a>',
count($this->integrityResultsRegistry->getFailedMatches()),
count($this->getFailedMatches()),
$this->urlBuilder->getUrl('sansec_composer_integrity_checker/index/index')
);
}

public function isDisplayed(): bool
{
return (bool)$this->integrityResultsRegistry->getFailedMatches();
return (bool)$this->getFailedMatches();
}

public function getSeverity(): int
Expand All @@ -42,4 +44,13 @@ public function getIdentity(): string
{
return 'sansec_composer_integrity_checker_failed_scan';
}

public function getFailedMatches(): array
{
$failedMatches = $this->integrityResultsRegistry->getFailedMatches();

return $this->moduleConfig->isIgnoreListEnabled() && $this->moduleConfig->shouldRemoveIgnoredPackagesFromAdminNotification()
? $this->integrityResultsRegistry->removeIgnoredPackages($failedMatches)
: $failedMatches;
}
}
2 changes: 1 addition & 1 deletion Service/EmailNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
*/
public function sendErrorNotification(array $data): void
{
if (!($recipients = $this->moduleConfig->getSansecComposerIntegrityEmailRecipients())) {
if (!$data || !($recipients = $this->moduleConfig->getSansecComposerIntegrityEmailRecipients())) {
return;
}

Expand Down

0 comments on commit 5960815

Please sign in to comment.