@@ -25,21 +25,37 @@ pub async fn pipelines_list(
2525 from : String ,
2626 to : String ,
2727 limit : i32 ,
28+ branch : Option < String > ,
29+ pipeline_name : Option < String > ,
2830) -> Result < ( ) > {
2931 let api = crate :: make_api!( CIVisibilityPipelinesAPI , cfg) ;
3032
3133 let from_ms = util:: parse_time_to_unix_millis ( & from) ?;
3234 let to_ms = util:: parse_time_to_unix_millis ( & to) ?;
3335 let from_str = chrono:: DateTime :: from_timestamp_millis ( from_ms)
34- . unwrap ( )
36+ . ok_or_else ( || anyhow :: anyhow! ( "--from value {from_ms}ms is out of representable range" ) ) ?
3537 . to_rfc3339 ( ) ;
3638 let to_str = chrono:: DateTime :: from_timestamp_millis ( to_ms)
37- . unwrap ( )
39+ . ok_or_else ( || anyhow :: anyhow! ( "--to value {to_ms}ms is out of representable range" ) ) ?
3840 . to_rfc3339 ( ) ;
3941
40- let mut filter = CIAppPipelinesQueryFilter :: new ( ) . from ( from_str ) . to ( to_str ) ;
42+ let mut query_parts : Vec < String > = Vec :: new ( ) ;
4143 if let Some ( q) = query {
42- filter = filter. query ( q) ;
44+ query_parts. push ( q) ;
45+ }
46+ if let Some ( b) = branch {
47+ query_parts. push ( format ! ( "@git.branch:\" {}\" " , b. replace( '"' , "\\ \" " ) ) ) ;
48+ }
49+ if let Some ( p) = pipeline_name {
50+ query_parts. push ( format ! (
51+ "@ci.pipeline.name:\" {}\" " ,
52+ p. replace( '"' , "\\ \" " )
53+ ) ) ;
54+ }
55+
56+ let mut filter = CIAppPipelinesQueryFilter :: new ( ) . from ( from_str) . to ( to_str) ;
57+ if !query_parts. is_empty ( ) {
58+ filter = filter. query ( query_parts. join ( " " ) ) ;
4359 }
4460
4561 let body = CIAppPipelineEventsRequest :: new ( )
@@ -88,18 +104,27 @@ pub async fn events_search(
88104 from : String ,
89105 to : String ,
90106 limit : i32 ,
107+ sort : String ,
91108) -> Result < ( ) > {
92109 let api = crate :: make_api!( CIVisibilityPipelinesAPI , cfg) ;
93110
94111 let from_ms = util:: parse_time_to_unix_millis ( & from) ?;
95112 let to_ms = util:: parse_time_to_unix_millis ( & to) ?;
96113 let from_str = chrono:: DateTime :: from_timestamp_millis ( from_ms)
97- . unwrap ( )
114+ . ok_or_else ( || anyhow :: anyhow! ( "--from value {from_ms}ms is out of representable range" ) ) ?
98115 . to_rfc3339 ( ) ;
99116 let to_str = chrono:: DateTime :: from_timestamp_millis ( to_ms)
100- . unwrap ( )
117+ . ok_or_else ( || anyhow :: anyhow! ( "--to value {to_ms}ms is out of representable range" ) ) ?
101118 . to_rfc3339 ( ) ;
102119
120+ let sort_val = match sort. as_str ( ) {
121+ "asc" | "timestamp" => CIAppSort :: TIMESTAMP_ASCENDING ,
122+ "desc" | "-timestamp" => CIAppSort :: TIMESTAMP_DESCENDING ,
123+ other => anyhow:: bail!(
124+ "invalid --sort value: {other:?}\n Expected: asc (ascending) or desc (descending)"
125+ ) ,
126+ } ;
127+
103128 let filter = CIAppPipelinesQueryFilter :: new ( )
104129 . from ( from_str)
105130 . to ( to_str)
@@ -108,7 +133,7 @@ pub async fn events_search(
108133 let body = CIAppPipelineEventsRequest :: new ( )
109134 . filter ( filter)
110135 . page ( CIAppQueryPageOptions :: new ( ) . limit ( limit) )
111- . sort ( CIAppSort :: TIMESTAMP_DESCENDING ) ;
136+ . sort ( sort_val ) ;
112137
113138 let params = SearchCIAppPipelineEventsOptionalParams :: default ( ) . body ( body) ;
114139 let resp = api
@@ -124,10 +149,10 @@ pub async fn events_aggregate(cfg: &Config, query: String, from: String, to: Str
124149 let from_ms = util:: parse_time_to_unix_millis ( & from) ?;
125150 let to_ms = util:: parse_time_to_unix_millis ( & to) ?;
126151 let from_str = chrono:: DateTime :: from_timestamp_millis ( from_ms)
127- . unwrap ( )
152+ . ok_or_else ( || anyhow :: anyhow! ( "--from value {from_ms}ms is out of representable range" ) ) ?
128153 . to_rfc3339 ( ) ;
129154 let to_str = chrono:: DateTime :: from_timestamp_millis ( to_ms)
130- . unwrap ( )
155+ . ok_or_else ( || anyhow :: anyhow! ( "--to value {to_ms}ms is out of representable range" ) ) ?
131156 . to_rfc3339 ( ) ;
132157
133158 let filter = CIAppPipelinesQueryFilter :: new ( )
@@ -157,10 +182,10 @@ pub async fn tests_search(
157182 let from_ms = util:: parse_time_to_unix_millis ( & from) ?;
158183 let to_ms = util:: parse_time_to_unix_millis ( & to) ?;
159184 let from_str = chrono:: DateTime :: from_timestamp_millis ( from_ms)
160- . unwrap ( )
185+ . ok_or_else ( || anyhow :: anyhow! ( "--from value {from_ms}ms is out of representable range" ) ) ?
161186 . to_rfc3339 ( ) ;
162187 let to_str = chrono:: DateTime :: from_timestamp_millis ( to_ms)
163- . unwrap ( )
188+ . ok_or_else ( || anyhow :: anyhow! ( "--to value {to_ms}ms is out of representable range" ) ) ?
164189 . to_rfc3339 ( ) ;
165190
166191 let filter = CIAppTestsQueryFilter :: new ( )
@@ -187,10 +212,10 @@ pub async fn tests_aggregate(cfg: &Config, query: String, from: String, to: Stri
187212 let from_ms = util:: parse_time_to_unix_millis ( & from) ?;
188213 let to_ms = util:: parse_time_to_unix_millis ( & to) ?;
189214 let from_str = chrono:: DateTime :: from_timestamp_millis ( from_ms)
190- . unwrap ( )
215+ . ok_or_else ( || anyhow :: anyhow! ( "--from value {from_ms}ms is out of representable range" ) ) ?
191216 . to_rfc3339 ( ) ;
192217 let to_str = chrono:: DateTime :: from_timestamp_millis ( to_ms)
193- . unwrap ( )
218+ . ok_or_else ( || anyhow :: anyhow! ( "--to value {to_ms}ms is out of representable range" ) ) ?
194219 . to_rfc3339 ( ) ;
195220
196221 let filter = CIAppTestsQueryFilter :: new ( )
@@ -314,7 +339,7 @@ mod tests {
314339 let mut s = mockito:: Server :: new_async ( ) . await ;
315340 let cfg = test_config ( & s. url ( ) ) ;
316341 mock_all ( & mut s, r#"{"data": []}"# ) . await ;
317- let _ = super :: pipelines_list ( & cfg, None , "1h" . into ( ) , "now" . into ( ) , 10 ) . await ;
342+ let _ = super :: pipelines_list ( & cfg, None , "1h" . into ( ) , "now" . into ( ) , 10 , None , None ) . await ;
318343 cleanup_env ( ) ;
319344 }
320345
@@ -358,4 +383,17 @@ mod tests {
358383 . contains( "invalid sort value" ) ) ;
359384 cleanup_env ( ) ;
360385 }
386+
387+ #[ tokio:: test]
388+ async fn test_cicd_events_search_invalid_sort ( ) {
389+ let cfg = test_config ( "http://unused.local" ) ;
390+ let result =
391+ super :: events_search ( & cfg, "*" . into ( ) , "1h" . into ( ) , "now" . into ( ) , 10 , "bogus" . into ( ) )
392+ . await ;
393+ assert ! ( result. is_err( ) ) ;
394+ assert ! ( result
395+ . unwrap_err( )
396+ . to_string( )
397+ . contains( "invalid --sort value" ) ) ;
398+ }
361399}
0 commit comments