-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track git information as part of incremental builds and link referenc…
…es (#79)
- Loading branch information
Showing
9 changed files
with
206 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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.IO.Abstractions; | ||
using System.Text.Json.Serialization; | ||
using IniParser; | ||
|
||
namespace Elastic.Markdown.IO; | ||
|
||
public record GitConfiguration | ||
{ | ||
[JsonPropertyName("branch")] | ||
public required string Branch { get; init; } | ||
[JsonPropertyName("remote")] | ||
public required string Remote { get; init; } | ||
[JsonPropertyName("ref")] | ||
public required string Ref { get; init; } | ||
|
||
// manual read because libgit2sharp is not yet AOT ready | ||
public static GitConfiguration Create(IFileSystem fileSystem) | ||
{ | ||
// filesystem is not real so return a dummy | ||
if (fileSystem is not FileSystem) | ||
{ | ||
var fakeRef = Guid.NewGuid().ToString().Substring(0, 16); | ||
return new GitConfiguration | ||
{ | ||
Branch = $"test-{fakeRef}", | ||
Remote = "elastic/docs-builder", | ||
Ref = fakeRef, | ||
}; | ||
} | ||
|
||
var gitConfig = Git(".git/config"); | ||
if (!gitConfig.Exists) | ||
throw new Exception($"{Paths.Root.FullName} is not a git repository."); | ||
|
||
var head = Read(".git/HEAD").Replace("ref: ", string.Empty); | ||
var gitRef = Read(".git/" + head); | ||
var branch = head.Replace("refs/heads/", string.Empty); | ||
|
||
var ini = new FileIniDataParser(); | ||
using var stream = gitConfig.OpenRead(); | ||
using var streamReader = new StreamReader(stream); | ||
var config = ini.ReadData(streamReader); | ||
var remoteName = config[$"branch \"{branch}\""]["remote"]; | ||
var remote = config[$"remote \"{remoteName}\""]["url"]; | ||
|
||
return new GitConfiguration | ||
{ | ||
Ref = gitRef, | ||
Branch = branch, | ||
Remote = remote | ||
}; | ||
|
||
IFileInfo Git(string path) => fileSystem.FileInfo.New(Path.Combine(Paths.Root.FullName, path)); | ||
|
||
string Read(string path) => | ||
fileSystem.File.ReadAllText(Git(path).FullName).Trim(Environment.NewLine.ToCharArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// 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.IO.Abstractions; | ||
using System.Text.Json.Serialization; | ||
using IniParser; | ||
|
||
namespace Elastic.Markdown.IO; | ||
|
||
public record LinkReference | ||
{ | ||
[JsonPropertyName("origin")] | ||
public required GitConfiguration Origin { get; init; } | ||
[JsonPropertyName("links")] | ||
public required string[] Links { get; init; } = []; | ||
|
||
public static LinkReference Create(DocumentationSet set) | ||
{ | ||
var links = set.FlatMappedFiles.Values | ||
.OfType<MarkdownFile>() | ||
.Select(m => m.RelativePath).ToArray(); | ||
return new LinkReference { Origin = set.Context.Git, Links = links }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/Elastic.Markdown.Tests/SiteMap/LinkReferenceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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 Elastic.Markdown.IO; | ||
using FluentAssertions; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elastic.Markdown.Tests.SiteMap; | ||
|
||
public class LinkReferenceTests(ITestOutputHelper output) : NavigationTestsBase(output) | ||
{ | ||
[Fact] | ||
public void Create() | ||
{ | ||
var reference = LinkReference.Create(Set); | ||
|
||
reference.Should().NotBeNull(); | ||
} | ||
} | ||
|
||
public class GitConfigurationTests(ITestOutputHelper output) : NavigationTestsBase(output) | ||
{ | ||
[Fact] | ||
public void Create() | ||
{ | ||
var git = GitConfiguration.Create(ReadFileSystem); | ||
|
||
git.Should().NotBeNull(); | ||
git!.Branch.Should().NotBeNullOrWhiteSpace(); | ||
// this validates we are not returning the test instance as were doing a real read | ||
git.Branch.Should().NotContain(git.Ref); | ||
git.Ref.Should().NotBeNullOrWhiteSpace(); | ||
git.Remote.Should().NotBeNullOrWhiteSpace(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters