repolens 2.0.1

A CLI tool to audit and prepare repositories for open source or enterprise standards
Documentation
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
//! Apply command - Apply planned changes to the repository

use colored::Colorize;
use console::Term;
use dialoguer::{Confirm, MultiSelect};
use indicatif::{ProgressBar, ProgressStyle};
use similar::{ChangeTag, TextDiff};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Duration;

use super::ApplyArgs;
use crate::actions::executor::ActionExecutor;
use crate::actions::git;
use crate::actions::plan::{Action, ActionOperation, ActionPlan};
use crate::actions::planner::ActionPlanner;
use crate::config::Config;
use crate::error::RepoLensError;
use crate::exit_codes;
use crate::providers::github::GitHubProvider;
use crate::rules::engine::RulesEngine;
use crate::rules::results::{AuditResults, Severity};
use crate::scanner::Scanner;

/// Display a visual summary of the actions to be applied
fn display_action_summary(actions: &[Action], audit_results: &AuditResults) {
    let term_width = Term::stdout().size().1 as usize;
    let separator = "=".repeat(term_width.min(80));

    println!();
    println!("{}", separator.dimmed());
    println!(
        "{}",
        "                     ACTION SUMMARY                     "
            .bold()
            .cyan()
    );
    println!("{}", separator.dimmed());
    println!();

    // Group actions by category
    let mut categories: HashMap<&str, Vec<&Action>> = HashMap::new();
    for action in actions {
        categories
            .entry(action.category())
            .or_default()
            .push(action);
    }

    // Display actions grouped by category
    for (category, category_actions) in &categories {
        let category_icon = get_category_icon(category);
        println!(
            "{} {} {}",
            category_icon,
            category.to_uppercase().bold(),
            format!(
                "({} action{})",
                category_actions.len(),
                if category_actions.len() > 1 { "s" } else { "" }
            )
            .dimmed()
        );

        for action in category_actions {
            println!("    {} {}", "+".green(), action.description());
            for detail in action.details() {
                println!("      {} {}", "-".dimmed(), detail.dimmed());
            }
        }
        println!();
    }

    // Display warning issue creation preview
    let mut warning_categories: std::collections::BTreeMap<String, usize> =
        std::collections::BTreeMap::new();
    for finding in audit_results.findings_by_severity(Severity::Warning) {
        *warning_categories
            .entry(finding.category.clone())
            .or_insert(0) += 1;
    }

    if !warning_categories.is_empty() {
        let total_warnings: usize = warning_categories.values().sum();
        println!(
            "[I] {} {}",
            "ISSUES".bold(),
            format!(
                "({} warning{} → GitHub issues)",
                total_warnings,
                if total_warnings > 1 { "s" } else { "" }
            )
            .dimmed()
        );
        for (category, count) in &warning_categories {
            println!(
                "    {} {} ({} warning{})",
                "+".green(),
                category,
                count,
                if *count > 1 { "s" } else { "" }
            );
        }
        println!();
    }

    let total = actions.len()
        + if warning_categories.is_empty() {
            0
        } else {
            warning_categories.len()
        };
    println!("{}", separator.dimmed());
    println!(
        "  {} {} action{} to apply",
        "Total:".bold(),
        total.to_string().cyan().bold(),
        if total > 1 { "s" } else { "" }
    );
    println!("{}", separator.dimmed());
    println!();
}

/// Get an icon for each category
fn get_category_icon(category: &str) -> &'static str {
    match category.to_lowercase().as_str() {
        "gitignore" | "files" => "[F]",
        "security" => "[S]",
        "github" => "[G]",
        "docs" | "documentation" => "[D]",
        "workflows" => "[W]",
        "quality" => "[Q]",
        _ => "[*]",
    }
}

/// Display a diff between old and new content
fn display_diff(old_content: &str, new_content: &str, filename: &str) {
    let term_width = Term::stdout().size().1 as usize;
    let separator = "-".repeat(term_width.min(60));

    println!();
    println!("  {} {}", "Diff for:".dimmed(), filename.cyan().bold());
    println!("  {}", separator.dimmed());

    let diff = TextDiff::from_lines(old_content, new_content);

    for change in diff.iter_all_changes() {
        let line = change.value().trim_end_matches('\n');
        match change.tag() {
            ChangeTag::Delete => {
                println!("  {} {}", "-".red().bold(), line.red());
            }
            ChangeTag::Insert => {
                println!("  {} {}", "+".green().bold(), line.green());
            }
            ChangeTag::Equal => {
                println!("  {} {}", " ".dimmed(), line.dimmed());
            }
        }
    }

    println!("  {}", separator.dimmed());
    println!();
}

/// Preview what an action will change (before/after diff)
fn preview_action_diff(action: &Action) {
    match action.operation() {
        ActionOperation::UpdateGitignore { entries } => {
            let gitignore_path = Path::new(".gitignore");
            let old_content = if gitignore_path.exists() {
                fs::read_to_string(gitignore_path).unwrap_or_default()
            } else {
                String::new()
            };

            let mut new_content = old_content.clone();
            if !new_content.is_empty() && !new_content.ends_with('\n') {
                new_content.push('\n');
            }
            if !old_content.is_empty() {
                new_content.push_str("\n# Added by repolens\n");
            }
            for entry in entries {
                // Check if entry already exists
                if !old_content.lines().any(|l| l.trim() == entry.trim()) {
                    new_content.push_str(entry);
                    new_content.push('\n');
                }
            }

            display_diff(&old_content, &new_content, ".gitignore");
        }
        ActionOperation::CreateFile {
            path,
            template,
            variables,
        } => {
            let file_path = Path::new(path);
            let old_content = if file_path.exists() {
                fs::read_to_string(file_path).unwrap_or_default()
            } else {
                "(file does not exist)".to_string()
            };

            // Generate a preview of the new content
            let new_content = format!(
                "(New file from template: {})\n\n{}",
                template,
                if variables.is_empty() {
                    "(No variable substitutions)".to_string()
                } else {
                    variables
                        .iter()
                        .map(|(k, v)| format!("  {} = {}", k, v))
                        .collect::<Vec<_>>()
                        .join("\n")
                }
            );

            display_diff(&old_content, &new_content, path);
        }
        ActionOperation::ConfigureBranchProtection { branch, settings } => {
            let old_content = "(Current branch protection settings)".to_string();
            let new_content = format!(
                "Branch: {}\n\
                 Required approvals: {}\n\
                 Require status checks: {}\n\
                 Require conversation resolution: {}\n\
                 Require linear history: {}\n\
                 Block force push: {}\n\
                 Block deletions: {}\n\
                 Enforce for admins: {}\n\
                 Require signed commits: {}",
                branch,
                settings.required_approvals,
                settings.require_status_checks,
                settings.require_conversation_resolution,
                settings.require_linear_history,
                settings.block_force_push,
                settings.block_deletions,
                settings.enforce_admins,
                settings.require_signed_commits
            );

            display_diff(
                &old_content,
                &new_content,
                &format!("Branch protection: {}", branch),
            );
        }
        ActionOperation::UpdateGitHubSettings { settings } => {
            let old_content = "(Current repository settings)".to_string();
            let mut changes = Vec::new();
            if let Some(v) = settings.enable_discussions {
                changes.push(format!("Enable discussions: {}", v));
            }
            if let Some(v) = settings.enable_issues {
                changes.push(format!("Enable issues: {}", v));
            }
            if let Some(v) = settings.enable_wiki {
                changes.push(format!("Enable wiki: {}", v));
            }
            if let Some(v) = settings.enable_vulnerability_alerts {
                changes.push(format!("Enable vulnerability alerts: {}", v));
            }
            if let Some(v) = settings.enable_automated_security_fixes {
                changes.push(format!("Enable automated security fixes: {}", v));
            }
            let new_content = if changes.is_empty() {
                "(No changes)".to_string()
            } else {
                changes.join("\n")
            };

            display_diff(&old_content, &new_content, "GitHub repository settings");
        }
    }
}

/// Run interactive mode for action selection
fn run_interactive_selection(actions: &[Action]) -> Result<Vec<usize>, RepoLensError> {
    let items: Vec<String> = actions
        .iter()
        .map(|a| format!("[{}] {}", a.category(), a.description()))
        .collect();

    // Default all items to selected
    let defaults: Vec<bool> = vec![true; items.len()];

    println!();
    println!(
        "{}",
        "Select the actions to apply (Space to toggle, Enter to confirm):".bold()
    );
    println!();

    let selections = MultiSelect::new()
        .items(&items)
        .defaults(&defaults)
        .interact()
        .map_err(|e| {
            RepoLensError::Action(crate::error::ActionError::ExecutionFailed {
                message: format!("Failed to get user selection: {}", e),
            })
        })?;

    Ok(selections)
}

/// Show diff preview for selected actions
fn show_diff_previews(actions: &[Action], selected_indices: &[usize]) -> Result<(), RepoLensError> {
    println!();
    println!("{}", "Preview of changes:".bold().cyan());

    for &idx in selected_indices {
        if let Some(action) = actions.get(idx) {
            preview_action_diff(action);
        }
    }

    Ok(())
}

/// Create a progress bar for multiple actions
fn create_progress_bar(total: u64) -> ProgressBar {
    let pb = ProgressBar::new(total);
    pb.set_style(
        ProgressStyle::default_bar()
            .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}")
            .unwrap()
            .progress_chars("#>-"),
    );
    pb.enable_steady_tick(Duration::from_millis(100));
    pb
}

/// Create a spinner for individual actions
fn create_spinner(message: &str) -> ProgressBar {
    let sp = ProgressBar::new_spinner();
    sp.set_style(
        ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg}")
            .unwrap(),
    );
    sp.set_message(message.to_string());
    sp.enable_steady_tick(Duration::from_millis(80));
    sp
}

pub async fn execute(args: ApplyArgs) -> Result<i32, RepoLensError> {
    // Load configuration
    let config = Config::load_or_default()?;

    // Initialize scanner
    let scanner = Scanner::new(PathBuf::from("."));

    // Run the rules engine to get current state
    let engine = RulesEngine::new(config.clone());
    let audit_results = engine.run(&scanner).await?;

    // Generate action plan
    let planner = ActionPlanner::new(config.clone());
    let mut action_plan = planner.create_plan(&audit_results).await?;

    // Apply filters if specified
    if let Some(only) = &args.only {
        action_plan.filter_only(only);
    }
    if let Some(skip) = &args.skip {
        action_plan.filter_skip(skip);
    }

    // Check if there are any actions or warning issues to create
    let has_warnings = audit_results.has_warnings();
    if action_plan.is_empty() && (!has_warnings || args.no_issues) {
        println!("{}", "No actions to perform.".green());
        return Ok(exit_codes::SUCCESS);
    }

    // If only warning issues to create (no plan actions), handle that directly
    if action_plan.is_empty() && has_warnings && !args.no_issues {
        create_warning_issues(&audit_results);
        return Ok(exit_codes::SUCCESS);
    }

    // Display visual summary
    display_action_summary(action_plan.actions(), &audit_results);

    // Dry run mode
    if args.dry_run {
        println!("{}", "Dry run mode - no changes made.".yellow());

        // Show what would happen
        println!();
        println!("{}", "Preview of changes that would be made:".bold());
        for action in action_plan.actions() {
            preview_action_diff(action);
        }

        return Ok(exit_codes::SUCCESS);
    }

    // Determine which actions to execute based on mode
    let actions_to_execute: Vec<&Action>;

    if args.interactive {
        // Interactive mode: let user select actions
        let selected_indices = run_interactive_selection(action_plan.actions())?;

        if selected_indices.is_empty() {
            println!("{}", "No actions selected.".yellow());
            return Ok(exit_codes::SUCCESS);
        }

        // Show diff previews for selected actions
        show_diff_previews(action_plan.actions(), &selected_indices)?;

        // Confirm after seeing diffs
        let confirm = Confirm::new()
            .with_prompt(format!(
                "Apply these {} selected action{}?",
                selected_indices.len(),
                if selected_indices.len() > 1 { "s" } else { "" }
            ))
            .default(false)
            .interact()
            .map_err(|e| {
                RepoLensError::Action(crate::error::ActionError::ExecutionFailed {
                    message: format!("Failed to get user input: {}", e),
                })
            })?;

        if !confirm {
            println!("{}", "Aborted.".yellow());
            return Ok(exit_codes::SUCCESS);
        }

        actions_to_execute = selected_indices
            .iter()
            .filter_map(|&i| action_plan.actions().get(i))
            .collect();
    } else if args.yes {
        // Auto-accept mode: execute all actions without confirmation
        actions_to_execute = action_plan.actions().iter().collect();
    } else {
        // Standard mode: confirm before execution
        let confirm = Confirm::new()
            .with_prompt("Apply these changes?")
            .default(false)
            .interact()
            .map_err(|e| {
                RepoLensError::Action(crate::error::ActionError::ExecutionFailed {
                    message: format!("Failed to get user input: {}", e),
                })
            })?;

        if !confirm {
            println!("{}", "Aborted.".yellow());
            return Ok(exit_codes::SUCCESS);
        }

        actions_to_execute = action_plan.actions().iter().collect();
    }

    // Create a filtered action plan with only selected actions
    let mut filtered_plan = ActionPlan::new();
    for action in actions_to_execute {
        filtered_plan.add(action.clone());
    }

    // Execute actions with progress bar
    let executor = ActionExecutor::new(config);

    println!();
    println!("{}", "Executing actions...".bold());
    println!();

    let progress_bar = create_progress_bar(filtered_plan.actions().len() as u64);
    let mut results = Vec::new();

    for action in filtered_plan.actions() {
        let spinner = create_spinner(action.description());

        // Execute the single action
        let single_plan = {
            let mut p = ActionPlan::new();
            p.add(action.clone());
            p
        };

        let action_results = executor.execute(&single_plan).await?;
        spinner.finish_and_clear();

        if let Some(result) = action_results.into_iter().next() {
            if result.success {
                progress_bar.println(format!(
                    "  {} {}",
                    "[OK]".green().bold(),
                    action.description()
                ));
            } else {
                progress_bar.println(format!(
                    "  {} {} - {}",
                    "[FAIL]".red().bold(),
                    action.description(),
                    result.error.as_deref().unwrap_or("Unknown error")
                ));
            }
            results.push(result);
        }

        progress_bar.inc(1);
    }

    progress_bar.finish_and_clear();

    // Display results summary
    println!();
    let mut success_count = 0;
    let mut error_count = 0;

    for result in &results {
        if result.success {
            success_count += 1;
        } else {
            error_count += 1;
        }
    }

    // Final summary
    let term_width = Term::stdout().size().1 as usize;
    let separator = "=".repeat(term_width.min(80));

    println!("{}", separator.dimmed());
    println!(
        "{}",
        "                     EXECUTION SUMMARY                     "
            .bold()
            .cyan()
    );
    println!("{}", separator.dimmed());
    println!();
    println!(
        "  {} {} succeeded",
        "[OK]".green().bold(),
        success_count.to_string().green().bold()
    );
    println!(
        "  {} {} failed",
        "[FAIL]".red().bold(),
        error_count.to_string().red().bold()
    );
    println!();
    println!("{}", separator.dimmed());

    // Create GitHub issues for warning findings (unless --no-issues is set)
    if !args.no_issues {
        create_warning_issues(&audit_results);
    }

    // Handle git operations and PR creation if there were successful file changes
    if success_count > 0 {
        let repo_root = PathBuf::from(".");
        let should_create_pr = if args.no_pr {
            false
        } else {
            args.create_pr
                .unwrap_or_else(|| git::is_git_repository(&repo_root))
        };

        if should_create_pr && git::is_git_repository(&repo_root) {
            if let Err(e) = handle_git_operations(&repo_root, &filtered_plan, &results).await {
                eprintln!(
                    "{} {}",
                    "[WARN]".yellow().bold(),
                    format!("Failed to create PR: {}", e).yellow()
                );
                // Don't fail the whole command if PR creation fails
            }
        }
    }

    // Determine exit code based on results:
    // - All actions succeeded: SUCCESS (0)
    // - Some actions failed: WARNINGS (2)
    // - All actions failed: CRITICAL_ISSUES (1)
    let exit_code = if error_count == 0 {
        exit_codes::SUCCESS
    } else if success_count == 0 {
        // All actions failed
        exit_codes::CRITICAL_ISSUES
    } else {
        // Some actions failed (partial success)
        exit_codes::WARNINGS
    };

    Ok(exit_code)
}

/// Create GitHub issues for warning findings grouped by category
fn create_warning_issues(audit_results: &AuditResults) {
    // Check if GitHub CLI is available
    if !GitHubProvider::is_available() {
        println!(
            "{} {}",
            "[WARN]".yellow().bold(),
            "GitHub CLI not available, skipping issue creation.".yellow()
        );
        return;
    }

    let github_provider = match GitHubProvider::new() {
        Ok(provider) => provider,
        Err(e) => {
            println!(
                "{} {}",
                "[WARN]".yellow().bold(),
                format!("Unable to create issues: {}. Skipping.", e).yellow()
            );
            return;
        }
    };

    // Group warnings by category
    let mut warning_categories: HashMap<String, Vec<&crate::rules::results::Finding>> =
        HashMap::new();
    for finding in audit_results.findings_by_severity(Severity::Warning) {
        warning_categories
            .entry(finding.category.clone())
            .or_default()
            .push(finding);
    }

    if warning_categories.is_empty() {
        return;
    }

    let term_width = Term::stdout().size().1 as usize;
    let separator = "=".repeat(term_width.min(50));

    println!();
    println!("{}", separator.dimmed());
    println!("{}", "  ISSUES CREATED".bold().cyan());
    println!();

    let mut sorted_categories: Vec<_> = warning_categories.into_iter().collect();
    sorted_categories.sort_by(|a, b| a.0.cmp(&b.0));

    for (category, findings) in &sorted_categories {
        let count = findings.len();

        let title = format!("[RepoLens] {} warning(s) -- {}", count, category);

        // Build markdown table body
        let mut body =
            String::from("| Rule ID | Message | Location |\n|---------|---------|----------|\n");
        for finding in findings {
            let location = finding.location.as_deref().unwrap_or("-");
            body.push_str(&format!(
                "| {} | {} | {} |\n",
                finding.rule_id, finding.message, location
            ));
        }

        let labels = vec!["repolens-audit"];

        match github_provider.create_issue(&title, &body, &labels) {
            Ok(url) => {
                println!(
                    "  {} {} ({} warning{}) -> {}",
                    "[OK]".green().bold(),
                    category,
                    count,
                    if count > 1 { "s" } else { "" },
                    url.cyan()
                );
            }
            Err(e) => {
                println!(
                    "  {} {} -- {}",
                    "[FAIL]".red().bold(),
                    category,
                    format!("Failed to create issue: {}", e).red()
                );
            }
        }
    }

    println!();
    println!("{}", separator.dimmed());
}

/// Handle git operations: create branch, commit, push, and create PR
async fn handle_git_operations(
    repo_root: &Path,
    action_plan: &ActionPlan,
    results: &[crate::actions::executor::ActionResult],
) -> Result<(), RepoLensError> {
    // Check if there are any file-related changes by checking the action plan
    let has_file_changes = action_plan.actions().iter().any(|action| {
        matches!(
            action.operation(),
            ActionOperation::CreateFile { .. } | ActionOperation::UpdateGitignore { .. }
        )
    });

    if !has_file_changes {
        // Only file changes trigger PR creation
        return Ok(());
    }

    // Check if there are actual changes to commit
    if !git::has_changes(repo_root)? {
        println!(
            "{}",
            "No file changes detected, skipping PR creation.".dimmed()
        );
        return Ok(());
    }

    println!();
    println!("{}", "Creating branch and preparing PR...".dimmed());

    // Create new branch
    let branch_name = git::create_branch(repo_root)?;
    println!(
        "  {} Branch created: {}",
        "[OK]".green().bold(),
        branch_name.cyan()
    );

    // Collect file paths from actions that modify files
    let file_paths: Vec<String> = action_plan
        .actions()
        .iter()
        .filter_map(|action| match action.operation() {
            ActionOperation::CreateFile { path, .. } => Some(path.clone()),
            ActionOperation::UpdateGitignore { .. } => Some(".gitignore".to_string()),
            _ => None,
        })
        .collect();

    // Stage only the specific files changed by actions
    git::stage_files(repo_root, &file_paths)?;
    println!("  {} Changes staged", "[OK]".green().bold());

    // Create commit message
    let commit_message = format!(
        "chore: apply RepoLens fixes\n\n{}\n\nActions applied:\n{}",
        "This commit contains automatic fixes applied by RepoLens.",
        action_plan
            .actions()
            .iter()
            .map(|a| format!("- {}", a.description()))
            .collect::<Vec<_>>()
            .join("\n")
    );

    // Create commit
    git::create_commit(repo_root, &commit_message)?;
    println!("  {} Commit created", "[OK]".green().bold());

    // Push branch
    git::push_branch(repo_root, &branch_name)?;
    println!("  {} Branch pushed to origin", "[OK]".green().bold());

    // Create PR - check if GitHub CLI is available first
    if !GitHubProvider::is_available() {
        println!(
            "{} {}",
            "[WARN]".yellow().bold(),
            "GitHub CLI not available, PR not created. Changes are in the local branch.".yellow()
        );
        return Ok(());
    }

    let github_provider = match GitHubProvider::new() {
        Ok(provider) => provider,
        Err(e) => {
            println!(
                "{} {}",
                "[WARN]".yellow().bold(),
                format!(
                    "Unable to create PR: {}. Changes are in the local branch.",
                    e
                )
                .yellow()
            );
            return Ok(());
        }
    };

    let default_branch = git::get_default_branch(repo_root).unwrap_or_else(|| "main".to_string());

    let pr_title = format!(
        "RepoLens: Automatic fixes ({})",
        chrono::Local::now().format("%Y-%m-%d %H:%M")
    );

    let pr_body = format!(
        "# RepoLens Automatic Fixes\n\n\
        This PR contains automatic fixes applied by RepoLens.\n\n\
        ## Actions Applied\n\n\
        {}\n\n\
        ## Details\n\n\
        {}",
        action_plan
            .actions()
            .iter()
            .map(|a| format!("- **{}**: {}", a.category(), a.description()))
            .collect::<Vec<_>>()
            .join("\n"),
        results
            .iter()
            .filter(|r| r.success)
            .map(|r| format!("- [OK] {}", r.action_name))
            .collect::<Vec<_>>()
            .join("\n")
    );

    let pr_url = github_provider.create_pull_request(
        &pr_title,
        &pr_body,
        &branch_name,
        Some(&default_branch),
    )?;

    println!();
    println!(
        "{} {}",
        "[OK]".green().bold(),
        format!("Pull Request created: {}", pr_url.cyan()).green()
    );

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::actions::plan::{
        Action, ActionOperation, BranchProtectionSettings, GitHubRepoSettings,
    };
    use std::collections::HashMap;

    #[test]
    fn test_get_category_icon() {
        assert_eq!(get_category_icon("gitignore"), "[F]");
        assert_eq!(get_category_icon("files"), "[F]");
        assert_eq!(get_category_icon("security"), "[S]");
        assert_eq!(get_category_icon("github"), "[G]");
        assert_eq!(get_category_icon("docs"), "[D]");
        assert_eq!(get_category_icon("documentation"), "[D]");
        assert_eq!(get_category_icon("workflows"), "[W]");
        assert_eq!(get_category_icon("quality"), "[Q]");
        assert_eq!(get_category_icon("unknown"), "[*]");
    }

    #[test]
    fn test_display_diff_additions() {
        // This test verifies that the diff display function runs without panic
        // Visual output is not directly testable, but we ensure no errors occur
        let old = "line1\nline2\n";
        let new = "line1\nline2\nline3\n";
        display_diff(old, new, "test.txt");
    }

    #[test]
    fn test_display_diff_deletions() {
        let old = "line1\nline2\nline3\n";
        let new = "line1\nline3\n";
        display_diff(old, new, "test.txt");
    }

    #[test]
    fn test_display_diff_modifications() {
        let old = "line1\nold_line\nline3\n";
        let new = "line1\nnew_line\nline3\n";
        display_diff(old, new, "test.txt");
    }

    #[test]
    fn test_display_action_summary() {
        let actions = vec![
            Action::new(
                "test-1",
                "gitignore",
                "Update .gitignore",
                ActionOperation::UpdateGitignore {
                    entries: vec![".env".to_string()],
                },
            ),
            Action::new(
                "test-2",
                "files",
                "Create README.md",
                ActionOperation::CreateFile {
                    path: "README.md".to_string(),
                    template: "README".to_string(),
                    variables: HashMap::new(),
                },
            ),
        ];

        let results = AuditResults::new("test-repo", "opensource");

        // This test verifies the summary display function runs without panic
        display_action_summary(&actions, &results);
    }

    #[test]
    fn test_preview_action_diff_gitignore() {
        let action = Action::new(
            "test-gitignore",
            "gitignore",
            "Update .gitignore",
            ActionOperation::UpdateGitignore {
                entries: vec![".env".to_string(), "*.key".to_string()],
            },
        );

        // This test verifies the preview function runs without panic
        preview_action_diff(&action);
    }

    #[test]
    fn test_preview_action_diff_create_file() {
        let mut variables = HashMap::new();
        variables.insert("author".to_string(), "Test Author".to_string());

        let action = Action::new(
            "test-file",
            "files",
            "Create LICENSE",
            ActionOperation::CreateFile {
                path: "LICENSE".to_string(),
                template: "LICENSE/MIT".to_string(),
                variables,
            },
        );

        preview_action_diff(&action);
    }

    #[test]
    fn test_preview_action_diff_branch_protection() {
        let action = Action::new(
            "test-branch",
            "security",
            "Configure branch protection",
            ActionOperation::ConfigureBranchProtection {
                branch: "main".to_string(),
                settings: BranchProtectionSettings::default(),
            },
        );

        preview_action_diff(&action);
    }

    #[test]
    fn test_preview_action_diff_github_settings() {
        let action = Action::new(
            "test-github",
            "github",
            "Update GitHub settings",
            ActionOperation::UpdateGitHubSettings {
                settings: GitHubRepoSettings {
                    enable_discussions: Some(true),
                    enable_issues: Some(true),
                    enable_wiki: Some(false),
                    enable_vulnerability_alerts: Some(true),
                    enable_automated_security_fixes: Some(true),
                },
            },
        );

        preview_action_diff(&action);
    }

    #[test]
    fn test_create_progress_bar() {
        let pb = create_progress_bar(10);
        pb.inc(1);
        pb.finish_and_clear();
    }

    #[test]
    fn test_create_spinner() {
        let sp = create_spinner("Testing...");
        sp.finish_and_clear();
    }
}