Skip to content

Add handling for speculative matches in content source logic #1297

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

Merged
merged 1 commit into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/preview-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
run: |
echo "Non sensitive data, echo'ing here temporarily to validate this job before connecting it further into the build job"
echo "content-source-match=${{ steps.event-check.outputs.content-source-match != '' && steps.event-check.outputs.content-source-match || steps.match.outputs.content-source-match }}"
echo "content-source-name=${{ steps.event-check.outputs.content-source-next != '' && steps.event-check.outputs.content-source-next || steps.match.outputs.content-source-next }}"
echo "content-source-next=${{ steps.event-check.outputs.content-source-next != '' && steps.event-check.outputs.content-source-next || steps.match.outputs.content-source-next }}"
echo "content-source-current=${{ steps.event-check.outputs.content-source-current != '' && steps.event-check.outputs.content-source-current || steps.match.outputs.content-source-current }}"
echo "ref=${{ github.ref_name }}"
echo "repo=${{ github.repository }}"
Expand Down
2 changes: 2 additions & 0 deletions actions/assembler-match/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ outputs:
description: "true/false indicating the branch acts as the next content source"
content-source-current:
description: "true/false indicating the branch acts as the current content source"
content-source-speculative:
description: "true/false speculative match, used to build version branches before they are marked as current/next"

runs:
using: 'docker'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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

using System.Text.RegularExpressions;
using Elastic.Documentation.Serialization;
using YamlDotNet.Serialization;
using YamlStaticContext = Elastic.Documentation.Configuration.Serialization.YamlStaticContext;
Expand Down Expand Up @@ -83,19 +84,45 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string
/// <paramref name="repository"/>.
public ContentSourceMatch Match(string repository, string branchOrTag)
{
var repositoryName = repository.Split('/').Last();
var match = new ContentSourceMatch(null, null);
var match = new ContentSourceMatch(null, null, false);
var tokens = repository.Split('/');
var repositoryName = tokens.Last();
var owner = tokens.First();

if (tokens.Length < 2 || owner != "elastic")
return match;

if (ReferenceRepositories.TryGetValue(repositoryName, out var r))
{
if (r.GetBranch(ContentSource.Current) == branchOrTag)
var current = r.GetBranch(ContentSource.Current);
var next = r.GetBranch(ContentSource.Next);
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
if (current == branchOrTag)
match = match with { Current = ContentSource.Current };
if (r.GetBranch(ContentSource.Next) == branchOrTag)
if (next == branchOrTag)
match = match with { Next = ContentSource.Next };
if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))
{
// if the current branch is a version, only speculatively match if branch is actually a new version
if (SemVersion.TryParse(current + ".0", out var currentVersion))
{
if (v >= currentVersion)
match = match with { Speculative = true };
}
// assume we are newly onboarding the repository to current/next
else
match = match with { Speculative = true };
}
return match;
}

if (repositoryName != NarrativeRepository.RepositoryName)
return match;
{
// this is an unknown new elastic repository
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
if (isVersionBranch || branchOrTag == "main" || branchOrTag == "master")
return match with { Speculative = true };
}

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

public record ContentSourceMatch(ContentSource? Current, ContentSource? Next);
public record ContentSourceMatch(ContentSource? Current, ContentSource? Next, bool Speculative);

}

internal static partial class ContentSourceRegex
{
[GeneratedRegex(@"^\d+\.\d+$", RegexOptions.IgnoreCase, "en-US")]
public static partial Regex MatchVersionBranch();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace Elastic.Markdown.Helpers;
namespace Elastic.Documentation;

/// <summary>
/// A semver2 compatible version.
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Myst/Directives/VersionBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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

using Elastic.Documentation;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.Helpers;
using static System.StringSplitOptions;
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Myst/FrontMatter/AllVersions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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

using Elastic.Documentation;
using Elastic.Markdown.Helpers;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
Expand Down
1 change: 1 addition & 0 deletions src/Elastic.Markdown/Myst/FrontMatter/Applicability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using Elastic.Documentation;
using Elastic.Markdown.Helpers;
using YamlDotNet.Serialization;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information

using System.Diagnostics;
using Elastic.Documentation;
using Elastic.Markdown.Diagnostics;
using Elastic.Markdown.Helpers;
using Elastic.Markdown.Myst.FrontMatter;
Expand Down
4 changes: 3 additions & 1 deletion src/tooling/docs-assembler/Cli/ContentSourceCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ public async Task<int> Match([Argument] string? repository = null, [Argument] st
AllowIndexing = false
};
var matches = assembleContext.Configuration.Match(repo, refName);
if (matches is { Current: null, Next: null })
if (matches is { Current: null, Next: null, Speculative: false })
{
logger.LogInformation("'{Repository}' '{BranchOrTag}' combination not found in configuration.", repo, refName);
await githubActionsService.SetOutputAsync("content-source-match", "false");
await githubActionsService.SetOutputAsync("content-source-next", "false");
await githubActionsService.SetOutputAsync("content-source-current", "false");
await githubActionsService.SetOutputAsync("content-source-speculative", "false");
}
else
{
Expand All @@ -72,6 +73,7 @@ public async Task<int> Match([Argument] string? repository = null, [Argument] st
await githubActionsService.SetOutputAsync("content-source-match", "true");
await githubActionsService.SetOutputAsync("content-source-next", matches.Next is not null ? "true" : "false");
await githubActionsService.SetOutputAsync("content-source-current", matches.Current is not null ? "true" : "false");
await githubActionsService.SetOutputAsync("content-source-speculative", matches.Speculative ? "true" : "false");
}

await collector.StopAsync(ctx);
Expand Down
1 change: 1 addition & 0 deletions src/tooling/docs-builder/Cli/CheckForUpdatesFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Reflection;
using ConsoleAppFramework;
using Elastic.Documentation;
using Elastic.Markdown.Helpers;
using Elastic.Markdown.IO;

Expand Down
1 change: 1 addition & 0 deletions tests/Elastic.Markdown.Tests/Directives/VersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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

using Elastic.Documentation;
using Elastic.Markdown.Helpers;
using Elastic.Markdown.Myst.Directives;
using FluentAssertions;
Expand Down
Loading