use crate::github::types::{CommitInfo, CommitResponse, TIME_LAYOUT};
use chrono::{DateTime, Utc};
pub fn filter_commits(commits: &[CommitResponse]) -> Vec<CommitInfo> {
commits
.iter()
.filter(|c| !should_skip(c))
.filter_map(|c| {
let date = parse_and_format_date(&c.commit.author.date)?;
let (subject, body) = split_commit_message(&c.commit.message);
Some(CommitInfo {
date,
subject,
body,
})
})
.collect()
}
pub fn should_skip(commit: &CommitResponse) -> bool {
let msg = &commit.commit.message;
msg.contains("Merge") || msg.contains("Revert") || msg.starts_with("Merge ")
}
pub fn split_commit_message(full: &str) -> (String, String) {
let mut parts = full.splitn(2, '\n');
let subject = parts.next().unwrap_or("").trim().to_string();
let body = parts.next().unwrap_or("").trim().to_string();
(subject, body)
}
fn parse_and_format_date(date_str: &str) -> Option<String> {
let parsed: DateTime<Utc> = date_str.parse().ok()?;
Some(parsed.format(TIME_LAYOUT).to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn splits_subject_and_body() {
let (subject, body) = split_commit_message("fix: something\n\nDetailed explanation");
assert_eq!(subject, "fix: something");
assert_eq!(body, "Detailed explanation");
}
#[test]
fn skips_merge_commits() {
let commit = CommitResponse {
commit: crate::github::types::Commit {
author: crate::github::types::CommitAuthor {
date: "2026-01-01T00:00:00Z".to_string(),
},
message: "Merge branch 'main' into feature".to_string(),
},
};
assert!(should_skip(&commit));
}
#[test]
fn skips_revert_commits() {
let commit = CommitResponse {
commit: crate::github::types::Commit {
author: crate::github::types::CommitAuthor {
date: "2026-01-01T00:00:00Z".to_string(),
},
message: "Revert \"some change\"".to_string(),
},
};
assert!(should_skip(&commit));
}
#[test]
fn keeps_normal_commits() {
let commit = CommitResponse {
commit: crate::github::types::Commit {
author: crate::github::types::CommitAuthor {
date: "2026-01-01T00:00:00Z".to_string(),
},
message: "feat: add new feature".to_string(),
},
};
assert!(!should_skip(&commit));
}
}