| 
3 | 3 | // See the LICENSE file in the project root for more information  | 
4 | 4 | 
 
  | 
5 | 5 | using System.Text.RegularExpressions;  | 
 | 6 | +using Elastic.Documentation.Configuration.Products;  | 
6 | 7 | using Elastic.Documentation.Extensions;  | 
 | 8 | +using Microsoft.Extensions.Logging;  | 
7 | 9 | using YamlDotNet.Serialization;  | 
8 |  | -using YamlStaticContext = Elastic.Documentation.Configuration.Serialization.YamlStaticContext;  | 
9 | 10 | 
 
  | 
10 | 11 | namespace Elastic.Documentation.Configuration.Assembler;  | 
11 | 12 | 
 
  | 
@@ -132,112 +133,110 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string  | 
132 | 133 | 
 
  | 
133 | 134 | 	/// Returns whether the <paramref name="branchOrTag"/> is configured as an integration branch or tag for the given  | 
134 | 135 | 	/// <paramref name="repository"/>.  | 
135 |  | -	public ContentSourceMatch Match(string repository, string branchOrTag)  | 
 | 136 | +	public ContentSourceMatch Match(ILoggerFactory logFactory, string repository, string branchOrTag, Product? product)  | 
136 | 137 | 	{  | 
 | 138 | +		var logger = logFactory.CreateLogger<ContentSourceMatch>();  | 
137 | 139 | 		var match = new ContentSourceMatch(null, null, null, false);  | 
138 | 140 | 		var tokens = repository.Split('/');  | 
139 | 141 | 		var repositoryName = tokens.Last();  | 
140 | 142 | 		var owner = tokens.First();  | 
 | 143 | +		var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);  | 
141 | 144 | 
 
  | 
142 | 145 | 		if (tokens.Length < 2 || owner != "elastic")  | 
143 |  | -			return match;  | 
144 |  | - | 
145 |  | -		if (ReferenceRepositories.TryGetValue(repositoryName, out var r))  | 
146 | 146 | 		{  | 
147 |  | -			var current = r.GetBranch(ContentSource.Current);  | 
148 |  | -			var next = r.GetBranch(ContentSource.Next);  | 
149 |  | -			var edge = r.GetBranch(ContentSource.Edge);  | 
150 |  | -			var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);  | 
151 |  | -			if (current == branchOrTag)  | 
152 |  | -			{  | 
153 |  | -				match = match with  | 
154 |  | -				{  | 
155 |  | -					Current = ContentSource.Current  | 
156 |  | -				};  | 
157 |  | -			}  | 
158 |  | - | 
159 |  | -			if (next == branchOrTag)  | 
160 |  | -			{  | 
161 |  | -				match = match with  | 
162 |  | -				{  | 
163 |  | -					Next = ContentSource.Next  | 
164 |  | -				};  | 
165 |  | -			}  | 
166 |  | - | 
167 |  | -			if (edge == branchOrTag)  | 
168 |  | -			{  | 
169 |  | -				match = match with  | 
170 |  | -				{  | 
171 |  | -					Edge = ContentSource.Edge  | 
172 |  | -				};  | 
173 |  | -			}  | 
174 |  | - | 
175 |  | -			if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))  | 
176 |  | -			{  | 
177 |  | -				// if the current branch is a version, only speculatively match if branch is actually a new version  | 
178 |  | -				if (SemVersion.TryParse(current + ".0", out var currentVersion))  | 
179 |  | -				{  | 
180 |  | -					if (v >= currentVersion)  | 
181 |  | -					{  | 
182 |  | -						match = match with  | 
183 |  | -						{  | 
184 |  | -							Speculative = true  | 
185 |  | -						};  | 
186 |  | -					}  | 
187 |  | -				}  | 
188 |  | -				// assume we are newly onboarding the repository to current/next  | 
189 |  | -				else  | 
190 |  | -				{  | 
191 |  | -					match = match with  | 
192 |  | -					{  | 
193 |  | -						Speculative = true  | 
194 |  | -					};  | 
195 |  | -				}  | 
196 |  | -			}  | 
197 |  | - | 
 | 147 | +			logger.LogInformation("Repository {Repository} is not a valid elastic repository but {Owner}", repository, owner);  | 
198 | 148 | 			return match;  | 
199 | 149 | 		}  | 
200 | 150 | 
 
  | 
201 |  | -		if (repositoryName != NarrativeRepository.RepositoryName)  | 
 | 151 | +		// Check for new repositories  | 
 | 152 | +		if (!AvailableRepositories.TryGetValue(repositoryName, out var r))  | 
202 | 153 | 		{  | 
 | 154 | +			logger.LogInformation("Repository {Repository} has not yet been onboarded into assembler.yml", repository);  | 
203 | 155 | 			// this is an unknown new elastic repository  | 
204 |  | -			var isVersionBranch = ContentSourceRegex.MatchVersionBranch().IsMatch(branchOrTag);  | 
205 | 156 | 			if (isVersionBranch || branchOrTag == "main" || branchOrTag == "master")  | 
206 | 157 | 			{  | 
 | 158 | +				logger.LogInformation("Speculatively building {Repository} since it looks like an integration branch", repository);  | 
207 | 159 | 				return match with  | 
208 | 160 | 				{  | 
209 | 161 | 					Speculative = true  | 
210 | 162 | 				};  | 
211 | 163 | 			}  | 
 | 164 | +			logger.LogInformation("{Repository} on '{Branch}' does not look like it needs a speculative build", repository, branchOrTag);  | 
 | 165 | +			return match;  | 
212 | 166 | 		}  | 
213 | 167 | 
 
  | 
214 |  | -		if (Narrative.GetBranch(ContentSource.Current) == branchOrTag)  | 
 | 168 | +		var current = r.GetBranch(ContentSource.Current);  | 
 | 169 | +		var next = r.GetBranch(ContentSource.Next);  | 
 | 170 | +		var edge = r.GetBranch(ContentSource.Edge);  | 
 | 171 | +		logger.LogInformation("Active content-sources for {Repository}. current: {Current}, next: {Next}, edge: {Edge}' ", repository, current, next, edge);  | 
 | 172 | +		if (current == branchOrTag)  | 
215 | 173 | 		{  | 
 | 174 | +			logger.LogInformation("Content-Source current: {Current} matches: {Branch}", current, branchOrTag);  | 
216 | 175 | 			match = match with  | 
217 | 176 | 			{  | 
218 | 177 | 				Current = ContentSource.Current  | 
219 | 178 | 			};  | 
220 | 179 | 		}  | 
221 | 180 | 
 
  | 
222 |  | -		if (Narrative.GetBranch(ContentSource.Next) == branchOrTag)  | 
 | 181 | +		if (next == branchOrTag)  | 
223 | 182 | 		{  | 
 | 183 | +			logger.LogInformation("Content-Source next: {Next} matches: {Branch}", next, branchOrTag);  | 
224 | 184 | 			match = match with  | 
225 | 185 | 			{  | 
226 | 186 | 				Next = ContentSource.Next  | 
227 | 187 | 			};  | 
228 | 188 | 		}  | 
229 | 189 | 
 
  | 
230 |  | -		if (Narrative.GetBranch(ContentSource.Edge) == branchOrTag)  | 
 | 190 | +		if (edge == branchOrTag)  | 
231 | 191 | 		{  | 
 | 192 | +			logger.LogInformation("Content-Source edge: {Edge} matches: {Branch}", edge, branchOrTag);  | 
232 | 193 | 			match = match with  | 
233 | 194 | 			{  | 
234 | 195 | 				Edge = ContentSource.Edge  | 
235 | 196 | 			};  | 
236 | 197 | 		}  | 
237 | 198 | 
 
  | 
 | 199 | +		// check version branches  | 
 | 200 | +		if (isVersionBranch && SemVersion.TryParse(branchOrTag + ".0", out var v))  | 
 | 201 | +		{  | 
 | 202 | +			logger.LogInformation("Branch or tag {Branch} is a versioned branch", branchOrTag);  | 
 | 203 | +			// if the current branch is a version, only speculatively match if branch is actually a new version  | 
 | 204 | +			if (SemVersion.TryParse(current + ".0", out var currentVersion))  | 
 | 205 | +			{  | 
 | 206 | +				logger.LogInformation("Current is already using versioned branches {Current}", currentVersion);  | 
 | 207 | +				if (v >= currentVersion)  | 
 | 208 | +				{  | 
 | 209 | +					logger.LogInformation("Speculative build because {Branch} is gte current {Current}", branchOrTag, currentVersion);  | 
 | 210 | +					match = match with  | 
 | 211 | +					{  | 
 | 212 | +						Speculative = true  | 
 | 213 | +					};  | 
 | 214 | +				}  | 
 | 215 | +				else  | 
 | 216 | +					logger.LogInformation("NO speculative build because {Branch} is lt {Current}", branchOrTag, currentVersion);  | 
 | 217 | +			}  | 
 | 218 | +			// assume we are newly onboarding the repository to current/next  | 
 | 219 | +			else if (product?.VersioningSystem is { } versioningSystem)  | 
 | 220 | +			{  | 
 | 221 | +				logger.LogInformation("Current is not using versioned branches checking product info");  | 
 | 222 | +				var productCurrentVersion = versioningSystem.Current;  | 
 | 223 | +				if (v >= productCurrentVersion)  | 
 | 224 | +				{  | 
 | 225 | +					logger.LogInformation("Speculative build {Branch} is gte product current '{ProductCurrent}'", branchOrTag, productCurrentVersion);  | 
 | 226 | +					match = match with  | 
 | 227 | +					{  | 
 | 228 | +						Speculative = true  | 
 | 229 | +					};  | 
 | 230 | +				}  | 
 | 231 | +				else  | 
 | 232 | +					logger.LogInformation("NO speculative build {Branch} is lte product current '{ProductCurrent}'", branchOrTag, productCurrentVersion);  | 
 | 233 | +			}  | 
 | 234 | +			else  | 
 | 235 | +				logger.LogInformation("No versioning system found for {Repository} on {Branch}", repository, branchOrTag);  | 
 | 236 | +		}  | 
 | 237 | + | 
238 | 238 | 		// if we haven't matched anything yet, and the branch is 'main' or 'master' always build  | 
239 |  | -		if (match is { Current: null, Next: null, Edge: null, Speculative: false }  | 
240 |  | -			&& branchOrTag is "main" or "master")  | 
 | 239 | +		if (match is { Current: null, Next: null, Edge: null, Speculative: false } && branchOrTag is "main" or "master")  | 
241 | 240 | 		{  | 
242 | 241 | 			return match with  | 
243 | 242 | 			{  | 
 | 
0 commit comments