Skip to content

feature/config folder arg #1580

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions config/assembler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ references:
current: 9.0
next: main
skip: true
sparse_paths: ["docs", "config"]
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.Collections;
using System.Runtime.Serialization;
using YamlDotNet.Serialization;

Expand Down Expand Up @@ -44,6 +45,9 @@ public record Repository
[YamlMember(Alias = "edge")]
public string GitReferenceEdge { get; set; } = "main";

[YamlMember(Alias = "sparse_paths")]
public string[] SparsePaths { get; set; } = ["docs"];

public string GetBranch(ContentSource contentSource) => contentSource switch
{
ContentSource.Current => GitReferenceCurrent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@

using System.IO.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using NetEscapades.EnumGenerators;

namespace Elastic.Documentation.Configuration;

[EnumExtensions]
public enum ConfigurationSource
{
Local,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use local and put the config in elastic/docs-internal-workflows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, as source of truth or as a copy?

I think we want the source of truth to remain in docs-builder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just me. But I like to think that the config is close to the place where it's being deployed from.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think config should live where its read.

If it lives in elastic/docs-internal-workflows we'd need multiple copies for each environment.

We can do a lot more PR validations from this repository too IMO. Let's discuss today :)

Checkout,
Embedded
}

public class ConfigurationFileProvider
{
private readonly IFileSystem _fileSystem;
private readonly string _assemblyName;

public ConfigurationSource ConfigurationSource { get; private set; } = ConfigurationSource.Embedded;
public string? GitReference { get; }

public ConfigurationFileProvider(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
Expand All @@ -22,6 +34,9 @@ public ConfigurationFileProvider(IFileSystem fileSystem)
AssemblerFile = CreateTemporaryConfigurationFile("assembler.yml");
NavigationFile = CreateTemporaryConfigurationFile("navigation.yml");
LegacyUrlMappingsFile = CreateTemporaryConfigurationFile("legacy-url-mappings.yml");
var path = GetAppDataPath("git-ref.txt");
if (ConfigurationSource == ConfigurationSource.Checkout && _fileSystem.File.Exists(path))
GitReference = _fileSystem.File.ReadAllText(path);
}

private IDirectoryInfo TemporaryDirectory { get; }
Expand All @@ -45,11 +60,21 @@ private IFileInfo CreateTemporaryConfigurationFile(string fileName)

private StreamReader GetLocalOrEmbedded(string fileName)
{
var configPath = GetLocalPath(fileName);
if (!_fileSystem.File.Exists(configPath))
return GetEmbeddedStream(fileName);
var reader = _fileSystem.File.OpenText(configPath);
return reader;
var localPath = GetLocalPath(fileName);
var appDataPath = GetAppDataPath(fileName);
if (_fileSystem.File.Exists(localPath))
{
ConfigurationSource = ConfigurationSource.Local;
var reader = _fileSystem.File.OpenText(localPath);
return reader;
}
if (_fileSystem.File.Exists(appDataPath))
{
ConfigurationSource = ConfigurationSource.Checkout;
var reader = _fileSystem.File.OpenText(appDataPath);
return reader;
}
return GetEmbeddedStream(fileName);
}

private StreamReader GetEmbeddedStream(string fileName)
Expand All @@ -60,9 +85,11 @@ private StreamReader GetEmbeddedStream(string fileName)
return reader;
}

public static string LocalConfigurationDirectory => Path.Combine(Paths.WorkingDirectoryRoot.FullName, "config");
private static string AppDataConfigurationDirectory { get; } = Path.Combine(Paths.ApplicationData.FullName, "config-clone", "config");
private static string LocalConfigurationDirectory { get; } = Path.Combine(Paths.WorkingDirectoryRoot.FullName, "config");

private static string GetLocalPath(string file) => Path.Combine(LocalConfigurationDirectory, file);
private static string GetAppDataPath(string file) => Path.Combine(AppDataConfigurationDirectory, file);
}

public static class ConfigurationFileProviderServiceCollectionExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// 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.Reflection;
using ConsoleAppFramework;
using Elastic.Documentation.Configuration;
using Microsoft.Extensions.Logging;

namespace Elastic.Documentation.Tooling.Filters;

public class InfoLoggerFilter(ConsoleAppFilter next, ILogger<InfoLoggerFilter> logger, ConfigurationFileProvider fileProvider) : ConsoleAppFilter(next)
{
public override async Task InvokeAsync(ConsoleAppContext context, Cancel cancellationToken)
{
logger.LogInformation("Configuration source: {ConfigurationSource}", fileProvider.ConfigurationSource.ToStringFast(true));
if (fileProvider.ConfigurationSource == ConfigurationSource.Checkout)
logger.LogInformation("Configuration source git reference: {ConfigurationSourceGitReference}", fileProvider.GitReference);
var assemblyVersion = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyInformationalVersionAttribute>()
.FirstOrDefault()?.InformationalVersion;
logger.LogInformation("Version: {Version}", assemblyVersion);
await Next.InvokeAsync(context, cancellationToken);
}
}
33 changes: 33 additions & 0 deletions src/tooling/docs-assembler/Cli/RepositoryCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ private void AssignOutputLogger()
ConsoleApp.LogError = msg => _log.LogError(msg);
}

/// <summary> Clone the configuration folder </summary>
/// <param name="gitRef">The git reference of the config, defaults to 'main'</param>
[Command("init-config")]
public async Task<int> CloneConfigurationFolder(string? gitRef = null, Cancel ctx = default)
{
await using var collector = new ConsoleDiagnosticsCollector(logFactory, githubActionsService).StartAsync(ctx);

var fs = new FileSystem();
var cachedPath = Path.Combine(Paths.ApplicationData.FullName, "config-clone");
var checkoutFolder = fs.DirectoryInfo.New(cachedPath);
var cloner = new RepositorySourcer(logFactory, checkoutFolder, fs, collector);

// relies on the embedded configuration, but we don't expect this to change
var repository = assemblyConfiguration.ReferenceRepositories["docs-builder"];
repository = repository with
{
SparsePaths = ["config"]
};
if (string.IsNullOrEmpty(gitRef))
gitRef = "main";

_log.LogInformation("Cloning configuration ({GitReference})", gitRef);
var checkout = cloner.CloneRef(repository, gitRef, appendRepositoryName: false);
_log.LogInformation("Cloned configuration ({GitReference}) to {ConfigurationFolder}", checkout.HeadReference, checkout.Directory.FullName);

var gitRefInformationFile = Path.Combine(cachedPath, "config", "git-ref.txt");
await fs.File.WriteAllTextAsync(gitRefInformationFile, checkout.HeadReference, ctx);

await collector.StopAsync(ctx);
return collector.Errors;
}


/// <summary> Clones all repositories </summary>
/// <param name="strict"> Treat warnings as errors and fail the build on warnings</param>
/// <param name="environment"> The environment to build</param>
Expand Down
1 change: 1 addition & 0 deletions src/tooling/docs-assembler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

var app = ConsoleApp.Create();
app.UseFilter<ReplaceLogFilter>();
app.UseFilter<InfoLoggerFilter>();
app.UseFilter<StopwatchFilter>();
app.UseFilter<CatchExceptionFilter>();

Expand Down
4 changes: 2 additions & 2 deletions src/tooling/docs-assembler/Sourcing/GitFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IGitRepository
bool IsInitialized();
void Pull(string branch);
void Fetch(string reference);
void EnableSparseCheckout(string folder);
void EnableSparseCheckout(string[] folders);
void DisableSparseCheckout();
void Checkout(string reference);
}
Expand All @@ -40,7 +40,7 @@ public class SingleCommitOptimizedGitRepository(DiagnosticsCollector collector,
public bool IsInitialized() => Directory.Exists(Path.Combine(WorkingDirectory.FullName, ".git"));
public void Pull(string branch) => ExecIn(EnvironmentVars, "git", "pull", "--depth", "1", "--allow-unrelated-histories", "--no-ff", "origin", branch);
public void Fetch(string reference) => ExecIn(EnvironmentVars, "git", "fetch", "--no-tags", "--prune", "--no-recurse-submodules", "--depth", "1", "origin", reference);
public void EnableSparseCheckout(string folder) => ExecIn(EnvironmentVars, "git", "sparse-checkout", "set", folder);
public void EnableSparseCheckout(string[] folders) => ExecIn(EnvironmentVars, "git", ["sparse-checkout", "set", .. folders]);
public void DisableSparseCheckout() => ExecIn(EnvironmentVars, "git", "sparse-checkout", "disable");
public void Checkout(string reference) => ExecIn(EnvironmentVars, "git", "checkout", "--force", reference);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ public class RepositorySourcer(ILoggerFactory logFactory, IDirectoryInfo checkou
// </summary>
// <param name="repository">The repository to clone.</param>
// <param name="gitRef">The git reference to check out. Branch, commit or tag</param>
public Checkout CloneRef(Repository repository, string gitRef, bool pull = false, int attempt = 1)
public Checkout CloneRef(Repository repository, string gitRef, bool pull = false, int attempt = 1, bool appendRepositoryName = true)
{
var checkoutFolder = readFileSystem.DirectoryInfo.New(Path.Combine(checkoutDirectory.FullName, repository.Name));
var checkoutFolder =
appendRepositoryName
? readFileSystem.DirectoryInfo.New(Path.Combine(checkoutDirectory.FullName, repository.Name))
: checkoutDirectory;
IGitRepository git = new SingleCommitOptimizedGitRepository(collector, checkoutFolder);
if (attempt > 3)
{
Expand Down Expand Up @@ -228,7 +231,7 @@ private static void FetchAndCheckout(IGitRepository git, Repository repository,
git.DisableSparseCheckout();
break;
case CheckoutStrategy.Partial:
git.EnableSparseCheckout("docs");
git.EnableSparseCheckout(repository.SparsePaths);
break;
default:
throw new ArgumentOutOfRangeException(nameof(repository), repository.CheckoutStrategy, null);
Expand Down
1 change: 1 addition & 0 deletions src/tooling/docs-builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
var app = ConsoleApp.Create();

app.UseFilter<ReplaceLogFilter>();
app.UseFilter<InfoLoggerFilter>();
app.UseFilter<StopwatchFilter>();
app.UseFilter<CatchExceptionFilter>();
app.UseFilter<CheckForUpdatesFilter>();
Expand Down
Loading