|
| 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 | +} |
0 commit comments