From a90618f406f0336e5888f9835bae1d8b4fa847dd Mon Sep 17 00:00:00 2001 From: Chaoyi Zha Date: Fri, 21 Apr 2017 17:32:30 -0400 Subject: [PATCH] Check for updates using GitHub rreleases API on the client side --- docs/user-guide/upgrading.md | 8 ++-- public/css/admin.css | 4 ++ public/js/AdminCtrl.js | 57 +++++++++++++++++++++++++- resources/views/admin.blade.php | 15 +++++++ resources/views/layouts/base.blade.php | 3 ++ 5 files changed, 82 insertions(+), 5 deletions(-) diff --git a/docs/user-guide/upgrading.md b/docs/user-guide/upgrading.md index e55f288..ac3133f 100644 --- a/docs/user-guide/upgrading.md +++ b/docs/user-guide/upgrading.md @@ -5,10 +5,10 @@ To upgrade your Polr instance to the latest `master` or to a new release, you mu ## Upgrading from 2.x: - Back up your database and files -- Update your files by using `git pull` or downloading a release -- Run `composer install --no-dev -o` to ensure dependencies are up to date -- Migrate database with `php artisan migrate` to ensure database structure is up to date +- Update your files using `git pull` or `git checkout ` (e.g `git checkout 2.2.0`) +- Run `composer install --no-dev -o` to ensure your dependencies are up to date +- Migrate your database with `php artisan migrate` to ensure your table structure is up to date ## Upgrading from 1.x: -There are breaking changes between 2.x and 1.x; it is not yet possible to automatically upgrade to 2.x. +There are breaking changes between 2.x and 1.x, thus it is not yet possible to automatically upgrade from 1.x to 2.x. diff --git a/public/css/admin.css b/public/css/admin.css index b718f20..9f2275d 100644 --- a/public/css/admin.css +++ b/public/css/admin.css @@ -23,6 +23,10 @@ display: none; } +.update-status { + margin-left: 0.3em; +} + .api-quota { display: inline; } diff --git a/public/js/AdminCtrl.js b/public/js/AdminCtrl.js index cd0c606..432cc52 100644 --- a/public/js/AdminCtrl.js +++ b/public/js/AdminCtrl.js @@ -1,6 +1,11 @@ polr.controller('AdminCtrl', function($scope, $compile) { $scope.state = { - showNewUserWell: false + showNewUserWell: false, + updates: { + statusClass: 'fa fa-spin fa-gear', + status: 'Checking for updates...', + newVersionAvailable: false + } }; $scope.datatables = {}; @@ -70,6 +75,51 @@ polr.controller('AdminCtrl', function($scope, $compile) { }, datatables_config)); }; + $scope.compSemver = function(a, b) { + var pa = a.split('.'); + var pb = b.split('.'); + for (var i = 0; i < 3; i++) { + var na = Number(pa[i]); + var nb = Number(pb[i]); + if (na > nb) return 1; + if (nb > na) return -1; + if (!isNaN(na) && isNaN(nb)) return 1; + if (isNaN(na) && !isNaN(nb)) return -1; + } + return 0; + }; + + $scope.checkPolrUpdates = function() { + $.ajax({ + url: 'https://api.github.com/repos/cydrobolt/polr/releases/latest', + method: 'GET', + dataType: 'json', + headers: null + }).done(function(data) { + console.log(data); + // Given the latest release, compare to currently installed release. + // Show a notice to admins if a newer version is available. + // https://developer.github.com/v3/repos/releases/#get-the-latest-release + var remoteVersion = data.tag_name; // e.g 2.2.0 + var updateDescription = data.body; // detailed changelog + + var compResult = $scope.compSemver(POLR_VERSION, remoteVersion); + + if (compResult == 1) { + $scope.state.updates.statusClass = 'fa fa-gears'; + $scope.state.updates.status = 'A new update is available: ' + data.name; + $scope.state.updates.newVersionAvailable = true; + } + else { + $scope.state.updates.statusClass = 'fa fa-check'; + $scope.state.updates.status = 'You are running the latest version of Polr'; + } + }).fail(function(error) { + alert('lmao it no work'); + }); + + }; + // Append modals to Angular root $scope.appendModal = function(html, id) { id = esc_selector(id); @@ -302,6 +352,11 @@ polr.controller('AdminCtrl', function($scope, $compile) { }); $scope.initTables(); + + if (true) { + // FIXME should check if admin + $scope.checkPolrUpdates(); + } }; $scope.init(); diff --git a/resources/views/admin.blade.php b/resources/views/admin.blade.php index 1940187..64be9ba 100644 --- a/resources/views/admin.blade.php +++ b/resources/views/admin.blade.php @@ -15,6 +15,7 @@ @if ($role == $admin_role) + @endif @if ($api_active == 1) @@ -85,8 +86,22 @@ @include('snippets.user_table', [ 'table_id' => 'admin_users_table' ]) + + +
+

Updates

+

+ + Checking for updates... +

+

+ To upgrade Polr, follow the instructions provided in the upgrade guide. +

+
+ + @endif @if ($api_active == 1) diff --git a/resources/views/layouts/base.blade.php b/resources/views/layouts/base.blade.php index 85d0ea2..c3a90c6 100644 --- a/resources/views/layouts/base.blade.php +++ b/resources/views/layouts/base.blade.php @@ -78,6 +78,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. toastr["error"](`{{ str_replace('`', '\`', $error) }}`, "Error") @endforeach @endif + + // Define Polr version + var POLR_VERSION = '{{ env('POLR_VERSION') }}'; @yield('js')