Skip to content

Commit 5465984

Browse files
committed
Improve product version inference
1 parent 2feb57e commit 5465984

File tree

2 files changed

+69
-7
lines changed

2 files changed

+69
-7
lines changed

src/Elastic.Documentation.Configuration/Versions/VersionInference.cs

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using Elastic.Documentation.AppliesTo;
56
using Elastic.Documentation.Configuration.LegacyUrlMappings;
67
using Elastic.Documentation.Configuration.Products;
78

89
namespace Elastic.Documentation.Configuration.Versions;
910

1011
public interface IVersionInferrerService
1112
{
12-
VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages);
13+
VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages, IReadOnlyCollection<Product>? products, ApplicableTo? applicableTo);
1314
}
1415

1516
public class ProductVersionInferrerService(ProductsConfiguration productsConfiguration, VersionsConfiguration versionsConfiguration) : IVersionInferrerService
1617
{
1718
private ProductsConfiguration ProductsConfiguration { get; } = productsConfiguration;
1819
private VersionsConfiguration VersionsConfiguration { get; } = versionsConfiguration;
19-
public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages)
20+
public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages, IReadOnlyCollection<Product>? products, ApplicableTo? applicableTo)
2021
{
21-
var versioning = legacyPages is not null && legacyPages.Count > 0
22-
? legacyPages.ElementAt(0).Product.VersioningSystem! // If the page has a legacy page mapping, use the versioning system of the legacy page
23-
: ProductsConfiguration.Products.TryGetValue(repositoryName, out var belonging)
22+
// docs-content handles content from multiple products which should preferably be inferred through frontmatter metadata
23+
if (repositoryName.Equals("docs-content", StringComparison.OrdinalIgnoreCase))
24+
{
25+
if (products is { Count: > 0 }) // If the page is from multiple products, use the versioning system of the first product
26+
return products.First().VersioningSystem!;
27+
if (applicableTo is not null)
28+
{
29+
var versioningFromApplicability = VersioningFromApplicability(applicableTo); // Try to infer the versioning system from the applicability metadata
30+
if (versioningFromApplicability is not null)
31+
return versioningFromApplicability;
32+
}
33+
}
34+
if (legacyPages is { Count: > 0 })
35+
return legacyPages.ElementAt(0).Product.VersioningSystem!; // If the page has a legacy page mapping, use the versioning system of the legacy page
36+
37+
var versioning = ProductsConfiguration.Products.TryGetValue(repositoryName, out var belonging)
2438
? belonging.VersioningSystem! //If the page's docset has a name with a direct product match, use the versioning system of the product
2539
: ProductsConfiguration.Products.Values.SingleOrDefault(p =>
2640
p.Repository is not null && p.Repository.Equals(repositoryName, StringComparison.OrdinalIgnoreCase)) is { } repositoryMatch
@@ -29,11 +43,59 @@ public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<
2943

3044
return versioning;
3145
}
46+
47+
private VersioningSystem? VersioningFromApplicability(ApplicableTo applicableTo)
48+
{
49+
VersioningSystem? versioning = null;
50+
if (applicableTo.ProductApplicability is not null)
51+
{
52+
versioning = applicableTo.ProductApplicability switch
53+
{
54+
{ ApmAgentAndroid: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentAndroid],
55+
{ ApmAgentIos: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentIos],
56+
{ ApmAgentJava: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentJava],
57+
{ ApmAgentDotnet: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentDotnet],
58+
{ ApmAgentGo: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentGo],
59+
{ ApmAgentNode: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentNode],
60+
{ ApmAgentPhp: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentPhp],
61+
{ ApmAgentPython: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentPython],
62+
{ ApmAgentRuby: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentRuby],
63+
{ ApmAgentRumJs: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.ApmAgentRumJs],
64+
{ Curator: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.Curator],
65+
{ Ecctl: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.Ecctl],
66+
{ EdotAndroid: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotAndroid],
67+
{ EdotCfAws: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotCfAws],
68+
{ EdotCfAzure: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotCfAzure],
69+
{ EdotCollector: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotCollector],
70+
{ EdotIos: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotIos],
71+
{ EdotJava: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotJava],
72+
{ EdotNode: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotNode],
73+
{ EdotDotnet: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotDotnet],
74+
{ EdotPhp: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotPhp],
75+
{ EdotPython: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.EdotPython],
76+
_ => null
77+
};
78+
}
79+
if (versioning is not null)
80+
return versioning;
81+
if (applicableTo.Deployment is not null)
82+
{
83+
versioning = applicableTo.Deployment switch
84+
{
85+
{ Ece: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.Ece],
86+
{ Eck: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.Eck],
87+
{ Ess: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.Ess],
88+
{ Self: not null } => VersionsConfiguration.VersioningSystems[VersioningSystemId.Self],
89+
_ => null
90+
};
91+
}
92+
return versioning;
93+
}
3294
}
3395

3496
public class NoopVersionInferrer : IVersionInferrerService
3597
{
36-
public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages) => new()
98+
public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages, IReadOnlyCollection<Product>? products, ApplicableTo? applicableTo) => new()
3799
{
38100
Id = VersioningSystemId.Stack,
39101
Base = new SemVersion(0, 0, 0),

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
105105
fullNavigationRenderResult
106106
);
107107

108-
var pageVersioning = VersionInferrerService.InferVersion(DocumentationSet.Context.Git.RepositoryName, legacyPages);
108+
var pageVersioning = VersionInferrerService.InferVersion(DocumentationSet.Context.Git.RepositoryName, legacyPages, markdown.YamlFrontMatter?.Products, markdown.YamlFrontMatter?.AppliesTo);
109109

110110
var currentBaseVersion = $"{pageVersioning.Base.Major}.{pageVersioning.Base.Minor}+";
111111

0 commit comments

Comments
 (0)