Skip to content

Commit cb8c7df

Browse files
authored
Smarter matches using product's versioning system (#2100)
* Smarter matches using product's versioning system * revert changes to main.css
1 parent 3ab5230 commit cb8c7df

File tree

4 files changed

+76
-65
lines changed

4 files changed

+76
-65
lines changed

src/Elastic.Documentation.Configuration/Assembler/AssemblyConfiguration.cs

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.Text.RegularExpressions;
6+
using Elastic.Documentation.Configuration.Products;
67
using Elastic.Documentation.Extensions;
8+
using Microsoft.Extensions.Logging;
79
using YamlDotNet.Serialization;
8-
using YamlStaticContext = Elastic.Documentation.Configuration.Serialization.YamlStaticContext;
910

1011
namespace Elastic.Documentation.Configuration.Assembler;
1112

@@ -132,112 +133,110 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string
132133

133134
/// Returns whether the <paramref name="branchOrTag"/> is configured as an integration branch or tag for the given
134135
/// <paramref name="repository"/>.
135-
public ContentSourceMatch Match(string repository, string branchOrTag)
136+
public ContentSourceMatch Match(ILoggerFactory logFactory, string repository, string branchOrTag, Product? product)
136137
{
138+
var logger = logFactory.CreateLogger<ContentSourceMatch>();
137139
var match = new ContentSourceMatch(null, null, null, false);
138140
var tokens = repository.Split('/');
139141
var repositoryName = tokens.Last();
140142
var owner = tokens.First();
143+
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
141144

142145
if (tokens.Length < 2 || owner != "elastic")
143-
return match;
144-
145-
if (ReferenceRepositories.TryGetValue(repositoryName, out var r))
146146
{
147-
var current = r.GetBranch(ContentSource.Current);
148-
var next = r.GetBranch(ContentSource.Next);
149-
var edge = r.GetBranch(ContentSource.Edge);
150-
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
151-
if (current == branchOrTag)
152-
{
153-
match = match with
154-
{
155-
Current = ContentSource.Current
156-
};
157-
}
158-
159-
if (next == branchOrTag)
160-
{
161-
match = match with
162-
{
163-
Next = ContentSource.Next
164-
};
165-
}
166-
167-
if (edge == branchOrTag)
168-
{
169-
match = match with
170-
{
171-
Edge = ContentSource.Edge
172-
};
173-
}
174-
175-
if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))
176-
{
177-
// if the current branch is a version, only speculatively match if branch is actually a new version
178-
if (SemVersion.TryParse(current + ".0", out var currentVersion))
179-
{
180-
if (v >= currentVersion)
181-
{
182-
match = match with
183-
{
184-
Speculative = true
185-
};
186-
}
187-
}
188-
// assume we are newly onboarding the repository to current/next
189-
else
190-
{
191-
match = match with
192-
{
193-
Speculative = true
194-
};
195-
}
196-
}
197-
147+
logger.LogInformation("Repository {Repository} is not a valid elastic repository but {Owner}", repository, owner);
198148
return match;
199149
}
200150

201-
if (repositoryName != NarrativeRepository.RepositoryName)
151+
// Check for new repositories
152+
if (!AvailableRepositories.TryGetValue(repositoryName, out var r))
202153
{
154+
logger.LogInformation("Repository {Repository} has not yet been onboarded into assembler.yml", repository);
203155
// this is an unknown new elastic repository
204-
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
205156
if (isVersionBranch || branchOrTag == "main" || branchOrTag == "master")
206157
{
158+
logger.LogInformation("Speculatively building {Repository} since it looks like an integration branch", repository);
207159
return match with
208160
{
209161
Speculative = true
210162
};
211163
}
164+
logger.LogInformation("{Repository} on '{Branch}' does not look like it needs a speculative build", repository, branchOrTag);
165+
return match;
212166
}
213167

214-
if (Narrative.GetBranch(ContentSource.Current) == branchOrTag)
168+
var current = r.GetBranch(ContentSource.Current);
169+
var next = r.GetBranch(ContentSource.Next);
170+
var edge = r.GetBranch(ContentSource.Edge);
171+
logger.LogInformation("Active content-sources for {Repository}. current: {Current}, next: {Next}, edge: {Edge}' ", repository, current, next, edge);
172+
if (current == branchOrTag)
215173
{
174+
logger.LogInformation("Content-Source current: {Current} matches: {Branch}", current, branchOrTag);
216175
match = match with
217176
{
218177
Current = ContentSource.Current
219178
};
220179
}
221180

222-
if (Narrative.GetBranch(ContentSource.Next) == branchOrTag)
181+
if (next == branchOrTag)
223182
{
183+
logger.LogInformation("Content-Source next: {Next} matches: {Branch}", next, branchOrTag);
224184
match = match with
225185
{
226186
Next = ContentSource.Next
227187
};
228188
}
229189

230-
if (Narrative.GetBranch(ContentSource.Edge) == branchOrTag)
190+
if (edge == branchOrTag)
231191
{
192+
logger.LogInformation("Content-Source edge: {Edge} matches: {Branch}", edge, branchOrTag);
232193
match = match with
233194
{
234195
Edge = ContentSource.Edge
235196
};
236197
}
237198

199+
// check version branches
200+
if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))
201+
{
202+
logger.LogInformation("Branch or tag {Branch} is a versioned branch", branchOrTag);
203+
// if the current branch is a version, only speculatively match if branch is actually a new version
204+
if (SemVersion.TryParse(current + ".0", out var currentVersion))
205+
{
206+
logger.LogInformation("Current is already using versioned branches {Current}", currentVersion);
207+
if (v >= currentVersion)
208+
{
209+
logger.LogInformation("Speculative build because {Branch} is gte current {Current}", branchOrTag, currentVersion);
210+
match = match with
211+
{
212+
Speculative = true
213+
};
214+
}
215+
else
216+
logger.LogInformation("NO speculative build because {Branch} is lt {Current}", branchOrTag, currentVersion);
217+
}
218+
// assume we are newly onboarding the repository to current/next
219+
else if (product?.VersioningSystem is { } versioningSystem)
220+
{
221+
logger.LogInformation("Current is not using versioned branches checking product info");
222+
var productCurrentVersion = versioningSystem.Current;
223+
if (v >= productCurrentVersion)
224+
{
225+
logger.LogInformation("Speculative build {Branch} is gte product current '{ProductCurrent}'", branchOrTag, productCurrentVersion);
226+
match = match with
227+
{
228+
Speculative = true
229+
};
230+
}
231+
else
232+
logger.LogInformation("NO speculative build {Branch} is lte product current '{ProductCurrent}'", branchOrTag, productCurrentVersion);
233+
}
234+
else
235+
logger.LogInformation("No versioning system found for {Repository} on {Branch}", repository, branchOrTag);
236+
}
237+
238238
// if we haven't matched anything yet, and the branch is 'main' or 'master' always build
239-
if (match is { Current: null, Next: null, Edge: null, Speculative: false }
240-
&& branchOrTag is "main" or "master")
239+
if (match is { Current: null, Next: null, Edge: null, Speculative: false } && branchOrTag is "main" or "master")
241240
{
242241
return match with
243242
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ namespace Elastic.Documentation.Configuration.Products;
1111
public record ProductsConfiguration
1212
{
1313
public required FrozenDictionary<string, Product> Products { get; init; }
14+
15+
16+
public Product? GetProductByRepositoryName(string repository)
17+
{
18+
var tokens = repository.Split('/');
19+
var repositoryName = tokens.Last();
20+
if (Products.TryGetValue(repositoryName, out var product))
21+
return product;
22+
var match = Products.Values.SingleOrDefault(p => p.Repository is not null && p.Repository.Equals(repositoryName, StringComparison.OrdinalIgnoreCase));
23+
return match;
24+
}
1425
}
1526

1627
[YamlSerializable]

src/services/Elastic.Documentation.Assembler/ContentSources/RepositoryBuildMatchingService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public async Task<bool> ShouldBuild(IDiagnosticsCollector collector, string? rep
4141

4242
// environment does not matter to check the configuration, defaulting to dev
4343
var assembleContext = new AssembleContext(configuration, configurationContext, "dev", collector, fileSystem, fileSystem, null, null);
44-
var matches = assembleContext.Configuration.Match(repo, refName);
44+
var product = assembleContext.ProductsConfiguration.GetProductByRepositoryName(repo);
45+
var matches = assembleContext.Configuration.Match(logFactory, repo, refName, product);
4546
if (matches is { Current: null, Next: null, Edge: null, Speculative: false })
4647
{
4748
_logger.LogInformation("'{Repository}' '{BranchOrTag}' combination not found in configuration.", repo, refName);

src/services/Elastic.Documentation.Services/ServiceInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public async Task<int> InvokeAsync(Cancel ctx)
5757
if (!success && task.Strict && collector.Errors + collector.Warnings == 0)
5858
collector.EmitGlobalError($"Service {task.ServiceName} registered as strict but returned false without emitting errors or warnings ");
5959
if (!success && !task.Strict && collector.Errors == 0)
60-
collector.EmitGlobalError($"Service {task.ServiceName} but returned false without emitting errors");
60+
collector.EmitGlobalError($"Service {task.ServiceName} returned false without emitting errors");
6161
}
6262
catch (Exception ex)
6363
{

0 commit comments

Comments
 (0)