diff --git a/actions/assembler-match/action.yml b/actions/assembler-match/action.yml new file mode 100644 index 000000000..112e421f2 --- /dev/null +++ b/actions/assembler-match/action.yml @@ -0,0 +1,26 @@ +name: 'Documentation Assembler Match' +description: 'Checks if the current commit is on either a current or next branch' + +branding: + icon: 'filter' + color: 'blue' + +inputs: + repository: + description: 'the repository name' + required: true + ref_name: + description: 'The branch or tag name' + required: true + +outputs: + content-source-match: + description: 'true/false indicating the branch matches a content-source' + content-source-name: + description: "The name of the content source that matches (current/next) or empty" + +runs: + using: 'docker' + image: "docker://ghcr.io/elastic/docs-assembler:edge" + env: + INPUT_COMMAND: "content-source match" diff --git a/docs-builder.sln b/docs-builder.sln index d5d61dbb1..3dff4f102 100644 --- a/docs-builder.sln +++ b/docs-builder.sln @@ -89,6 +89,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tooling", "tooling", "{73AB EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "authoring", "authoring", "{059E787F-85C1-43BE-9DD6-CE319E106383}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assembler-filter", "assembler-filter", "{FB1C1954-D8E2-4745-BA62-04DD82FB4792}" + ProjectSection(SolutionItems) = preProject + actions\assembler-filter\action.yml = actions\assembler-filter\action.yml + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -172,5 +177,6 @@ Global {4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA} = {73ABAE37-118F-4A53-BC2C-F19333555C90} {059E787F-85C1-43BE-9DD6-CE319E106383} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A} {7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166} = {059E787F-85C1-43BE-9DD6-CE319E106383} + {FB1C1954-D8E2-4745-BA62-04DD82FB4792} = {245023D2-D3CA-47B9-831D-DAB91A2FFDC7} EndGlobalSection EndGlobal diff --git a/src/Elastic.Documentation.Configuration/Assembler/AssemblyConfiguration.cs b/src/Elastic.Documentation.Configuration/Assembler/AssemblyConfiguration.cs index 5308eec03..652465f01 100644 --- a/src/Elastic.Documentation.Configuration/Assembler/AssemblyConfiguration.cs +++ b/src/Elastic.Documentation.Configuration/Assembler/AssemblyConfiguration.cs @@ -78,4 +78,29 @@ private static TRepository RepositoryDefaults(TRepository r, string [YamlMember(Alias = "named_git_references")] public Dictionary NamedGitReferences { get; set; } = []; + + /// Returns whether the is configured as an integration branch or tag for the given + /// . + public ContentSource? Match(string repository, string branchOrTag) + { + var repositoryName = repository.Split('/').Last(); + if (ReferenceRepositories.TryGetValue(repositoryName, out var r)) + { + if (r.GetBranch(ContentSource.Current) == branchOrTag) + return ContentSource.Current; + if (r.GetBranch(ContentSource.Next) == branchOrTag) + return ContentSource.Next; + return null; + } + + if (repositoryName == NarrativeRepository.RepositoryName) + { + if (Narrative.GetBranch(ContentSource.Current) == branchOrTag) + return ContentSource.Current; + if (Narrative.GetBranch(ContentSource.Next) == branchOrTag) + return ContentSource.Next; + } + + return null; + } } diff --git a/src/tooling/docs-assembler/Cli/ContentSourceCommands.cs b/src/tooling/docs-assembler/Cli/ContentSourceCommands.cs new file mode 100644 index 000000000..3f8a5e4de --- /dev/null +++ b/src/tooling/docs-assembler/Cli/ContentSourceCommands.cs @@ -0,0 +1,74 @@ +// 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.Diagnostics.CodeAnalysis; +using System.IO.Abstractions; +using Actions.Core.Services; +using ConsoleAppFramework; +using Elastic.Documentation.Configuration.Assembler; +using Elastic.Documentation.Tooling.Diagnostics.Console; +using Microsoft.Extensions.Logging; + +namespace Documentation.Assembler.Cli; + +internal sealed class ContentSourceCommands(ICoreService githubActionsService, ILoggerFactory logFactory) +{ + [SuppressMessage("Usage", "CA2254:Template should be a static expression")] + private void AssignOutputLogger() + { + var log = logFactory.CreateLogger(); + ConsoleApp.Log = msg => log.LogInformation(msg); + ConsoleApp.LogError = msg => log.LogError(msg); + } + + /// + /// + /// + /// + [Command("match")] + public async Task Match([Argument] string? repository = null, [Argument] string? branchOrTag = null, Cancel ctx = default) + { + AssignOutputLogger(); + var repo = repository ?? githubActionsService.GetInput("repository"); + if (string.IsNullOrEmpty(repo)) + throw new ArgumentNullException(nameof(repository)); + var refName = branchOrTag ?? githubActionsService.GetInput("ref_name"); + if (string.IsNullOrEmpty(refName)) + throw new ArgumentNullException(nameof(branchOrTag)); + + await using var collector = new ConsoleDiagnosticsCollector(logFactory, githubActionsService) + { + NoHints = true + }; + + _ = collector.StartAsync(ctx); + + // environment does not matter to check the configuration, defaulting to dev + var assembleContext = new AssembleContext("dev", collector, new FileSystem(), new FileSystem(), null, null) + { + Force = false, + AllowIndexing = false + }; + var logger = logFactory.CreateLogger(); + var matches = assembleContext.Configuration.Match(repo, refName); + if (matches == null) + { + logger.LogInformation("'{Repository}' '{BranchOrTag}' combination not found in configuration.", repository, branchOrTag); + await githubActionsService.SetOutputAsync("content-source-match", "false"); + await githubActionsService.SetOutputAsync("content-source-name", ""); + } + else + { + var name = matches.Value.ToStringFast(true); + logger.LogInformation("'{Repository}' '{BranchOrTag}' is configured as '{Matches}' content-source", repository, branchOrTag, name); + + await githubActionsService.SetOutputAsync("content-source-match", "true"); + await githubActionsService.SetOutputAsync("content-source-name", name); + } + + await collector.StopAsync(ctx); + return matches != null && collector.Errors == 0 ? 0 : 1; + } + +} diff --git a/src/tooling/docs-assembler/Cli/RepositoryCommands.cs b/src/tooling/docs-assembler/Cli/RepositoryCommands.cs index 2e0c7aafc..c25585efe 100644 --- a/src/tooling/docs-assembler/Cli/RepositoryCommands.cs +++ b/src/tooling/docs-assembler/Cli/RepositoryCommands.cs @@ -90,7 +90,7 @@ public async Task BuildAll( var assembleContext = new AssembleContext(environment, collector, new FileSystem(), new FileSystem(), null, null) { Force = force ?? false, - AllowIndexing = allowIndexing ?? false, + AllowIndexing = allowIndexing ?? false }; // this validates all path prefixes are unique, early exit if duplicates are detected diff --git a/src/tooling/docs-assembler/Program.cs b/src/tooling/docs-assembler/Program.cs index 96f198715..95c845250 100644 --- a/src/tooling/docs-assembler/Program.cs +++ b/src/tooling/docs-assembler/Program.cs @@ -25,9 +25,10 @@ app.Add("inbound-links"); app.Add("repo"); app.Add("navigation"); +app.Add("content-source"); var githubActions = ConsoleApp.ServiceProvider.GetService(); -var command = githubActions?.GetInput("COMMAND") ?? Environment.GetEnvironmentVariable("INPUT_COMMAND"); +var command = githubActions?.GetInput("COMMAND"); if (!string.IsNullOrEmpty(command)) args = command.Split(' ');