Skip to content

Commit

Permalink
Check that version is equal to or newer than latest published version.
Browse files Browse the repository at this point in the history
…Resolves metabase#3658
  • Loading branch information
tlrobinson committed Nov 8, 2016
1 parent 9b9309c commit e12da92
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component, PropTypes } from "react";

import MetabaseSettings from "metabase/lib/settings";
import MetabaseUtils from "metabase/lib/utils";
import SettingsSetting from "./SettingsSetting.jsx";

import _ from "underscore";
Expand Down Expand Up @@ -68,7 +69,7 @@ export default class SettingsUpdatesForm extends Component {
*/

if (!versionInfo || currentVersion === versionInfo.latest.version) {
if (!versionInfo || MetabaseUtils.compareVersions(currentVersion, versionInfo.latest.version) >= 0) {
return (
<div className="p2 bg-brand bordered rounded border-brand text-white text-bold">
You're running Metabase {this.removeVersionPrefixIfNeeded(currentVersion)} which is the latest and greatest!
Expand Down
36 changes: 36 additions & 0 deletions frontend/src/metabase/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,42 @@ var MetabaseUtils = {

copy: function(a) {
return JSON.parse(JSON.stringify(a));
},

// this should correctly compare all version formats Metabase uses, e.x.
// 0.0.9, 0.0.10-snapshot, 0.0.10-alpha1, 0.0.10-rc1, 0.0.10-rc2, 0.0.10-rc10
// 0.0.10, 0.1.0, 0.2.0, 0.10.0, 1.1.0
compareVersions: function(aVersion, bVersion) {
const SPECIAL_COMPONENTS = {
"snapshot": -4,
"alpha": -3,
"beta": -2,
"rc": -1,
};

const getComponents = (x) =>
// v1.2.3-BETA1
x.toLowerCase()
// v1.2.3-beta1
.replace(/^v/, "")
// 1.2.3-beta1
.split(/[.-]*([0-9]+)[.-]*/).filter(c => c)
// ["1", "2", "3", "beta", "1"]
.map(c => SPECIAL_COMPONENTS[c] || parseInt(c, 10));
// [1, 2, 3, -2, 1]

let aComponents = getComponents(aVersion);
let bComponents = getComponents(bVersion);
for (let i = 0; i < Math.max(aComponents.length, bComponents.length); i++) {
let a = aComponents[i];
let b = bComponents[i];
if (b == undefined || a < b) {
return -1;
} else if (a == undefined || b < a) {
return 1;
}
}
return 0;
}
}

Expand Down
29 changes: 29 additions & 0 deletions frontend/test/unit/lib/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,33 @@ describe('utils', () => {
);
});
});

describe("compareVersions", () => {
fit ("should compare versions correctly", () => {
let expected = [
"0.0.9",
"0.0.10-snapshot",
"0.0.10-alpha1",
"0.0.10-rc1",
"0.0.10-rc2",
"0.0.10-rc10",
"0.0.10",
"0.1.0",
"0.2.0",
"0.10.0",
"1.1.0"
];
let shuffled = expected.slice();
shuffle(shuffled);
shuffled.sort(MetabaseUtils.compareVersions);
expect(shuffled).toEqual(expected);
})
})
});

function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}

0 comments on commit e12da92

Please sign in to comment.