Skip to content

Commit

Permalink
PLUG-129: check installed version of plugin (#38)
Browse files Browse the repository at this point in the history
* check for new version on admin page load

* Update view/adminhtml/templates/system/config/button/version.phtml

Co-authored-by: Stefan Danaita <[email protected]>

* add more detail to error messages

---------

Co-authored-by: Stefan Danaita <[email protected]>
  • Loading branch information
artyom-jaksov-tl and stefandanaita authored Oct 29, 2024
1 parent 6a2ea8e commit 16657ef
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
65 changes: 57 additions & 8 deletions Block/Adminhtml/System/Config/Button/VersionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace TrueLayer\Connect\Block\Adminhtml\System\Config\Button;

use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\HTTP\Client\Curl;
use TrueLayer\Connect\Api\Config\RepositoryInterface as ConfigRepository;
use TrueLayer\Connect\Api\Log\LogServiceInterface;

/**
* Version check class
Expand All @@ -22,10 +24,6 @@ class VersionCheck extends Field
* @var string
*/
protected $_template = 'TrueLayer_Connect::system/config/button/version.phtml';
/**
* @var ConfigRepository
*/
private $configRepository;

/**
* VersionCheck constructor.
Expand All @@ -35,10 +33,11 @@ class VersionCheck extends Field
*/
public function __construct(
Context $context,
ConfigRepository $configRepository,
private ConfigRepository $configRepository,
private Curl $curl,
private LogServiceInterface $logger,
array $data = []
) {
$this->configRepository = $configRepository;
parent::__construct($context, $data);
}

Expand Down Expand Up @@ -66,6 +65,56 @@ public function _getElementHtml(AbstractElement $element): string
*/
public function getVersion(): string
{
return 'v' . $this->configRepository->getExtensionVersion();
return $this->configRepository->getExtensionVersion();
}

public function getLatestVersion()
{
$curlVersion = $this->getCurlVersion();
$this->curl->addHeader('Accept', 'application/vnd.github+json');
$this->curl->addHeader('User-Agent', 'curl/'.$curlVersion);
$this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
$this->curl->get('https://api.github.com/repos/TrueLayer/magento2/releases');
$responseStatus = $this->curl->getStatus();
if ($responseStatus !== 200) {
$this->logger->error('Plugin version check failed, could not retrieve releases from github api', [
'response_status' => $responseStatus,
'response_body' => $this->curl->getBody()
]);
return false;
}
$response = $this->curl->getBody();
try {
$releases = json_decode($response, true, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
$this->logger->error('Plugin version check failed, json_decode error', [
'response_body' => $response,
'json_exception' => $e->getMessage()
]);
return false;
}
foreach ($releases as $release) {
if (!$release['draft'] && !$release['prerelease']) {
$latestRelease = $release;
break;
}
}
if (!isset($latestRelease)) {
$this->logger->error('Plugin version check failed, no valid release in github api response');
return false;
}
$latestVersion = ltrim($latestRelease['name'], 'v');
return $latestVersion;
}

private function getCurlVersion()
{
$curlVersion = curl_version();
if (is_array($curlVersion) && array_key_exists('version', $curlVersion)) {
$curlVersion = $curlVersion['version'];
} else {
$curlVersion = 'unknown';
}
return $curlVersion;
}
}
26 changes: 24 additions & 2 deletions view/adminhtml/templates/system/config/button/version.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,29 @@ use TrueLayer\Connect\Block\Adminhtml\System\Config\Button\VersionCheck;
* @see \TrueLayer\Connect\Block\Adminhtml\System\Config\Button\VersionCheck
* @var VersionCheck $block
*/
$currentVersion = $block->getVersion();
$latestVersion = $block->getLatestVersion();
$upToDate = !$latestVersion || version_compare($currentVersion, $latestVersion, '>=');
?>
<div class="mm-ui-result_version-wrapper">
<span><?= $block->escapeHtml($block->getVersion()); ?></span>

<div class="mm-ui-result_version-wrapper" data-bind="scope:'truelayer-plugin-version'">
<span id="truelayer-installed-version" data-bind="text: `v${getCurrentVersion()}`"></span>
<span id="truelayer-latest-version" data-bind="visible: !isUpToDate(), text: ` - A new version is available: v${getLatestVersion()}`"></span>
</div>

<script type="text/x-magento-init">
{
".mm-ui-result_version-wrapper": {
"Magento_Ui/js/core/app": {
"components": {
"truelayer-plugin-version": {
"component": "TrueLayer_Connect/js/version",
"currentVersion": "<?= $currentVersion ?>",
"latestVersion": "<?= $latestVersion ?>",
"upToDate": "<?= $upToDate ?>"
}
}
}
}
}
</script>
26 changes: 26 additions & 0 deletions view/adminhtml/web/js/version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright © TrueLayer Ltd, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'uiComponent',
], function (Component) {
'use strict';

return Component.extend({
initialize() {
this._super();
},

getCurrentVersion() {
return this.currentVersion;
},
getLatestVersion() {
return this.latestVersion;
},
isUpToDate() {
return this.upToDate;
}
});
});

0 comments on commit 16657ef

Please sign in to comment.