diff --git a/app/routes/crate/version.js b/app/routes/crate/version.js index 4f26c77f9aa..e41d3eec034 100644 --- a/app/routes/crate/version.js +++ b/app/routes/crate/version.js @@ -10,9 +10,49 @@ export default Ember.Route.extend({ const controller = this.controllerFor(this.routeName); const maxVersion = crate.get('max_version'); - // Fall back to the crate's `max_version` property + const isUnstableVersion = version => { + const versionLen = version.length; + let majorMinorPatchChars = 0; + let result = false; + + for (let i = 0; i < versionLen; i++) { + const char = version.charAt(i); + + if (!isNaN(parseInt(char)) || char === '.') { + majorMinorPatchChars++; + } else { + break; + } + } + + if (versionLen !== majorMinorPatchChars) { + result = true; + } + + return result; + }; + + // Fallback to the crate's last stable version + // If `max_version` is `0.0.0` then all versions have been yanked if (!requestedVersion && maxVersion !== '0.0.0') { - params.version_num = maxVersion; + if (isUnstableVersion(maxVersion)) { + crate.get('versions').then(versions => { + const latestStableVersion = versions.find(version => { + if (!isUnstableVersion(version.get('num'))) { + return version; + } + }); + + if (latestStableVersion == null) { + // If no stable version exists, fallback to `maxVersion` + params.version_num = maxVersion; + } else { + params.version_num = latestStableVersion.get('num'); + } + }); + } else { + params.version_num = maxVersion; + } } controller.set('crate', crate); diff --git a/mirage/fixtures/crate.js b/mirage/fixtures/crate.js index 3d4e670ab65..51ea63abf62 100644 --- a/mirage/fixtures/crate.js +++ b/mirage/fixtures/crate.js @@ -20,11 +20,12 @@ export default { "version_downloads": "/api/v1/crates/nanomsg/downloads", "versions": null }, - "max_version": "0.6.1", + "max_version": "0.7.0-alpha", "name": "nanomsg", "repository": "https://github.com/thehydroimpulse/nanomsg.rs", - "updated_at": "2016-12-27T08:40:00Z", + "updated_at": "2016-12-28T08:40:00Z", "versions": [ + 40906, 40905, 28431, 21273, @@ -48,6 +49,26 @@ export default { } ], "versions": [ + { + "crate": "nanomsg", + "created_at": "2016-12-28T08:40:00Z", + "dl_path": "/api/v1/crates/nanomsg/0.7.0-alpha/download", + "downloads": 260, + "features": { + "bundled": [ + "nanomsg-sys/bundled" + ] + }, + "id": 40906, + "links": { + "authors": "/api/v1/crates/nanomsg/0.7.0-alpha/authors", + "dependencies": "/api/v1/crates/nanomsg/0.7.0-alpha/dependencies", + "version_downloads": "/api/v1/crates/nanomsg/0.7.0-alpha/downloads" + }, + "num": "0.7.0-alpha", + "updated_at": "2016-12-28T08:40:00Z", + "yanked": false + }, { "crate": "nanomsg", "created_at": "2016-12-27T08:40:00Z", diff --git a/tests/acceptance/crate-test.js b/tests/acceptance/crate-test.js index 21973f44dcc..47bad3f728c 100644 --- a/tests/acceptance/crate-test.js +++ b/tests/acceptance/crate-test.js @@ -27,7 +27,7 @@ test('navigating to the all versions page', function(assert) { click('#crate-versions span.small a'); andThen(function() { - matchesText(assert, '.info', /All 12 versions of nanomsg since December \d+, 2014/); + matchesText(assert, '.info', /All 13 versions of nanomsg since December \d+, 2014/); }); });