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
17 changes: 13 additions & 4 deletions src/Elastic.Markdown/CrossLinks/CrossLinkResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -39,19 +40,27 @@ public interface ICrossLinkResolver

public class CrossLinkResolver(CrossLinkFetcher fetcher) : ICrossLinkResolver
{
private FetchedCrossLinks _linkReferences = FetchedCrossLinks.Empty;
private FetchedCrossLinks _crossLinks = FetchedCrossLinks.Empty;

public async Task<FetchedCrossLinks> FetchLinks()
{
_linkReferences = await fetcher.Fetch();
return _linkReferences;
_crossLinks = await fetcher.Fetch();
return _crossLinks;
}

public bool TryResolve(Action<string> errorEmitter, Uri crossLinkUri, [NotNullWhen(true)] out Uri? resolvedUri) =>
TryResolve(errorEmitter, _linkReferences, crossLinkUri, out resolvedUri);
TryResolve(errorEmitter, _crossLinks, crossLinkUri, out resolvedUri);

private static Uri BaseUri { get; } = new("https://docs-v3-preview.elastic.dev");

public FetchedCrossLinks UpdateLinkReference(string repository, LinkReference linkReference)
{
var dictionary = _crossLinks.LinkReferences.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
dictionary[repository] = linkReference;
_crossLinks = _crossLinks with { LinkReferences = dictionary.ToFrozenDictionary() };
return _crossLinks;
}

public static bool TryResolve(
Action<string> errorEmitter,
FetchedCrossLinks fetchedCrossLinks,
Expand Down
11 changes: 4 additions & 7 deletions src/docs-assembler/Links/LinkIndexLinkChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,28 @@ Cancel ctx
if (!Path.IsPathRooted(localLinksJson))
localLinksJson = Path.Combine(Paths.Root.FullName, localLinksJson);

var dictionary = crossLinks.LinkReferences.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
try
{
var json = await File.ReadAllTextAsync(localLinksJson, ctx);
var localLinkReference = LinkReference.Deserialize(json);

dictionary[repository] = localLinkReference;
crossLinks = resolver.UpdateLinkReference(repository, localLinkReference);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to read {LocalLinksJson}", localLinksJson);
throw;
}
crossLinks = crossLinks with { LinkReferences = dictionary.ToFrozenDictionary() };

_logger.LogInformation("Validating all cross links to {Repository}:// from all repositories published to link-index.json", repository);

return await ValidateCrossLinks(githubActionsService, crossLinks, resolver, [repository], ctx);
return await ValidateCrossLinks(githubActionsService, crossLinks, resolver, repository, ctx);
}

private async Task<int> ValidateCrossLinks(
ICoreService githubActionsService,
FetchedCrossLinks crossLinks,
CrossLinkResolver resolver,
string[]? repositoryFilter,
string? currentRepository,
Cancel ctx)
{
var collector = new ConsoleDiagnosticsCollector(logger, githubActionsService);
Expand All @@ -85,7 +82,7 @@ private async Task<int> ValidateCrossLinks(
// if we are filtering we only want errors from inbound links to a certain
// repository
var uri = new Uri(crossLink);
if (repositoryFilter != null && uri.Scheme != repository)
if (currentRepository != null && uri.Scheme != currentRepository)
continue;

_ = resolver.TryResolve(s =>
Expand Down