Skip to content

Commit 5bb383a

Browse files
committed
add automatic version sorting
Signed-off-by: bmorelli25 <[email protected]>
1 parent 0d4a3e7 commit 5bb383a

File tree

4 files changed

+106
-10
lines changed

4 files changed

+106
-10
lines changed

docs/syntax/applies.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Where:
7070
- The [version](#version) is optional
7171
- You can specify multiple states by separating them with a comma. For example: `stack: preview 9.1, ga 9.4`
7272

73+
:::{note}
74+
**Automatic Version Sorting**: When you specify multiple versions for the same product, the build system automatically sorts them in descending order (highest version first) regardless of the order you write them in the source file. For example, `stack: ga 8.18.6, ga 9.1.2, ga 8.19.2, ga 9.0.6` will be displayed as `stack: ga 9.1.2, ga 9.0.6, ga 8.19.2, ga 8.18.6`. Items without versions (like `ga` without a version or `all`) are sorted last.
75+
:::
76+
7377
Note that a key without any value doesn't show any badge in the output.
7478

7579
### Lifecycle

src/Elastic.Documentation/AppliesTo/Applicability.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public static bool TryParse(string? value, IList<(Severity, string)> diagnostics
3737
if (applications.Count == 0)
3838
return false;
3939

40-
availability = new AppliesCollection([.. applications]);
40+
// Sort by version in descending order (highest version first)
41+
// Items without versions (AllVersions.Instance) are sorted last
42+
var sortedApplications = applications.OrderByDescending(a => a.Version, new SemVersionComparer()).ToArray();
43+
availability = new AppliesCollection(sortedApplications);
4144
return true;
4245
}
4346

@@ -211,3 +214,26 @@ public static bool TryParse(string? value, IList<(Severity, string)> diagnostics
211214
return true;
212215
}
213216
}
217+
218+
/// <summary>
219+
/// Comparer for SemVersion objects that handles AllVersions.Instance as the lowest priority
220+
/// </summary>
221+
public class SemVersionComparer : IComparer<SemVersion?>
222+
{
223+
public int Compare(SemVersion? x, SemVersion? y)
224+
{
225+
// Handle null cases and AllVersions.Instance cases
226+
var xIsNonVersioned = x is null || ReferenceEquals(x, AllVersions.Instance);
227+
var yIsNonVersioned = y is null || ReferenceEquals(y, AllVersions.Instance);
228+
229+
if (xIsNonVersioned && yIsNonVersioned)
230+
return 0;
231+
if (xIsNonVersioned)
232+
return -1; // Non-versioned items sort last
233+
if (yIsNonVersioned)
234+
return 1; // Non-versioned items sort last
235+
236+
// Use default SemVersion comparison for actual versions
237+
return x!.CompareTo(y!);
238+
}
239+
}

tests/authoring/Applicability/AppliesToFrontMatter.fs

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ applies_to:
163163
let ``apply matches expected`` () =
164164
markdown |> appliesTo (ApplicableTo(
165165
Product=AppliesCollection([
166-
Applicability.op_Explicit "preview 9.5";
167-
Applicability.op_Explicit "removed 9.7"
166+
Applicability.op_Explicit "removed 9.7";
167+
Applicability.op_Explicit "preview 9.5"
168168
] |> Array.ofList)
169169
))
170170

@@ -212,3 +212,69 @@ applies_to:
212212
[<Fact>]
213213
let ``does not render label`` () =
214214
markdown |> appliesTo (Unchecked.defaultof<ApplicableTo>)
215+
216+
type ``sorts applies_to versions in descending order`` () =
217+
static let markdown = frontMatter """
218+
applies_to:
219+
stack: ga 8.18.6, ga 9.1.2, ga 8.19.2, ga 9.0.6
220+
"""
221+
[<Fact>]
222+
let ``versions are sorted highest to lowest`` () =
223+
let expectedVersions = [
224+
Applicability.op_Explicit "ga 9.1.2"
225+
Applicability.op_Explicit "ga 9.0.6"
226+
Applicability.op_Explicit "ga 8.19.2"
227+
Applicability.op_Explicit "ga 8.18.6"
228+
]
229+
markdown |> appliesTo (ApplicableTo(
230+
Stack=AppliesCollection(expectedVersions |> Array.ofList)
231+
))
232+
233+
type ``sorts applies_to with mixed versioned and non-versioned items`` () =
234+
static let markdown = frontMatter """
235+
applies_to:
236+
stack: ga 8.18.6, ga, ga 9.1.2, all, ga 8.19.2
237+
"""
238+
[<Fact>]
239+
let ``versioned items are sorted first, non-versioned items last`` () =
240+
let expectedVersions = [
241+
Applicability.op_Explicit "ga 9.1.2"
242+
Applicability.op_Explicit "ga 8.19.2"
243+
Applicability.op_Explicit "ga 8.18.6"
244+
Applicability.op_Explicit "ga"
245+
Applicability.op_Explicit "all"
246+
]
247+
markdown |> appliesTo (ApplicableTo(
248+
Stack=AppliesCollection(expectedVersions |> Array.ofList)
249+
))
250+
251+
type ``sorts applies_to with patch versions correctly`` () =
252+
static let markdown = frontMatter """
253+
applies_to:
254+
stack: ga 9.1, ga 9.1.1, ga 9.0.5
255+
"""
256+
[<Fact>]
257+
let ``patch versions are sorted correctly`` () =
258+
let expectedVersions = [
259+
Applicability.op_Explicit "ga 9.1.1"
260+
Applicability.op_Explicit "ga 9.1"
261+
Applicability.op_Explicit "ga 9.0.5"
262+
]
263+
markdown |> appliesTo (ApplicableTo(
264+
Stack=AppliesCollection(expectedVersions |> Array.ofList)
265+
))
266+
267+
type ``sorts applies_to with major versions correctly`` () =
268+
static let markdown = frontMatter """
269+
applies_to:
270+
stack: ga 3.x, ga 5.x
271+
"""
272+
[<Fact>]
273+
let ``major versions are sorted correctly`` () =
274+
let expectedVersions = [
275+
Applicability.op_Explicit "ga 5.x"
276+
Applicability.op_Explicit "ga 3.x"
277+
]
278+
markdown |> appliesTo (ApplicableTo(
279+
Stack=AppliesCollection(expectedVersions |> Array.ofList)
280+
))

tests/authoring/Inline/AppliesToRole.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ This is an inline {applies_to}`stack: preview 9.0, ga 9.1` element.
133133
let directives = markdown |> converts "index.md" |> parses<AppliesToRole>
134134
test <@ directives.Length = 1 @>
135135
directives |> appliesToDirective (ApplicableTo(
136-
Stack=AppliesCollection.op_Explicit "preview 9.0, ga 9.1"
136+
Stack=AppliesCollection.op_Explicit "ga 9.1, preview 9.0"
137137
))
138138

139139
[<Fact>]
@@ -143,20 +143,20 @@ This is an inline {applies_to}`stack: preview 9.0, ga 9.1` element.
143143
<span class="applies applies-inline">
144144
<span class="applicable-info" data-tippy-content="We plan to add this functionality in a future Elastic&nbsp;Stack update. Subject to change.
145145
146-
This functionality may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.">
146+
If this functionality is unavailable or behaves differently when deployed on ECH, ECE, ECK, or a self-managed installation, it will be indicated on the page.">
147147
<span class="applicable-name">Stack</span>
148148
<span class="applicable-separator"></span>
149-
<span class="applicable-meta applicable-meta-preview">
150-
Planned
149+
<span class="applicable-meta applicable-meta-ga">
150+
GA planned
151151
</span>
152152
</span>
153153
<span class="applicable-info" data-tippy-content="We plan to add this functionality in a future Elastic&nbsp;Stack update. Subject to change.
154154
155-
If this functionality is unavailable or behaves differently when deployed on ECH, ECE, ECK, or a self-managed installation, it will be indicated on the page.">
155+
This functionality may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features.">
156156
<span class="applicable-name">Stack</span>
157157
<span class="applicable-separator"></span>
158-
<span class="applicable-meta applicable-meta-ga">
159-
GA planned
158+
<span class="applicable-meta applicable-meta-preview">
159+
Planned
160160
</span>
161161
</span>
162162
</span>

0 commit comments

Comments
 (0)