Skip to content

Handle alpha versions correctly #39

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

Merged
merged 3 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Elastic.Elasticsearch.Managed/ClusterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public IDisposable Start(IConsoleLineHandler writer, TimeSpan waitForStarted)
if (!Started)
{
var nodeExceptions = Nodes.Select(n => n.LastSeenException).Where(e => e != null).ToList();
var message = $"{{{GetType().Name}.{nameof(Start)}}} cluster did not start succesfully";
var message = $"{{{GetType().Name}.{nameof(Start)}}} cluster did not start successfully";
var seeLogsMessage = SeeLogsMessage(message);
writer?.WriteError(seeLogsMessage);
throw new AggregateException(seeLogsMessage, nodeExceptions);
Expand Down
47 changes: 26 additions & 21 deletions src/Elastic.Stack.ArtifactsApi/ElasticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ namespace Elastic.Stack.ArtifactsApi
{
public class ElasticVersion : Version, IComparable<string>
{
private readonly ConcurrentDictionary<string, Artifact>
_resolved = new ConcurrentDictionary<string, Artifact>();
private readonly ConcurrentDictionary<string, Artifact> _resolved = new();

protected ElasticVersion(string version, ArtifactBuildState state, string buildHash = null) : base(version)
{
Expand All @@ -28,7 +27,7 @@ protected ElasticVersion(string version, ArtifactBuildState state, string buildH

public int CompareTo(string other)
{
var v = (ElasticVersion) other;
var v = (ElasticVersion)other;
return CompareTo(v);
}

Expand Down Expand Up @@ -73,25 +72,29 @@ ArtifactBuildState GetReleaseState(string s)
: ArtifactBuildState.BuildCandidate;
}

if (string.IsNullOrWhiteSpace(managedVersionString)) return null;
if (string.IsNullOrWhiteSpace(managedVersionString))
return null;

var version = managedVersionString;
var state = GetReleaseState(version);
var buildHash = string.Empty;

switch (managedVersionString)
{
case string _ when managedVersionString.StartsWith("latest-", StringComparison.OrdinalIgnoreCase):
case { } when managedVersionString.StartsWith("latest-", StringComparison.OrdinalIgnoreCase):
var major = int.Parse(managedVersionString.Replace("latest-", ""));
version = SnapshotApiResolver.LatestReleaseOrSnapshotForMajor(major).ToString();
state = GetReleaseState(version);
if (state == ArtifactBuildState.BuildCandidate)
buildHash = ApiResolver.LatestBuildHash(version);
break;
case string _ when managedVersionString.EndsWith("-snapshot", StringComparison.OrdinalIgnoreCase):
// When the version is not yet released but contains the alpha label, we treat it in the same way as snapshots so it is resolved correctly
case { } _ when managedVersionString.EndsWith("-snapshot", StringComparison.OrdinalIgnoreCase)
|| state != ArtifactBuildState.Released &&
managedVersionString.IndexOf("-alpha", StringComparison.OrdinalIgnoreCase) >= 0:
state = ArtifactBuildState.Snapshot;
break;
case string _ when TryParseBuildCandidate(managedVersionString, out var v, out buildHash):
case { } _ when TryParseBuildCandidate(managedVersionString, out var v, out buildHash):
state = ArtifactBuildState.BuildCandidate;
version = v;
break;
Expand All @@ -109,7 +112,8 @@ internal static bool TryParseBuildCandidate(string passedVersion, out string ver
version = null;
gitHash = null;
var tokens = passedVersion.Split(':');
if (tokens.Length < 2) return false;
if (tokens.Length < 2)
return false;
version = tokens[1].Trim();
gitHash = tokens[0].Trim();
return true;
Expand All @@ -124,7 +128,8 @@ public bool InRange(string range)
public bool InRange(Range versionRange)
{
var satisfied = versionRange.IsSatisfied(this);
if (satisfied) return true;
if (satisfied)
return true;

//Semver can only match snapshot version with ranges on the same major and minor
//anything else fails but we want to know e.g 2.4.5-SNAPSHOT satisfied by <5.0.0;
Expand All @@ -135,24 +140,24 @@ public bool InRange(Range versionRange)

public static implicit operator ElasticVersion(string version) => From(version);

public static bool operator <(ElasticVersion first, string second) => first < (ElasticVersion) second;
public static bool operator >(ElasticVersion first, string second) => first > (ElasticVersion) second;
public static bool operator <(ElasticVersion first, string second) => first < (ElasticVersion)second;
public static bool operator >(ElasticVersion first, string second) => first > (ElasticVersion)second;

public static bool operator <(string first, ElasticVersion second) => (ElasticVersion) first < second;
public static bool operator >(string first, ElasticVersion second) => (ElasticVersion) first > second;
public static bool operator <(string first, ElasticVersion second) => (ElasticVersion)first < second;
public static bool operator >(string first, ElasticVersion second) => (ElasticVersion)first > second;

public static bool operator <=(ElasticVersion first, string second) => first <= (ElasticVersion) second;
public static bool operator >=(ElasticVersion first, string second) => first >= (ElasticVersion) second;
public static bool operator <=(ElasticVersion first, string second) => first <= (ElasticVersion)second;
public static bool operator >=(ElasticVersion first, string second) => first >= (ElasticVersion)second;

public static bool operator <=(string first, ElasticVersion second) => (ElasticVersion) first <= second;
public static bool operator >=(string first, ElasticVersion second) => (ElasticVersion) first >= second;
public static bool operator <=(string first, ElasticVersion second) => (ElasticVersion)first <= second;
public static bool operator >=(string first, ElasticVersion second) => (ElasticVersion)first >= second;

public static bool operator ==(ElasticVersion first, string second) => first == (ElasticVersion) second;
public static bool operator !=(ElasticVersion first, string second) => first != (ElasticVersion) second;
public static bool operator ==(ElasticVersion first, string second) => first == (ElasticVersion)second;
public static bool operator !=(ElasticVersion first, string second) => first != (ElasticVersion)second;


public static bool operator ==(string first, ElasticVersion second) => (ElasticVersion) first == second;
public static bool operator !=(string first, ElasticVersion second) => (ElasticVersion) first != second;
public static bool operator ==(string first, ElasticVersion second) => (ElasticVersion)first == second;
public static bool operator !=(string first, ElasticVersion second) => (ElasticVersion)first != second;

// ReSharper disable once UnusedMember.Local
private bool Equals(ElasticVersion other) => base.Equals(other);
Expand Down