From 704f944b400ead00e80cc297393178ec55e75781 Mon Sep 17 00:00:00 2001 From: Jorge Lobo <47326048+jloboescalona2@users.noreply.github.com> Date: Mon, 18 Nov 2019 16:39:43 +0100 Subject: [PATCH] B #3167: Fix upgrade banner (#3950) Was triggering false positives. --- .../app/tabs/official-support-tab/actions.js | 93 ++++++++++++------- .../tabs/official-support-tab/utils/common.js | 1 + src/sunstone/routes/support.rb | 43 ++++----- src/sunstone/sunstone-server.rb | 2 +- 4 files changed, 78 insertions(+), 61 deletions(-) diff --git a/src/sunstone/public/app/tabs/official-support-tab/actions.js b/src/sunstone/public/app/tabs/official-support-tab/actions.js index dd37e4e1eac..a86676f2e9e 100644 --- a/src/sunstone/public/app/tabs/official-support-tab/actions.js +++ b/src/sunstone/public/app/tabs/official-support-tab/actions.js @@ -23,28 +23,6 @@ define(function(require) { var RESOURCE = "official-support"; var TAB_ID = require("./tabId"); - var majorVersion = function(version){ - var r = 0; - if(version && version.length){ - var major = version.substring(0, version.lastIndexOf(".")); - if(major && major.length){ - r = parseFloat(major); - } - } - return r; - }; - - var minorVersion = function(version){ - var r = 0; - if(version && version.length){ - var minor = version.substring(version.lastIndexOf(".")+1); - if(minor && minor.length){ - r = parseFloat(minor); - } - } - return r; - }; - var _actions = { "Support.check":{ type: "list", @@ -66,25 +44,68 @@ define(function(require) { if($("#footer>a").length){ var localVersion = $("#footer>a").text().replace("OpenNebula ", ""); if(req && req.version && req.version!=="0" && localVersion.length){ - var version = req.version; - var remoteMajorVersion = majorVersion(version); - var remoteMinorVersion = minorVersion(version); - var localMajorVersion = majorVersion(localVersion); - var localMinorVersion = minorVersion(localVersion); - var link = $("", {href:"https://opennebula.org/software/"}).text( - "(new version available: " + version + ")" - ); - if(remoteMajorVersion > localMajorVersion){ - $("#latest_version").show().empty().append(link); - return; - } - if(remoteMajorVersion === localMajorVersion && remoteMinorVersion > localMinorVersion){ + var gitVersion = req.version; + var splitGitVersion = gitVersion.split("."); + var splitGitLocalVersion = localVersion.split("."); + + var major = false; + var minor = false; + var message = false; + + splitGitVersion.forEach(function(position, index){ + var numberPosition = parseInt(position); + var numberLocalPosition = parseInt(splitGitLocalVersion[index]); + + switch (index) { + case 0: + if(numberPosition > numberLocalPosition){ + message = true; + return; + } + break; + case 1: + if(numberPosition > numberLocalPosition && major){ + message = true; + return; + } + break; + case 2: + if(numberPosition > numberLocalPosition && major && minor){ + message = true; + return; + } + break; + default: + break; + } + + if(numberPosition === numberLocalPosition){ + switch (index) { + case 0: + major = true; + break; + case 1: + minor = true; + break; + default: + break; + } + } + + }); + + if (message){ + var link = $("", {href:"https://opennebula.org/software/"}).text( + "(new version available: " + gitVersion + ")" + ); $("#latest_version").show().empty().append(link); + $("#li_upgrade-top-tab").show(); return; } + } } - $("#latest_version").hide().empty(); + }, error: function(request){ if (request && request.status && request.status >= 400) { diff --git a/src/sunstone/public/app/tabs/official-support-tab/utils/common.js b/src/sunstone/public/app/tabs/official-support-tab/utils/common.js index 2c237153e81..929c8469e6f 100644 --- a/src/sunstone/public/app/tabs/official-support-tab/utils/common.js +++ b/src/sunstone/public/app/tabs/official-support-tab/utils/common.js @@ -54,6 +54,7 @@ define(function(require) { } function _check_last_version_support(){ + $("#li_upgrade-top-tab").hide(); Sunstone.runAction("Support.checkversion"); } diff --git a/src/sunstone/routes/support.rb b/src/sunstone/routes/support.rb index 33e461d0a09..a1852f1fdf9 100644 --- a/src/sunstone/routes/support.rb +++ b/src/sunstone/routes/support.rb @@ -252,12 +252,17 @@ def check_zendesk_api_gem get '/support/check/version' do $conf[:one_version_time] = 0 if $conf[:one_version_time].nil? $conf[:one_last_version] = '0' if $conf[:one_last_version].nil? + + def returnRoute(version,httpCode=200) + return [httpCode, JSON.pretty_generate(:version => version )] + end + find = 'release-' validate_time = Time.now.to_i - $conf[:one_version_time] + if validate_time < 86400 - return [200, JSON.pretty_generate(:version => $conf[:one_last_version])] + return returnRoute($conf[:one_last_version]) end - begin http = Curl.get(GITHUB_TAGS_URL) do |request| if !$conf[:proxy].nil? && !$conf[:proxy].empty? @@ -266,7 +271,7 @@ def check_zendesk_api_gem request.headers['User-Agent'] = 'OpenNebula Version Validation' end rescue StandardError - return [400, JSON.pretty_generate(:version => 0)] + return returnRoute(0, 400) end if !http.nil? && http.response_code == 200 @@ -277,35 +282,25 @@ def check_zendesk_api_gem !tag['name'].empty? && tag['name'].start_with?(find) - version = tag['name'].tr(find, '') - split_version = version.split('.') + git_version = tag['name'].tr(find, '') + split_version = git_version.split('.') + + gem_git_version = Gem::Version.new(git_version) + gem_local_version = Gem::Version.new($conf[:one_last_version]) + next unless split_version && split_version[1] && split_version[1].to_i && split_version[1].to_i.even? - memory_version = $conf[:one_last_version] - minor_version = - version.slice(version.rindex('.').to_i + 1..-1).to_i - - if version.to_f > memory_version.to_f - $conf[:one_last_version] = version - end - - memory_version_index = memory_version.rindex('.').to_i - minor_memory_version = - memory_version.slice(memory_version_index.to_i + 1..-1).to_i - - if version.to_f == memory_version.to_f && - minor_version >= minor_memory_version - $conf[:one_last_version] = version + if gem_git_version > gem_local_version + $conf[:one_last_version] = git_version + $conf[:one_version_time] = Time.now.to_i end + return returnRoute($conf[:one_last_version]) end - $conf[:one_version_time] = Time.now.to_i - [200, JSON.pretty_generate(:version => $conf[:one_last_version])] - else - [400, JSON.pretty_generate(:version => 0)] end + return returnRoute(0, 400) end post '/support/request/:id/action' do diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 4b265644acd..9c7c8417e06 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -236,7 +236,7 @@ } UPGRADE = { - :upgrade => "Upgrade Available ", + :upgrade => "Upgrade Available ", :no_upgrade => "", :url => "http://opennebula.org/software/" }