-
Notifications
You must be signed in to change notification settings - Fork 34
Automate ordering of applies-switch tabs and badges #2437
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/automate-ordering-tabs-badges
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4b06fa
Initial plan
Copilot a62304a
Add automatic ordering for applies-switch tabs and badges
Copilot 42ad317
Add comprehensive tests for automatic ordering
Copilot a9e500a
Update F# authoring tests for new badge ordering
Copilot 4cc5445
Document automatic ordering in applies-switch and applies docs
Copilot a213b07
Potential fix for pull request finding 'Missed opportunity to use Sel…
reakaleek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
176 changes: 176 additions & 0 deletions
176
src/Elastic.Documentation/AppliesTo/ApplicableToOrderComparer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| namespace Elastic.Documentation.AppliesTo; | ||
|
|
||
| /// <summary> | ||
| /// Comparer for ordering ApplicableTo objects according to documentation standards. | ||
| /// Orders by: | ||
| /// 1. Serverless first, then Stack (latest to oldest) | ||
| /// 2. For deployments: ech/ess, ece, eck, self-managed | ||
| /// 3. Unavailable lifecycle comes last | ||
| /// </summary> | ||
| public class ApplicableToOrderComparer : IComparer<ApplicableTo?> | ||
| { | ||
| public int Compare(ApplicableTo? x, ApplicableTo? y) | ||
| { | ||
| if (ReferenceEquals(x, y)) | ||
| return 0; | ||
| if (x is null) | ||
| return 1; | ||
| if (y is null) | ||
| return -1; | ||
|
|
||
| // Check if either has unavailable lifecycle - unavailable goes last | ||
| var xIsUnavailable = IsUnavailable(x); | ||
| var yIsUnavailable = IsUnavailable(y); | ||
|
|
||
| if (xIsUnavailable && !yIsUnavailable) | ||
| return 1; | ||
| if (!xIsUnavailable && yIsUnavailable) | ||
| return -1; | ||
|
|
||
| // Both unavailable or both available, continue with normal ordering | ||
|
|
||
| // Determine the primary category for each | ||
| var xCategory = GetPrimaryCategory(x); | ||
| var yCategory = GetPrimaryCategory(y); | ||
|
|
||
| // Compare by category first | ||
| var categoryComparison = xCategory.CompareTo(yCategory); | ||
| if (categoryComparison != 0) | ||
| return categoryComparison; | ||
|
|
||
| // Within the same category, apply specific ordering rules | ||
| return xCategory switch | ||
| { | ||
| ApplicabilityCategory.Serverless => 0, | ||
| ApplicabilityCategory.Stack => CompareStack(x, y), | ||
| ApplicabilityCategory.Deployment => CompareDeployment(x, y), | ||
| _ => 0 | ||
| }; | ||
| } | ||
|
|
||
| private static bool IsUnavailable(ApplicableTo applicableTo) | ||
| { | ||
| // Check if any applicability has unavailable lifecycle | ||
| if (applicableTo.Stack is not null && | ||
| applicableTo.Stack.Any(a => a.Lifecycle == ProductLifecycle.Unavailable)) | ||
| return true; | ||
|
|
||
| if (applicableTo.Serverless is not null) | ||
| { | ||
| var serverless = applicableTo.Serverless; | ||
| if ((serverless.Elasticsearch?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false) || | ||
| (serverless.Observability?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false) || | ||
| (serverless.Security?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false)) | ||
| return true; | ||
| } | ||
|
|
||
| if (applicableTo.Deployment is not null) | ||
| { | ||
| var deployment = applicableTo.Deployment; | ||
| if ((deployment.Ess?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false) || | ||
| (deployment.Ece?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false) || | ||
| (deployment.Eck?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false) || | ||
| (deployment.Self?.Any(a => a.Lifecycle == ProductLifecycle.Unavailable) ?? false)) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static ApplicabilityCategory GetPrimaryCategory(ApplicableTo applicableTo) | ||
| { | ||
| // Serverless takes priority | ||
| if (applicableTo.Serverless is not null) | ||
| return ApplicabilityCategory.Serverless; | ||
|
|
||
| // Then Stack | ||
| if (applicableTo.Stack is not null) | ||
| return ApplicabilityCategory.Stack; | ||
|
|
||
| // Then Deployment | ||
| if (applicableTo.Deployment is not null) | ||
| return ApplicabilityCategory.Deployment; | ||
|
|
||
| // Default | ||
| return ApplicabilityCategory.Other; | ||
| } | ||
|
|
||
| private static int CompareStack(ApplicableTo x, ApplicableTo y) | ||
| { | ||
| // Stack: order from latest to oldest version | ||
| var xVersion = GetLatestVersion(x.Stack); | ||
| var yVersion = GetLatestVersion(y.Stack); | ||
|
|
||
| if (xVersion is null && yVersion is null) | ||
| return 0; | ||
| if (xVersion is null) | ||
| return 1; | ||
| if (yVersion is null) | ||
| return -1; | ||
|
|
||
| // Higher version comes first (descending order) | ||
| return yVersion.CompareTo(xVersion); | ||
| } | ||
|
|
||
| private static int CompareDeployment(ApplicableTo x, ApplicableTo y) | ||
| { | ||
| // Deployment order: ech/ess, ece, eck, self-managed | ||
| var xDeploymentType = GetPrimaryDeploymentType(x.Deployment!); | ||
| var yDeploymentType = GetPrimaryDeploymentType(y.Deployment!); | ||
|
|
||
| return xDeploymentType.CompareTo(yDeploymentType); | ||
| } | ||
|
|
||
| private static DeploymentType GetPrimaryDeploymentType(DeploymentApplicability deployment) | ||
| { | ||
| // Return the first deployment type found in priority order | ||
| if (deployment.Ess is not null) | ||
| return DeploymentType.Ech; // ESS = ECH (Elastic Cloud Hosted) | ||
| if (deployment.Ece is not null) | ||
| return DeploymentType.Ece; | ||
| if (deployment.Eck is not null) | ||
| return DeploymentType.Eck; | ||
| if (deployment.Self is not null) | ||
| return DeploymentType.SelfManaged; | ||
|
|
||
| return DeploymentType.Unknown; | ||
| } | ||
|
|
||
| private static SemVersion? GetLatestVersion(AppliesCollection? collection) | ||
| { | ||
| if (collection is null || collection.Count == 0) | ||
| return null; | ||
|
|
||
| // Find the highest version in the collection | ||
| SemVersion? latest = null; | ||
| foreach (var applicability in collection) | ||
| { | ||
| var version = applicability.Version?.Min; | ||
| if (version is not null && (latest is null || version > latest)) | ||
| latest = version; | ||
| } | ||
|
|
||
| return latest; | ||
| } | ||
|
|
||
| private enum ApplicabilityCategory | ||
| { | ||
| Serverless = 0, // Serverless first | ||
| Stack = 1, // Stack second | ||
| Deployment = 2, // Deployment third | ||
| Other = 3 // Everything else last | ||
| } | ||
|
|
||
| private enum DeploymentType | ||
| { | ||
| Ech = 0, // ECH/ESS first | ||
| Ece = 1, // ECE second | ||
| Eck = 2, // ECK third | ||
| SelfManaged = 3, // Self-managed last | ||
| Unknown = 4 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.