Skip to content

Commit

Permalink
Merge pull request #120 from kbond/symfony-check
Browse files Browse the repository at this point in the history
Update SymfonyVersion check to use packagist and check for end of life
  • Loading branch information
kbond committed Dec 11, 2015
2 parents 6802d8d + d6c3205 commit 2bc3ac8
Showing 1 changed file with 76 additions and 34 deletions.
110 changes: 76 additions & 34 deletions Check/SymfonyVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,49 @@

use Symfony\Component\HttpKernel\Kernel;
use ZendDiagnostics\Check\CheckInterface;
use ZendDiagnostics\Result\Failure;
use ZendDiagnostics\Result\Success;
use ZendDiagnostics\Result\Warning;

/**
* Checks the version of this app against the latest stable release.
*
* @author Roderik van der Veer <roderik@vanderveer.be>
* @author Kevin Bond <kevinbond@gmail.com>
*/
class SymfonyVersion implements CheckInterface
{
const PACKAGIST_URL = 'https://packagist.org/packages/symfony/symfony.json';
const VERSION_CHECK_URL = 'http://symfony.com/roadmap.json?version=%s';

/**
* {@inheritdoc}
*/
public function check()
{
$currentBranch = Kernel::MAJOR_VERSION.'.'.Kernel::MINOR_VERSION;

// use symfony.com version checker to see if current branch is still maintained
$response = $this->getResponseAndDecode(sprintf(self::VERSION_CHECK_URL, $currentBranch));

if (!isset($response['eol']) || !isset($response['is_eoled'])) {
throw new \Exception('Invalid response from Symfony version checker.');
}

$endOfLife = \DateTime::createFromFormat('m/Y', $response['eol'])->format('F, Y');

if (true === $response['is_eoled']) {
return new Failure(sprintf('Symfony branch "%s" reached it\'s end of life in %s.', $currentBranch, $endOfLife));
}

$currentVersion = Kernel::VERSION;
$latestRelease = $this->getLatestSymfonyVersion(); // eg. 2.0.12
$latestRelease = $this->getLatestVersion($currentBranch); // eg. 2.0.12

if (version_compare($currentVersion, $latestRelease) >= 0) {
return new Success();
if (version_compare($currentVersion, $latestRelease) < 0) {
return new Warning(sprintf('There is a new release - update to %s from %s.', $latestRelease, $currentVersion));
}

return new Warning(sprintf('Update to %s from %s.', $latestRelease, $currentVersion));
return new Success(sprintf('Your current Symfony branch reaches it\'s end of life in %s.', $endOfLife));
}

/**
Expand All @@ -37,49 +57,71 @@ public function getLabel()
return 'Symfony version';
}

private function getLatestSymfonyVersion()
/**
* @param string $branch
*
* @return string
*
* @throws \Exception
*/
private function getLatestVersion($branch)
{
// Get GitHub JSON request

$opts = array(
'http' => array(
'method' => 'GET',
'header' => "User-Agent: LiipMonitorBundle\r\n",
),
);
$response = $this->getResponseAndDecode(self::PACKAGIST_URL);

$context = stream_context_create($opts);
if (!isset($response['package']['versions'])) {
throw new \Exception('Invalid response from packagist.');
}

$githubUrl = 'https://api.github.com/repos/symfony/symfony/tags';
$githubJSONResponse = file_get_contents($githubUrl, false, $context);
$branch = 'v'.$branch;

// Convert it to a PHP object
// filter out branches and versions without current minor version
$versions = array_filter(
$response['package']['versions'],
function ($value) use ($branch) {
$value = $value['version'];

$githubResponseArray = json_decode($githubJSONResponse, true);
if (empty($githubResponseArray)) {
throw new \Exception('No valid response or no tags received from GitHub.');
}
if (stripos($value, 'PR') || stripos($value, 'RC') && stripos($value, 'BETA')) {
return false;
}

$tags = array();
return 0 === strpos($value, $branch);
}
);

foreach ($githubResponseArray as $tag) {
$tags[] = $tag['name'];
}
// just get versions
$versions = array_keys($versions);

// Sort tags
// sort tags
usort($versions, 'version_compare');

usort($tags, 'version_compare');
// reverse to ensure latest is first
$versions = array_reverse($versions);

// Filter out non final tags
return str_replace('v', '', $versions[0]);
}

$filteredTagList = array_filter($tags, function ($tag) {
return !stripos($tag, 'PR') && !stripos($tag, 'RC') && !stripos($tag, 'BETA');
});
/**
* @param $url
*
* @return array
*
* @throws \Exception
*/
private function getResponseAndDecode($url)
{
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "User-Agent: LiipMonitorBundle\r\n",
),
);

// The first one is the last stable release for Symfony 2
$array = json_decode(file_get_contents($url, false, stream_context_create($opts)), true);

$reverseFilteredTagList = array_reverse($filteredTagList);
if (empty($array)) {
throw new \Exception(sprintf('Invalid response from "%s".', $url));
}

return str_replace('v', '', $reverseFilteredTagList[0]);
return $array;
}
}

0 comments on commit 2bc3ac8

Please sign in to comment.