From 246b034cd08e13c6dacd8452937e311062dca231 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Fri, 28 Feb 2025 15:10:47 +0100 Subject: [PATCH] Add logging to git remote resolving --- .../IO/Discovery/GitCheckoutInformation.cs | 30 +++++++++++++++++-- src/docs-assembler/Cli/InboundLinkCommands.cs | 4 +-- .../DocSet/LinkReferenceTests.cs | 3 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs b/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs index c940b5bfa..0334e9316 100644 --- a/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs +++ b/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs @@ -4,6 +4,7 @@ using System.IO.Abstractions; using System.Text.Json.Serialization; +using Microsoft.Extensions.Logging; using SoftCircuits.IniFileParser; namespace Elastic.Markdown.IO.Discovery; @@ -37,7 +38,7 @@ public string? RepositoryName } // manual read because libgit2sharp is not yet AOT ready - public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem fileSystem) + public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem fileSystem, ILogger? logger = null) { if (fileSystem is not FileSystem) { @@ -53,7 +54,10 @@ public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem f var fakeRef = Guid.NewGuid().ToString()[..16]; var gitConfig = Git(source, ".git/config"); if (!gitConfig.Exists) + { + logger?.LogInformation("Git checkout information not available."); return Unavailable; + } var head = Read(source, ".git/HEAD") ?? fakeRef; var gitRef = head; @@ -74,20 +78,38 @@ public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem f ini.Load(streamReader); var remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY"); + logger?.LogInformation("Remote from environment: {GitRemote}", remote); if (string.IsNullOrEmpty(remote)) + { remote = BranchTrackingRemote(branch, ini); + logger?.LogInformation("Remote from branch: {GitRemote}", remote); + } + if (string.IsNullOrEmpty(remote)) + { remote = BranchTrackingRemote("main", ini); + logger?.LogInformation("Remote from main branch: {GitRemote}", remote); + } + if (string.IsNullOrEmpty(remote)) + { remote = BranchTrackingRemote("master", ini); + logger?.LogInformation("Remote from master branch: {GitRemote}", remote); + } + if (string.IsNullOrEmpty(remote)) + { remote = "elastic/docs-builder-unknown"; + logger?.LogInformation("Remote from fallback: {GitRemote}", remote); + } remote = remote.AsSpan().TrimEnd("git").TrimEnd('.').ToString(); + + logger?.LogInformation("Remote trimmed: {GitRemote}", remote); if (remote.EndsWith("docs-conten")) remote += "t"; - return new GitCheckoutInformation + var info = new GitCheckoutInformation { Ref = gitRef, Branch = branch, @@ -95,6 +117,10 @@ public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem f RepositoryName = remote.Split('/').Last() }; + logger?.LogInformation("-> Remote Name: {GitRemote}", info.Remote); + logger?.LogInformation("-> Repository Name: {RepositoryName}", info.RepositoryName); + return info; + IFileInfo Git(IDirectoryInfo directoryInfo, string path) => fileSystem.FileInfo.New(Path.Combine(directoryInfo.FullName, path)); diff --git a/src/docs-assembler/Cli/InboundLinkCommands.cs b/src/docs-assembler/Cli/InboundLinkCommands.cs index a74228a77..f32dc3afb 100644 --- a/src/docs-assembler/Cli/InboundLinkCommands.cs +++ b/src/docs-assembler/Cli/InboundLinkCommands.cs @@ -50,7 +50,7 @@ public async Task ValidateRepoInboundLinks(string? from = null, string? to var root = fs.DirectoryInfo.New(Paths.Root.FullName); if (from == null && to == null) { - from ??= GitCheckoutInformation.Create(root, new FileSystem()).RepositoryName; + from ??= GitCheckoutInformation.Create(root, new FileSystem(), logger.CreateLogger(nameof(GitCheckoutInformation))).RepositoryName; if (from == null) throw new Exception("Unable to determine repository name"); } @@ -69,7 +69,7 @@ public async Task ValidateLocalLinkReference(string? file = null, Cancel ct file ??= ".artifacts/docs/html/links.json"; var fs = new FileSystem(); var root = fs.DirectoryInfo.New(Paths.Root.FullName); - var repository = GitCheckoutInformation.Create(root, new FileSystem()).RepositoryName + var repository = GitCheckoutInformation.Create(root, new FileSystem(), logger.CreateLogger(nameof(GitCheckoutInformation))).RepositoryName ?? throw new Exception("Unable to determine repository name"); return await _linkIndexLinkChecker.CheckWithLocalLinksJson(repository, file, ctx); diff --git a/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs b/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs index 4f17e636f..cdb6c1581 100644 --- a/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs +++ b/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs @@ -6,6 +6,7 @@ using Elastic.Markdown.IO.Discovery; using Elastic.Markdown.IO.State; using FluentAssertions; +using Microsoft.Extensions.Logging; namespace Elastic.Markdown.Tests.DocSet; @@ -34,7 +35,7 @@ public class GitCheckoutInformationTests(ITestOutputHelper output) : NavigationT public void Create() { var root = ReadFileSystem.DirectoryInfo.New(Paths.Root.FullName); - var git = GitCheckoutInformation.Create(root, ReadFileSystem); + var git = GitCheckoutInformation.Create(root, ReadFileSystem, LoggerFactory.CreateLogger(nameof(GitCheckoutInformation))); git.Should().NotBeNull(); git.Branch.Should().NotBeNullOrWhiteSpace();