From eae1c9c83195958cdf5334005b92baa858e0b3f5 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Mon, 24 Feb 2025 19:33:59 +0100 Subject: [PATCH] Prefer GITHUB_REPOSITORY for remote name (#594) * Prefer GITHUB_REPOSITORY for remote name * Fix reading remote name sometimes trimming last character when reading from .git * update all usages --- src/Elastic.Markdown/BuildContext.cs | 2 +- .../IO/Discovery/GitCheckoutInformation.cs | 25 +++++++++++-------- src/docs-assembler/Cli/LinkCommands.cs | 5 +++- .../DocSet/LinkReferenceTests.cs | 4 ++- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Elastic.Markdown/BuildContext.cs b/src/Elastic.Markdown/BuildContext.cs index abfd405f..74b4788c 100644 --- a/src/Elastic.Markdown/BuildContext.cs +++ b/src/Elastic.Markdown/BuildContext.cs @@ -65,7 +65,7 @@ public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem, if (ConfigurationPath.FullName != SourcePath.FullName) SourcePath = ConfigurationPath.Directory!; - Git = GitCheckoutInformation.Create(ReadFileSystem); + Git = GitCheckoutInformation.Create(SourcePath, ReadFileSystem); Configuration = new ConfigurationFile(ConfigurationPath, SourcePath, this); } diff --git a/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs b/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs index e61a3d49..548d561d 100644 --- a/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs +++ b/src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs @@ -33,11 +33,11 @@ public record GitCheckoutInformation public string? RepositoryName { get => _repositoryName ??= Remote.Split('/').Last(); - set => _repositoryName = value; + init => _repositoryName = value; } // manual read because libgit2sharp is not yet AOT ready - public static GitCheckoutInformation Create(IFileSystem fileSystem) + public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem fileSystem) { if (fileSystem is not FileSystem) { @@ -51,18 +51,18 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem) } var fakeRef = Guid.NewGuid().ToString()[..16]; - var gitConfig = Git(".git/config"); + var gitConfig = Git(source, ".git/config"); if (!gitConfig.Exists) return Unavailable; - var head = Read(".git/HEAD") ?? fakeRef; + var head = Read(source, ".git/HEAD") ?? fakeRef; var gitRef = head; var branch = head.Replace("refs/heads/", string.Empty); //not detached HEAD if (head.StartsWith("ref:")) { head = head.Replace("ref: ", string.Empty); - gitRef = Read(".git/" + head) ?? fakeRef; + gitRef = Read(source, ".git/" + head) ?? fakeRef; branch = branch.Replace("ref: ", string.Empty); } else @@ -73,15 +73,17 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem) using var streamReader = new StreamReader(stream); ini.Load(streamReader); - var remote = BranchTrackingRemote(branch, ini); + var remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY"); + if (string.IsNullOrEmpty(remote)) + remote = BranchTrackingRemote(branch, ini); if (string.IsNullOrEmpty(remote)) remote = BranchTrackingRemote("main", ini); if (string.IsNullOrEmpty(remote)) remote = BranchTrackingRemote("master", ini); if (string.IsNullOrEmpty(remote)) - remote = Environment.GetEnvironmentVariable("GITHUB_REPOSITORY") ?? "elastic/docs-builder-unknown"; + remote = "elastic/docs-builder-unknown"; - remote = remote.AsSpan().TrimEnd(".git").ToString(); + remote = remote.AsSpan().TrimEnd("git").TrimEnd('.').ToString(); return new GitCheckoutInformation { @@ -91,11 +93,12 @@ public static GitCheckoutInformation Create(IFileSystem fileSystem) RepositoryName = remote.Split('/').Last() }; - IFileInfo Git(string path) => fileSystem.FileInfo.New(Path.Combine(Paths.Root.FullName, path)); + IFileInfo Git(IDirectoryInfo directoryInfo, string path) => + fileSystem.FileInfo.New(Path.Combine(directoryInfo.FullName, path)); - string? Read(string path) + string? Read(IDirectoryInfo directoryInfo, string path) { - var gitPath = Git(path).FullName; + var gitPath = Git(directoryInfo, path).FullName; return !fileSystem.File.Exists(gitPath) ? null : fileSystem.File.ReadAllText(gitPath).Trim(Environment.NewLine.ToCharArray()); diff --git a/src/docs-assembler/Cli/LinkCommands.cs b/src/docs-assembler/Cli/LinkCommands.cs index cf7ac67f..86b0bdf2 100644 --- a/src/docs-assembler/Cli/LinkCommands.cs +++ b/src/docs-assembler/Cli/LinkCommands.cs @@ -10,6 +10,7 @@ using ConsoleAppFramework; using Documentation.Assembler.Links; using Elastic.Markdown.CrossLinks; +using Elastic.Markdown.IO; using Elastic.Markdown.IO.Discovery; using Microsoft.Extensions.Logging; @@ -48,7 +49,9 @@ public async Task ValidateLocalInboundLinks(string? repository = null, stri { AssignOutputLogger(); file ??= ".artifacts/docs/html/links.json"; - repository ??= GitCheckoutInformation.Create(new FileSystem()).RepositoryName; + var fs = new FileSystem(); + var root = fs.DirectoryInfo.New(Paths.Root.FullName); + repository ??= GitCheckoutInformation.Create(root, new FileSystem()).RepositoryName; if (repository == null) throw new Exception("Unable to determine repository name"); diff --git a/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs b/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs index f1864d3d..4f17e636 100644 --- a/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs +++ b/tests/Elastic.Markdown.Tests/DocSet/LinkReferenceTests.cs @@ -2,6 +2,7 @@ // 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 Elastic.Markdown.IO; using Elastic.Markdown.IO.Discovery; using Elastic.Markdown.IO.State; using FluentAssertions; @@ -32,7 +33,8 @@ public class GitCheckoutInformationTests(ITestOutputHelper output) : NavigationT [Fact] public void Create() { - var git = GitCheckoutInformation.Create(ReadFileSystem); + var root = ReadFileSystem.DirectoryInfo.New(Paths.Root.FullName); + var git = GitCheckoutInformation.Create(root, ReadFileSystem); git.Should().NotBeNull(); git.Branch.Should().NotBeNullOrWhiteSpace();