-
Notifications
You must be signed in to change notification settings - Fork 30
Add Documentation Assembler GitHub Action #501
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: 'Documentation Assembler' | ||
description: 'Run the documenation assembler commands' | ||
|
||
branding: | ||
icon: 'filter' | ||
color: 'red' | ||
|
||
inputs: | ||
command: | ||
description: 'The assembler command to run' | ||
required: true | ||
|
||
runs: | ||
using: 'docker' | ||
image: "docker://ghcr.io/elastic/docs-assembler:edge" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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.Collections.Concurrent; | ||
using System.Diagnostics; | ||
using ConsoleAppFramework; | ||
using Elastic.Markdown.IO; | ||
using Microsoft.Extensions.Logging; | ||
using ProcNet; | ||
using ProcNet.Std; | ||
|
||
namespace Documentation.Assembler.Cli; | ||
|
||
public class ConsoleLineHandler(string prefix) : IConsoleLineHandler | ||
{ | ||
public void Handle(LineOut lineOut) => lineOut.CharsOrString( | ||
r => Console.Write(prefix + ": " + r), | ||
l => Console.WriteLine(prefix + ": " + l)); | ||
|
||
public void Handle(Exception e) { } | ||
} | ||
|
||
internal class RepositoryCommands(ILoggerFactory logger) | ||
{ | ||
private void AssignOutputLogger() | ||
{ | ||
var log = logger.CreateLogger<Program>(); | ||
ConsoleApp.Log = msg => log.LogInformation(msg); | ||
ConsoleApp.LogError = msg => log.LogError(msg); | ||
} | ||
|
||
// would love to use libgit2 so there is no git dependency but | ||
// libgit2 is magnitudes slower to clone repositories https://github.com/libgit2/libgit2/issues/4674 | ||
/// <summary> Clones all repositories </summary> | ||
/// <param name="ctx"></param> | ||
[Command("clone-all")] | ||
public async Task CloneAll(Cancel ctx = default) | ||
{ | ||
var configFile = Path.Combine(Paths.Root.FullName, "src/docs-assembler/conf.yml"); | ||
var config = AssemblyConfiguration.Deserialize(File.ReadAllText(configFile)); | ||
|
||
Console.WriteLine(config.Repositories.Count); | ||
var dict = new ConcurrentDictionary<string, Stopwatch>(); | ||
await Parallel.ForEachAsync(config.Repositories, | ||
new ParallelOptions { CancellationToken = ctx, MaxDegreeOfParallelism = Environment.ProcessorCount / 4 }, async (kv, c) => | ||
{ | ||
await Task.Run(() => | ||
{ | ||
var name = kv.Key; | ||
var repository = kv.Value; | ||
var checkoutFolder = Path.Combine(Paths.Root.FullName, $".artifacts/assembly/{name}"); | ||
|
||
var sw = Stopwatch.StartNew(); | ||
dict.AddOrUpdate(name, sw, (_, _) => sw); | ||
Console.WriteLine($"Checkout: {name}\t{repository}\t{checkoutFolder}"); | ||
var branch = repository.Branch ?? "main"; | ||
var args = new StartArguments( | ||
"git", "clone", repository.Origin, checkoutFolder, "--depth", "1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about sparse-checkout? Did you already try that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed the 4 clone patterns defined here: https://github.blog/open-source/git/get-up-to-speed-with-partial-clone-and-shallow-clone/ But can't remember the results :) Will go over it again when we set up our runner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. In our case we can only download the docs folder. |
||
, "--single-branch", "--branch", branch | ||
); | ||
Proc.StartRedirected(args, new ConsoleLineHandler(name)); | ||
sw.Stop(); | ||
}, c); | ||
}).ConfigureAwait(false); | ||
|
||
foreach (var kv in dict.OrderBy(kv => kv.Value.Elapsed)) | ||
Console.WriteLine($"-> {kv.Key}\ttook: {kv.Value.Elapsed}"); | ||
} | ||
|
||
/// <summary> List all checked out repositories </summary> | ||
/// <param name="ctx"></param> | ||
[Command("list")] | ||
public async Task ListRepositories(Cancel ctx = default) | ||
{ | ||
var assemblyPath = Path.Combine(Paths.Root.FullName, $".artifacts/assembly"); | ||
var dir = new DirectoryInfo(assemblyPath); | ||
var dictionary = new Dictionary<string, string>(); | ||
foreach (var d in dir.GetDirectories()) | ||
{ | ||
var checkoutFolder = Path.Combine(assemblyPath, d.Name); | ||
|
||
var capture = Proc.Start( | ||
new StartArguments("git", "rev-parse", "--abbrev-ref", "HEAD") { WorkingDirectory = checkoutFolder } | ||
); | ||
dictionary.Add(d.Name, capture.ConsoleOut.FirstOrDefault()?.Line ?? "unknown"); | ||
} | ||
|
||
foreach (var kv in dictionary.OrderBy(kv => kv.Value)) | ||
Console.WriteLine($"-> {kv.Key}\tbranch: {kv.Value}"); | ||
|
||
await Task.CompletedTask; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This is not really required. And I think you can only ever see this if it was published in the marketplace