Merge pull request #1189 from cachethq/update-system

Update check system
This commit is contained in:
Joe Cohen
2015-12-01 02:22:11 -06:00
12 changed files with 163 additions and 18 deletions
+70
View File
@@ -0,0 +1,70 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\GitHub;
use GuzzleHttp\Client;
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
class Release
{
/**
* Cache instance.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;
/**
* Config repository.
*
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* Creates a new release instance.
*
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param \Illuminate\Contracts\Config\Repository $config
*
* @return void
*/
public function __construct(CacheRepository $cache, ConfigRepository $config)
{
$this->cache = $cache;
$this->config = $config;
}
/**
* Returns the latest GitHub release.
*
* @return string
*/
public function latest()
{
$release = $this->cache->remember('version', 720, function () {
$headers = ['Accept' => 'application/vnd.github.v3+json'];
// We can re-use the Emoji token here, if we have it.
if ($token = $this->config->get('services.github.token')) {
$headers['OAUTH-TOKEN'] = $token;
}
return json_decode((new Client())->get('https://api.github.com/repos/cachethq/cachet/releases/latest', [
'headers' => $headers,
])->getBody(), true);
});
return $release['tag_name'];
}
}
@@ -11,6 +11,7 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use CachetHQ\Cachet\GitHub\Release;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\IncidentTemplate;
@@ -18,6 +19,7 @@ use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Response;
class ApiController extends Controller
{
@@ -89,4 +91,20 @@ class ApiController extends Controller
throw new ModelNotFoundException("Incident template for $templateSlug could not be found.");
}
/**
* Checks if Cachet is up to date.
*
* @return \Illuminate\Http\JsonResponse
*/
public function checkVersion()
{
$latest = app(Release::class)->latest();
return Response::json([
'cachet_version' => CACHET_VERSION,
'latest_version' => $latest,
'is_latest' => version_compare(CACHET_VERSION, $latest) === 1,
]);
}
}
+1
View File
@@ -265,6 +265,7 @@ class DashboardRoutes
$router->post('components/groups/order', 'ApiController@postUpdateComponentGroupOrder');
$router->post('components/order', 'ApiController@postUpdateComponentOrder');
$router->post('components/{component}', 'ApiController@postUpdateComponent');
$router->get('system/version', 'ApiController@checkVersion');
});
});
}
+45
View File
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Providers;
use CachetHQ\Cachet\GitHub\Release;
use Illuminate\Support\ServiceProvider;
class GitHubServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerRelease();
}
/**
* Register the releases class.
*
* @return void
*/
protected function registerRelease()
{
$this->app->singleton('cachet.release', function ($app) {
$cache = $app['cache.store'];
$config = $app['config'];
return new Release($cache, $config);
});
$this->app->alias('cachet.release', Release::class);
}
}