Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loosely detect semver versions #1628

Merged
merged 13 commits into from
Jul 26, 2018
Merged

loosely detect semver versions #1628

merged 13 commits into from
Jul 26, 2018

Conversation

RedSparr0w
Copy link
Member

@RedSparr0w RedSparr0w commented Mar 31, 2018

Closes #1622
Closes #1652
Closes #1682
refer: https://www.npmjs.com/package/semver#functions

All methods and classes take a final loose boolean argument that, if true, will be more forgiving about not-quite-valid semver strings. The resulting output will always be 100% strict, of course.

This PR will allow "not-quite-valid semver strings" example: 1.023.4, 1.23

semver.valid('1.023.4') // null
semver.valid('1.023.4', true) // '1.23.4'
semver.maxSatisfying(['v2.079.0', 'v2.079.0-rc.1'], '') // TypeError: Invalid Version: v2.079.0
semver.maxSatisfying(['v2.079.0', 'v2.079.0-rc.1'], '', true) // 'v2.079.0'

Note:

Only the change on line 21 is needed to close #1622, but figured we should add support for the other places these functions are used also.

Note2/Edit:

versions = ['v1.2.3-beta', 'v1.2.3-alpha'];
semver.maxSatisfying(versions, '') // null
semver.maxSatisfying(versions, '', true) // null
versions.sort((a, b) => {
  semver.compare(a, b, true);
})[0]; // 'v1.2.3-beta'

@shields-ci
Copy link

shields-ci commented Mar 31, 2018

Warnings
⚠️

This PR modified the server but none of the service tests. That's okay so long as it's refactoring existing code.

Messages
📖

✨ Thanks for your contribution to Shields, @RedSparr0w!

Generated by 🚫 dangerJS

Copy link
Member

@paulmelnikow paulmelnikow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I haven't concluded whether or not this is the right thing to do, though I thought I would nudge this forward with a review comment.

lib/version.js Outdated
@@ -18,11 +18,13 @@ function latest(versions) {
return (/[0-9]/).test(version);
});
try {
version = semver.maxSatisfying(versions, '');
version = semver.maxSatisfying(versions, '', true) || versions.sort((a, b) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we write these calls like maxSatisfying(versions, /* range */ '', /* loose */ true) to make it clearer what these parameters are? Same for the other uses of the loose parameter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have updated to include the /* loose */ true comments, but it does look a little messy.
Do you think it would be better to do something like:

const semverLoose = true;
version = maxSatisfying(versions, /* range */ '', semverLoose);
const loose = true;
const range = '';
version = maxSatisfying(versions, range, loose);

@paulmelnikow paulmelnikow added the core Server, BaseService, GitHub auth, Shared helpers label Apr 2, 2018
@RedSparr0w
Copy link
Member Author

RedSparr0w commented Apr 19, 2018

Updated to first only compare all "semver" versions (1.2, 1.2.3 etc.) /\d+\.\d+/
If none are found it will check for any single number versions (1, 2, 3)

both could still fail if there are any strange versions like 18w11a which i believe would be interpreted as 18.0.0.

If that fails then it will just be alphabetically sorted as before.

Edit: this also closes #1652

@RedSparr0w
Copy link
Member Author

Updated to also include a fix for #1682
Supporting pre-release versions.
eg: v1.0.1-alpha.1 > v1.0.0 (previously it would not pick this up as being newer)

@RedSparr0w
Copy link
Member Author

Updated to make pre-release versions opt in as per @paulmelnikow comment

lib/version.js Outdated
@@ -11,18 +11,31 @@ const semver = require('semver');

// Given a list of versions (as strings), return the latest version.
// Return undefined if no version could be found.
function latest(versions) {
function latest(versions, pre = false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this an object?

function latest(versions, { pre = false } = {}) {

That way, when it's invoked, the parameter can be named, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!
👍

lib/version.js Outdated
if (!pre){
versions = versions.filter(function(version) {
return !(/\d+-\w+/).test(version);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit difficult to follow.

I understand the purpose served by filtering out the prereleases. Could you explain what the rest of the filtering is for? Would it not work to just pass the other versions to the loose compare function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the \d+\.\d+ test first to get what we would assume is definitely a version.
If that returns no results, we fall back to just look for anything that could possibly be a version.

Otherwise in an array of:

["abc123f312", "1.2.3", "1.3"]

abc123f312 would be the newest version as it's interpreted as 123

var format = match[2];
var apiUrl = 'https://pub.dartlang.org/packages/' + userRepo + '.json';
var badgeData = getBadgeData('pub', data);
const includePre = Boolean(match[1]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@RedSparr0w
Copy link
Member Author

Think this should be ready for merge now!
🤞

@RedSparr0w RedSparr0w merged commit 329db7d into badges:master Jul 26, 2018
@shields-deployment
Copy link

This pull request was merged to master branch. Now this change is waiting for deployment.
Deploys usually happen every few weeks. After deployment changes are copied to gh-pages branch.

This badge displays deployment status:

slodki added a commit to slodki/moneymanagerex that referenced this pull request Aug 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
3 participants