bat-cli 0.8.10

Blockchain Auditor Toolkit (BAT)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::batbelt::bat_dialoguer::BatDialoguer;
use crate::batbelt::command_line::{execute_command, CodeEditor};
use crate::batbelt::git::git_commit::GitCommit;
use crate::batbelt::parser::entrypoint_parser::EntrypointParser;
use crate::batbelt::path::{BatFile, BatFolder};
use crate::batbelt::templates::code_overhaul_template::{
    CodeOverhaulSection, CodeOverhaulTemplate, CoderOverhaulTemplatePlaceholders,
};
use crate::batbelt::BatEnumerator;
use crate::commands::{BatCommandEnumerator, CommandError, CommandResult};
use std::fs;

use crate::batbelt::analytics::entry_points_flow::EntryPointFlowAnalytics;
use crate::batbelt::analytics::BatAnalytics;
use crate::{batbelt, Suggestion};
use clap::Subcommand;
use colored::Colorize;
use error_stack::{FutureExt, IntoReport, Report, ResultExt};
use lazy_regex::regex;
use regex::Regex;

use crate::batbelt::metadata::context_accounts_metadata::ContextAccountsMetadata;
use crate::batbelt::metadata::miro_metadata::{SignerInfo, SignerType};
use crate::batbelt::metadata::program_accounts_metadata::ProgramAccountMetadata;
use crate::batbelt::metadata::{BatMetadata, BatMetadataCommit, BatMetadataParser, MiroMetadata};
use crate::batbelt::miro::connector::ConnectorOptions;
use crate::batbelt::miro::frame::{MiroCodeOverhaulConfig, MiroFrame};
use crate::batbelt::miro::image::{MiroImage, MiroImageType};

use crate::batbelt::miro::sticky_note::MiroStickyNote;
use crate::batbelt::miro::MiroConfig;
use crate::batbelt::parser::code_overhaul_parser::CodeOverhaulParser;
use crate::batbelt::parser::solana_account_parser::{SolanaAccountParser, SolanaAccountType};
use crate::batbelt::parser::source_code_parser::SourceCodeScreenshotOptions;
use crate::commands::miro_commands::{miro_command_functions, MiroCommand};

#[derive(
    Subcommand, Debug, strum_macros::Display, PartialEq, Clone, strum_macros::EnumIter, Default,
)]
pub enum CodeOverhaulCommand {
    /// Starts a code-overhaul file audit
    Start {
        /// Skips miro deployment
        #[arg(long)]
        skip_miro: bool,
        /// Starts a guided process to start a co file
        #[arg(long)]
        interactive: bool,
    },
    /// Moves the code-overhaul file from to-review to finished
    #[default]
    Finish,
    /// creates a code-overhaul summary from the code-overhaul finished notes
    Summary,
    /// creates program accounts metadata
    CreateProgramAccountsMetadata,
    /// calculates state changes from the program accounts metadata
    UpdateProgramAccountsMetadata,
}

impl BatEnumerator for CodeOverhaulCommand {}

impl BatCommandEnumerator for CodeOverhaulCommand {
    fn execute_command(&self) -> CommandResult<()> {
        unimplemented!()
    }

    fn check_metadata_is_initialized(&self) -> bool {
        true
    }

    fn check_correct_branch(&self) -> bool {
        true
    }
}

impl CodeOverhaulCommand {
    pub async fn execute_command(&self) -> CommandResult<()> {
        match self {
            CodeOverhaulCommand::Start {
                skip_miro,
                interactive,
            } => self.execute_start(*skip_miro, *interactive).await,
            CodeOverhaulCommand::Finish => self.execute_finish(),
            CodeOverhaulCommand::Summary => self.execute_summary(),
            CodeOverhaulCommand::CreateProgramAccountsMetadata => {
                self.execute_program_accounts_metadata()
            }
            CodeOverhaulCommand::UpdateProgramAccountsMetadata => self.execute_calculate_sc(),
        }
    }

    fn execute_calculate_sc(&self) -> CommandResult<()> {
        ProgramAccountMetadata::update_program_accounts_metadata_file()
            .change_context(CommandError)?;
        println!("Programs account metadata created");
        Ok(())
    }

    fn execute_program_accounts_metadata(&self) -> CommandResult<()> {
        ProgramAccountMetadata::create_program_accounts_metadata_file()
            .change_context(CommandError)?;
        println!("Programs account metadata created");
        Ok(())
    }

    fn execute_summary(&self) -> CommandResult<()> {
        let mut co_summary_content = String::new();
        let co_finished_bat_files_vec = BatFolder::CodeOverhaulFinished
            .get_all_bat_files(true, None, None)
            .change_context(CommandError)?;
        for finished_co_file in co_finished_bat_files_vec {
            let entry_point_name = finished_co_file
                .get_file_name()
                .change_context(CommandError)?
                .trim_end_matches(".md")
                .to_string();
            let co_parser = CodeOverhaulParser::new_from_entry_point_name(entry_point_name)
                .change_context(CommandError)?;
            log::debug!("co_parser:\n{:#?}", co_parser);
            let state_changes_section_content = co_parser.section_content.state_changes.replace(
                &CodeOverhaulSection::StateChanges.to_markdown_header(),
                &CodeOverhaulSection::StateChanges
                    .to_markdown_header()
                    .replace("#", "##"),
            );
            let mut notes_section_content = co_parser.section_content.notes.replace(
                &CodeOverhaulSection::Notes.to_markdown_header(),
                &CodeOverhaulSection::Notes
                    .to_markdown_header()
                    .replace("#", "##"),
            );
            let miro_frame_url_section_content = co_parser.section_content.miro_frame_url.replace(
                &CodeOverhaulSection::MiroFrameUrl.to_markdown_header(),
                &CodeOverhaulSection::MiroFrameUrl
                    .to_markdown_header()
                    .replace("#", "##"),
            );
            let co_file_name = finished_co_file
                .get_file_name()
                .change_context(CommandError)?;

            let checkbox_regex = regex!(r#"- \[x\]"#);
            let notes_section_filtered = notes_section_content
                .lines()
                .filter(|line| !checkbox_regex.is_match(line))
                .collect::<Vec<_>>();

            if notes_section_filtered.len() == 2 && notes_section_filtered[1] == "" {
                continue;
            }

            notes_section_content = notes_section_filtered.join("\n");

            let finished_file_summary = format!(
                "# {}\n\n{}\n\n{}\n\n## Code overhaul file path:\n\n[{}](code-overhaul/finished/{})",
                co_file_name,
                notes_section_content,
                miro_frame_url_section_content,
                co_file_name,
                co_file_name
            );
            co_summary_content = if co_summary_content.is_empty() {
                finished_file_summary
            } else {
                format!("{}\n\n{}", co_summary_content, finished_file_summary)
            }
        }

        let code_overhaul_summary_bat_file = BatFile::CodeOverhaulSummaryFile;
        code_overhaul_summary_bat_file
            .write_content(false, &co_summary_content)
            .change_context(CommandError)?;

        Ok(())
    }

    fn execute_finish(&self) -> error_stack::Result<(), CommandError> {
        // get to-review files
        let started_entrypoint_direntry_vec = BatFolder::CodeOverhaulStarted
            .get_all_files_dir_entries(true, None, None)
            .change_context(CommandError)?;
        let started_entrypoint_names = started_entrypoint_direntry_vec
            .into_iter()
            .map(|dir_entry| dir_entry.file_name().to_str().unwrap().to_string())
            .collect::<Vec<_>>();

        if started_entrypoint_names.is_empty() {
            return Err(Report::new(CommandError).attach_printable(format!(
                "{} folder is empty",
                "code-overhaul/started".green()
            )));
        }

        let finished_endpoint = if started_entrypoint_names.len() == 1 {
            let selected = started_entrypoint_names[0].clone();
            println!("Moving {} to finished", selected.green());
            selected
        } else {
            let prompt_text = "Select the code-overhaul to finish:";
            let selection = BatDialoguer::select(
                prompt_text.to_string(),
                started_entrypoint_names.clone(),
                None,
            )
            .change_context(CommandError)?;
            started_entrypoint_names[selection].clone()
        };

        let finished_co_folder_path = BatFolder::CodeOverhaulFinished
            .get_path(true)
            .change_context(CommandError)?;
        let started_co_bat_file = BatFile::CodeOverhaulStarted {
            file_name: finished_endpoint.clone(),
        };
        let started_co_bat_file_path = started_co_bat_file
            .get_path(true)
            .change_context(CommandError)?;
        let started_co_bat_file_content = started_co_bat_file
            .read_content(true)
            .change_context(CommandError)?;
        let miro_placeholder =
            CoderOverhaulTemplatePlaceholders::CompleteWithMiroFrameUrl.to_placeholder();
        if started_co_bat_file_content.contains(&miro_placeholder) {
            let entrypoint_name = finished_endpoint.trim_end_matches(".md").to_string();
            if let Ok(miro_co_metadata) =
                MiroMetadata::get_co_metadata_by_entrypoint_name(entrypoint_name)
                    .change_context(CommandError)
            {
                let miro_frame_url =
                    MiroFrame::get_frame_url_by_frame_id(&miro_co_metadata.miro_frame_id)
                        .change_context(CommandError)?;
                let new_content =
                    started_co_bat_file_content.replace(&miro_placeholder, &miro_frame_url);
                started_co_bat_file
                    .write_content(true, &new_content)
                    .change_context(CommandError)?;
            }
        }

        co_commands_functions::check_code_overhaul_file_completed(started_co_bat_file)?;
        execute_command(
            "mv",
            &[&started_co_bat_file_path, &finished_co_folder_path],
            false,
        )
        .change_context(CommandError)?;
        GitCommit::FinishCO {
            entrypoint_name: finished_endpoint.clone(),
        }
        .create_commit(true)
        .change_context(CommandError)?;

        println!("{} moved to finished", finished_endpoint.green());
        Ok(())
    }

    async fn execute_start(
        &self,
        skip_miro: bool,
        interactive: bool,
    ) -> error_stack::Result<(), CommandError> {
        // if interactive {
        //     let entry_point_name = self.execute_start_interactive()?;
        //     if !skip_miro {
        //         co_commands_functions::prompt_deploy_miro(entry_point_name).await?;
        //     }
        //     return Ok(());
        // }

        let review_files = BatFolder::CodeOverhaulToReview
            .get_all_files_names(true, None, None)
            .change_context(CommandError)?;

        if review_files.is_empty() {
            return Err(Report::new(CommandError).attach_printable(format!(
                "{} folder is empty",
                "code-overhaul/to-review".green()
            )));
        }
        let prompt_text = "Select the code-overhaul file to start:";
        let selection = BatDialoguer::select(prompt_text.to_string(), review_files.clone(), None)
            .change_context(CommandError)?;

        // user select file
        let to_start_file_name = &review_files[selection].clone();
        let entrypoint_name = to_start_file_name.trim_end_matches(".md");

        BatFile::CodeOverhaulToReview {
            file_name: to_start_file_name.clone(),
        }
        .remove_file()
        .change_context(CommandError)?;

        let started_bat_file = BatFile::CodeOverhaulStarted {
            file_name: to_start_file_name.clone(),
        };

        let started_template =
            CodeOverhaulTemplate::new(entrypoint_name, true).change_context(CommandError)?;

        let started_markdown_content = started_template
            .get_markdown_content()
            .change_context(CommandError)?;

        started_bat_file
            .write_content(false, &started_markdown_content)
            .change_context(CommandError)?;

        println!("{to_start_file_name} file moved to started");

        GitCommit::StartCO {
            entrypoint_name: to_start_file_name.clone(),
        }
        .create_commit(true)
        .change_context(CommandError)?;

        started_bat_file
            .open_in_editor(true, None)
            .change_context(CommandError)?;

        // open entrypoint file at the entrypoint function line
        if let Some(ep_parser) = started_template.entrypoint_parser {
            let ep_bat_file = BatFile::Generic {
                file_path: ep_parser.entry_point_function.path.clone(),
            };
            ep_bat_file
                .open_in_editor(true, Some(ep_parser.entry_point_function.start_line_index))
                .change_context(CommandError)?;
        }
        if !skip_miro {
            let deployed = co_commands_functions::prompt_deploy_miro(entrypoint_name.to_string()).await?;
            if deployed {
                GitCommit::UpdateCO {
                    entrypoint_name: to_start_file_name.clone(),
                }
                .create_commit(true)
                .change_context(CommandError)?;
            }
        }
        Ok(())
    }

    // fn execute_start_interactive(&self) -> CommandResult<String> {
    //     let suggested_entry_point_cache =
    //         EntryPointFlow::get_suggested_next_entry_point()
    //             .change_context(CommandError)?;
    //     let entry_point_name = suggested_entry_point_cache.entry_point_name;
    //     let ep_parser =
    //         EntrypointParser::new_from_name(&entry_point_name).change_context(CommandError)?;
    //     let handler_sc_metadata = ep_parser.handler.unwrap();
    //     let handler_sc_parser = handler_sc_metadata.to_source_code_parser(None);
    //
    //     CodeEditor::open_file_in_editor(
    //         &handler_sc_parser.path,
    //         Some(handler_sc_parser.start_line_index),
    //     )?;
    //
    //     let handler_content = handler_sc_parser.get_source_code_content();
    //
    //     let handler_content_lines = handler_content.lines().collect::<Vec<_>>();
    //     if !suggested_entry_point_cache.init_program_accounts.is_empty() {
    //         for init_program_account in suggested_entry_point_cache.init_program_accounts {
    //             println!("Initializing: {}", init_program_account.bright_green());
    //             let solana_account_parser =
    //                 SolanaAccountParser::new_from_struct_name_and_solana_account_type(
    //                     init_program_account,
    //                     SolanaAccountType::ProgramStateAccount,
    //                 )
    //                 .change_context(CommandError)?;
    //             for account in solana_account_parser.accounts {
    //                 let prompt_text = format!(
    //                     "Is the {}[{}] value assigned on this handler?:",
    //                     account.account_name, account.account_type
    //                 );
    //                 let is_assigned = BatDialoguer::select_yes_or_no(prompt_text)?;
    //                 if !is_assigned {
    //                     continue;
    //                 }
    //                 let prompt_text = format!(
    //                     "Select the lines with the value for {}[{}] with the space bar",
    //                     account.account_name.bright_green(),
    //                     account.account_type.bright_yellow()
    //                 );
    //                 let selection = BatDialoguer::multiselect(
    //                     prompt_text,
    //                     handler_content_lines.clone(),
    //                     None,
    //                     true,
    //                 )?;
    //                 let parsed_value = if selection.len() == 1 {
    //                     co_commands_functions::get_value_single_line(
    //                         handler_content_lines[selection[0]],
    //                     )
    //                 } else {
    //                     co_commands_functions::get_value_single_line(
    //                         handler_content_lines[selection[0]],
    //                     )
    //                 };
    //                 // let parse_value = println!(
    //                 //     "handler_content_lines[selection]: {}",
    //                 //     handler_content_lines[selection]
    //                 // );
    //             }
    //         }
    //     }
    //     // println!(
    //     //     "init_program_ca_metadata: {:#?}",
    //     //     suggested_entry_point_cache
    //     // );
    //     Ok("".to_string())
    // }
}

mod co_commands_functions {
    use super::*;
    use lazy_regex::regex;
    use regex::Match;

    pub fn get_value_single_line(line: &str) -> CommandResult<String> {
        let inline_assignment_regex = regex!(r#"[\w_.()? ]+= "#);
        match inline_assignment_regex.find(line.trim()) {
            None => {}
            Some(line_match) => {}
        }
        let struct_assignment_regex = regex!(r#"[\w_ ]+: ]"#);
        Ok("".to_string())
    }

    pub fn get_value_multi_line(
        lines_vec: Vec<&str>,
        selection_vec: Vec<usize>,
    ) -> CommandResult<String> {
        // let inline_assignment_regex = regex!(r#"[\w_.()? ]+= "#);
        // match inline_assignment_regex.find(line.trim()) {
        //     None => {}
        //     Some(line_match) => {}
        // }
        // let struct_assignment_regex = regex!(r#"[\w_ ]+: ]"#);
        Ok("".to_string())
    }

    pub async fn prompt_deploy_miro(entry_point_name: String) -> CommandResult<bool> {
        let prompt_text = format!(
            "Do you want to deploy the code-overhaul screenshots to Miro for {} now?",
            entry_point_name.clone().bright_green()
        );
        let deploy_frame = BatDialoguer::select_yes_or_no(prompt_text)?;
        if deploy_frame {
            MiroCommand::CodeOverhaulScreenshots {
                entry_point_name: Some(entry_point_name.to_string()),
            }
            .execute_command()
            .await?
        }
        Ok(deploy_frame)
    }

    pub fn check_code_overhaul_file_completed(
        bat_file: BatFile,
    ) -> error_stack::Result<(), CommandError> {
        let file_data = bat_file.read_content(true).change_context(CommandError)?;
        let file_name = bat_file.get_file_name().change_context(CommandError)?;
        let mut suggestions_vec = vec![];
        let state_changes_checked_placeholders =
            CoderOverhaulTemplatePlaceholders::get_state_changes_checked_placeholders_vec();
        for checked_placeholder in state_changes_checked_placeholders {
            if file_data.contains(&checked_placeholder) {
                suggestions_vec.push(Suggestion(format!(
                    "Delete or update the `{}` place holder from the State changes section",
                    checked_placeholder.clone().bright_red()
                )))
            }
        }
        if !suggestions_vec.is_empty() {
            let mut report = Report::new(CommandError).attach_printable(format!(
                "\"State changes\" section of the {file_name} is not finished"
            ));
            for suggestion in suggestions_vec {
                report = report.attach(suggestion);
            }
            bat_file
                .open_in_editor(false, None)
                .change_context(CommandError)?;
            return Err(report);
        }

        if file_data
            .contains(&CoderOverhaulTemplatePlaceholders::CompleteWithNotes.to_placeholder())
        {
            let user_decided_to_continue = batbelt::bat_dialoguer::select_yes_or_no(
                "Notes section not completed, do you want to proceed anyway?",
            )
            .change_context(CommandError)?;
            if !user_decided_to_continue {
                return Err(Report::new(CommandError).attach_printable("Aborted by the user"));
            }
        }

        if file_data.contains(
            &CoderOverhaulTemplatePlaceholders::CompleteWithSignerDescription.to_placeholder(),
        ) {
            bat_file
                .open_in_editor(false, None)
                .change_context(CommandError)?;
            return Err(Report::new(CommandError)
                .attach_printable(format!(
                    "Please complete the \"Signers\" section of the {file_name} file"
                ))
                .attach(Suggestion(format!(
                    "Delete {} from the Signers section",
                    CoderOverhaulTemplatePlaceholders::CompleteWithSignerDescription
                        .to_placeholder()
                ))));
        }

        if file_data
            .contains(&CoderOverhaulTemplatePlaceholders::NoValidationsDetected.to_placeholder())
        {
            let user_decided_to_continue = BatDialoguer::select_yes_or_no(format!(
                "{} section not completed, do you want to proceed anyway?",
                "Validations".green()
            ))
            .change_context(CommandError)?;
            if !user_decided_to_continue {
                return Err(Report::new(CommandError).attach_printable("Aborted by the user"));
            }
        }

        if file_data
            .contains(&CoderOverhaulTemplatePlaceholders::CompleteWithMiroFrameUrl.to_placeholder())
        {
            let user_decided_to_continue = batbelt::bat_dialoguer::select_yes_or_no(
                "Miro frame url section is not completed, do you want to proceed anyway?",
            )
            .change_context(CommandError)?;
            if !user_decided_to_continue {
                return Err(Report::new(CommandError).attach_printable("Aborted by the user"));
            }
        }
        Ok(())
    }

    pub fn extract_section_content(
        co_file_content: &str,
        section_header: &str,
        next_section_header: &str,
    ) -> CommandResult<String> {
        let section_content_regex = Regex::new(&format!(
            r#"({})[\s\S]+({})"#,
            section_header, next_section_header
        ))
        .into_report()
        .change_context(CommandError)?;
        log::debug!("{co_file_content}");
        log::debug!("{section_header}");
        log::debug!("{next_section_header}");
        let section_content = section_content_regex
            .find(&co_file_content)
            .ok_or(CommandError)
            .into_report()?
            .as_str()
            .to_string()
            .replace(section_header, &section_header.replace("#", "##"))
            .trim_end_matches(next_section_header)
            .trim()
            .to_string();
        Ok(section_content)
    }
}