repolens 2.0.0

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
996
997
998
999
1000
1001
1002
1003
//! Action planner - Creates action plans based on audit results
//!
//! This module provides functionality to generate action plans from audit results.
//! It analyzes findings and creates appropriate actions to fix issues.

use std::collections::HashMap;

use crate::config::Config;
use crate::providers::github::GitHubProvider;
use crate::rules::results::AuditResults;

use super::plan::{
    Action, ActionOperation, ActionPlan, BranchProtectionSettings, GitHubRepoSettings,
};

/// Parameters for planning file creation
struct FileCreationParams<'a> {
    rule_id: &'a str,
    file_path: &'a str,
    template: &'a str,
    action_id: &'a str,
    action_description: &'a str,
    detail: Option<&'a str>,
}

/// Creates action plans based on audit results and configuration
///
/// The `ActionPlanner` analyzes audit findings and generates a plan of actions
/// to fix detected issues. Actions can include:
/// - Creating missing files (LICENSE, CONTRIBUTING.md, etc.)
/// - Updating .gitignore
/// - Configuring branch protection
/// - Updating GitHub repository settings
pub struct ActionPlanner {
    /// Configuration for action planning
    config: Config,
}

impl ActionPlanner {
    /// Create a new action planner with the given configuration
    ///
    /// # Arguments
    ///
    /// * `config` - The configuration that determines which actions to plan
    ///
    /// # Returns
    ///
    /// A new `ActionPlanner` instance
    pub fn new(config: Config) -> Self {
        Self { config }
    }

    /// Create an action plan based on audit results
    ///
    /// Analyzes the audit results and generates actions to fix detected issues.
    /// Only actions enabled in the configuration will be included.
    ///
    /// # Arguments
    ///
    /// * `results` - The audit results to analyze
    ///
    /// # Returns
    ///
    /// An `ActionPlan` containing all planned actions
    pub async fn create_plan(
        &self,
        results: &AuditResults,
    ) -> Result<ActionPlan, crate::error::RepoLensError> {
        let mut plan = ActionPlan::new();

        // Plan gitignore updates
        if self.config.actions.gitignore {
            if let Some(action) = self.plan_gitignore_update(results) {
                plan.add(action);
            }
        }

        // Plan license creation
        if self.config.actions.license.enabled {
            if let Some(action) = self.plan_license_creation(results) {
                plan.add(action);
            }
        }

        // Plan CONTRIBUTING creation
        if self.config.actions.contributing {
            if let Some(action) = self.plan_contributing_creation(results) {
                plan.add(action);
            }
        }

        // Plan CODE_OF_CONDUCT creation
        if self.config.actions.code_of_conduct {
            if let Some(action) = self.plan_code_of_conduct_creation(results) {
                plan.add(action);
            }
        }

        // Plan SECURITY.md creation
        if self.config.actions.security_policy {
            if let Some(action) = self.plan_security_creation(results) {
                plan.add(action);
            }
        }

        // Plan branch protection (only if not already configured)
        if self.config.actions.branch_protection.enabled {
            if let Some(action) = self.plan_branch_protection_if_needed().await? {
                plan.add(action);
            }
        }

        // Plan GitHub settings (only if not already configured)
        if let Some(action) = self.plan_github_settings_if_needed().await? {
            plan.add(action);
        }

        Ok(plan)
    }

    /// Plan .gitignore updates based on findings
    ///
    /// Collects entries that should be added to .gitignore from audit findings.
    /// The findings already contain language-specific recommendations based on
    /// detected languages in the repository.
    ///
    /// # Arguments
    ///
    /// * `results` - The audit results
    ///
    /// # Returns
    ///
    /// An `Action` to update .gitignore, or `None` if no updates are needed
    fn plan_gitignore_update(&self, results: &AuditResults) -> Option<Action> {
        // Collect entries that should be added to .gitignore from findings
        // These findings are already language-aware thanks to check_gitignore
        let mut entries = Vec::new();

        // Extract patterns from FILE003 findings
        for finding in results.findings_by_category("files") {
            if finding.rule_id == "FILE003" {
                // Extract the pattern from the message
                // Format: ".gitignore missing recommended entry: <pattern>"
                if let Some(pattern) = finding.message.split("entry: ").nth(1) {
                    entries.push(pattern.trim().to_string());
                }
            }
        }

        if entries.is_empty() {
            return None;
        }

        Some(
            Action::new(
                "gitignore-update",
                "gitignore",
                "Add entries to .gitignore",
                ActionOperation::UpdateGitignore {
                    entries: entries.clone(),
                },
            )
            .with_details(entries),
        )
    }

    /// Plan LICENSE file creation
    ///
    /// Creates a LICENSE file if one is missing and license creation is enabled.
    ///
    /// # Arguments
    ///
    /// * `results` - The audit results
    ///
    /// # Returns
    ///
    /// An `Action` to create LICENSE, or `None` if not needed
    fn plan_license_creation(&self, results: &AuditResults) -> Option<Action> {
        // Check if LICENSE is missing
        let needs_license = results
            .findings_by_category("docs")
            .any(|f| f.rule_id == "DOC004");

        if !needs_license {
            return None;
        }

        let license_type = &self.config.actions.license.license_type;
        let mut variables = HashMap::new();

        if let Some(author) = &self.config.actions.license.author {
            variables.insert("author".to_string(), author.clone());
        }

        let year = self
            .config
            .actions
            .license
            .year
            .clone()
            .unwrap_or_else(|| chrono::Utc::now().format("%Y").to_string());
        variables.insert("year".to_string(), year);

        Some(
            Action::new(
                "license-create",
                "file",
                "Create LICENSE file",
                ActionOperation::CreateFile {
                    path: "LICENSE".to_string(),
                    template: format!("LICENSE/{}", license_type),
                    variables,
                },
            )
            .with_detail(format!("License type: {}", license_type)),
        )
    }

    /// Generic helper to plan file creation from template
    ///
    /// # Arguments
    ///
    /// * `results` - The audit results
    /// * `params` - Parameters for file creation
    ///
    /// # Returns
    ///
    /// An `Action` if the file needs to be created, `None` otherwise
    fn plan_file_creation(
        &self,
        results: &AuditResults,
        params: FileCreationParams<'_>,
    ) -> Option<Action> {
        let needs_file = results
            .findings_by_category("docs")
            .any(|f| f.rule_id == params.rule_id);

        if !needs_file {
            return None;
        }

        let mut action = Action::new(
            params.action_id,
            "file",
            params.action_description,
            ActionOperation::CreateFile {
                path: params.file_path.to_string(),
                template: params.template.to_string(),
                variables: HashMap::new(),
            },
        );

        if let Some(detail) = params.detail {
            action = action.with_detail(detail);
        }

        Some(action)
    }

    fn plan_contributing_creation(&self, results: &AuditResults) -> Option<Action> {
        self.plan_file_creation(
            results,
            FileCreationParams {
                rule_id: "DOC005",
                file_path: "CONTRIBUTING.md",
                template: "CONTRIBUTING.md",
                action_id: "contributing-create",
                action_description: "Create CONTRIBUTING.md",
                detail: None,
            },
        )
    }

    fn plan_code_of_conduct_creation(&self, results: &AuditResults) -> Option<Action> {
        self.plan_file_creation(
            results,
            FileCreationParams {
                rule_id: "DOC006",
                file_path: "CODE_OF_CONDUCT.md",
                template: "CODE_OF_CONDUCT.md",
                action_id: "coc-create",
                action_description: "Create CODE_OF_CONDUCT.md",
                detail: Some("Using Contributor Covenant template"),
            },
        )
    }

    fn plan_security_creation(&self, results: &AuditResults) -> Option<Action> {
        self.plan_file_creation(
            results,
            FileCreationParams {
                rule_id: "DOC007",
                file_path: "SECURITY.md",
                template: "SECURITY.md",
                action_id: "security-create",
                action_description: "Create SECURITY.md",
                detail: None,
            },
        )
    }

    /// Plan branch protection configuration if needed
    ///
    /// Checks the current branch protection status and creates an action only if
    /// the current settings don't match the desired configuration.
    ///
    /// # Returns
    ///
    /// An `Action` to configure branch protection, or `None` if already configured correctly
    async fn plan_branch_protection_if_needed(
        &self,
    ) -> Result<Option<Action>, crate::error::RepoLensError> {
        let bp = &self.config.actions.branch_protection;

        // Try to get current branch protection status
        let provider = match GitHubProvider::new() {
            Ok(p) => p,
            Err(_) => {
                // If GitHub CLI is not available, still plan the action
                // (it will fail gracefully during apply)
                return Ok(Some(self.create_branch_protection_action()));
            }
        };

        let current_protection = match provider.get_branch_protection(&bp.branch) {
            Ok(Some(protection)) => protection,
            Ok(None) => {
                // No protection exists, plan the action
                return Ok(Some(self.create_branch_protection_action()));
            }
            Err(_) => {
                // Error fetching protection, plan the action to be safe
                return Ok(Some(self.create_branch_protection_action()));
            }
        };

        // Check if current protection matches desired settings
        let needs_update = {
            // Check required approvals
            let current_approvals = current_protection
                .required_pull_request_reviews
                .as_ref()
                .map(|r| r.required_approving_review_count)
                .unwrap_or(0);
            let needs_approvals = current_approvals != bp.required_approvals;

            // Check status checks
            let has_status_checks = current_protection.required_status_checks.is_some();
            let needs_status_checks = has_status_checks != bp.require_status_checks;

            // Check force push blocking
            // If block_force_push is true, we need allow_force_pushes.enabled to be false
            // If block_force_push is false, we need allow_force_pushes.enabled to be true
            let allows_force_push = current_protection
                .allow_force_pushes
                .as_ref()
                .map(|a| a.enabled)
                .unwrap_or(true);
            // We need to update if: (block_force_push && allows_force_push) || (!block_force_push && !allows_force_push)
            // Which simplifies to: allows_force_push == block_force_push
            let needs_force_push_block = allows_force_push == bp.block_force_push;

            needs_approvals || needs_status_checks || needs_force_push_block
        };

        if needs_update {
            Ok(Some(self.create_branch_protection_action()))
        } else {
            Ok(None)
        }
    }

    /// Create a branch protection action
    fn create_branch_protection_action(&self) -> Action {
        let bp = &self.config.actions.branch_protection;

        let settings = BranchProtectionSettings {
            required_approvals: bp.required_approvals,
            require_status_checks: bp.require_status_checks,
            require_conversation_resolution: true,
            require_linear_history: true,
            block_force_push: bp.block_force_push,
            block_deletions: true,
            enforce_admins: true,
            require_signed_commits: bp.require_signed_commits,
        };

        let mut details = vec![
            format!("Require PR reviews: {}", bp.required_approvals),
            format!("Require status checks: {}", bp.require_status_checks),
            format!("Block force push: {}", bp.block_force_push),
        ];

        if bp.require_signed_commits {
            details.push("Require signed commits".to_string());
        }

        Action::new(
            "branch-protection",
            "github",
            format!("Enable branch protection on '{}'", bp.branch),
            ActionOperation::ConfigureBranchProtection {
                branch: bp.branch.clone(),
                settings,
            },
        )
        .with_details(details)
    }

    /// Plan GitHub repository settings updates if needed
    ///
    /// Checks the current repository settings and creates an action only if
    /// the current settings don't match the desired configuration.
    ///
    /// # Returns
    ///
    /// An `Action` to update GitHub settings, or `None` if already configured correctly
    async fn plan_github_settings_if_needed(
        &self,
    ) -> Result<Option<Action>, crate::error::RepoLensError> {
        let gs = &self.config.actions.github_settings;

        // Try to get current repository settings
        let provider = match GitHubProvider::new() {
            Ok(p) => p,
            Err(_) => {
                // If GitHub CLI is not available, still plan the action
                // (it will fail gracefully during apply)
                return Ok(Some(self.create_github_settings_action()));
            }
        };

        let current_settings = match provider.get_repo_settings() {
            Ok(settings) => settings,
            Err(_) => {
                // Error fetching settings, plan the action to be safe
                return Ok(Some(self.create_github_settings_action()));
            }
        };

        // Check vulnerability alerts
        let current_vuln_alerts = match provider.has_vulnerability_alerts() {
            Ok(enabled) => enabled,
            Err(e) => {
                tracing::debug!("Could not check vulnerability alerts status: {:?}", e);
                // If we can't check, assume it needs to be set (safer)
                true
            }
        };
        let needs_vuln_alerts = current_vuln_alerts != gs.vulnerability_alerts;

        // Check automated security fixes
        let current_auto_fixes = match provider.has_automated_security_fixes() {
            Ok(enabled) => enabled,
            Err(e) => {
                tracing::debug!("Could not check automated security fixes status: {:?}", e);
                // If we can't check, assume it needs to be set (safer)
                true
            }
        };
        let needs_auto_fixes = current_auto_fixes != gs.automated_security_fixes;

        // Check discussions
        let needs_discussions = current_settings.has_discussions_enabled != gs.discussions;

        // Check issues (if configured)
        let needs_issues = current_settings.has_issues_enabled != gs.issues;

        // Check wiki (if configured)
        let needs_wiki = current_settings.has_wiki_enabled != gs.wiki;

        tracing::debug!(
            "GitHub settings check: discussions={} (current={}, desired={}), vuln_alerts={} (current={}, desired={}), auto_fixes={} (current={}, desired={})",
            needs_discussions,
            current_settings.has_discussions_enabled,
            gs.discussions,
            needs_vuln_alerts,
            current_vuln_alerts,
            gs.vulnerability_alerts,
            needs_auto_fixes,
            current_auto_fixes,
            gs.automated_security_fixes
        );

        // Only create action if something needs to be changed
        if needs_discussions || needs_issues || needs_wiki || needs_vuln_alerts || needs_auto_fixes
        {
            Ok(Some(self.create_github_settings_action_filtered(
                needs_discussions,
                needs_issues,
                needs_wiki,
                needs_vuln_alerts,
                needs_auto_fixes,
            )))
        } else {
            Ok(None)
        }
    }

    /// Create a GitHub settings action with all settings
    fn create_github_settings_action(&self) -> Action {
        let gs = &self.config.actions.github_settings;

        let settings = GitHubRepoSettings {
            enable_discussions: Some(gs.discussions),
            enable_issues: Some(gs.issues),
            enable_wiki: Some(gs.wiki),
            enable_vulnerability_alerts: Some(gs.vulnerability_alerts),
            enable_automated_security_fixes: Some(gs.automated_security_fixes),
        };

        let mut details = Vec::new();

        if gs.discussions {
            details.push("Enable discussions".to_string());
        }
        if gs.vulnerability_alerts {
            details.push("Enable vulnerability alerts".to_string());
        }
        if gs.automated_security_fixes {
            details.push("Enable automated security fixes".to_string());
        }

        Action::new(
            "github-settings",
            "github",
            "Update repository settings",
            ActionOperation::UpdateGitHubSettings { settings },
        )
        .with_details(details)
    }

    /// Create a GitHub settings action with only settings that need to be changed
    fn create_github_settings_action_filtered(
        &self,
        needs_discussions: bool,
        needs_issues: bool,
        needs_wiki: bool,
        needs_vuln_alerts: bool,
        needs_auto_fixes: bool,
    ) -> Action {
        let gs = &self.config.actions.github_settings;

        let settings = GitHubRepoSettings {
            enable_discussions: if needs_discussions {
                Some(gs.discussions)
            } else {
                None
            },
            enable_issues: if needs_issues { Some(gs.issues) } else { None },
            enable_wiki: if needs_wiki { Some(gs.wiki) } else { None },
            enable_vulnerability_alerts: if needs_vuln_alerts {
                Some(gs.vulnerability_alerts)
            } else {
                None
            },
            enable_automated_security_fixes: if needs_auto_fixes {
                Some(gs.automated_security_fixes)
            } else {
                None
            },
        };

        let mut details = Vec::new();

        if needs_discussions && gs.discussions {
            details.push("Enable discussions".to_string());
        }
        if needs_vuln_alerts && gs.vulnerability_alerts {
            details.push("Enable vulnerability alerts".to_string());
        }
        if needs_auto_fixes && gs.automated_security_fixes {
            details.push("Enable automated security fixes".to_string());
        }

        Action::new(
            "github-settings",
            "github",
            "Update repository settings",
            ActionOperation::UpdateGitHubSettings { settings },
        )
        .with_details(details)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::rules::results::{AuditResults, Finding, Severity};

    #[tokio::test]
    async fn test_create_plan_includes_gitignore() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "FILE003",
            "files",
            Severity::Info,
            ".gitignore missing recommended entry: .env",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(!plan.is_empty());
        assert!(plan.actions().iter().any(|a| a.id() == "gitignore-update"));
    }

    #[tokio::test]
    async fn test_create_plan_includes_license() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC004",
            "docs",
            Severity::Critical,
            "LICENSE file is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(plan.actions().iter().any(|a| a.id() == "license-create"));
    }

    #[tokio::test]
    async fn test_create_plan_includes_contributing() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC005",
            "docs",
            Severity::Warning,
            "CONTRIBUTING file is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(
            plan.actions()
                .iter()
                .any(|a| a.id() == "contributing-create")
        );
    }

    #[tokio::test]
    async fn test_create_plan_filters_by_config() {
        let mut config = Config::default();
        config.actions.contributing = false;

        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC005",
            "docs",
            Severity::Warning,
            "CONTRIBUTING file is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        // Should not include contributing because it's disabled in config
        assert!(
            !plan
                .actions()
                .iter()
                .any(|a| a.id() == "contributing-create")
        );
    }

    #[tokio::test]
    async fn test_create_plan_includes_code_of_conduct() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC006",
            "docs",
            Severity::Warning,
            "CODE_OF_CONDUCT file is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(plan.actions().iter().any(|a| a.id() == "coc-create"));
    }

    #[tokio::test]
    async fn test_create_plan_includes_security_policy() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC007",
            "docs",
            Severity::Warning,
            "SECURITY.md is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(plan.actions().iter().any(|a| a.id() == "security-create"));
    }

    #[tokio::test]
    async fn test_create_plan_includes_branch_protection() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);
        let results = AuditResults::new("test-repo", "opensource");

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(plan.actions().iter().any(|a| a.id() == "branch-protection"));
    }

    #[tokio::test]
    async fn test_create_plan_includes_github_settings() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);
        let results = AuditResults::new("test-repo", "opensource");

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(plan.actions().iter().any(|a| a.id() == "github-settings"));
    }

    #[tokio::test]
    async fn test_create_plan_no_gitignore_needed() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);
        let results = AuditResults::new("test-repo", "opensource");

        let plan = planner.create_plan(&results).await.unwrap();

        // No FILE003 findings, so no gitignore update
        assert!(!plan.actions().iter().any(|a| a.id() == "gitignore-update"));
    }

    #[tokio::test]
    async fn test_create_plan_license_with_author_and_year() {
        let mut config = Config::default();
        config.actions.license.author = Some("Test Author".to_string());
        config.actions.license.year = Some("2024".to_string());

        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC004",
            "docs",
            Severity::Critical,
            "LICENSE file is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        assert!(plan.actions().iter().any(|a| a.id() == "license-create"));
    }

    #[tokio::test]
    async fn test_branch_protection_with_signed_commits() {
        let mut config = Config::default();
        config.actions.branch_protection.require_signed_commits = true;

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

        let plan = planner.create_plan(&results).await.unwrap();

        let bp_action = plan
            .actions()
            .iter()
            .find(|a| a.id() == "branch-protection");
        assert!(bp_action.is_some());
    }

    #[tokio::test]
    async fn test_create_plan_multiple_gitignore_entries() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "FILE003",
            "files",
            Severity::Info,
            ".gitignore missing recommended entry: .env",
        ));
        results.add_finding(Finding::new(
            "FILE003",
            "files",
            Severity::Info,
            ".gitignore missing recommended entry: *.log",
        ));
        results.add_finding(Finding::new(
            "FILE003",
            "files",
            Severity::Info,
            ".gitignore missing recommended entry: node_modules/",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        let gitignore_action = plan
            .actions()
            .iter()
            .find(|a| a.id() == "gitignore-update")
            .expect("Should have gitignore action");

        // Should collect all entries
        match gitignore_action.operation() {
            ActionOperation::UpdateGitignore { entries } => {
                assert_eq!(entries.len(), 3);
                assert!(entries.contains(&".env".to_string()));
                assert!(entries.contains(&"*.log".to_string()));
                assert!(entries.contains(&"node_modules/".to_string()));
            }
            _ => panic!("Expected UpdateGitignore operation"),
        }
    }

    #[tokio::test]
    async fn test_plan_license_uses_default_year() {
        let mut config = Config::default();
        config.actions.license.year = None; // No year specified, should use current year

        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC004",
            "docs",
            Severity::Critical,
            "LICENSE file is missing",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        let license_action = plan
            .actions()
            .iter()
            .find(|a| a.id() == "license-create")
            .expect("Should have license action");

        match license_action.operation() {
            ActionOperation::CreateFile { variables, .. } => {
                assert!(variables.contains_key("year"));
                // Year should be the current year (4 digits)
                assert_eq!(variables.get("year").unwrap().len(), 4);
            }
            _ => panic!("Expected CreateFile operation"),
        }
    }

    #[tokio::test]
    async fn test_plan_all_docs_disabled() {
        let mut config = Config::default();
        config.actions.contributing = false;
        config.actions.code_of_conduct = false;
        config.actions.security_policy = false;
        config.actions.license.enabled = false;
        config.actions.gitignore = false;
        config.actions.branch_protection.enabled = false;

        let planner = ActionPlanner::new(config);

        let mut results = AuditResults::new("test-repo", "opensource");
        results.add_finding(Finding::new(
            "DOC004",
            "docs",
            Severity::Critical,
            "LICENSE file is missing",
        ));
        results.add_finding(Finding::new(
            "DOC005",
            "docs",
            Severity::Warning,
            "CONTRIBUTING file is missing",
        ));
        results.add_finding(Finding::new(
            "DOC006",
            "docs",
            Severity::Warning,
            "CODE_OF_CONDUCT file is missing",
        ));
        results.add_finding(Finding::new(
            "DOC007",
            "docs",
            Severity::Warning,
            "SECURITY.md is missing",
        ));
        results.add_finding(Finding::new(
            "FILE003",
            "files",
            Severity::Info,
            ".gitignore missing recommended entry: .env",
        ));

        let plan = planner.create_plan(&results).await.unwrap();

        // Only github-settings should be present (always planned)
        assert!(!plan.actions().iter().any(|a| a.id() == "license-create"));
        assert!(
            !plan
                .actions()
                .iter()
                .any(|a| a.id() == "contributing-create")
        );
        assert!(!plan.actions().iter().any(|a| a.id() == "coc-create"));
        assert!(!plan.actions().iter().any(|a| a.id() == "security-create"));
        assert!(!plan.actions().iter().any(|a| a.id() == "gitignore-update"));
        assert!(!plan.actions().iter().any(|a| a.id() == "branch-protection"));
    }

    #[test]
    fn test_action_planner_new() {
        let config = Config::default();
        let planner = ActionPlanner::new(config.clone());
        // Verify planner was created (indirect test via create_plan)
        assert!(planner.config.actions.gitignore);
    }

    #[tokio::test]
    async fn test_create_branch_protection_action_directly() {
        let mut config = Config::default();
        config.actions.branch_protection.branch = "develop".to_string();
        config.actions.branch_protection.required_approvals = 2;
        config.actions.branch_protection.require_status_checks = false;
        config.actions.branch_protection.block_force_push = true;

        let planner = ActionPlanner::new(config);
        let action = planner.create_branch_protection_action();

        assert_eq!(action.id(), "branch-protection");
        assert!(action.description().contains("develop"));

        match action.operation() {
            ActionOperation::ConfigureBranchProtection { branch, settings } => {
                assert_eq!(branch, "develop");
                assert_eq!(settings.required_approvals, 2);
                assert!(!settings.require_status_checks);
                assert!(settings.block_force_push);
            }
            _ => panic!("Expected ConfigureBranchProtection operation"),
        }
    }

    #[test]
    fn test_create_github_settings_action_directly() {
        let mut config = Config::default();
        config.actions.github_settings.discussions = true;
        config.actions.github_settings.vulnerability_alerts = true;
        config.actions.github_settings.automated_security_fixes = true;

        let planner = ActionPlanner::new(config);
        let action = planner.create_github_settings_action();

        assert_eq!(action.id(), "github-settings");

        match action.operation() {
            ActionOperation::UpdateGitHubSettings { settings } => {
                assert_eq!(settings.enable_discussions, Some(true));
                assert_eq!(settings.enable_vulnerability_alerts, Some(true));
                assert_eq!(settings.enable_automated_security_fixes, Some(true));
            }
            _ => panic!("Expected UpdateGitHubSettings operation"),
        }
    }

    #[test]
    fn test_create_github_settings_action_filtered() {
        let config = Config::default();
        let planner = ActionPlanner::new(config);

        // Only discussions needs update
        let action = planner.create_github_settings_action_filtered(
            true,  // needs_discussions
            false, // needs_issues
            false, // needs_wiki
            false, // needs_vuln_alerts
            false, // needs_auto_fixes
        );

        match action.operation() {
            ActionOperation::UpdateGitHubSettings { settings } => {
                assert!(settings.enable_discussions.is_some());
                assert!(settings.enable_issues.is_none());
                assert!(settings.enable_wiki.is_none());
                assert!(settings.enable_vulnerability_alerts.is_none());
                assert!(settings.enable_automated_security_fixes.is_none());
            }
            _ => panic!("Expected UpdateGitHubSettings operation"),
        }
    }
}