-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathDocSetConfigurationCrossLinkFetcher.cs
More file actions
93 lines (82 loc) · 3.74 KB
/
DocSetConfigurationCrossLinkFetcher.cs
File metadata and controls
93 lines (82 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.Collections.Frozen;
using Elastic.Documentation.Configuration;
using Elastic.Documentation.Configuration.Builder;
using Elastic.Documentation.LinkIndex;
using Microsoft.Extensions.Logging;
namespace Elastic.Documentation.Links.CrossLinks;
/// Fetches cross-links from all the declared repositories in the docset.yml configuration see <see cref="ConfigurationFile"/>
public class DocSetConfigurationCrossLinkFetcher(
ILoggerFactory logFactory,
ConfigurationFile configuration,
ILinkIndexReader? linkIndexProvider = null,
ILinkIndexReader? codexLinkIndexReader = null)
: CrossLinkFetcher(logFactory, linkIndexProvider ?? Aws3LinkIndexReader.CreateAnonymous(), ownsReader: linkIndexProvider is null)
{
private readonly ILogger _logger = logFactory.CreateLogger(nameof(DocSetConfigurationCrossLinkFetcher));
// _codexReader is injected by the caller who retains ownership and is responsible for disposal.
// ReloadableGeneratorState, the primary caller, disposes it directly in its own Dispose().
private readonly ILinkIndexReader? _codexReader = codexLinkIndexReader;
public override async Task<FetchedCrossLinks> FetchCrossLinks(Cancel ctx)
{
Logger.LogInformation("Fetching cross-links for all repositories defined in docset.yml");
var linkReferences = new Dictionary<string, RepositoryLinks>();
var linkIndexEntries = new Dictionary<string, LinkRegistryEntry>();
var registryUrlsByRepository = new Dictionary<string, string>();
var codexRepositories = new HashSet<string>();
var declaredRepositories = new HashSet<string>();
var publicReader = LinkIndexProvider;
var useDualRegistry = configuration.Registry != DocSetRegistry.Public && _codexReader is not null;
foreach (var entry in configuration.CrossLinkEntries)
{
_ = declaredRepositories.Add(entry.Repository);
var isCodexEntry = useDualRegistry && entry.Registry != DocSetRegistry.Public;
var reader = isCodexEntry ? _codexReader! : publicReader;
if (isCodexEntry)
_ = codexRepositories.Add(entry.Repository);
try
{
var linkReference = await FetchCrossLinksFromReader(reader, entry.Repository, this, ctx);
linkReferences.Add(entry.Repository, linkReference);
registryUrlsByRepository[entry.Repository] = reader.RegistryUrl;
var registry = await reader.GetRegistry(ctx);
if (registry.Repositories.TryGetValue(entry.Repository, out var repoBranches))
{
var linkIndexEntry = GetNextContentSourceLinkIndexEntry(repoBranches, entry.Repository);
linkIndexEntries.Add(entry.Repository, linkIndexEntry);
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Error fetching link data for repository '{Repository}'. Cross-links to this repository may not resolve correctly.", entry.Repository);
_ = registryUrlsByRepository.TryAdd(entry.Repository, reader.RegistryUrl);
if (!linkReferences.ContainsKey(entry.Repository))
{
linkReferences.Add(entry.Repository, new RepositoryLinks
{
Links = [],
Origin = new GitCheckoutInformation
{
Branch = "main",
RepositoryName = entry.Repository,
Remote = "origin",
Ref = "refs/heads/main"
},
UrlPathPrefix = "",
CrossLinks = []
});
}
}
}
return new FetchedCrossLinks
{
DeclaredRepositories = declaredRepositories,
LinkReferences = linkReferences.ToFrozenDictionary(),
LinkIndexEntries = linkIndexEntries.ToFrozenDictionary(),
RegistryUrlsByRepository = registryUrlsByRepository.ToFrozenDictionary(),
CodexRepositories = codexRepositories.Count > 0 ? codexRepositories.ToFrozenSet() : null,
};
}
}