Skip to content

Commit 7941c79

Browse files
authored
Add content-source matching functionality to the assembler (#1229)
Introduce logic to match branches or tags against configured content sources. Extend CLI with 'content-source' commands and integrate GitHub action for content-source validation. Update project solution and configuration accordingly.
1 parent e69d739 commit 7941c79

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
lines changed

actions/assembler-match/action.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: 'Documentation Assembler Match'
2+
description: 'Checks if the current commit is on either a current or next branch'
3+
4+
branding:
5+
icon: 'filter'
6+
color: 'blue'
7+
8+
inputs:
9+
repository:
10+
description: 'the repository name'
11+
required: true
12+
ref_name:
13+
description: 'The branch or tag name'
14+
required: true
15+
16+
outputs:
17+
content-source-match:
18+
description: 'true/false indicating the branch matches a content-source'
19+
content-source-name:
20+
description: "The name of the content source that matches (current/next) or empty"
21+
22+
runs:
23+
using: 'docker'
24+
image: "docker://ghcr.io/elastic/docs-assembler:edge"
25+
env:
26+
INPUT_COMMAND: "content-source match"

docs-builder.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tooling", "tooling", "{73AB
8989
EndProject
9090
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "authoring", "authoring", "{059E787F-85C1-43BE-9DD6-CE319E106383}"
9191
EndProject
92+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assembler-filter", "assembler-filter", "{FB1C1954-D8E2-4745-BA62-04DD82FB4792}"
93+
ProjectSection(SolutionItems) = preProject
94+
actions\assembler-filter\action.yml = actions\assembler-filter\action.yml
95+
EndProjectSection
96+
EndProject
9297
Global
9398
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9499
Debug|Any CPU = Debug|Any CPU
@@ -172,5 +177,6 @@ Global
172177
{4CCE599A-B9FE-4DF2-8763-34CF0A99D4AA} = {73ABAE37-118F-4A53-BC2C-F19333555C90}
173178
{059E787F-85C1-43BE-9DD6-CE319E106383} = {BE6011CC-1200-4957-B01F-FCCA10C5CF5A}
174179
{7D36DDDA-9E0B-4D2C-8033-5D62FF8B6166} = {059E787F-85C1-43BE-9DD6-CE319E106383}
180+
{FB1C1954-D8E2-4745-BA62-04DD82FB4792} = {245023D2-D3CA-47B9-831D-DAB91A2FFDC7}
175181
EndGlobalSection
176182
EndGlobal

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,29 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string
7878

7979
[YamlMember(Alias = "named_git_references")]
8080
public Dictionary<string, string> NamedGitReferences { get; set; } = [];
81+
82+
/// Returns whether the <paramref name="branchOrTag"/> is configured as an integration branch or tag for the given
83+
/// <paramref name="repository"/>.
84+
public ContentSource? Match(string repository, string branchOrTag)
85+
{
86+
var repositoryName = repository.Split('/').Last();
87+
if (ReferenceRepositories.TryGetValue(repositoryName, out var r))
88+
{
89+
if (r.GetBranch(ContentSource.Current) == branchOrTag)
90+
return ContentSource.Current;
91+
if (r.GetBranch(ContentSource.Next) == branchOrTag)
92+
return ContentSource.Next;
93+
return null;
94+
}
95+
96+
if (repositoryName == NarrativeRepository.RepositoryName)
97+
{
98+
if (Narrative.GetBranch(ContentSource.Current) == branchOrTag)
99+
return ContentSource.Current;
100+
if (Narrative.GetBranch(ContentSource.Next) == branchOrTag)
101+
return ContentSource.Next;
102+
}
103+
104+
return null;
105+
}
81106
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.IO.Abstractions;
7+
using Actions.Core.Services;
8+
using ConsoleAppFramework;
9+
using Elastic.Documentation.Configuration.Assembler;
10+
using Elastic.Documentation.Tooling.Diagnostics.Console;
11+
using Microsoft.Extensions.Logging;
12+
13+
namespace Documentation.Assembler.Cli;
14+
15+
internal sealed class ContentSourceCommands(ICoreService githubActionsService, ILoggerFactory logFactory)
16+
{
17+
[SuppressMessage("Usage", "CA2254:Template should be a static expression")]
18+
private void AssignOutputLogger()
19+
{
20+
var log = logFactory.CreateLogger<Program>();
21+
ConsoleApp.Log = msg => log.LogInformation(msg);
22+
ConsoleApp.LogError = msg => log.LogError(msg);
23+
}
24+
25+
/// <summary> </summary>
26+
/// <param name="repository"></param>
27+
/// <param name="branchOrTag"></param>
28+
/// <param name="ctx"></param>
29+
[Command("match")]
30+
public async Task<int> Match([Argument] string? repository = null, [Argument] string? branchOrTag = null, Cancel ctx = default)
31+
{
32+
AssignOutputLogger();
33+
var repo = repository ?? githubActionsService.GetInput("repository");
34+
if (string.IsNullOrEmpty(repo))
35+
throw new ArgumentNullException(nameof(repository));
36+
var refName = branchOrTag ?? githubActionsService.GetInput("ref_name");
37+
if (string.IsNullOrEmpty(refName))
38+
throw new ArgumentNullException(nameof(branchOrTag));
39+
40+
await using var collector = new ConsoleDiagnosticsCollector(logFactory, githubActionsService)
41+
{
42+
NoHints = true
43+
};
44+
45+
_ = collector.StartAsync(ctx);
46+
47+
// environment does not matter to check the configuration, defaulting to dev
48+
var assembleContext = new AssembleContext("dev", collector, new FileSystem(), new FileSystem(), null, null)
49+
{
50+
Force = false,
51+
AllowIndexing = false
52+
};
53+
var logger = logFactory.CreateLogger<ContentSourceCommands>();
54+
var matches = assembleContext.Configuration.Match(repo, refName);
55+
if (matches == null)
56+
{
57+
logger.LogInformation("'{Repository}' '{BranchOrTag}' combination not found in configuration.", repository, branchOrTag);
58+
await githubActionsService.SetOutputAsync("content-source-match", "false");
59+
await githubActionsService.SetOutputAsync("content-source-name", "");
60+
}
61+
else
62+
{
63+
var name = matches.Value.ToStringFast(true);
64+
logger.LogInformation("'{Repository}' '{BranchOrTag}' is configured as '{Matches}' content-source", repository, branchOrTag, name);
65+
66+
await githubActionsService.SetOutputAsync("content-source-match", "true");
67+
await githubActionsService.SetOutputAsync("content-source-name", name);
68+
}
69+
70+
await collector.StopAsync(ctx);
71+
return matches != null && collector.Errors == 0 ? 0 : 1;
72+
}
73+
74+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public async Task<int> BuildAll(
9090
var assembleContext = new AssembleContext(environment, collector, new FileSystem(), new FileSystem(), null, null)
9191
{
9292
Force = force ?? false,
93-
AllowIndexing = allowIndexing ?? false,
93+
AllowIndexing = allowIndexing ?? false
9494
};
9595

9696
// this validates all path prefixes are unique, early exit if duplicates are detected

src/tooling/docs-assembler/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
app.Add<InboundLinkCommands>("inbound-links");
2626
app.Add<RepositoryCommands>("repo");
2727
app.Add<NavigationCommands>("navigation");
28+
app.Add<ContentSourceCommands>("content-source");
2829

2930
var githubActions = ConsoleApp.ServiceProvider.GetService<ICoreService>();
30-
var command = githubActions?.GetInput("COMMAND") ?? Environment.GetEnvironmentVariable("INPUT_COMMAND");
31+
var command = githubActions?.GetInput("COMMAND");
3132
if (!string.IsNullOrEmpty(command))
3233
args = command.Split(' ');
3334

0 commit comments

Comments
 (0)