use crate::{binary, config::Config, error::Result, events, outputs, sarif};
pub async fn run(config: &Config) -> Result<i32> {
log::info!(
"SecretScout v{} starting in GitHub Actions mode...",
env!("CARGO_PKG_VERSION")
);
log::info!("Parsing event context...");
let event_context = events::parse_event_context(config).await?;
log::info!("Event type: {:?}", event_context.event_type);
log::info!("Base ref: {}", event_context.base_ref);
log::info!("Head ref: {}", event_context.head_ref);
log::info!("Obtaining gitleaks binary...");
let binary_path = binary::obtain_binary(config).await?;
log::info!("Using binary: {}", binary_path.display());
let log_opts = events::build_log_opts(&event_context);
let args = binary::build_arguments(config, &log_opts);
log::debug!("Gitleaks arguments: {:?}", args);
log::info!("Executing gitleaks scan...");
let execution_result =
binary::execute_gitleaks(&binary_path, &args, &config.workspace_path).await?;
match execution_result.exit_code {
0 => {
log::info!("No secrets detected");
if config.enable_summary {
let summary = outputs::generate_success_summary();
outputs::write_summary(&summary)?;
}
Ok(0)
}
2 => {
log::warn!("Secrets detected!");
log::info!("Parsing SARIF report...");
let findings = sarif::parse_and_extract(config.sarif_path())?;
log::warn!("Found {} secret(s)", findings.len());
if config.enable_comments
&& matches!(event_context.event_type, events::EventType::PullRequest)
{
log::info!("Posting PR comments...");
match outputs::post_pr_comments(config, &event_context, &findings).await {
Ok(count) => log::info!("Posted {} comments", count),
Err(e) => log::warn!("Failed to post some comments: {}", e),
}
}
if config.enable_summary {
log::info!("Generating job summary...");
let summary =
outputs::generate_findings_summary(&event_context.repository, &findings);
outputs::write_summary(&summary)?;
}
if config.enable_upload_artifact {
log::info!(
"SARIF report ready for artifact upload: {}",
config.sarif_path().display()
);
}
Ok(1)
}
1 => {
log::error!("Gitleaks exited with error code 1");
log::error!("Stderr: {}", execution_result.stderr);
if config.enable_summary {
let summary = outputs::generate_error_summary(1);
outputs::write_summary(&summary)?;
}
Ok(1)
}
code => {
log::error!("Unexpected gitleaks exit code: {}", code);
log::error!("Stdout: {}", execution_result.stdout);
log::error!("Stderr: {}", execution_result.stderr);
if config.enable_summary {
let summary = outputs::generate_error_summary(code);
outputs::write_summary(&summary)?;
}
Ok(code)
}
}
}