@@ -64,14 +64,15 @@ pub(super) async fn handle_command(
6464 start_date. checked_add_signed ( Duration :: days ( 10 ) ) . unwrap ( ) ;
6565
6666 let mut current: BTreeMap < String , Option < UserStatus > > = BTreeMap :: new ( ) ;
67- let history: BTreeMap < String , Vec < UserStatus > > = BTreeMap :: new ( ) ;
67+ let mut history: BTreeMap < String , Vec < UserStatus > > = BTreeMap :: new ( ) ;
6868
6969 // TODO
7070 // change this to be entered by the user as part of the command
7171 // it should match the same team that we check for above when determining if the user is a member
7272 let team = github:: get_team ( & ctx. github , & "T-lang" ) . await ?. unwrap ( ) ;
7373 for member in team. members {
74- current. insert ( member. name , None ) ;
74+ current. insert ( member. name . clone ( ) , None ) ;
75+ history. insert ( member. name . clone ( ) , Vec :: new ( ) ) ;
7576 }
7677
7778 current. insert (
@@ -128,25 +129,24 @@ fn build_status_comment(
128129 current : & BTreeMap < String , Option < UserStatus > > ,
129130) -> anyhow:: Result < String > {
130131 let mut comment = "| Team member | State |\n |-------------|-------|" . to_owned ( ) ;
131- for ( user, statuses ) in history {
132+ for ( user, status ) in current {
132133 let mut user_statuses = format ! ( "\n | {} |" , user) ;
133134
134135 // previous stasuses
135- for status in statuses {
136- let status_item = format ! ( " ~~{}~~ " , status. resolution) ;
137- user_statuses. push_str ( & status_item) ;
136+ match history. get ( user) {
137+ Some ( statuses) => {
138+ for status in statuses {
139+ let status_item = format ! ( " ~~{}~~ " , status. resolution) ;
140+ user_statuses. push_str ( & status_item) ;
141+ }
142+ }
143+ None => bail ! ( "user {} not present in history statuses list" , user) ,
138144 }
139145
140146 // current status
141- let user_resolution = match current. get ( user) {
142- Some ( current_status) => {
143- if let Some ( status) = current_status {
144- format ! ( "**{}**" , status. resolution)
145- } else {
146- "" . to_string ( )
147- }
148- }
149- None => bail ! ( "user {} not present in current statuses list" , user) ,
147+ let user_resolution = match status {
148+ Some ( status) => format ! ( "**{}**" , status. resolution) ,
149+ _ => "" . to_string ( ) ,
150150 } ;
151151
152152 let status_item = format ! ( " {} |" , user_resolution) ;
@@ -276,7 +276,40 @@ mod tests {
276276 let build_result = build_status_comment ( & history, & current_statuses) ;
277277 assert_eq ! (
278278 format!( "{}" , build_result. unwrap_err( ) ) ,
279- "user Barbara not present in current statuses list"
279+ "user Martin not present in history statuses list"
280280 ) ;
281281 }
282+
283+ #[ test]
284+ fn test_successfuly_build_comment_no_history ( ) {
285+ let mut history: BTreeMap < String , Vec < UserStatus > > = BTreeMap :: new ( ) ;
286+ let mut current_statuses: BTreeMap < String , Option < UserStatus > > = BTreeMap :: new ( ) ;
287+
288+ // user 1
289+ let mut user_1_statuses: Vec < UserStatus > = Vec :: new ( ) ;
290+ user_1_statuses. push ( create ! ( UserStatus ) ) ;
291+ user_1_statuses. push ( create ! ( UserStatus , : hold) ) ;
292+
293+ current_statuses. insert ( "Niklaus" . to_string ( ) , Some ( create ! ( UserStatus ) ) ) ;
294+ history. insert ( "Niklaus" . to_string ( ) , Vec :: new ( ) ) ;
295+
296+ // user 2
297+ let mut user_2_statuses: Vec < UserStatus > = Vec :: new ( ) ;
298+ user_2_statuses. push ( create ! ( UserStatus , : hold) ) ;
299+ user_2_statuses. push ( create ! ( UserStatus ) ) ;
300+
301+ current_statuses. insert ( "Barbara" . to_string ( ) , Some ( create ! ( UserStatus ) ) ) ;
302+ history. insert ( "Barbara" . to_string ( ) , Vec :: new ( ) ) ;
303+
304+ let build_result = build_status_comment ( & history, & current_statuses)
305+ . expect ( "it shouldn't fail building the message" ) ;
306+ let expected_comment = "| Team member | State |\n \
307+ |-------------|-------|\n \
308+ | Barbara | **merge** |\n \
309+ | Niklaus | **merge** |\
310+ "
311+ . to_string ( ) ;
312+
313+ assert_eq ! ( build_result, expected_comment) ;
314+ }
282315}
0 commit comments