use super::common::generate_changes_content;
use super::models::{BreakingChange, ChangeEntry, ChangeMetrics, ChangelogResponse, ChangelogType};
use super::prompt;
use crate::common::DetailLevel;
use crate::config::Config;
use crate::git::GitRepo;
use anyhow::{Context, Result};
use chrono;
use colored::Colorize;
use log::debug;
use regex;
use std::fmt::Write as FmtWrite;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::sync::Arc;
pub struct ChangelogGenerator;
impl ChangelogGenerator {
pub async fn generate(
git_repo: Arc<GitRepo>,
from: &str,
to: &str,
config: &Config,
detail_level: DetailLevel,
) -> Result<String> {
let changelog: ChangelogResponse = generate_changes_content::<ChangelogResponse>(
git_repo,
from,
to,
config,
detail_level,
prompt::create_changelog_system_prompt,
prompt::create_changelog_user_prompt,
)
.await?;
Ok(format_changelog_response(&changelog))
}
#[allow(clippy::too_many_lines)]
pub fn update_changelog_file(
changelog_content: &str,
changelog_path: &str,
git_repo: &Arc<GitRepo>,
to_ref: &str,
version_name: Option<String>,
) -> Result<()> {
let path = Path::new(changelog_path);
let default_header = "# Changelog\n\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n";
let commit_date = match git_repo.get_commit_date(to_ref) {
Ok(date) => {
debug!("Got commit date for {to_ref}: {date}");
date
}
Err(e) => {
debug!("Failed to get commit date for {to_ref}: {e}");
chrono::Local::now().format("%Y-%m-%d").to_string()
}
};
let stripped_content = strip_ansi_codes(changelog_content);
let clean_content =
if stripped_content.starts_with("━") || stripped_content.starts_with('-') {
if let Some(pos) = stripped_content.find('\n') {
stripped_content[pos + 1..].to_string()
} else {
stripped_content
}
} else {
stripped_content
};
let mut version_content = if clean_content.contains("## [") {
let parts: Vec<&str> = clean_content.split("## [").collect();
if parts.len() > 1 {
format!("## [{}", parts[1])
} else {
clean_content
}
} else {
clean_content
};
if let Some(version) = version_name {
if version_content.contains("## [") {
let re = regex::Regex::new(r"## \[([^\]]+)\]").expect("Failed to compile regex");
version_content = re
.replace(&version_content, &format!("## [{version}]"))
.to_string();
debug!("Replaced version with user-provided version: {version}");
} else {
debug!("Could not find version header to replace in changelog content");
}
}
if version_content.contains(" - \n") {
version_content = version_content.replace(" - \n", &format!(" - {commit_date}\n"));
debug!("Replaced empty date with commit date: {commit_date}");
} else if version_content.contains("] - ") && !version_content.contains("] - 20") {
let parts: Vec<&str> = version_content.splitn(2, "] - ").collect();
if parts.len() == 2 {
version_content = format!(
"{}] - {}\n{}",
parts[0],
commit_date,
parts[1].trim_start_matches(['\n', ' '])
);
debug!("Added commit date after dash: {commit_date}");
}
} else if !version_content.contains("] - ") {
let line_end = version_content.find('\n').unwrap_or(version_content.len());
let version_line = &version_content[..line_end];
if version_line.contains("## [") && version_line.contains(']') {
let bracket_pos = version_line
.rfind(']')
.expect("Failed to find closing bracket in version line");
version_content = format!(
"{} - {}{}",
&version_content[..=bracket_pos],
commit_date,
&version_content[bracket_pos + 1..]
);
debug!("Added date to version line: {commit_date}");
}
}
let separator =
"\n<!-- -------------------------------------------------------------- -->\n\n";
let version_content_with_separator = format!("{version_content}{separator}");
let updated_content = if path.exists() {
let existing_content = fs::read_to_string(path)
.with_context(|| format!("Failed to read changelog file: {changelog_path}"))?;
if existing_content.contains("# Changelog")
&& existing_content.contains("Keep a Changelog")
{
if existing_content.contains("## [") {
let parts: Vec<&str> = existing_content.split("## [").collect();
let header = parts[0];
if parts.len() > 1 {
let existing_versions = parts[1..].join("## [");
format!("{header}{version_content_with_separator}## [{existing_versions}")
} else {
format!("{header}{version_content_with_separator}")
}
} else {
format!("{existing_content}{version_content_with_separator}")
}
} else {
format!("{default_header}{version_content_with_separator}")
}
} else {
format!("{default_header}{version_content_with_separator}")
};
let mut file = fs::File::create(path)
.with_context(|| format!("Failed to create changelog file: {changelog_path}"))?;
file.write_all(updated_content.as_bytes())
.with_context(|| format!("Failed to write to changelog file: {changelog_path}"))?;
Ok(())
}
}
fn strip_ansi_codes(s: &str) -> String {
let re = regex::Regex::new(r"\x1B\[([0-9]{1,2}(;[0-9]{1,2})*)?[m|K]")
.expect("Failed to compile ANSI escape code regex");
re.replace_all(s, "").to_string()
}
fn format_changelog_response(response: &ChangelogResponse) -> String {
let mut formatted = String::new();
formatted.push_str(&"# Changelog\n\n".bright_cyan().bold().to_string());
formatted.push_str("All notable changes to this project will be documented in this file.\n\n");
formatted.push_str(
"The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\n",
);
formatted.push_str("and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n");
let version = response
.version
.clone()
.unwrap_or_else(|| "Unreleased".to_string());
write!(formatted, "## [{}] - \n\n", version.bright_green().bold())
.expect("writing to string should never fail");
let ordered_types = [
ChangelogType::Added,
ChangelogType::Changed,
ChangelogType::Fixed,
ChangelogType::Removed,
ChangelogType::Deprecated,
ChangelogType::Security,
];
for change_type in &ordered_types {
if let Some(entries) = response.sections.get(change_type)
&& !entries.is_empty()
{
formatted.push_str(&format_change_type(change_type));
for entry in entries {
formatted.push_str(&format_change_entry(entry));
}
formatted.push('\n');
}
}
if !response.breaking_changes.is_empty() {
formatted.push_str(
&"### ⚠️ Breaking Changes\n\n"
.bright_red()
.bold()
.to_string(),
);
for breaking_change in &response.breaking_changes {
formatted.push_str(&format_breaking_change(breaking_change));
}
formatted.push('\n');
}
formatted.push_str(&"### 📊 Metrics\n\n".bright_magenta().bold().to_string());
formatted.push_str(&format_metrics(&response.metrics));
formatted
}
fn format_change_type(change_type: &ChangelogType) -> String {
let (emoji, text) = match change_type {
ChangelogType::Added => ("✨", "Added"),
ChangelogType::Changed => ("🔄", "Changed"),
ChangelogType::Deprecated => ("⚠️", "Deprecated"),
ChangelogType::Removed => ("🗑️", "Removed"),
ChangelogType::Fixed => ("🐛", "Fixed"),
ChangelogType::Security => ("🔒", "Security"),
};
format!("### {} {}\n\n", emoji, text.bright_blue().bold())
}
fn format_change_entry(entry: &ChangeEntry) -> String {
let mut formatted = format!("- {}", entry.description);
if !entry.associated_issues.is_empty() {
write!(
formatted,
" ({})",
entry.associated_issues.join(", ").yellow()
)
.expect("writing to string should never fail");
}
if let Some(pr) = &entry.pull_request {
write!(formatted, " [{}]", pr.bright_purple())
.expect("writing to string should never fail");
}
writeln!(formatted, " ({})", entry.commit_hashes.join(", ").dimmed())
.expect("writing to string should never fail");
formatted
}
fn format_breaking_change(breaking_change: &BreakingChange) -> String {
format!(
"- {} ({})\n",
breaking_change.description,
breaking_change.commit_hash.dimmed()
)
}
fn format_metrics(metrics: &ChangeMetrics) -> String {
format!(
"- Total Commits: {}\n- Files Changed: {}\n- Insertions: {}\n- Deletions: {}\n",
metrics.total_commits.to_string().green(),
metrics.files_changed.to_string().yellow(),
metrics.insertions.to_string().green(),
metrics.deletions.to_string().red()
)
}