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
//! # Rules Evaluation Engine
//!
//! This module provides the main rules evaluation engine that orchestrates
//! the execution of all audit rule categories.
//!
//! ## Overview
//!
//! The [`RulesEngine`] is responsible for:
//!
//! - Loading and configuring rule categories
//! - Running rules against the scanned repository
//! - Collecting and aggregating findings
//! - Tracking timing information
//! - Reporting progress during execution
//!
//! ## Examples
//!
//! ### Basic Usage
//!
//! ```rust,no_run
//! use repolens::{config::Config, rules::engine::RulesEngine, scanner::Scanner};
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), repolens::RepoLensError> {
//! let config = Config::default();
//! let scanner = Scanner::new(PathBuf::from("."));
//! let engine = RulesEngine::new(config);
//!
//! let results = engine.run(&scanner).await?;
//! println!("Found {} findings", results.findings().len());
//! # Ok(())
//! # }
//! ```
//!
//! ### With Progress Callback
//!
//! ```rust,no_run
//! use repolens::{config::Config, rules::engine::RulesEngine, scanner::Scanner};
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), repolens::RepoLensError> {
//! let config = Config::default();
//! let scanner = Scanner::new(PathBuf::from("."));
//! let mut engine = RulesEngine::new(config);
//!
//! engine.set_progress_callback(Box::new(|category, current, total, timing| {
//!     if let Some((findings, duration_ms)) = timing {
//!         println!("[{}/{}] {} - {} findings in {}ms",
//!             current, total, category, findings, duration_ms);
//!     } else {
//!         println!("[{}/{}] Running {}...", current, total, category);
//!     }
//! }));
//!
//! let results = engine.run(&scanner).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Filtering Categories
//!
//! ```rust,no_run
//! use repolens::{config::Config, rules::engine::RulesEngine, scanner::Scanner};
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), repolens::RepoLensError> {
//! let config = Config::default();
//! let scanner = Scanner::new(PathBuf::from("."));
//! let mut engine = RulesEngine::new(config);
//!
//! // Only run specific categories
//! engine.set_only_categories(vec!["secrets".to_string(), "security".to_string()]);
//!
//! // Or skip certain categories
//! // engine.set_skip_categories(vec!["docs".to_string()]);
//!
//! let results = engine.run(&scanner).await?;
//! # Ok(())
//! # }
//! ```

use crate::cache::AuditCache;
use crate::error::RepoLensError;
use crate::utils::{AuditTiming, CategoryTiming, Timer};
use tracing::{Level, debug, info, span};

use super::categories::{
    codeowners::CodeownersRules, custom::CustomRules, dependencies::DependencyRules,
    docker::DockerRules, docs::DocsRules, files::FilesRules, git::GitRules, history::HistoryRules,
    issues::IssuesRules, licenses::LicenseRules, metadata::MetadataRules, quality::QualityRules,
    secrets::SecretsRules, security::SecurityRules, workflows::WorkflowsRules,
};
use super::results::AuditResults;
use crate::config::Config;
use crate::scanner::Scanner;

/// Trait for rule categories.
///
/// Each rule category (secrets, files, docs, etc.) implements this trait
/// to provide its specific audit functionality.
///
/// # Implementing a Custom Category
///
/// ```rust,ignore
/// use repolens::rules::engine::RuleCategory;
/// use repolens::rules::Finding;
/// use repolens::config::Config;
/// use repolens::scanner::Scanner;
/// use repolens::RepoLensError;
///
/// struct MyCustomRules;
///
/// #[async_trait::async_trait]
/// impl RuleCategory for MyCustomRules {
///     fn name(&self) -> &'static str {
///         "custom"
///     }
///
///     async fn run(
///         &self,
///         scanner: &Scanner,
///         config: &Config,
///     ) -> Result<Vec<Finding>, RepoLensError> {
///         let mut findings = Vec::new();
///         // ... implement custom rules ...
///         Ok(findings)
///     }
/// }
/// ```
#[async_trait::async_trait]
pub trait RuleCategory: Send + Sync {
    /// Get the category name (e.g., "secrets", "files", "docs").
    fn name(&self) -> &'static str;

    /// Run the rules in this category against the scanned repository.
    ///
    /// # Arguments
    ///
    /// * `scanner` - Scanner instance with repository file information
    /// * `config` - Configuration for rule behavior
    ///
    /// # Returns
    ///
    /// A vector of findings, or an error if rule execution fails.
    async fn run(
        &self,
        scanner: &Scanner,
        config: &Config,
    ) -> Result<Vec<super::Finding>, RepoLensError>;
}

/// Callback function type for progress reporting during rule execution.
///
/// The callback receives:
/// - `category_name` - Name of the category being processed
/// - `current_index` - Current category index (1-based)
/// - `total_count` - Total number of categories to process
/// - `timing` - Optional tuple of (findings_count, duration_ms) when category completes
///
/// # Example
///
/// ```rust
/// use repolens::rules::engine::ProgressCallback;
///
/// let callback: ProgressCallback = Box::new(|category, current, total, timing| {
///     if let Some((findings, duration_ms)) = timing {
///         println!("[{}/{}] {} completed: {} findings in {}ms",
///             current, total, category, findings, duration_ms);
///     } else {
///         println!("[{}/{}] Running {}...", current, total, category);
///     }
/// });
/// ```
pub type ProgressCallback = Box<dyn Fn(&str, usize, usize, Option<(usize, u64)>) + Send + Sync>;

/// Main rules evaluation engine for running audits.
///
/// The `RulesEngine` coordinates the execution of all rule categories
/// and collects their findings into a unified result.
///
/// # Example
///
/// ```rust,no_run
/// use repolens::{config::Config, rules::engine::RulesEngine, scanner::Scanner};
/// use std::path::PathBuf;
///
/// # async fn example() -> Result<(), repolens::RepoLensError> {
/// let config = Config::default();
/// let scanner = Scanner::new(PathBuf::from("."));
///
/// let mut engine = RulesEngine::new(config);
///
/// // Optionally configure the engine
/// engine.set_only_categories(vec!["secrets".to_string()]);
///
/// // Run the audit
/// let (results, timing) = engine.run_with_timing(&scanner).await?;
///
/// println!("Audit completed in {}", timing.total_duration_formatted());
/// println!("Found {} critical issues", results.count_by_severity(repolens::rules::Severity::Critical));
/// # Ok(())
/// # }
/// ```
pub struct RulesEngine {
    config: Config,
    only_categories: Option<Vec<String>>,
    skip_categories: Option<Vec<String>>,
    progress_callback: Option<ProgressCallback>,
    cache: Option<AuditCache>,
}

impl RulesEngine {
    /// Create a new rules engine with the given configuration
    pub fn new(config: Config) -> Self {
        Self {
            config,
            only_categories: None,
            skip_categories: None,
            progress_callback: None,
            cache: None,
        }
    }

    /// Set a callback function to report progress
    ///
    /// The callback will be called with category names as they are being processed.
    pub fn set_progress_callback(&mut self, callback: ProgressCallback) {
        self.progress_callback = Some(callback);
    }

    /// Set categories to exclusively run
    pub fn set_only_categories(&mut self, categories: Vec<String>) {
        self.only_categories = Some(categories);
    }

    /// Set categories to skip
    pub fn set_skip_categories(&mut self, categories: Vec<String>) {
        self.skip_categories = Some(categories);
    }

    /// Set the audit cache
    pub fn set_cache(&mut self, cache: AuditCache) {
        self.cache = Some(cache);
    }

    /// Take ownership of the cache (for saving after audit)
    pub fn take_cache(&mut self) -> Option<AuditCache> {
        self.cache.take()
    }

    /// Get a reference to the cache
    #[allow(dead_code)]
    pub fn cache(&self) -> Option<&AuditCache> {
        self.cache.as_ref()
    }

    /// Get a mutable reference to the cache
    #[allow(dead_code)]
    pub fn cache_mut(&mut self) -> Option<&mut AuditCache> {
        self.cache.as_mut()
    }

    /// Check if a category should be run
    fn should_run_category(&self, category: &str) -> bool {
        if let Some(only) = &self.only_categories {
            return only.iter().any(|c| c == category);
        }

        if let Some(skip) = &self.skip_categories {
            return !skip.iter().any(|c| c == category);
        }

        true
    }

    /// Run all enabled rules and return results
    pub async fn run(&self, scanner: &Scanner) -> Result<AuditResults, RepoLensError> {
        let (results, _) = self.run_with_timing(scanner).await?;
        Ok(results)
    }

    /// Run all enabled rules and return results along with timing information
    pub async fn run_with_timing(
        &self,
        scanner: &Scanner,
    ) -> Result<(AuditResults, AuditTiming), RepoLensError> {
        info!("Starting audit with preset: {}", self.config.preset);

        let total_timer = Timer::start();
        let mut audit_timing = AuditTiming::new();

        let repo_name = scanner.repository_name();
        let repo_name_ref = &repo_name;
        let mut results = AuditResults::new(repo_name.clone(), &self.config.preset);

        // Get all rule categories
        let categories: Vec<Box<dyn RuleCategory>> = vec![
            Box::new(SecretsRules),
            Box::new(FilesRules),
            Box::new(DocsRules),
            Box::new(SecurityRules),
            Box::new(WorkflowsRules),
            Box::new(QualityRules),
            Box::new(DependencyRules),
            Box::new(LicenseRules),
            Box::new(DockerRules),
            Box::new(GitRules),
            Box::new(HistoryRules),
            Box::new(MetadataRules),
            Box::new(IssuesRules),
            Box::new(CodeownersRules),
            Box::new(CustomRules),
        ];

        // Count categories that will be executed
        let total: usize = categories
            .iter()
            .filter(|c| self.should_run_category(c.name()))
            .count();
        let mut current = 0;

        // Run each category
        for category in categories {
            let category_name = category.name();

            if !self.should_run_category(category_name) {
                debug!(category = category_name, "Skipping category");
                continue;
            }

            current += 1;
            // Call progress callback with None timing (before execution)
            if let Some(ref callback) = self.progress_callback {
                callback(category_name, current, total, None);
            }

            let span = span!(Level::INFO, "category", category = category_name, repository = %repo_name_ref);
            let _guard = span.enter();

            debug!(category = category_name, "Running category");

            // Time the category execution
            let category_timer = Timer::start();

            match category.run(scanner, &self.config).await {
                Ok(findings) => {
                    let category_duration = category_timer.elapsed();
                    let findings_count = findings.len();
                    debug!(
                        category = category_name,
                        findings_count = findings_count,
                        duration_ms = category_duration.as_millis(),
                        "Category completed"
                    );

                    // Record timing (rule_count is findings_count as we don't have individual rule counts yet)
                    audit_timing.add_category(CategoryTiming::new(
                        category_name,
                        0, // We don't track individual rules yet
                        findings_count,
                        category_duration,
                    ));

                    // Call progress callback with timing info (after execution)
                    if let Some(ref callback) = self.progress_callback {
                        callback(
                            category_name,
                            current,
                            total,
                            Some((findings_count, category_duration.as_millis() as u64)),
                        );
                    }

                    results.add_findings(findings);
                }
                Err(e) => {
                    let category_duration = category_timer.elapsed();
                    tracing::warn!(
                        category = category_name,
                        error = %e,
                        duration_ms = category_duration.as_millis(),
                        "Error running category"
                    );

                    // Still record timing for failed categories
                    audit_timing.add_category(CategoryTiming::new(
                        category_name,
                        0,
                        0,
                        category_duration,
                    ));
                }
            }
        }

        // Set total duration
        audit_timing.set_total_duration(total_timer.elapsed());

        info!(
            "Audit complete: {} critical, {} warnings, {} info (total time: {})",
            results.count_by_severity(super::Severity::Critical),
            results.count_by_severity(super::Severity::Warning),
            results.count_by_severity(super::Severity::Info),
            audit_timing.total_duration_formatted(),
        );

        Ok((results, audit_timing))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::scanner::Scanner;
    use std::fs;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_rules_engine_runs_all_categories() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create a basic file structure
        fs::write(root.join("README.md"), "# Test Project").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let engine = RulesEngine::new(config);

        let results = engine.run(&scanner).await.unwrap();

        // Verify that results are returned (may be empty if no issues found)
        let _ = results.findings().len();
        assert_eq!(results.preset, "opensource");
    }

    #[tokio::test]
    async fn test_rules_engine_filters_with_only() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        fs::write(root.join("README.md"), "# Test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let mut engine = RulesEngine::new(config);
        engine.set_only_categories(vec!["secrets".to_string()]);

        let results = engine.run(&scanner).await.unwrap();

        // Verify that only secrets category was run
        // All findings should be from secrets category
        for finding in results.findings() {
            assert_eq!(finding.category, "secrets");
        }
    }

    #[tokio::test]
    async fn test_rules_engine_filters_with_skip() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        fs::write(root.join("README.md"), "# Test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let mut engine = RulesEngine::new(config);
        engine.set_skip_categories(vec!["secrets".to_string()]);

        let results = engine.run(&scanner).await.unwrap();

        // Verify that secrets category was skipped
        for finding in results.findings() {
            assert_ne!(finding.category, "secrets");
        }
    }

    #[tokio::test]
    async fn test_rules_engine_handles_category_errors_gracefully() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create a file that might cause issues
        fs::write(root.join("test.txt"), "test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let engine = RulesEngine::new(config);

        // Should not panic even if a category fails
        let results = engine.run(&scanner).await;
        assert!(results.is_ok());
    }

    #[tokio::test]
    async fn test_rules_engine_collects_all_findings() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create files that should trigger findings
        fs::write(
            root.join("test.js"),
            "const apiKey = 'sk_test_1234567890abcdef';",
        )
        .unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let engine = RulesEngine::new(config);

        let results = engine.run(&scanner).await.unwrap();

        // Should have collected findings from multiple categories
        // (at least secrets should find something)
        let _ = results.findings().len();
    }

    #[test]
    fn test_should_run_category_with_only() {
        let config = Config::default();
        let mut engine = RulesEngine::new(config);
        engine.set_only_categories(vec!["secrets".to_string(), "files".to_string()]);

        assert!(engine.should_run_category("secrets"));
        assert!(engine.should_run_category("files"));
        assert!(!engine.should_run_category("docs"));
    }

    #[test]
    fn test_should_run_category_with_skip() {
        let config = Config::default();
        let mut engine = RulesEngine::new(config);
        engine.set_skip_categories(vec!["secrets".to_string()]);

        assert!(!engine.should_run_category("secrets"));
        assert!(engine.should_run_category("files"));
        assert!(engine.should_run_category("docs"));
    }

    #[test]
    fn test_should_run_category_default() {
        let config = Config::default();
        let engine = RulesEngine::new(config);

        // By default, all categories should run
        assert!(engine.should_run_category("secrets"));
        assert!(engine.should_run_category("files"));
        assert!(engine.should_run_category("docs"));
    }

    #[test]
    fn test_set_progress_callback() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let config = Config::default();
        let mut engine = RulesEngine::new(config);

        let call_count = Arc::new(AtomicUsize::new(0));
        let call_count_clone = call_count.clone();

        engine.set_progress_callback(Box::new(move |_name, _current, _total, _timing| {
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        }));

        assert!(engine.progress_callback.is_some());
    }

    #[test]
    fn test_cache_operations() {
        let temp_dir = TempDir::new().unwrap();
        let config = Config::default();
        let mut engine = RulesEngine::new(config.clone());

        // Initially no cache
        assert!(engine.cache().is_none());
        assert!(engine.cache_mut().is_none());

        // Set cache
        let cache = AuditCache::new(temp_dir.path(), config.cache);
        engine.set_cache(cache);

        // Now has cache
        assert!(engine.cache().is_some());
        assert!(engine.cache_mut().is_some());

        // Take cache
        let taken = engine.take_cache();
        assert!(taken.is_some());
        assert!(engine.cache().is_none());
    }

    #[tokio::test]
    async fn test_rules_engine_with_progress_callback() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("README.md"), "# Test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let mut engine = RulesEngine::new(config);

        let call_count = Arc::new(AtomicUsize::new(0));
        let call_count_clone = call_count.clone();

        engine.set_progress_callback(Box::new(move |_name, _current, _total, _timing| {
            call_count_clone.fetch_add(1, Ordering::SeqCst);
        }));

        let _ = engine.run(&scanner).await.unwrap();

        // Should have called progress callback for each category
        assert!(call_count.load(Ordering::SeqCst) > 0);
    }

    #[tokio::test]
    async fn test_rules_engine_with_cache() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("README.md"), "# Test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let mut engine = RulesEngine::new(config.clone());
        engine.set_cache(AuditCache::new(root, config.cache));

        let results = engine.run(&scanner).await.unwrap();
        assert_eq!(results.preset, "opensource");
    }

    #[tokio::test]
    async fn test_rules_engine_run_with_timing() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("README.md"), "# Test Project").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let engine = RulesEngine::new(config);

        let (results, timing) = engine.run_with_timing(&scanner).await.unwrap();

        // Verify results are returned
        assert_eq!(results.preset, "opensource");

        // Verify timing information is populated
        assert!(!timing.categories().is_empty());
        assert!(timing.total_duration.as_nanos() > 0);
    }

    #[tokio::test]
    async fn test_rules_engine_timing_captures_category_durations() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("README.md"), "# Test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let mut engine = RulesEngine::new(config);
        engine.set_only_categories(vec!["files".to_string()]);

        let (_, timing) = engine.run_with_timing(&scanner).await.unwrap();

        // Should have exactly one category timing
        assert_eq!(timing.categories().len(), 1);
        assert_eq!(timing.categories()[0].name, "files");
    }

    #[tokio::test]
    async fn test_rules_engine_timing_with_progress_callback() {
        use std::sync::{Arc, Mutex};

        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();
        fs::write(root.join("README.md"), "# Test").unwrap();

        let config = Config::default();
        let scanner = Scanner::new(root.to_path_buf());
        let mut engine = RulesEngine::new(config);
        engine.set_only_categories(vec!["files".to_string()]);

        // Track timing info received in callback
        let timing_info = Arc::new(Mutex::new(Vec::new()));
        let timing_info_clone = timing_info.clone();

        engine.set_progress_callback(Box::new(move |name, _current, _total, timing| {
            if let Some((findings, duration_ms)) = timing {
                timing_info_clone
                    .lock()
                    .unwrap()
                    .push((name.to_string(), findings, duration_ms));
            }
        }));

        let _ = engine.run_with_timing(&scanner).await.unwrap();

        let captured = timing_info.lock().unwrap();
        // Should have captured timing for files category
        assert!(!captured.is_empty());
        assert_eq!(captured[0].0, "files");
    }
}