22// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33// See the LICENSE file in the project root for more information
44
5+ using System . Text . RegularExpressions ;
56using Elastic . Documentation . Serialization ;
67using YamlDotNet . Serialization ;
78using YamlStaticContext = Elastic . Documentation . Configuration . Serialization . YamlStaticContext ;
@@ -83,19 +84,45 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string
8384 /// <paramref name="repository"/>.
8485 public ContentSourceMatch Match ( string repository , string branchOrTag )
8586 {
86- var repositoryName = repository . Split ( '/' ) . Last ( ) ;
87- var match = new ContentSourceMatch ( null , null ) ;
87+ var match = new ContentSourceMatch ( null , null , false ) ;
88+ var tokens = repository . Split ( '/' ) ;
89+ var repositoryName = tokens . Last ( ) ;
90+ var owner = tokens . First ( ) ;
91+
92+ if ( tokens . Length < 2 || owner != "elastic" )
93+ return match ;
94+
8895 if ( ReferenceRepositories . TryGetValue ( repositoryName , out var r ) )
8996 {
90- if ( r . GetBranch ( ContentSource . Current ) == branchOrTag )
97+ var current = r . GetBranch ( ContentSource . Current ) ;
98+ var next = r . GetBranch ( ContentSource . Next ) ;
99+ var isVersionBranch = ContentSourceRegex . MatchVersionBranch ( ) . IsMatch ( branchOrTag ) ;
100+ if ( current == branchOrTag )
91101 match = match with { Current = ContentSource . Current } ;
92- if ( r . GetBranch ( ContentSource . Next ) == branchOrTag )
102+ if ( next == branchOrTag )
93103 match = match with { Next = ContentSource . Next } ;
104+ if ( isVersionBranch && SemVersion . TryParse ( branchOrTag + ".0" , out var v ) )
105+ {
106+ // if the current branch is a version, only speculatively match if branch is actually a new version
107+ if ( SemVersion . TryParse ( current + ".0" , out var currentVersion ) )
108+ {
109+ if ( v >= currentVersion )
110+ match = match with { Speculative = true } ;
111+ }
112+ // assume we are newly onboarding the repository to current/next
113+ else
114+ match = match with { Speculative = true } ;
115+ }
94116 return match ;
95117 }
96118
97119 if ( repositoryName != NarrativeRepository . RepositoryName )
98- return match ;
120+ {
121+ // this is an unknown new elastic repository
122+ var isVersionBranch = ContentSourceRegex . MatchVersionBranch ( ) . IsMatch ( branchOrTag ) ;
123+ if ( isVersionBranch || branchOrTag == "main" || branchOrTag == "master" )
124+ return match with { Speculative = true } ;
125+ }
99126
100127 if ( Narrative . GetBranch ( ContentSource . Current ) == branchOrTag )
101128 match = match with { Current = ContentSource . Current } ;
@@ -105,5 +132,12 @@ public ContentSourceMatch Match(string repository, string branchOrTag)
105132 return match ;
106133 }
107134
108- public record ContentSourceMatch ( ContentSource ? Current , ContentSource ? Next ) ;
135+ public record ContentSourceMatch ( ContentSource ? Current , ContentSource ? Next , bool Speculative ) ;
136+
137+ }
138+
139+ internal static partial class ContentSourceRegex
140+ {
141+ [ GeneratedRegex ( @"^\d+\.\d+$" , RegexOptions . IgnoreCase , "en-US" ) ]
142+ public static partial Regex MatchVersionBranch ( ) ;
109143}
0 commit comments