Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -74,27 +78,49 @@ 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,
Remote = remote,
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));

Expand Down
4 changes: 2 additions & 2 deletions src/docs-assembler/Cli/InboundLinkCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task<int> 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");
}
Expand All @@ -69,7 +69,7 @@ public async Task<int> 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);
Expand Down
3 changes: 2 additions & 1 deletion tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down