2
2
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3
3
// See the LICENSE file in the project root for more information
4
4
5
+ using System . Text . RegularExpressions ;
5
6
using Elastic . Documentation . Serialization ;
6
7
using YamlDotNet . Serialization ;
7
8
using YamlStaticContext = Elastic . Documentation . Configuration . Serialization . YamlStaticContext ;
@@ -83,19 +84,45 @@ private static TRepository RepositoryDefaults<TRepository>(TRepository r, string
83
84
/// <paramref name="repository"/>.
84
85
public ContentSourceMatch Match ( string repository , string branchOrTag )
85
86
{
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
+
88
95
if ( ReferenceRepositories . TryGetValue ( repositoryName , out var r ) )
89
96
{
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 )
91
101
match = match with { Current = ContentSource . Current } ;
92
- if ( r . GetBranch ( ContentSource . Next ) == branchOrTag )
102
+ if ( next == branchOrTag )
93
103
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
+ }
94
116
return match ;
95
117
}
96
118
97
119
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
+ }
99
126
100
127
if ( Narrative . GetBranch ( ContentSource . Current ) == branchOrTag )
101
128
match = match with { Current = ContentSource . Current } ;
@@ -105,5 +132,12 @@ public ContentSourceMatch Match(string repository, string branchOrTag)
105
132
return match ;
106
133
}
107
134
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 ( ) ;
109
143
}
0 commit comments