use crate::analyze::get_code_snippet;
use crate::prelude::*;
use crate::rules::get_rule_description;
pub fn export_all_violations_to_excel(
violations: &[RuleViolation],
excel_path: &str,
_manifest: &RuleManifest,
) -> Result<()> {
use rust_xlsxwriter::{Color as XlsxColor, Format, Workbook};
let registry = RuleRegistry::new();
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
let header_format = Format::new()
.set_bold()
.set_background_color(XlsxColor::RGB(0xD9D9D9));
let headers = [
"Title",
"Description",
"Work Item Type",
"State",
"Severity",
"Priority",
];
for (col, header) in headers.iter().enumerate() {
worksheet.write_string_with_format(0, col as u16, *header, &header_format)?;
}
for (row, violation) in (1u32..).zip(violations.iter()) {
let file_hash = calculate_file_hash(&violation.file_path)?;
let title = format!(
"{}:{}:{} version:{}",
violation.rule_id, violation.file_path, violation.line, file_hash
);
let code_snippet = get_code_snippet(&violation.file_path, violation.line)?;
let rule_description = get_rule_description(®istry, &violation.rule_id);
let description = format!(
"{} - {}: {}",
violation.rule_id, rule_description, code_snippet
);
worksheet.write_string(row, 0, &title)?;
worksheet.write_string(row, 1, &description)?;
worksheet.write_string(row, 2, "Bug")?;
worksheet.write_string(row, 3, "Proposed")?;
worksheet.write_string(row, 4, "1 - Critical")?;
worksheet.write_string(row, 5, "1")?;
}
worksheet.autofit();
workbook.save(excel_path)?;
Ok(())
}