Skip to content

Commit 62afb90

Browse files
authored
Improve versioning system inference (#2070)
* Improve versioning system inference * Move inference logic to a service and use it
1 parent 6a86bc4 commit 62afb90

File tree

7 files changed

+66
-6
lines changed

7 files changed

+66
-6
lines changed

config/products.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,25 @@ products:
9191
versioning: 'stack'
9292
edot-ios:
9393
display: 'Elastic Distribution of OpenTelemetry iOS'
94+
repository: 'apm-agent-ios'
9495
edot-android:
9596
display: 'Elastic Distribution of OpenTelemetry Android'
97+
repository: 'apm-agent-android'
9698
edot-dotnet:
9799
display: 'Elastic Distribution of OpenTelemetry .NET'
100+
repository: 'elastic-otel-dotnet'
98101
edot-java:
99102
display: 'Elastic Distribution of OpenTelemetry Java'
103+
repository: 'elastic-otel-java'
100104
edot-node:
101105
display: 'Elastic Distribution of OpenTelemetry Node'
106+
repository: 'elastic-otel-node'
102107
edot-php:
103108
display: 'Elastic Distribution of OpenTelemetry PHP'
109+
repository: 'elastic-otel-php'
104110
edot-python:
105111
display: 'Elastic Distribution of OpenTelemetry Python'
112+
repository: 'elastic-otel-python'
106113
edot-cf-aws:
107114
display: 'EDOT Cloud Forwarder for AWS'
108115
edot-cf-azure:

docs/configure/site/products.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ products:
1010
edot-collector:
1111
display: 'Elastic Distribution of OpenTelemetry Collector'
1212
versioning: 'stack'
13+
repository: 'elastic-edot-collector'
1314
#...
1415
```
1516

@@ -19,6 +20,7 @@ products:
1920
: A YAML mapping where each key is an Elastic product.
2021
* `display`: A friendly name for the product.
2122
* `versioning`: The versioning system used by the project. The value for this field must match one of the versioning systems defined in [`versions.yml`](https://github.com/elastic/docs-builder/blob/main/config/versions.yml)
23+
* `repository`: The repository name for the product. It's optional and primarily intended for handling edge cases where there is a mismatch between the repository name and the product identifier.
2224

2325

2426

src/Elastic.Documentation.Configuration/Products/Product.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public record Product
1919
public required string Id { get; init; }
2020
public required string DisplayName { get; init; }
2121
public VersioningSystem? VersioningSystem { get; init; }
22+
public string? Repository { get; init; }
2223
}
2324

src/Elastic.Documentation.Configuration/Products/ProductExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public static ProductsConfiguration CreateProducts(this ConfigurationFileProvide
2121
{
2222
Id = kvp.Key,
2323
DisplayName = kvp.Value.Display,
24-
VersioningSystem = versionsConfiguration.GetVersioningSystem(VersionsConfigurationExtensions.ToVersioningSystemId(kvp.Value.Versioning ?? kvp.Key))
24+
VersioningSystem = versionsConfiguration.GetVersioningSystem(VersionsConfigurationExtensions.ToVersioningSystemId(kvp.Value.Versioning ?? kvp.Key)),
25+
Repository = kvp.Value.Repository ?? kvp.Key
2526
});
2627

2728
return new ProductsConfiguration
@@ -41,4 +42,5 @@ internal sealed record ProductDto
4142
{
4243
public string Display { get; set; } = string.Empty;
4344
public string? Versioning { get; set; }
45+
public string? Repository { get; set; }
4446
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Documentation.Configuration.LegacyUrlMappings;
6+
using Elastic.Documentation.Configuration.Products;
7+
8+
namespace Elastic.Documentation.Configuration.Versions;
9+
10+
public interface IVersionInferrerService
11+
{
12+
VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages);
13+
}
14+
15+
public class ProductVersionInferrerService(ProductsConfiguration productsConfiguration, VersionsConfiguration versionsConfiguration) : IVersionInferrerService
16+
{
17+
private ProductsConfiguration ProductsConfiguration { get; } = productsConfiguration;
18+
private VersionsConfiguration VersionsConfiguration { get; } = versionsConfiguration;
19+
public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages)
20+
{
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)
24+
? belonging.VersioningSystem! //If the page's docset has a name with a direct product match, use the versioning system of the product
25+
: ProductsConfiguration.Products.Values.SingleOrDefault(p =>
26+
p.Repository is not null && p.Repository.Equals(repositoryName, StringComparison.OrdinalIgnoreCase)) is { } repositoryMatch
27+
? repositoryMatch.VersioningSystem! // Verify if the page belongs to a repository linked to a product, and if so, use the versioning system of the product
28+
: VersionsConfiguration.VersioningSystems[VersioningSystemId.Stack]; // Fallback to the stack versioning system
29+
30+
return versioning;
31+
}
32+
}
33+
34+
public class NoopVersionInferrer : IVersionInferrerService
35+
{
36+
public VersioningSystem InferVersion(string repositoryName, IReadOnlyCollection<LegacyPageMapping>? legacyPages) => new()
37+
{
38+
Id = VersioningSystemId.Stack,
39+
Base = new SemVersion(0, 0, 0),
40+
Current = new SemVersion(0, 0, 0)
41+
};
42+
}

src/Elastic.Markdown/DocumentationGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Elastic.Documentation;
88
using Elastic.Documentation.Configuration;
99
using Elastic.Documentation.Configuration.LegacyUrlMappings;
10+
using Elastic.Documentation.Configuration.Versions;
1011
using Elastic.Documentation.Links;
1112
using Elastic.Documentation.Links.CrossLinks;
1213
using Elastic.Documentation.Serialization;
@@ -71,8 +72,9 @@ public DocumentationGenerator(
7172
DocumentationSet = docSet;
7273
Context = docSet.Context;
7374
CrossLinkResolver = docSet.CrossLinkResolver;
75+
var productVersionInferrer = new ProductVersionInferrerService(DocumentationSet.Context.ProductsConfiguration, DocumentationSet.Context.VersionsConfiguration);
7476
HtmlWriter = new HtmlWriter(DocumentationSet, _writeFileSystem, new DescriptionGenerator(), navigationHtmlWriter, legacyUrlMapper,
75-
positionalNavigation);
77+
positionalNavigation, productVersionInferrer);
7678
_documentationFileExporter =
7779
docSet.Context.AvailableExporters.Contains(Exporter.Html)
7880
? docSet.EnabledExtensions.FirstOrDefault(e => e.FileExporter != null)?.FileExporter

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class HtmlWriter(
2525
IDescriptionGenerator descriptionGenerator,
2626
INavigationHtmlWriter? navigationHtmlWriter = null,
2727
ILegacyUrlMapper? legacyUrlMapper = null,
28-
IPositionalNavigation? positionalNavigation = null
28+
IPositionalNavigation? positionalNavigation = null,
29+
IVersionInferrerService? versionInferrerService = null
2930
)
3031
: IMarkdownStringRenderer
3132
{
@@ -38,6 +39,8 @@ public class HtmlWriter(
3839
private ILegacyUrlMapper LegacyUrlMapper { get; } = legacyUrlMapper ?? new NoopLegacyUrlMapper();
3940
private IPositionalNavigation PositionalNavigation { get; } = positionalNavigation ?? documentationSet;
4041

42+
private IVersionInferrerService VersionInferrerService { get; } = versionInferrerService ?? new NoopVersionInferrer();
43+
4144
/// <inheritdoc />
4245
public string Render(string markdown, IFileInfo? source)
4346
{
@@ -102,9 +105,10 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
102105
fullNavigationRenderResult
103106
);
104107

105-
var currentBaseVersion = legacyPages is { Count: > 0 }
106-
? $"{legacyPages.ElementAt(0).Product.VersioningSystem?.Base.Major}.{legacyPages.ElementAt(0).Product.VersioningSystem?.Base.Minor}+"
107-
: $"{DocumentationSet.Context.VersionsConfiguration.VersioningSystems[VersioningSystemId.Stack].Base.Major}.{DocumentationSet.Context.VersionsConfiguration.VersioningSystems[VersioningSystemId.Stack].Base.Minor}+";
108+
var pageVersioning = VersionInferrerService.InferVersion(DocumentationSet.Context.Git.RepositoryName, legacyPages);
109+
110+
var currentBaseVersion = $"{pageVersioning.Base.Major}.{pageVersioning.Base.Minor}+";
111+
108112
//TODO should we even distinctby
109113
var breadcrumbs = parents.Reverse().DistinctBy(p => p.Url).ToArray();
110114
var breadcrumbsList = CreateStructuredBreadcrumbsData(markdown, breadcrumbs);

0 commit comments

Comments
 (0)