@@ -10,7 +10,7 @@ use warp::{self, Filter, Rejection};
10
10
11
11
lazy_static ! {
12
12
static ref GIT_REVISION_RE : Regex =
13
- Regex :: new( r"^crater(-agent)?/(?P<sha>[a-f0-9]{7,40})$" ) . unwrap( ) ;
13
+ Regex :: new( r"^crater(-agent)?/(?P<sha>[a-f0-9]{7,40})( \(.*\))? $" ) . unwrap( ) ;
14
14
}
15
15
16
16
#[ derive( Copy , Clone ) ]
@@ -38,15 +38,17 @@ fn parse_token(authorization: &str) -> Option<&str> {
38
38
None
39
39
}
40
40
41
+ fn git_revision ( user_agent : & str ) -> Option < String > {
42
+ GIT_REVISION_RE
43
+ . captures ( user_agent)
44
+ . map ( |cap| cap[ "sha" ] . to_string ( ) )
45
+ }
46
+
41
47
fn check_auth ( data : & Data , headers : & HeaderMap , token_type : TokenType ) -> Option < AuthDetails > {
42
48
// Try to extract the git revision from the User-Agent header
43
49
let git_revision = if let Some ( ua_value) = headers. get ( USER_AGENT ) {
44
50
if let Ok ( ua) = ua_value. to_str ( ) {
45
- if let Some ( cap) = GIT_REVISION_RE . captures ( ua) {
46
- Some ( cap[ "sha" ] . to_string ( ) )
47
- } else {
48
- None
49
- }
51
+ git_revision ( ua)
50
52
} else {
51
53
None
52
54
}
@@ -177,7 +179,7 @@ impl ACL {
177
179
178
180
#[ cfg( test) ]
179
181
mod tests {
180
- use super :: parse_token;
182
+ use super :: { git_revision , parse_token} ;
181
183
182
184
#[ test]
183
185
fn test_parse_token ( ) {
@@ -187,4 +189,28 @@ mod tests {
187
189
assert_eq ! ( parse_token( "CraterToken foo" ) , Some ( "foo" ) ) ;
188
190
assert_eq ! ( parse_token( "CraterToken foo bar" ) , None ) ;
189
191
}
192
+
193
+ #[ test]
194
+ fn test_git_revision ( ) {
195
+ for sha in & [ "0000000" , "0000000000000000000000000000000000000000" ] {
196
+ assert_eq ! (
197
+ git_revision( & format!( "crater/{}" , sha) ) ,
198
+ Some ( sha. to_string( ) )
199
+ ) ;
200
+ assert_eq ! (
201
+ git_revision( & format!( "crater/{} (foo bar!)" , sha) ) ,
202
+ Some ( sha. to_string( ) )
203
+ ) ;
204
+ }
205
+
206
+ // Test with too few and too many digits
207
+ assert ! ( git_revision( "crater/000000" ) . is_none( ) ) ;
208
+ assert ! ( git_revision( "crater/00000000000000000000000000000000000000000" ) . is_none( ) ) ;
209
+
210
+ // Test invalid syntaxes
211
+ assert ! ( git_revision( "crater/ggggggg" ) . is_none( ) ) ;
212
+ assert ! ( git_revision( "crater/0000000(foo bar!)" ) . is_none( ) ) ;
213
+ assert ! ( git_revision( "crater/0000000 (foo bar!) " ) . is_none( ) ) ;
214
+ assert ! ( git_revision( "crate/0000000" ) . is_none( ) ) ;
215
+ }
190
216
}
0 commit comments