full_analysis/
full_analysis.rs

1// Example: Full organizational defect pattern analysis
2// Demonstrates complete workflow: GitHub org → Git analysis → Classification → YAML report
3//
4// Usage:
5//   cargo run --example full_analysis
6//
7// This example shows the complete Phase 1 pipeline
8
9use anyhow::Result;
10use chrono::Utc;
11use organizational_intelligence_plugin::analyzer::OrgAnalyzer;
12use organizational_intelligence_plugin::github::GitHubMiner;
13use organizational_intelligence_plugin::report::{
14    AnalysisMetadata, AnalysisReport, ReportGenerator,
15};
16use std::path::PathBuf;
17use tempfile::TempDir;
18
19#[tokio::main]
20async fn main() -> Result<()> {
21    // Initialize logging
22    tracing_subscriber::fmt::fmt()
23        .with_max_level(tracing::Level::INFO)
24        .init();
25
26    println!("šŸš€ Organizational Intelligence Plugin - Full Analysis Example\n");
27    println!("Demonstrates: GitHub API → Git Mining → Classification → YAML Report\n");
28
29    // Step 1: Fetch repositories from GitHub organization
30    println!("šŸ“„ Step 1: Fetching repositories from GitHub...");
31    let github_miner = GitHubMiner::new(None); // Unauthenticated for demo
32    let org_name = "tokio-rs";
33
34    let repos = github_miner.fetch_organization_repos(org_name).await?;
35    println!("āœ… Found {} repositories in {}\n", repos.len(), org_name);
36
37    // Display top repositories
38    let mut sorted_repos = repos.clone();
39    sorted_repos.sort_by(|a, b| b.stars.cmp(&a.stars));
40
41    println!("⭐ Top 3 repositories:");
42    for (i, repo) in sorted_repos.iter().take(3).enumerate() {
43        println!("   {}. {} ({} ⭐)", i + 1, repo.name, repo.stars);
44    }
45    println!();
46
47    // Step 2: Analyze repositories for defect patterns
48    println!("šŸ” Step 2: Analyzing commit history for defect patterns...");
49
50    // Create temporary directory for cloning
51    let temp_dir = TempDir::new()?;
52    let analyzer = OrgAnalyzer::new(temp_dir.path());
53
54    // Analyze top repository (limit to 100 commits for demo speed)
55    let top_repo = &sorted_repos[0];
56    println!("   Analyzing: {} (up to 100 commits)", top_repo.name);
57
58    let patterns = analyzer
59        .analyze_repository(
60            &format!("https://github.com/{}/{}", org_name, top_repo.name),
61            &top_repo.name,
62            100,
63        )
64        .await?;
65
66    if patterns.is_empty() {
67        println!("   ā„¹ļø  No defect patterns detected in last 100 commits");
68    } else {
69        println!("   āœ… Found {} defect categories\n", patterns.len());
70
71        // Display patterns
72        println!("šŸ“Š Defect Patterns Found:");
73        for pattern in &patterns {
74            println!(
75                "   • {} (n={}, confidence={:.0}%)",
76                pattern.category.as_str(),
77                pattern.frequency,
78                pattern.confidence * 100.0
79            );
80            if !pattern.examples.is_empty() {
81                let example = &pattern.examples[0];
82                println!("     Example: {}: {}", example.commit_hash, example.message);
83            }
84        }
85        println!();
86    }
87
88    // Step 3: Generate YAML report
89    println!("šŸ“ Step 3: Generating YAML report...");
90    let report_generator = ReportGenerator::new();
91
92    let metadata = AnalysisMetadata {
93        organization: org_name.to_string(),
94        analysis_date: Utc::now().to_rfc3339(),
95        repositories_analyzed: 1, // We analyzed 1 repo in this demo
96        commits_analyzed: 100,
97        analyzer_version: env!("CARGO_PKG_VERSION").to_string(),
98    };
99
100    let report = AnalysisReport {
101        version: "1.0".to_string(),
102        metadata,
103        defect_patterns: patterns,
104    };
105
106    // Write to file
107    let output_path = PathBuf::from("full-analysis-report.yaml");
108    report_generator
109        .write_to_file(&report, &output_path)
110        .await?;
111
112    println!("āœ… Report saved to: {}", output_path.display());
113
114    // Display YAML preview
115    let yaml_content = tokio::fs::read_to_string(&output_path).await?;
116    println!("\nšŸ“„ Report Preview (first 20 lines):");
117    println!("---");
118    for (i, line) in yaml_content.lines().enumerate() {
119        if i >= 20 {
120            println!(
121                "   ... (truncated, see {} for full report)",
122                output_path.display()
123            );
124            break;
125        }
126        println!("{}", line);
127    }
128    println!("---");
129
130    // Summary
131    println!("\nšŸŽÆ Full Analysis Complete!");
132    println!("   āœ… GitHub API integration");
133    println!("   āœ… Git repository cloning");
134    println!("   āœ… Commit history analysis");
135    println!("   āœ… Rule-based defect classification");
136    println!("   āœ… YAML report generation");
137    println!("\n   Phase 1 MVP complete!");
138    println!("   Next: User feedback mechanism for Phase 2 ML training");
139
140    Ok(())
141}