-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathAssemblyConfiguration.cs
More file actions
274 lines (243 loc) · 10.4 KB
/
AssemblyConfiguration.cs
File metadata and controls
274 lines (243 loc) · 10.4 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// 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.Text.RegularExpressions;
using Elastic.Documentation.Configuration.Products;
using Elastic.Documentation.Extensions;
using Elastic.Documentation.Versions;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
namespace Elastic.Documentation.Configuration.Assembler;
public record AssemblyConfiguration
{
public static AssemblyConfiguration Create(ConfigurationFileProvider provider) =>
Deserialize(provider.AssemblerFile.ReadToEnd(), skipPrivateRepositories: provider.SkipPrivateRepositories);
public static AssemblyConfiguration Deserialize(string yaml, bool skipPrivateRepositories = false)
{
var input = new StringReader(yaml);
try
{
var config = ConfigurationFileProvider.Deserializer.Deserialize<AssemblyConfiguration>(input);
foreach (var (name, r) in config.ReferenceRepositories)
{
var repository = RepositoryDefaults(r, name);
config.ReferenceRepositories[name] = repository;
}
// If we are skipping private repositories, and we can locate the solution directory. include the local docs-content repository
// this allows us to test new docset features as part of the assembler build
if (skipPrivateRepositories
&& config.ReferenceRepositories.TryGetValue("docs-builder", out var docsContentRepository)
&& Paths.GetSolutionDirectory() is { } solutionDir
)
{
var docsRepositoryPath = Path.Join(solutionDir.FullName, "docs");
config.ReferenceRepositories["docs-builder"] = docsContentRepository with
{
Skip = false,
Path = docsRepositoryPath
};
}
var privateRepositories = config.ReferenceRepositories.Where(r => r.Value.Private).ToList();
foreach (var (name, _) in privateRepositories)
{
if (skipPrivateRepositories)
_ = config.ReferenceRepositories.Remove(name);
}
foreach (var (name, env) in config.Environments)
env.Name = name;
config.Narrative = RepositoryDefaults(config.Narrative, NarrativeRepository.RepositoryName);
config.AvailableRepositories = config.ReferenceRepositories.Values
.Where(r => !r.Skip)
.Concat([config.Narrative]).ToDictionary(kvp => kvp.Name, kvp => kvp);
config.PrivateRepositories = privateRepositories
.Where(r => !r.Value.Skip)
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
return config;
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(e.InnerException);
throw;
}
}
private static TRepository RepositoryDefaults<TRepository>(TRepository r, string name)
where TRepository : Repository, new()
{
// ReSharper disable NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
var repository = r ?? new TRepository();
// ReSharper restore NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
repository = repository with
{
Name = name,
GitReferenceCurrent = string.IsNullOrEmpty(repository.GitReferenceCurrent) ? "main" : repository.GitReferenceCurrent,
GitReferenceNext = string.IsNullOrEmpty(repository.GitReferenceNext) ? "main" : repository.GitReferenceNext,
GitReferenceEdge = string.IsNullOrEmpty(repository.GitReferenceEdge) ? "main" : repository.GitReferenceEdge,
};
// ensure we always null path if we are running in CI
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("CI")))
{
repository = repository with
{
Path = null
};
}
if (string.IsNullOrEmpty(repository.Origin))
{
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("GITHUB_ACTIONS")))
{
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
repository.Origin = !string.IsNullOrEmpty(token)
? $"https://oauth2:{token}@github.com/elastic/{name}.git"
: $"https://github.com/elastic/{name}.git";
}
else
repository.Origin = $"git@github.com:elastic/{name}.git";
}
return repository;
}
[YamlMember(Alias = "narrative")]
public NarrativeRepository Narrative { get; set; } = new();
[YamlMember(Alias = "references")]
public Dictionary<string, Repository> ReferenceRepositories { get; set; } = [];
/// All available repositories, combines <see cref="ReferenceRepositories"/> and <see cref="Narrative"/> and will filter private repositories if `skip-private-repositories`
/// is specified
[YamlIgnore]
public IReadOnlyDictionary<string, Repository> AvailableRepositories { get; private set; } = new Dictionary<string, Repository>();
/// Repositories marked as private, these are listed under <see cref="AvailableRepositories"/> if `--skip-private-repositories` is not specified
[YamlIgnore]
public IReadOnlyDictionary<string, Repository> PrivateRepositories { get; private set; } = new Dictionary<string, Repository>();
[YamlMember(Alias = "environments")]
public Dictionary<string, PublishEnvironment> Environments { get; set; } = [];
[YamlMember(Alias = "shared_configuration")]
public Dictionary<string, Repository> SharedConfigurations { get; set; } = [];
/// Returns whether the <paramref name="branchOrTag"/> is configured as an integration branch or tag for the given
/// <paramref name="repository"/>.
public ContentSourceMatch Match(ILoggerFactory logFactory, string repository, string branchOrTag, Product? product, bool alreadyPublishing)
{
var logger = logFactory.CreateLogger<ContentSourceMatch>();
var match = new ContentSourceMatch(null, null, null, false);
var tokens = repository.Split('/');
var repositoryName = tokens.Last();
var owner = tokens.First();
var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);
if (tokens.Length < 2 || owner != "elastic")
{
logger.LogInformation("Repository {Repository} is not a valid elastic repository but {Owner}", repository, owner);
return match;
}
// Check for new repositories
if (!AvailableRepositories.TryGetValue(repositoryName, out var r))
{
logger.LogInformation("Repository {Repository} has not yet been onboarded into assembler.yml", repository);
// this is an unknown new elastic repository
if (isVersionBranch || branchOrTag == "main" || branchOrTag == "master")
{
logger.LogInformation("Speculatively building {Repository} since it looks like an integration branch", repository);
return match with
{
Speculative = true
};
}
logger.LogInformation("{Repository} on '{Branch}' does not look like it needs a speculative build", repository, branchOrTag);
return match;
}
var current = r.GetBranch(ContentSource.Current);
var next = r.GetBranch(ContentSource.Next);
var edge = r.GetBranch(ContentSource.Edge);
logger.LogInformation("Active content-sources for {Repository}. current: {Current}, next: {Next}, edge: {Edge}' ", repository, current, next, edge);
if (current == branchOrTag)
{
logger.LogInformation("Content-Source current: {Current} matches: {Branch}", current, branchOrTag);
match = match with
{
Current = ContentSource.Current
};
}
if (next == branchOrTag)
{
logger.LogInformation("Content-Source next: {Next} matches: {Branch}", next, branchOrTag);
match = match with
{
Next = ContentSource.Next
};
}
if (edge == branchOrTag)
{
logger.LogInformation("Content-Source edge: {Edge} matches: {Branch}", edge, branchOrTag);
match = match with
{
Edge = ContentSource.Edge
};
}
// check version branches
if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))
{
logger.LogInformation("Branch or tag {Branch} is a versioned branch", branchOrTag);
// if the current branch is a version, only speculatively match if branch is actually a new version
if (SemVersion.TryParse(current + ".0", out var currentVersion))
{
logger.LogInformation("Current is already using versioned branches {Current}", currentVersion);
var previousCurrentVersion = new SemVersion(currentVersion.Major, Math.Max(currentVersion.Minor - 1, 0), 0);
if (v >= currentVersion)
{
logger.LogInformation("Speculative build because {Branch} is gte current {Current}", branchOrTag, currentVersion);
match = match with
{
Speculative = true
};
}
else if (v == previousCurrentVersion)
{
logger.LogInformation("Speculative build {Branch} is the previous minor '{ProductPreviousMinor}' of current {Current}", branchOrTag, previousCurrentVersion, currentVersion);
match = match with
{
Speculative = true
};
}
else
logger.LogInformation("NO speculative build because {Branch} is lt {Current}", branchOrTag, currentVersion);
}
// assume we are newly onboarding the repository to current/next
else if (product?.VersioningSystem is { } versioningSystem)
{
if (!alreadyPublishing)
{
logger.LogInformation("Current is not using versioned branches and is not publishing to the registry yet, using product information to determine speculative build is needed");
var productVersion = versioningSystem.Current;
var anchoredProductVersion = new SemVersion(productVersion.Major, productVersion.Minor, 0);
if (v > anchoredProductVersion)
{
logger.LogInformation("Speculative build {Branch} is gt product current '{ProductCurrent}' anchored at {ProductAnchored}", branchOrTag,
productVersion, anchoredProductVersion);
match = match with
{
Speculative = true
};
}
else
logger.LogInformation("NO speculative build {Branch} is lte product current '{ProductCurrent}'", branchOrTag, productVersion);
}
else
logger.LogInformation("NO speculative build, repository is not using version branches to publish to documentation and is already in the link registry");
}
else
logger.LogInformation("No versioning system found for {Repository} on {Branch}, can not determine speculative build until repository is specified in docs-builder configuration", repository, branchOrTag);
}
// if we haven't matched anything yet, and the branch is 'main' or 'master' always build
if (match is { Current: null, Next: null, Edge: null, Speculative: false } && branchOrTag is "main" or "master")
{
return match with
{
Speculative = true
};
}
return match;
}
public record ContentSourceMatch(ContentSource? Current, ContentSource? Next, ContentSource? Edge, bool Speculative);
}
internal static partial class ContentSourceRegex
{
[GeneratedRegex(@"^\d+\.\d+$", RegexOptions.IgnoreCase)]
public static partial Regex MatchVersionBranch();
}