Skip to content

Commit d00a252

Browse files
authored
Add handling for speculative matches in content source logic (#1297)
This will flag version branches as a speculative match ensuring we'll onboard new version branches before they are actively marked as current / next. If a current branch is already a version branch we will only build new version branches speculatively. This also ensures new repositories will speculatively build for `main` / `master` or version branches.
1 parent bf9dfd6 commit d00a252

File tree

11 files changed

+53
-9
lines changed

11 files changed

+53
-9
lines changed

.github/workflows/preview-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
run: |
8282
echo "Non sensitive data, echo'ing here temporarily to validate this job before connecting it further into the build job"
8383
echo "content-source-match=${{ steps.event-check.outputs.content-source-match != '' && steps.event-check.outputs.content-source-match || steps.match.outputs.content-source-match }}"
84-
echo "content-source-name=${{ steps.event-check.outputs.content-source-next != '' && steps.event-check.outputs.content-source-next || steps.match.outputs.content-source-next }}"
84+
echo "content-source-next=${{ steps.event-check.outputs.content-source-next != '' && steps.event-check.outputs.content-source-next || steps.match.outputs.content-source-next }}"
8585
echo "content-source-current=${{ steps.event-check.outputs.content-source-current != '' && steps.event-check.outputs.content-source-current || steps.match.outputs.content-source-current }}"
8686
echo "ref=${{ github.ref_name }}"
8787
echo "repo=${{ github.repository }}"

actions/assembler-match/action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ outputs:
2020
description: "true/false indicating the branch acts as the next content source"
2121
content-source-current:
2222
description: "true/false indicating the branch acts as the current content source"
23+
content-source-speculative:
24+
description: "true/false speculative match, used to build version branches before they are marked as current/next"
2325

2426
runs:
2527
using: 'docker'

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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 System.Text.RegularExpressions;
56
using Elastic.Documentation.Serialization;
67
using YamlDotNet.Serialization;
78
using YamlStaticContext = Elastic.Documentation.Configuration.Serialization.YamlStaticContext;
@@ -83,19 +84,45 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string
8384
/// <paramref name="repository"/>.
8485
public ContentSourceMatch Match(string repository, string branchOrTag)
8586
{
86-
var repositoryName = repository.Split('/').Last();
87-
var match = new ContentSourceMatch(null, null);
87+
var match = new ContentSourceMatch(null, null, false);
88+
var tokens = repository.Split('/');
89+
var repositoryName = tokens.Last();
90+
var owner = tokens.First();
91+
92+
if (tokens.Length < 2 || owner != "elastic")
93+
return match;
94+
8895
if (ReferenceRepositories.TryGetValue(repositoryName, out var r))
8996
{
90-
if (r.GetBranch(ContentSource.Current) == branchOrTag)
97+
var current = r.GetBranch(ContentSource.Current);
98+
var next = r.GetBranch(ContentSource.Next);
99+
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
100+
if (current == branchOrTag)
91101
match = match with { Current = ContentSource.Current };
92-
if (r.GetBranch(ContentSource.Next) == branchOrTag)
102+
if (next == branchOrTag)
93103
match = match with { Next = ContentSource.Next };
104+
if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))
105+
{
106+
// if the current branch is a version, only speculatively match if branch is actually a new version
107+
if (SemVersion.TryParse(current + ".0", out var currentVersion))
108+
{
109+
if (v >= currentVersion)
110+
match = match with { Speculative = true };
111+
}
112+
// assume we are newly onboarding the repository to current/next
113+
else
114+
match = match with { Speculative = true };
115+
}
94116
return match;
95117
}
96118

97119
if (repositoryName != NarrativeRepository.RepositoryName)
98-
return match;
120+
{
121+
// this is an unknown new elastic repository
122+
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
123+
if (isVersionBranch || branchOrTag == "main" || branchOrTag == "master")
124+
return match with { Speculative = true };
125+
}
99126

100127
if (Narrative.GetBranch(ContentSource.Current) == branchOrTag)
101128
match = match with { Current = ContentSource.Current };
@@ -105,5 +132,12 @@ public ContentSourceMatch Match(string repository, string branchOrTag)
105132
return match;
106133
}
107134

108-
public record ContentSourceMatch(ContentSource? Current, ContentSource? Next);
135+
public record ContentSourceMatch(ContentSource? Current, ContentSource? Next, bool Speculative);
136+
137+
}
138+
139+
internal static partial class ContentSourceRegex
140+
{
141+
[GeneratedRegex(@"^\d+\.\d+$", RegexOptions.IgnoreCase, "en-US")]
142+
public static partial Regex MatchVersionBranch();
109143
}

src/Elastic.Markdown/Helpers/SemVersion.cs renamed to src/Elastic.Documentation/SemVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Globalization;
77
using System.Text.RegularExpressions;
88

9-
namespace Elastic.Markdown.Helpers;
9+
namespace Elastic.Documentation;
1010

1111
/// <summary>
1212
/// A semver2 compatible version.

src/Elastic.Markdown/Myst/Directives/VersionBlock.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
56
using Elastic.Markdown.Diagnostics;
67
using Elastic.Markdown.Helpers;
78
using static System.StringSplitOptions;

src/Elastic.Markdown/Myst/FrontMatter/AllVersions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
56
using Elastic.Markdown.Helpers;
67
using YamlDotNet.Core;
78
using YamlDotNet.Core.Events;

src/Elastic.Markdown/Myst/FrontMatter/Applicability.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Text;
8+
using Elastic.Documentation;
89
using Elastic.Markdown.Helpers;
910
using YamlDotNet.Serialization;
1011

src/Elastic.Markdown/Myst/Roles/AppliesTo/AppliesToRole.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44

55
using System.Diagnostics;
6+
using Elastic.Documentation;
67
using Elastic.Markdown.Diagnostics;
78
using Elastic.Markdown.Helpers;
89
using Elastic.Markdown.Myst.FrontMatter;

src/tooling/docs-assembler/Cli/ContentSourceCommands.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,13 @@ public async Task<int> Match([Argument] string? repository = null, [Argument] st
5555
AllowIndexing = false
5656
};
5757
var matches = assembleContext.Configuration.Match(repo, refName);
58-
if (matches is { Current: null, Next: null })
58+
if (matches is { Current: null, Next: null, Speculative: false })
5959
{
6060
logger.LogInformation("'{Repository}' '{BranchOrTag}' combination not found in configuration.", repo, refName);
6161
await githubActionsService.SetOutputAsync("content-source-match", "false");
6262
await githubActionsService.SetOutputAsync("content-source-next", "false");
6363
await githubActionsService.SetOutputAsync("content-source-current", "false");
64+
await githubActionsService.SetOutputAsync("content-source-speculative", "false");
6465
}
6566
else
6667
{
@@ -72,6 +73,7 @@ public async Task<int> Match([Argument] string? repository = null, [Argument] st
7273
await githubActionsService.SetOutputAsync("content-source-match", "true");
7374
await githubActionsService.SetOutputAsync("content-source-next", matches.Next is not null ? "true" : "false");
7475
await githubActionsService.SetOutputAsync("content-source-current", matches.Current is not null ? "true" : "false");
76+
await githubActionsService.SetOutputAsync("content-source-speculative", matches.Speculative ? "true" : "false");
7577
}
7678

7779
await collector.StopAsync(ctx);

src/tooling/docs-builder/Cli/CheckForUpdatesFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System.Reflection;
66
using ConsoleAppFramework;
7+
using Elastic.Documentation;
78
using Elastic.Markdown.Helpers;
89
using Elastic.Markdown.IO;
910

tests/Elastic.Markdown.Tests/Directives/VersionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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;
56
using Elastic.Markdown.Helpers;
67
using Elastic.Markdown.Myst.Directives;
78
using FluentAssertions;

0 commit comments

Comments
 (0)