Skip to content

Commit

Permalink
EZP-30402: Make Dashboard code more solid when it comes to detecting …
Browse files Browse the repository at this point in the history
…trial or not (#36)

* EZP-30402: Make Dashboard code more solid when it comes to detecting trial or not

Adds logic to also read info from composer.json to detect if packages are pulled from ttl channel
or not.

Besides the current issues with updates.ez.no telling composer the packages are ttl when they are not (diff between packages.json and composer.json).
This also solves issues when people switch from TTL to BUL, and install won't show they have switched until all TTL paclages actually have a new release to pull in.

* Fix CS

* [Travis] Drop PHP 5.6 and 7.0 for 1.0 to align with composer.json

* Update SystemInfo/Exception/ComposerJsonFileNotFoundException.php
Co-Authored-By: Mikolaj Adamczyk <[email protected]>

* Type hints

* Apply suggestions from code review
Co-Authored-By: Gunnstein Lye <[email protected]>
  • Loading branch information
andrerom authored May 10, 2019
1 parent 4406f09 commit bdcf6ea
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 24 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ cache:
matrix:
fast_finish: true
include:
- php: 5.6
- php: 7.0
- php: 7.1
env: CHECK_CS=true
- php: 7.2
Expand Down
2 changes: 1 addition & 1 deletion AdminUi/Component/EzInfoTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function replaceUrlPlaceholders(): array
$urls = $this->urlList;
foreach ($this->urlList as $urlName => $url) {
foreach ($this->ezSystemInfo as $attribute => $value) {
if (is_string($value) && strpos($url,'{ez.' . $attribute . '}') !== false) {
if (\is_string($value) && strpos($url, '{ez.' . $attribute . '}') !== false) {
$urls[$urlName] = str_replace('{ez.' . $attribute . '}', $value, $url);
}
}
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ services:
- "@support_tools.system_info.collector.composer.lock_file"
- "%kernel.debug%"
# Can't tag this before v0.3 (2.5?) as it will blow up in admin UI for missing templates there
# And it does not look like there is anway to add it from this package, so maybe it needs to be made extensible(?)
# And it does not look like there is any way to add it from this package, so maybe it needs to be made extensible(?)
#tags: [{ name: "support_tools.system_info.collector", identifier: "ez" }]

support_tools.system_info.collector.composer.lock_file:
class: "%support_tools.system_info.collector.composer.lock_file.class%"
arguments:
- "%kernel.root_dir%/../composer.lock"
- "%kernel.root_dir%/../composer.json"
tags:
- { name: "support_tools.system_info.collector", identifier: "composer" }

Expand Down
2 changes: 1 addition & 1 deletion Resources/views/themes/admin/dashboard/block/ez.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% set levels = {0: "info", 1: "warning", 2: "danger"} %}
{% set icons = {0: "", 1: "&#9888;", 2: "&#9888;"} %}
{% set status %}
{% spaceless %}
{% spaceless %}
{% if not ez.release %}
{% set severity = 1 %}
<div class="alert alert-warning mb-0 mt-3" role="alert">
Expand Down
8 changes: 6 additions & 2 deletions SystemInfo/Collector/EzSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,19 @@ public function collect()
$ez->release = (string)(((float)$this->composerInfo->packages['ezsystems/ezpublish-kernel']->version) - 5);
}

// In case someone switches from TTL to BUL, make sure we only identify install as Trial if this is present,
// as well as TTL packages
$hasTTLComposerRepo = \in_array('https://updates.ez.no/ttl', $this->composerInfo->repositoryUrls);

if ($package = $this->getFirstPackage(self::ENTERPISE_PACKAGES)) {
$ez->isEnterpise = true;
$ez->isTrial = $package->license === 'TTL-2.0';
$ez->isTrial = $hasTTLComposerRepo && $package->license === 'TTL-2.0';
$ez->name = 'eZ Platform Enterprise';
}

if ($package = $this->getFirstPackage(self::COMMERCE_PACKAGES)) {
$ez->isCommerce = true;
$ez->isTrial = $ez->isTrial || $package->license === 'TTL-2.0';
$ez->isTrial = $ez->isTrial || $hasTTLComposerRepo && $package->license === 'TTL-2.0';
$ez->name = 'eZ Commerce';
}

Expand Down
69 changes: 58 additions & 11 deletions SystemInfo/Collector/JsonComposerLockSystemInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ class JsonComposerLockSystemInfoCollector implements SystemInfoCollector
*/
private $lockFile;

/**
* @var string Composer json file with full path.
*/
private $jsonFile;

/**
* @var Value\ComposerSystemInfo The collected value, cached in case info is collected by other collectors.
*/
private $value;

public function __construct($lockFile)
public function __construct($lockFile, $jsonFile)
{
$this->lockFile = $lockFile;
$this->jsonFile = $jsonFile;
}

/**
Expand All @@ -61,15 +67,33 @@ public function collect()
throw new Exception\ComposerLockFileNotFoundException($this->lockFile);
}

if (!file_exists($this->jsonFile)) {
throw new Exception\ComposerJsonFileNotFoundException($this->jsonFile);
}

$lockData = json_decode(file_get_contents($this->lockFile), true);
$jsonData = json_decode(file_get_contents($this->jsonFile), true);

return $this->value = new Value\ComposerSystemInfo([
'packages' => $this->extractPackages($lockData),
'repositoryUrls' => $this->extractRepositoryUrls($jsonData),
'minimumStability' => isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : null,
]);
}

/**
* @param array $lockData
*
* @return \EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerPackage[]
*/
private function extractPackages(array $lockData): array
{
$packages = [];
$rootAliases = [];
$lockData = json_decode(file_get_contents($this->lockFile), true);
foreach ($lockData['aliases'] as $alias) {
$rootAliases[$alias['package']] = $alias['alias'];
}

// For PHP 5.6, add variable locally to be able to use isset() on it.
$stabilities = self::STABILITIES;
foreach ($lockData['packages'] as $packageData) {
$package = new Value\ComposerPackage([
'name' => $packageData['name'],
Expand All @@ -83,8 +107,8 @@ public function collect()
if (isset($lockData['stability-flags'][$package->name])) {
$stabilityFlag = (int)$lockData['stability-flags'][$package->name];

if (isset($stabilities[$stabilityFlag])) {
$package->stability = $stabilities[$stabilityFlag];
if (isset(self::STABILITIES[$stabilityFlag])) {
$package->stability = self::STABILITIES[$stabilityFlag];
}
}

Expand All @@ -101,13 +125,36 @@ public function collect()

ksort($packages, SORT_FLAG_CASE | SORT_STRING);

return $this->value = new Value\ComposerSystemInfo([
'packages' => $packages,
'minimumStability' => isset($lockData['minimum-stability']) ? $lockData['minimum-stability'] : null,
]);
return $packages;
}

private static function setNormalizedVersion(Value\ComposerPackage $package)
/**
* @param array $jsonData
*
* @return string[]
*/
private function extractRepositoryUrls(array $jsonData): array
{
$repos = [];
foreach ($jsonData['repositories'] as $composerRepository) {

This comment has been minimized.

Copy link
@wizhippo

wizhippo May 10, 2019

This throws a notice and stops composer install. We should check it maybe?

    if (isset($jsonData['repositories'])) {

This comment has been minimized.

Copy link
@andrerom

andrerom May 11, 2019

Author Contributor

Good point

This comment has been minimized.

Copy link
@andrerom

andrerom May 11, 2019

Author Contributor

Look ok? 7c61b6d

if (empty($composerRepository['type']) || $composerRepository['type'] !== 'composer') {
continue;
}

if (empty($composerRepository['url'])) {
continue;
}

$repos[] = $composerRepository['url'];
}

return $repos;
}

/**
* @param Value\ComposerPackage $package
*/
private static function setNormalizedVersion(Value\ComposerPackage $package): void
{
$version = $package->alias ? $package->alias : $package->branch;
if ($version[0] === 'v') {
Expand Down
20 changes: 20 additions & 0 deletions SystemInfo/Exception/ComposerJsonFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* File containing the ComposerJsonFileNotFoundException class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\EzSupportToolsBundle\SystemInfo\Exception;

use Exception;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as BaseNotFoundException;

class ComposerJsonFileNotFoundException extends BaseNotFoundException
{
public function __construct(string $path, Exception $previous = null)
{
parent::__construct('Composer.json file', $path, $previous);
}
}
2 changes: 1 addition & 1 deletion SystemInfo/Exception/ComposerLockFileNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class ComposerLockFileNotFoundException extends BaseNotFoundException
{
public function __construct($path, Exception $previous = null)
{
parent::__construct('Composer lockfile', $path, $previous);
parent::__construct('composer.lock file', $path, $previous);
}
}
9 changes: 9 additions & 0 deletions SystemInfo/Value/ComposerSystemInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ class ComposerSystemInfo extends ValueObject implements SystemInfo
* @var string|null
*/
public $minimumStability;

/**
* Additional Composer repository urls used.
*
* Will contain urls used so would be possible to know if bul or ttl packages are used for instance.
*
* @var string[]
*/
public $repositoryUrls = [];
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ public function testCollect()
]),
],
'minimumStability' => 'stable',
'repositoryUrls' => ['https://updates.ez.no/bul'],
]);

$composerCollector = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/composer.lock');

$composerCollector = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/composer.lock', __DIR__ . '/_fixtures/composer.json');
$value = $composerCollector->collect();

self::assertInstanceOf('EzSystems\EzSupportToolsBundle\SystemInfo\Value\ComposerSystemInfo', $value);

self::assertEquals($expected, $value);
}

Expand All @@ -81,10 +80,20 @@ public function testCollect()
*
* @expectedException \EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerLockFileNotFoundException
*/
public function testCollectFileNotFound()
public function testCollectLockFileNotFound()
{
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/snafu.lock');
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/snafu.lock', __DIR__ . '/_fixtures/composer.json');
$composerCollectorNotFound->collect();
}

/**
* @covers \EzSystems\EzSupportToolsBundle\SystemInfo\Collector\JsonComposerLockSystemInfoCollector::collect()
*
* @expectedException \EzSystems\EzSupportToolsBundle\SystemInfo\Exception\ComposerJsonFileNotFoundException
*/
public function testCollectJsonFileNotFound()
{
$composerCollectorNotFound = new JsonComposerLockSystemInfoCollector(__DIR__ . '/_fixtures/composer.lock', __DIR__ . '/_fixtures/snafu.json');
$composerCollectorNotFound->collect();
}
}
Loading

0 comments on commit bdcf6ea

Please sign in to comment.